Search in sources :

Example 31 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)

Example 32 with ServerHttpRequest

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

the class DispatcherHandler method handle.

@Override
public Mono<Void> handle(ServerWebExchange exchange) {
    if (logger.isDebugEnabled()) {
        ServerHttpRequest request = exchange.getRequest();
        logger.debug("Processing " + request.getMethod() + " request for [" + request.getURI() + "]");
    }
    return Flux.fromIterable(this.handlerMappings).concatMap(mapping -> mapping.getHandler(exchange)).next().otherwiseIfEmpty(Mono.error(HANDLER_NOT_FOUND_EXCEPTION)).then(handler -> invokeHandler(exchange, handler)).then(result -> handleResult(exchange, result));
}
Also used : ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpWebHandlerAdapter(org.springframework.web.server.adapter.HttpWebHandlerAdapter) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) Mono(reactor.core.publisher.Mono) WebHandler(org.springframework.web.server.WebHandler) ApplicationContext(org.springframework.context.ApplicationContext) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Map(java.util.Map) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 33 with ServerHttpRequest

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

the class ResourceUrlProvider method getLookupPathIndex.

private int getLookupPathIndex(ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    String requestPath = request.getURI().getPath();
    String lookupPath = getUrlPathHelper().getLookupPathForRequest(exchange);
    return requestPath.indexOf(lookupPath);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 34 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 35 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)

Aggregations

ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)71 HttpHeaders (org.springframework.http.HttpHeaders)26 Test (org.junit.jupiter.api.Test)25 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)20 ServerWebExchange (org.springframework.web.server.ServerWebExchange)19 URI (java.net.URI)17 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)17 Mono (reactor.core.publisher.Mono)13 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)10 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)9 HttpMethod (org.springframework.http.HttpMethod)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Map (java.util.Map)6 HttpStatus (org.springframework.http.HttpStatus)6 MediaType (org.springframework.http.MediaType)6 Flux (reactor.core.publisher.Flux)6 Principal (java.security.Principal)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5