use of org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession in project spring-framework by spring-projects.
the class TomcatRequestUpgradeStrategy method upgrade.
// for old doUpgrade variant in Tomcat 9.0.55
@SuppressWarnings("deprecation")
@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);
HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
DataBufferFactory bufferFactory = response.bufferFactory();
// Trigger WebFlux preCommit actions and upgrade
return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
Endpoint endpoint = new StandardWebSocketHandlerAdapter(ContextWebSocketHandler.decorate(handler, contextView), session -> new TomcatWebSocketSession(session, handshakeInfo, bufferFactory));
String requestURI = servletRequest.getRequestURI();
DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
config.setSubprotocols(subProtocol != null ? Collections.singletonList(subProtocol) : Collections.emptyList());
WsServerContainer container = getContainer(servletRequest);
try {
container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
} catch (Exception ex) {
return Mono.error(ex);
}
return Mono.empty();
}));
}
Aggregations