Search in sources :

Example 1 with HandshakeHandler

use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.

the class TransportHandlingSockJsService method handleRawWebSocketRequest.

@Override
protected void handleRawWebSocketRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler) throws IOException {
    TransportHandler transportHandler = this.handlers.get(TransportType.WEBSOCKET);
    if (!(transportHandler instanceof HandshakeHandler)) {
        logger.error("No handler configured for raw WebSocket messages");
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;
    }
    HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, handler);
    HandshakeFailureException failure = null;
    try {
        Map<String, Object> attributes = new HashMap<>();
        if (!chain.applyBeforeHandshake(request, response, attributes)) {
            return;
        }
        ((HandshakeHandler) transportHandler).doHandshake(request, response, handler, attributes);
        chain.applyAfterHandshake(request, response, null);
    } catch (HandshakeFailureException ex) {
        failure = ex;
    } catch (Exception ex) {
        failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
    } finally {
        if (failure != null) {
            chain.applyAfterHandshake(request, response, failure);
            throw failure;
        }
    }
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HandshakeInterceptorChain(org.springframework.web.socket.server.support.HandshakeInterceptorChain) SockJsException(org.springframework.web.socket.sockjs.SockJsException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException)

Example 2 with HandshakeHandler

use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.

the class AbstractWebSocketHandlerRegistration method getMappings.

protected final M getMappings() {
    M mappings = createMappings();
    if (this.sockJsServiceRegistration != null) {
        SockJsService sockJsService = this.sockJsServiceRegistration.getSockJsService();
        this.handlerMap.forEach((wsHandler, paths) -> {
            for (String path : paths) {
                String pathPattern = (path.endsWith("/") ? path + "**" : path + "/**");
                addSockJsServiceMapping(mappings, sockJsService, wsHandler, pathPattern);
            }
        });
    } else {
        HandshakeHandler handshakeHandler = getOrCreateHandshakeHandler();
        HandshakeInterceptor[] interceptors = getInterceptors();
        this.handlerMap.forEach((wsHandler, paths) -> {
            for (String path : paths) {
                addWebSocketHandlerMapping(mappings, wsHandler, handshakeHandler, interceptors, path);
            }
        });
    }
    return mappings;
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) SockJsService(org.springframework.web.socket.sockjs.SockJsService) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor)

Example 3 with HandshakeHandler

use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.

the class WebSocketHandlerRegistrationTests method handshakeHandler.

@Test
public void handshakeHandler() {
    WebSocketHandler handler = new TextWebSocketHandler();
    HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler);
    List<Mapping> mappings = this.registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    Mapping mapping = mappings.get(0);
    assertThat(mapping.webSocketHandler).isEqualTo(handler);
    assertThat(mapping.path).isEqualTo("/foo");
    assertThat(mapping.handshakeHandler).isSameAs(handshakeHandler);
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) Test(org.junit.jupiter.api.Test)

Example 4 with HandshakeHandler

use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.

the class WebSocketHandlerRegistrationTests method handshakeHandlerPassedToSockJsRegistration.

@Test
public void handshakeHandlerPassedToSockJsRegistration() {
    WebSocketHandler handler = new TextWebSocketHandler();
    HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
    this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);
    List<Mapping> mappings = this.registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    Mapping mapping = mappings.get(0);
    assertThat(mapping.webSocketHandler).isEqualTo(handler);
    assertThat(mapping.path).isEqualTo("/foo/**");
    assertThat(mapping.sockJsService).isNotNull();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
    assertThat(transportHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) Test(org.junit.jupiter.api.Test)

Example 5 with HandshakeHandler

use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.

the class TestMessageCodec method webSocketHandlersAttributes.

@Test
public void webSocketHandlersAttributes() {
    loadBeanDefinitions("websocket-config-handlers-attributes.xml");
    HandlerMapping handlerMapping = this.appContext.getBean(HandlerMapping.class);
    assertThat(handlerMapping).isNotNull();
    boolean condition2 = handlerMapping instanceof SimpleUrlHandlerMapping;
    assertThat(condition2).isTrue();
    SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping;
    assertThat(urlHandlerMapping.getOrder()).isEqualTo(2);
    WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/foo");
    assertThat(handler).isNotNull();
    unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), FooWebSocketHandler.class);
    HandshakeHandler handshakeHandler = handler.getHandshakeHandler();
    assertThat(handshakeHandler).isNotNull();
    boolean condition1 = handshakeHandler instanceof TestHandshakeHandler;
    assertThat(condition1).isTrue();
    List<HandshakeInterceptor> interceptors = handler.getHandshakeInterceptors();
    assertThat(interceptors).extracting("class").containsExactlyInAnyOrder(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
    handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/test");
    assertThat(handler).isNotNull();
    unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
    handshakeHandler = handler.getHandshakeHandler();
    assertThat(handshakeHandler).isNotNull();
    boolean condition = handshakeHandler instanceof TestHandshakeHandler;
    assertThat(condition).isTrue();
    interceptors = handler.getHandshakeInterceptors();
    assertThat(interceptors).extracting("class").containsExactlyInAnyOrder(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) Test(org.junit.jupiter.api.Test)

Aggregations

HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)7 Test (org.junit.jupiter.api.Test)5 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)5 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)4 HandlerMapping (org.springframework.web.servlet.HandlerMapping)3 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)3 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)3 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)3 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)2 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 MessageHandler (org.springframework.messaging.MessageHandler)1 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)1