Search in sources :

Example 1 with WebSocketHttpRequestHandler

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

the class ServletWebSocketHandlerRegistration method addWebSocketHandlerMapping.

@Override
protected void addWebSocketHandlerMapping(MultiValueMap<HttpRequestHandler, String> mappings, WebSocketHandler webSocketHandler, HandshakeHandler handshakeHandler, HandshakeInterceptor[] interceptors, String path) {
    WebSocketHttpRequestHandler httpHandler = new WebSocketHttpRequestHandler(webSocketHandler, handshakeHandler);
    if (!ObjectUtils.isEmpty(interceptors)) {
        httpHandler.setHandshakeInterceptors(Arrays.asList(interceptors));
    }
    mappings.add(httpHandler, path);
}
Also used : WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)

Example 2 with WebSocketHttpRequestHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerAndInterceptorWithAllowedOrigins.

@Test
public void handshakeHandlerAndInterceptorWithAllowedOrigins() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    String origin = "https://mydomain.com";
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin);
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
    assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo"));
    WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
    assertThat(requestHandler.getWebSocketHandler()).isNotNull();
    assertThat(requestHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
    assertThat(requestHandler.getHandshakeInterceptors().size()).isEqualTo(2);
    assertThat(requestHandler.getHandshakeInterceptors().get(0)).isEqualTo(interceptor);
    assertThat(requestHandler.getHandshakeInterceptors().get(1).getClass()).isEqualTo(OriginHandshakeInterceptor.class);
}
Also used : HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) List(java.util.List) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) Test(org.junit.jupiter.api.Test)

Example 3 with WebSocketHttpRequestHandler

use of org.springframework.web.socket.server.support.WebSocketHttpRequestHandler 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)

Example 4 with WebSocketHttpRequestHandler

use of org.springframework.web.socket.server.support.WebSocketHttpRequestHandler in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurer method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    if (sameOriginDisabled()) {
        return;
    }
    String beanName = "stompWebSocketHandlerMapping";
    SimpleUrlHandlerMapping mapping = this.context.getBean(beanName, SimpleUrlHandlerMapping.class);
    Map<String, Object> mappings = mapping.getHandlerMap();
    for (Object object : mappings.values()) {
        if (object instanceof SockJsHttpRequestHandler) {
            setHandshakeInterceptors((SockJsHttpRequestHandler) object);
        } else if (object instanceof WebSocketHttpRequestHandler) {
            setHandshakeInterceptors((WebSocketHttpRequestHandler) object);
        } else {
            throw new IllegalStateException("Bean " + beanName + " is expected to contain mappings to either a " + "SockJsHttpRequestHandler or a WebSocketHttpRequestHandler but got " + object);
        }
    }
    if (this.inboundRegistry.containsMapping() && !this.inboundRegistry.isSimpDestPathMatcherConfigured()) {
        PathMatcher pathMatcher = getDefaultPathMatcher();
        this.inboundRegistry.simpDestPathMatcher(pathMatcher);
    }
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) AntPathMatcher(org.springframework.util.AntPathMatcher) PathMatcher(org.springframework.util.PathMatcher) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)

Example 5 with WebSocketHttpRequestHandler

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

the class WebMvcStompWebSocketEndpointRegistration method getMappings.

public final MultiValueMap<HttpRequestHandler, String> getMappings() {
    MultiValueMap<HttpRequestHandler, String> mappings = new LinkedMultiValueMap<>();
    if (this.registration != null) {
        SockJsService sockJsService = this.registration.getSockJsService();
        for (String path : this.paths) {
            String pattern = (path.endsWith("/") ? path + "**" : path + "/**");
            SockJsHttpRequestHandler handler = new SockJsHttpRequestHandler(sockJsService, this.webSocketHandler);
            mappings.add(handler, pattern);
        }
    } else {
        for (String path : this.paths) {
            WebSocketHttpRequestHandler handler;
            if (this.handshakeHandler != null) {
                handler = new WebSocketHttpRequestHandler(this.webSocketHandler, this.handshakeHandler);
            } else {
                handler = new WebSocketHttpRequestHandler(this.webSocketHandler);
            }
            HandshakeInterceptor[] interceptors = getInterceptors();
            if (interceptors.length > 0) {
                handler.setHandshakeInterceptors(Arrays.asList(interceptors));
            }
            mappings.add(handler, path);
        }
    }
    return mappings;
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) SockJsService(org.springframework.web.socket.sockjs.SockJsService) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)

Aggregations

WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)11 Test (org.junit.jupiter.api.Test)8 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)7 HttpRequestHandler (org.springframework.web.HttpRequestHandler)6 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)5 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)4 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)4 HandlerMapping (org.springframework.web.servlet.HandlerMapping)3 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)3 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)3 List (java.util.List)2 Map (java.util.Map)2 MultiValueMap (org.springframework.util.MultiValueMap)2 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)2 WebSocketSession (org.springframework.web.socket.WebSocketSession)2 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)2 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)2 ArrayList (java.util.ArrayList)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1