1
+ /*
2
+ Copyright 2017 The Kubernetes Authors.
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
12
+ */
1
13
package io .kubernetes .client .util ;
2
14
3
15
import io .kubernetes .client .ApiClient ;
4
16
import io .kubernetes .client .ApiException ;
5
17
import io .kubernetes .client .Pair ;
6
18
19
+ import com .google .common .net .HttpHeaders ;
7
20
import com .squareup .okhttp .MediaType ;
8
21
import com .squareup .okhttp .Request ;
9
22
import com .squareup .okhttp .Response ;
10
23
import com .squareup .okhttp .ResponseBody ;
11
24
import com .squareup .okhttp .ws .WebSocket ;
12
25
import com .squareup .okhttp .ws .WebSocketListener ;
26
+ import okio .Buffer ;
13
27
14
28
import static com .squareup .okhttp .ws .WebSocket .BINARY ;
15
29
import static com .squareup .okhttp .ws .WebSocket .TEXT ;
20
34
import java .util .ArrayList ;
21
35
import java .util .HashMap ;
22
36
23
- import okio .Buffer ;
24
-
25
37
public class WebSockets {
38
+ public static final String V4_STREAM_PROTOCOL = "v4.channel.k8s.io" ;
39
+ public static final String V3_STREAM_PROTOCOL = "v3.channel.k8s.io" ;
40
+ public static final String V2_STREAM_PROTOCOL = "v2.channel.k8s.io" ;
41
+ public static final String V1_STREAM_PROTOCOL = "channel.k8s.io" ;
42
+ public static final String STREAM_PROTOCOL_HEADER = "X-Stream-Protocol-Version" ;
43
+ public static final String SPDY_3_1 = "SPDY/3.1" ;
44
+
26
45
/**
27
46
* A simple interface for a listener on a web socket
28
47
*/
@@ -59,9 +78,10 @@ public interface SocketListener {
59
78
*/
60
79
public static void stream (String path , String method , ApiClient client , SocketListener listener ) throws ApiException , IOException {
61
80
HashMap <String , String > headers = new HashMap <String , String >();
62
- headers .put ("X-Stream-Protocol-Version" , "v4.channel.k8s.io,v3.channel.k8s.io,v2.channel.k8s.io,channel.k8s.io" );
63
- headers .put ("Connection" , "Upgrade" );
64
- headers .put ("Upgrade" , "SPDY/3.1" );
81
+ String allProtocols = String .format ("%s,%s,%s,%s" , V4_STREAM_PROTOCOL , V3_STREAM_PROTOCOL , V2_STREAM_PROTOCOL , V1_STREAM_PROTOCOL );
82
+ headers .put (STREAM_PROTOCOL_HEADER , allProtocols );
83
+ headers .put (HttpHeaders .CONNECTION , HttpHeaders .UPGRADE );
84
+ headers .put (HttpHeaders .UPGRADE , SPDY_3_1 );
65
85
66
86
Request request = client .buildRequest (path , method , new ArrayList <Pair >(), null , headers , new HashMap <String , Object >(), new String [0 ], null );
67
87
WebSocketCall .create (client .getHttpClient (), request ).enqueue (new Listener (listener ));
@@ -104,4 +124,4 @@ public void onFailure(IOException e, Response res) {
104
124
listener .close ();
105
125
}
106
126
}
107
- }
127
+ }
0 commit comments