|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.web.reactive.socket.client; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | +import java.security.NoSuchAlgorithmException; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import javax.net.ssl.SSLContext; |
| 23 | +import javax.net.ssl.SSLEngine; |
| 24 | + |
| 25 | +import io.netty.buffer.ByteBuf; |
| 26 | +import io.netty.buffer.ByteBufAllocator; |
| 27 | +import io.reactivex.netty.protocol.http.client.HttpClient; |
| 28 | +import io.reactivex.netty.protocol.http.ws.WebSocketConnection; |
| 29 | +import io.reactivex.netty.protocol.http.ws.client.WebSocketRequest; |
| 30 | +import reactor.core.publisher.Mono; |
| 31 | +import reactor.util.function.Tuples; |
| 32 | +import rx.Observable; |
| 33 | +import rx.RxReactiveStreams; |
| 34 | + |
| 35 | +import org.springframework.core.io.buffer.NettyDataBufferFactory; |
| 36 | +import org.springframework.http.HttpHeaders; |
| 37 | +import org.springframework.web.reactive.socket.WebSocketSession; |
| 38 | +import org.springframework.web.reactive.socket.adapter.HandshakeInfo; |
| 39 | +import org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession; |
| 40 | + |
| 41 | +/** |
| 42 | + * A {@link WebSocketClient} based on RxNetty. |
| 43 | + * |
| 44 | + * @author Rossen Stoyanchev |
| 45 | + * @since 5.0 |
| 46 | + */ |
| 47 | +public class RxNettyWebSocketClient implements WebSocketClient { |
| 48 | + |
| 49 | + private final Function<URI, HttpClient<ByteBuf, ByteBuf>> httpClientFactory; |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Default constructor that uses {@link HttpClient#newClient(String, int)} |
| 54 | + * to create HTTP client instances when connecting. |
| 55 | + */ |
| 56 | + public RxNettyWebSocketClient() { |
| 57 | + this(RxNettyWebSocketClient::createDefaultHttpClient); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Constructor with a function to create {@link HttpClient} instances. |
| 62 | + * @param httpClientFactory factory to create clients |
| 63 | + */ |
| 64 | + public RxNettyWebSocketClient(Function<URI, HttpClient<ByteBuf, ByteBuf>> httpClientFactory) { |
| 65 | + this.httpClientFactory = httpClientFactory; |
| 66 | + } |
| 67 | + |
| 68 | + private static HttpClient<ByteBuf, ByteBuf> createDefaultHttpClient(URI url) { |
| 69 | + boolean secure = "wss".equals(url.getScheme()); |
| 70 | + int port = url.getPort() > 0 ? url.getPort() : secure ? 443 : 80; |
| 71 | + HttpClient<ByteBuf, ByteBuf> httpClient = HttpClient.newClient(url.getHost(), port); |
| 72 | + if (secure) { |
| 73 | + try { |
| 74 | + SSLContext context = SSLContext.getDefault(); |
| 75 | + SSLEngine engine = context.createSSLEngine(url.getHost(), port); |
| 76 | + engine.setUseClientMode(true); |
| 77 | + httpClient.secure(engine); |
| 78 | + } |
| 79 | + catch (NoSuchAlgorithmException ex) { |
| 80 | + throw new IllegalStateException("Failed to create HttpClient for " + url, ex); |
| 81 | + } |
| 82 | + } |
| 83 | + return httpClient; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + @Override |
| 88 | + public Mono<WebSocketSession> connect(URI url) { |
| 89 | + return connect(url, new HttpHeaders()); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Mono<WebSocketSession> connect(URI url, HttpHeaders headers) { |
| 94 | + HandshakeInfo info = new HandshakeInfo(url, headers, Mono.empty()); |
| 95 | + Observable<WebSocketSession> observable = connectInternal(info); |
| 96 | + return Mono.from(RxReactiveStreams.toPublisher(observable)); |
| 97 | + } |
| 98 | + |
| 99 | + private Observable<WebSocketSession> connectInternal(HandshakeInfo info) { |
| 100 | + return createWebSocketRequest(info.getUri()) |
| 101 | + .flatMap(response -> { |
| 102 | + ByteBufAllocator allocator = response.unsafeNettyChannel().alloc(); |
| 103 | + NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(allocator); |
| 104 | + Observable<WebSocketConnection> conn = response.getWebSocketConnection(); |
| 105 | + return Observable.zip(conn, Observable.just(bufferFactory), Tuples::of); |
| 106 | + }) |
| 107 | + .map(tuple -> { |
| 108 | + WebSocketConnection conn = tuple.getT1(); |
| 109 | + NettyDataBufferFactory bufferFactory = tuple.getT2(); |
| 110 | + return new RxNettyWebSocketSession(conn, info, bufferFactory); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private WebSocketRequest<ByteBuf> createWebSocketRequest(URI url) { |
| 115 | + String query = url.getRawQuery(); |
| 116 | + return this.httpClientFactory.apply(url) |
| 117 | + .createGet(url.getRawPath() + (query != null ? "?" + query : "")) |
| 118 | + .requestWebSocketUpgrade(); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments