Search in sources :

Example 1 with WebSocketHandler

use of org.springframework.web.socket.WebSocketHandler 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();
        for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {
            for (String path : this.handlerMap.get(wsHandler)) {
                String pathPattern = (path.endsWith("/") ? path + "**" : path + "/**");
                addSockJsServiceMapping(mappings, sockJsService, wsHandler, pathPattern);
            }
        }
    } else {
        HandshakeHandler handshakeHandler = getOrCreateHandshakeHandler();
        HandshakeInterceptor[] interceptors = getInterceptors();
        for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {
            for (String path : this.handlerMap.get(wsHandler)) {
                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) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor)

Example 2 with WebSocketHandler

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

the class AbstractHandshakeHandler method determineHandlerSupportedProtocols.

/**
	 * Determine the sub-protocols supported by the given WebSocketHandler by
	 * checking whether it is an instance of {@link SubProtocolCapable}.
	 * @param handler the handler to check
	 * @return a list of supported protocols, or an empty list if none available
	 */
protected final List<String> determineHandlerSupportedProtocols(WebSocketHandler handler) {
    WebSocketHandler handlerToCheck = WebSocketHandlerDecorator.unwrap(handler);
    List<String> subProtocols = null;
    if (handlerToCheck instanceof SubProtocolCapable) {
        subProtocols = ((SubProtocolCapable) handlerToCheck).getSubProtocols();
    }
    return (subProtocols != null ? subProtocols : Collections.emptyList());
}
Also used : SubProtocolCapable(org.springframework.web.socket.SubProtocolCapable) WebSocketHandler(org.springframework.web.socket.WebSocketHandler)

Example 3 with WebSocketHandler

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

the class WebSocketMessageBrokerConfigurationSupport method stompWebSocketHandlerMapping.

@Bean
public HandlerMapping stompWebSocketHandlerMapping() {
    WebSocketHandler handler = decorateWebSocketHandler(subProtocolWebSocketHandler());
    WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(handler, getTransportRegistration(), messageBrokerTaskScheduler());
    registry.setApplicationContext(getApplicationContext());
    registerStompEndpoints(registry);
    return registry.getHandlerMapping();
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) Bean(org.springframework.context.annotation.Bean)

Example 4 with WebSocketHandler

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

the class WebSocketHandlerRegistrationTests method interceptors.

@Test
public void interceptors() {
    WebSocketHandler handler = new TextWebSocketHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);
    List<Mapping> mappings = this.registration.getMappings();
    assertEquals(1, mappings.size());
    Mapping mapping = mappings.get(0);
    assertEquals(handler, mapping.webSocketHandler);
    assertEquals("/foo", mapping.path);
    assertEquals(2, mapping.interceptors.length);
    assertEquals(interceptor, mapping.interceptors[0]);
    assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
Also used : HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) Test(org.junit.Test)

Example 5 with WebSocketHandler

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

the class WebSocketHandlerRegistrationTests method minimal.

@Test
public void minimal() {
    WebSocketHandler handler = new TextWebSocketHandler();
    this.registration.addHandler(handler, "/foo", "/bar");
    List<Mapping> mappings = this.registration.getMappings();
    assertEquals(2, mappings.size());
    Mapping m1 = mappings.get(0);
    assertEquals(handler, m1.webSocketHandler);
    assertEquals("/foo", m1.path);
    assertEquals(1, m1.interceptors.length);
    assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());
    Mapping m2 = mappings.get(1);
    assertEquals(handler, m2.webSocketHandler);
    assertEquals("/bar", m2.path);
    assertEquals(1, m2.interceptors.length);
    assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) Test(org.junit.Test)

Aggregations

WebSocketHandler (org.springframework.web.socket.WebSocketHandler)44 Test (org.junit.Test)36 HashMap (java.util.HashMap)14 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)14 WebSocketHttpHeaders (org.springframework.web.socket.WebSocketHttpHeaders)5 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)5 MockHttpSession (org.springframework.mock.web.test.MockHttpSession)4 TextMessage (org.springframework.web.socket.TextMessage)4 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)4 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)3 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)3 URI (java.net.URI)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 MessageHandler (org.springframework.messaging.MessageHandler)2 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)2 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)2 StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)2 DefaultUserDestinationResolver (org.springframework.messaging.simp.user.DefaultUserDestinationResolver)2