|
| 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 | +*/ |
| 13 | +package io.kubernetes.client.util; |
| 14 | + |
| 15 | +import io.kubernetes.client.ApiClient; |
| 16 | +import io.kubernetes.client.ApiException; |
| 17 | +import io.kubernetes.client.Pair; |
| 18 | + |
| 19 | +import com.google.common.net.HttpHeaders; |
| 20 | +import com.squareup.okhttp.MediaType; |
| 21 | +import com.squareup.okhttp.Request; |
| 22 | +import com.squareup.okhttp.Response; |
| 23 | +import com.squareup.okhttp.ResponseBody; |
| 24 | +import com.squareup.okhttp.ws.WebSocket; |
| 25 | +import com.squareup.okhttp.ws.WebSocketCall; |
| 26 | +import com.squareup.okhttp.ws.WebSocketListener; |
| 27 | +import okio.Buffer; |
| 28 | + |
| 29 | +import static com.squareup.okhttp.ws.WebSocket.BINARY; |
| 30 | +import static com.squareup.okhttp.ws.WebSocket.TEXT; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.InputStream; |
| 34 | +import java.io.Reader; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.HashMap; |
| 37 | + |
| 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 | + |
| 46 | + /** |
| 47 | + * A simple interface for a listener on a web socket |
| 48 | + */ |
| 49 | + public interface SocketListener { |
| 50 | + /** |
| 51 | + * Called when the socket is opened |
| 52 | + */ |
| 53 | + public void open(); |
| 54 | + |
| 55 | + /** |
| 56 | + * Callled when a binary media type message is received |
| 57 | + * @param in The input stream containing the binary data |
| 58 | + */ |
| 59 | + public void bytesMessage(InputStream in); |
| 60 | + |
| 61 | + /** |
| 62 | + * Called when a text media type message is received |
| 63 | + * @param in The character stream containing the message |
| 64 | + */ |
| 65 | + public void textMessage(Reader in); |
| 66 | + |
| 67 | + /** |
| 68 | + * Called when the stream is closed. |
| 69 | + */ |
| 70 | + public void close(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Create a new WebSocket stream |
| 75 | + * @param path The HTTP Path to request from the API |
| 76 | + * @param method The HTTP method to use for the call |
| 77 | + * @param client The ApiClient for communicating with the API |
| 78 | + * @param listener The socket listener to handle socket events |
| 79 | + */ |
| 80 | + public static void stream(String path, String method, ApiClient client, SocketListener listener) throws ApiException, IOException { |
| 81 | + HashMap<String, String> headers = new HashMap<String, String>(); |
| 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); |
| 86 | + |
| 87 | + Request request = client.buildRequest(path, method, new ArrayList<Pair>(), null, headers, new HashMap<String, Object>(), new String[0], null); |
| 88 | + WebSocketCall.create(client.getHttpClient(), request).enqueue(new Listener(listener)); |
| 89 | + } |
| 90 | + |
| 91 | + private static class Listener implements WebSocketListener { |
| 92 | + private SocketListener listener; |
| 93 | + |
| 94 | + public Listener(SocketListener listener) { |
| 95 | + this.listener = listener; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onOpen(WebSocket webSocket, Response response) { |
| 100 | + listener.open(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onMessage(ResponseBody body) throws IOException { |
| 105 | + if (body.contentType() == TEXT) { |
| 106 | + listener.bytesMessage(body.byteStream()); |
| 107 | + } else if (body.contentType() == BINARY) { |
| 108 | + listener.textMessage(body.charStream()); |
| 109 | + } |
| 110 | + body.close(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void onPong(Buffer payload) { |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onClose(int code, String reason) { |
| 119 | + listener.close(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onFailure(IOException e, Response res) { |
| 124 | + e.printStackTrace(); |
| 125 | + listener.close(); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments