Search in sources :

Example 6 with HandshakeInterceptor

use of org.springframework.web.socket.server.HandshakeInterceptor 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) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) 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)

Example 7 with HandshakeInterceptor

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

the class WebMvcStompWebSocketEndpointRegistration method getInterceptors.

protected HandshakeInterceptor[] getInterceptors() {
    List<HandshakeInterceptor> interceptors = new ArrayList<>();
    interceptors.addAll(this.interceptors);
    interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
    return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]);
}
Also used : OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) ArrayList(java.util.ArrayList) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor)

Example 8 with HandshakeInterceptor

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

the class HandshakeInterceptorChain method applyBeforeHandshake.

public boolean applyBeforeHandshake(ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> attributes) throws Exception {
    for (int i = 0; i < this.interceptors.size(); i++) {
        HandshakeInterceptor interceptor = this.interceptors.get(i);
        if (!interceptor.beforeHandshake(request, response, this.wsHandler, attributes)) {
            if (logger.isDebugEnabled()) {
                logger.debug(interceptor + " returns false from beforeHandshake - precluding handshake");
            }
            applyAfterHandshake(request, response, null);
            return false;
        }
        this.interceptorIndex = i;
    }
    return true;
}
Also used : HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor)

Example 9 with HandshakeInterceptor

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

the class AbstractWebSocketHandlerRegistration method withSockJS.

@Override
public SockJsServiceRegistration withSockJS() {
    this.sockJsServiceRegistration = new SockJsServiceRegistration(this.sockJsTaskScheduler);
    HandshakeInterceptor[] interceptors = getInterceptors();
    if (interceptors.length > 0) {
        this.sockJsServiceRegistration.setInterceptors(interceptors);
    }
    if (this.handshakeHandler != null) {
        WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
        this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
    }
    if (!this.allowedOrigins.isEmpty()) {
        this.sockJsServiceRegistration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()]));
    }
    return this.sockJsServiceRegistration;
}
Also used : HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)

Example 10 with HandshakeInterceptor

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

the class WebMvcStompWebSocketEndpointRegistration method withSockJS.

@Override
public SockJsServiceRegistration withSockJS() {
    this.registration = new StompSockJsServiceRegistration(this.sockJsTaskScheduler);
    HandshakeInterceptor[] interceptors = getInterceptors();
    if (interceptors.length > 0) {
        this.registration.setInterceptors(interceptors);
    }
    if (this.handshakeHandler != null) {
        WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
        this.registration.setTransportHandlerOverrides(transportHandler);
    }
    if (!this.allowedOrigins.isEmpty()) {
        this.registration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()]));
    }
    return this.registration;
}
Also used : HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)

Aggregations

HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)13 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)11 Test (org.junit.Test)5 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)5 SockJsService (org.springframework.web.socket.sockjs.SockJsService)5 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)5 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)4 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)4 ArrayList (java.util.ArrayList)3 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)3 TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)3 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)3 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)2 HttpRequestHandler (org.springframework.web.HttpRequestHandler)2 HandlerMapping (org.springframework.web.servlet.HandlerMapping)2 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)2 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)2 List (java.util.List)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1