Search in sources :

Example 11 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class JettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(handler, session -> {
        HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
        DataBufferFactory factory = response.bufferFactory();
        return new JettyWebSocketSession(session, info, factory);
    });
    startLazily(servletRequest);
    boolean isUpgrade = this.factory.isUpgradeRequest(servletRequest, servletResponse);
    Assert.isTrue(isUpgrade, "Not a WebSocket handshake");
    try {
        adapterHolder.set(new WebSocketHandlerContainer(adapter, subProtocol));
        this.factory.acceptWebSocket(servletRequest, servletResponse);
    } catch (IOException ex) {
        return Mono.error(ex);
    } finally {
        adapterHolder.remove();
    }
    return Mono.empty();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.reactive.ServletServerHttpResponse) JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 12 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class JettyRequestUpgradeStrategy 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);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 13 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest 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);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 14 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class TomcatRequestUpgradeStrategy 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);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 15 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class TomcatRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    Endpoint endpoint = new StandardWebSocketHandlerAdapter(handler, session -> {
        HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
        DataBufferFactory factory = response.bufferFactory();
        return new StandardWebSocketSession(session, info, factory);
    });
    String requestURI = servletRequest.getRequestURI();
    DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
    config.setSubprotocols(subProtocol.map(Collections::singletonList).orElse(Collections.emptyList()));
    try {
        WsServerContainer container = getContainer(servletRequest);
        container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
    } catch (ServletException | IOException ex) {
        return Mono.error(ex);
    }
    return Mono.empty();
}
Also used : WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.reactive.ServletServerHttpResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Endpoint(javax.websocket.Endpoint) StandardWebSocketSession(org.springframework.web.reactive.socket.adapter.StandardWebSocketSession) Collections(java.util.Collections) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Aggregations

ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)18 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)7 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)6 Principal (java.security.Principal)5 ServletServerHttpRequest (org.springframework.http.server.reactive.ServletServerHttpRequest)4 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)3 HttpHeaders (org.springframework.http.HttpHeaders)3 HttpMethod (org.springframework.http.HttpMethod)3 Mono (reactor.core.publisher.Mono)3 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ReactiveAdapter (org.springframework.core.ReactiveAdapter)2 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)2 ResolvableType (org.springframework.core.ResolvableType)2 MediaType (org.springframework.http.MediaType)2 ServletServerHttpResponse (org.springframework.http.server.reactive.ServletServerHttpResponse)2