Search in sources :

Example 31 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupportTests method handlerMapping.

@Test
public void handlerMapping() {
    ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
    SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) config.getBean(HandlerMapping.class);
    assertEquals(1, hm.getOrder());
    Map<String, Object> handlerMap = hm.getHandlerMap();
    assertEquals(1, handlerMap.size());
    assertNotNull(handlerMap.get("/simpleBroker"));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) HandlerMapping(org.springframework.web.servlet.HandlerMapping) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 32 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurer method afterSingletonsInstantiated.

public void afterSingletonsInstantiated() {
    if (sameOriginDisabled()) {
        return;
    }
    String beanName = "stompWebSocketHandlerMapping";
    SimpleUrlHandlerMapping mapping = context.getBean(beanName, SimpleUrlHandlerMapping.class);
    Map<String, Object> mappings = mapping.getHandlerMap();
    for (Object object : mappings.values()) {
        if (object instanceof SockJsHttpRequestHandler) {
            SockJsHttpRequestHandler sockjsHandler = (SockJsHttpRequestHandler) object;
            SockJsService sockJsService = sockjsHandler.getSockJsService();
            if (!(sockJsService instanceof TransportHandlingSockJsService)) {
                throw new IllegalStateException("sockJsService must be instance of TransportHandlingSockJsService got " + sockJsService);
            }
            TransportHandlingSockJsService transportHandlingSockJsService = (TransportHandlingSockJsService) sockJsService;
            List<HandshakeInterceptor> handshakeInterceptors = transportHandlingSockJsService.getHandshakeInterceptors();
            List<HandshakeInterceptor> interceptorsToSet = new ArrayList<HandshakeInterceptor>(handshakeInterceptors.size() + 1);
            interceptorsToSet.add(new CsrfTokenHandshakeInterceptor());
            interceptorsToSet.addAll(handshakeInterceptors);
            transportHandlingSockJsService.setHandshakeInterceptors(interceptorsToSet);
        } else if (object instanceof WebSocketHttpRequestHandler) {
            WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) object;
            List<HandshakeInterceptor> handshakeInterceptors = handler.getHandshakeInterceptors();
            List<HandshakeInterceptor> interceptorsToSet = new ArrayList<HandshakeInterceptor>(handshakeInterceptors.size() + 1);
            interceptorsToSet.add(new CsrfTokenHandshakeInterceptor());
            interceptorsToSet.addAll(handshakeInterceptors);
            handler.setHandshakeInterceptors(interceptorsToSet);
        } else {
            throw new IllegalStateException("Bean " + beanName + " is expected to contain mappings to either a SockJsHttpRequestHandler or a WebSocketHttpRequestHandler but got " + object);
        }
    }
    if (inboundRegistry.containsMapping() && !inboundRegistry.isSimpDestPathMatcherConfigured()) {
        PathMatcher pathMatcher = getDefaultPathMatcher();
        inboundRegistry.simpDestPathMatcher(pathMatcher);
    }
}
Also used : CsrfTokenHandshakeInterceptor(org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) ArrayList(java.util.ArrayList) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) CsrfTokenHandshakeInterceptor(org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) AntPathMatcher(org.springframework.util.AntPathMatcher) PathMatcher(org.springframework.util.PathMatcher) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) SockJsService(org.springframework.web.socket.sockjs.SockJsService) ArrayList(java.util.ArrayList) List(java.util.List)

Example 33 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-boot by spring-projects.

the class RequestMappingEndpointTests method beanUrlMappings.

@Test
public void beanUrlMappings() {
    StaticApplicationContext context = new StaticApplicationContext();
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setUrlMap(Collections.singletonMap("/foo", new Object()));
    mapping.setApplicationContext(context);
    mapping.initApplicationContext();
    context.getDefaultListableBeanFactory().registerSingleton("mapping", mapping);
    this.endpoint.setApplicationContext(context);
    Map<String, Object> result = this.endpoint.invoke();
    assertThat(result).hasSize(1);
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) result.get("/foo");
    assertThat(map.get("bean")).isEqualTo("mapping");
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Map(java.util.Map) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 34 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-boot by spring-projects.

the class RequestMappingEndpointTests method concreteUrlMappings.

@Test
public void concreteUrlMappings() {
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setUrlMap(Collections.singletonMap("/foo", new Object()));
    mapping.setApplicationContext(new StaticApplicationContext());
    mapping.initApplicationContext();
    this.endpoint.setHandlerMappings(Collections.<AbstractUrlHandlerMapping>singletonList(mapping));
    Map<String, Object> result = this.endpoint.invoke();
    assertThat(result).hasSize(1);
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) result.get("/foo");
    assertThat(map.get("type")).isEqualTo("java.lang.Object");
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Map(java.util.Map) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 35 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method getResourceTransformers.

protected List<ResourceTransformer> getResourceTransformers(String mapping) {
    SimpleUrlHandlerMapping handler = (SimpleUrlHandlerMapping) this.context.getBean("resourceHandlerMapping");
    ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler.getHandlerMap().get(mapping);
    return resourceHandler.getResourceTransformers();
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)

Aggregations

SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)36 Test (org.junit.Test)25 ResourceHttpRequestHandler (org.springframework.web.servlet.resource.ResourceHttpRequestHandler)9 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)8 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)7 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)6 Map (java.util.Map)5 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)5 HandlerMapping (org.springframework.web.servlet.HandlerMapping)5 ModelAndView (org.springframework.web.servlet.ModelAndView)5 DefaultServletHttpRequestHandler (org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler)5 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)5 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)5 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)5 LinkedHashMap (java.util.LinkedHashMap)3 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 ParameterizableViewController (org.springframework.web.servlet.mvc.ParameterizableViewController)3 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2