use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.
the class ReactorNettyRequestUpgradeStrategy method upgrade.
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
ReactorServerHttpResponse response = (ReactorServerHttpResponse) exchange.getResponse();
HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
return response.getReactorResponse().sendWebsocket(subProtocol.orElse(null), (in, out) -> handler.handle(new ReactorNettyWebSocketSession(in, out, info, bufferFactory)));
}
use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.
the class ReactorNettyRequestUpgradeStrategy method getHandshakeInfo.
private HandshakeInfo getHandshakeInfo(ServerWebExchange exchange, Optional<String> protocol) {
ServerHttpRequest request = exchange.getRequest();
Mono<Principal> principal = exchange.getPrincipal();
return new HandshakeInfo(request.getURI(), request.getHeaders(), principal, protocol);
}
use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.
the class JettyRequestUpgradeStrategy method upgrade.
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
HttpServletRequest servletRequest = ServerHttpRequestDecorator.getNativeRequest(request);
HttpServletResponse servletResponse = ServerHttpResponseDecorator.getNativeResponse(response);
ServletContext servletContext = servletRequest.getServletContext();
HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
DataBufferFactory factory = response.bufferFactory();
// Trigger WebFlux preCommit actions before upgrade
return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(ContextWebSocketHandler.decorate(handler, contextView), session -> new JettyWebSocketSession(session, handshakeInfo, factory));
JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
if (subProtocol != null) {
upgradeResponse.setAcceptedSubProtocol(subProtocol);
}
return adapter;
};
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
try {
container.upgrade(webSocketCreator, servletRequest, servletResponse);
} catch (Exception ex) {
return Mono.error(ex);
}
return Mono.empty();
}));
}
use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.
the class ReactorNettyWebSocketClient method execute.
@Override
public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) {
String protocols = StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols());
return getHttpClient().headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders)).websocket(buildSpec(protocols)).uri(url.toString()).handle((inbound, outbound) -> {
HttpHeaders responseHeaders = toHttpHeaders(inbound);
String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
HandshakeInfo info = new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
NettyDataBufferFactory factory = new NettyDataBufferFactory(outbound.alloc());
WebSocketSession session = new ReactorNettyWebSocketSession(inbound, outbound, info, factory, getMaxFramePayloadLength());
if (logger.isDebugEnabled()) {
logger.debug("Started session '" + session.getId() + "' for " + url);
}
return handler.handle(session).checkpoint(url + " [ReactorNettyWebSocketClient]");
}).doOnRequest(n -> {
if (logger.isDebugEnabled()) {
logger.debug("Connecting to " + url);
}
}).next();
}
use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.
the class StandardWebSocketClient method createHandshakeInfo.
private HandshakeInfo createHandshakeInfo(URI url, DefaultConfigurator configurator) {
HttpHeaders responseHeaders = configurator.getResponseHeaders();
String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
return new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
}
Aggregations