Search in sources :

Example 6 with DefaultHandshakeHandler

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

use of org.springframework.web.socket.server.support.DefaultHandshakeHandler 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 8 with DefaultHandshakeHandler

use of org.springframework.web.socket.server.support.DefaultHandshakeHandler in project jhipster-sample-app-websocket by jhipster.

the class WebsocketConfiguration method defaultHandshakeHandler.

private DefaultHandshakeHandler defaultHandshakeHandler() {
    return new DefaultHandshakeHandler() {

        @Override
        protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
            Principal principal = request.getPrincipal();
            if (principal == null) {
                Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
                authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.ANONYMOUS));
                principal = new AnonymousAuthenticationToken("WebsocketConfiguration", "anonymous", authorities);
            }
            return principal;
        }
    };
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) Principal(java.security.Principal)

Example 9 with DefaultHandshakeHandler

use of org.springframework.web.socket.server.support.DefaultHandshakeHandler in project ma-core-public by infiniteautomation.

the class MangoWebSocketConfigurer method handshakeHandler.

@Bean
public DefaultHandshakeHandler handshakeHandler() {
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
    policy.setInputBufferSize(8192);
    // We don't want timeouts..
    policy.setIdleTimeout(Integer.MAX_VALUE);
    WebSocketServerFactory factory = new WebSocketServerFactory(policy);
    return new DefaultHandshakeHandler(new JettyRequestUpgradeStrategy(factory));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) JettyRequestUpgradeStrategy(org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy) WebSocketServerFactory(org.eclipse.jetty.websocket.server.WebSocketServerFactory) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) Bean(org.springframework.context.annotation.Bean)

Example 10 with DefaultHandshakeHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins.

@Test
public void handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins() {
    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).withSockJS();
    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/**"));
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
    assertThat(requestHandler.getWebSocketHandler()).isNotNull();
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertThat(sockJsService).isNotNull();
    Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
    assertThat(transportHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
    assertThat(sockJsService.getHandshakeInterceptors().size()).isEqualTo(2);
    assertThat(sockJsService.getHandshakeInterceptors().get(0)).isEqualTo(interceptor);
    assertThat(sockJsService.getHandshakeInterceptors().get(1).getClass()).isEqualTo(OriginHandshakeInterceptor.class);
    assertThat(sockJsService.getAllowedOrigins().contains(origin)).isTrue();
}
Also used : WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) 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) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) List(java.util.List) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)13 Test (org.junit.jupiter.api.Test)7 List (java.util.List)5 Map (java.util.Map)5 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)5 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)5 MultiValueMap (org.springframework.util.MultiValueMap)4 HttpRequestHandler (org.springframework.web.HttpRequestHandler)4 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)4 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)4 Principal (java.security.Principal)3 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)3 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)3 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)2 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)2 TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)2