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 ;
25
+ import com .squareup .okhttp .ws .WebSocketCall ;
12
26
import com .squareup .okhttp .ws .WebSocketListener ;
27
+ import okio .Buffer ;
13
28
14
29
import static com .squareup .okhttp .ws .WebSocket .BINARY ;
15
30
import static com .squareup .okhttp .ws .WebSocket .TEXT ;
20
35
import java .util .ArrayList ;
21
36
import java .util .HashMap ;
22
37
23
- import okio .Buffer ;
24
-
25
38
public class WebSockets {
39
+ public static final String V4_STREAM_PROTOCOL = "v4.channel.k8s.io" ;
40
+ public static final String V3_STREAM_PROTOCOL = "v3.channel.k8s.io" ;
41
+ public static final String V2_STREAM_PROTOCOL = "v2.channel.k8s.io" ;
42
+ public static final String V1_STREAM_PROTOCOL = "channel.k8s.io" ;
43
+ public static final String STREAM_PROTOCOL_HEADER = "X-Stream-Protocol-Version" ;
44
+ public static final String SPDY_3_1 = "SPDY/3.1" ;
45
+
26
46
/**
27
47
* A simple interface for a listener on a web socket
28
48
*/
@@ -59,9 +79,10 @@ public interface SocketListener {
59
79
*/
60
80
public static void stream (String path , String method , ApiClient client , SocketListener listener ) throws ApiException , IOException {
61
81
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" );
82
+ String allProtocols = String .format ("%s,%s,%s,%s" , V4_STREAM_PROTOCOL , V3_STREAM_PROTOCOL , V2_STREAM_PROTOCOL , V1_STREAM_PROTOCOL );
83
+ headers .put (STREAM_PROTOCOL_HEADER , allProtocols );
84
+ headers .put (HttpHeaders .CONNECTION , HttpHeaders .UPGRADE );
85
+ headers .put (HttpHeaders .UPGRADE , SPDY_3_1 );
65
86
66
87
Request request = client .buildRequest (path , method , new ArrayList <Pair >(), null , headers , new HashMap <String , Object >(), new String [0 ], null );
67
88
WebSocketCall .create (client .getHttpClient (), request ).enqueue (new Listener (listener ));
@@ -104,4 +125,4 @@ public void onFailure(IOException e, Response res) {
104
125
listener .close ();
105
126
}
106
127
}
107
- }
128
+ }
0 commit comments