Search in sources :

Example 6 with OriginHandshakeInterceptor

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

the class HandlersBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext context) {
    Object source = context.extractSource(element);
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
    context.pushContainingComponent(compDefinition);
    String orderAttribute = element.getAttribute("order");
    int order = orderAttribute.isEmpty() ? DEFAULT_MAPPING_ORDER : Integer.valueOf(orderAttribute);
    RootBeanDefinition handlerMappingDef = new RootBeanDefinition(WebSocketHandlerMapping.class);
    handlerMappingDef.setSource(source);
    handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    handlerMappingDef.getPropertyValues().add("order", order);
    String handlerMappingName = context.getReaderContext().registerWithGeneratedName(handlerMappingDef);
    RuntimeBeanReference sockJsService = WebSocketNamespaceUtils.registerSockJsService(element, SOCK_JS_SCHEDULER_NAME, context, source);
    HandlerMappingStrategy strategy;
    if (sockJsService != null) {
        strategy = new SockJsHandlerMappingStrategy(sockJsService);
    } else {
        RuntimeBeanReference handshakeHandler = WebSocketNamespaceUtils.registerHandshakeHandler(element, context, source);
        Element interceptorsElement = DomUtils.getChildElementByTagName(element, "handshake-interceptors");
        ManagedList<? super Object> interceptors = WebSocketNamespaceUtils.parseBeanSubElements(interceptorsElement, context);
        String allowedOriginsAttribute = element.getAttribute("allowed-origins");
        List<String> allowedOrigins = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOriginsAttribute, ","));
        interceptors.add(new OriginHandshakeInterceptor(allowedOrigins));
        strategy = new WebSocketHandlerMappingStrategy(handshakeHandler, interceptors);
    }
    ManagedMap<String, Object> urlMap = new ManagedMap<>();
    urlMap.setSource(source);
    for (Element mappingElement : DomUtils.getChildElementsByTagName(element, "mapping")) {
        strategy.addMapping(mappingElement, urlMap, context);
    }
    handlerMappingDef.getPropertyValues().add("urlMap", urlMap);
    context.registerComponent(new BeanComponentDefinition(handlerMappingDef, handlerMappingName));
    context.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Aggregations

OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)6 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)2 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)2 Element (org.w3c.dom.Element)2 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)1 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)1 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)1 ManagedMap (org.springframework.beans.factory.support.ManagedMap)1 HandlerMapping (org.springframework.web.servlet.HandlerMapping)1 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)1 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)1 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)1 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)1 TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)1