Search in sources :

Example 1 with HandlerMapping

use of org.springframework.web.servlet.HandlerMapping in project spring-integration by spring-projects.

the class IntegrationGraphControllerTests method testIntegrationGraphControllerParser.

@Test
public void testIntegrationGraphControllerParser() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("IntegrationGraphControllerParserTests-context.xml", getClass());
    HandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class.getName(), HandlerMapping.class);
    HandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/foo");
    request.addHeader(HttpHeaders.ORIGIN, "http://foo.bar.com");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HandlerExecutionChain executionChain = handlerMapping.getHandler(request);
    assertNotNull(executionChain);
    Object handler = executionChain.getHandler();
    for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
        // Assert the CORS config
        assertTrue(handlerInterceptor.preHandle(request, response, handler));
    }
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertThat(response.getContentAsString(), containsString("\"name\":\"nullChannel\","));
    assertThat(response.getContentAsString(), not(containsString("\"name\":\"myChannel\",")));
    context.getBeanFactory().registerSingleton("myChannel", new DirectChannel());
    request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/foo/refresh");
    response = new MockHttpServletResponse();
    executionChain = handlerMapping.getHandler(request);
    assertNotNull(executionChain);
    handler = executionChain.getHandler();
    for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
        // Assert the CORS config
        assertTrue(handlerInterceptor.preHandle(request, response, handler));
    }
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertThat(response.getContentAsString(), containsString("\"name\":\"myChannel\","));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) HandlerAdapter(org.springframework.web.servlet.HandlerAdapter) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with HandlerMapping

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

the class HandlerMappingIntrospector method initFallback.

private static List<HandlerMapping> initFallback(ApplicationContext applicationContext) {
    Properties props;
    String path = "DispatcherServlet.properties";
    try {
        Resource resource = new ClassPathResource(path, DispatcherServlet.class);
        props = PropertiesLoaderUtils.loadProperties(resource);
    } catch (IOException ex) {
        throw new IllegalStateException("Could not load '" + path + "': " + ex.getMessage());
    }
    String value = props.getProperty(HandlerMapping.class.getName());
    String[] names = StringUtils.commaDelimitedListToStringArray(value);
    List<HandlerMapping> result = new ArrayList<>(names.length);
    for (String name : names) {
        try {
            Class<?> clazz = ClassUtils.forName(name, DispatcherServlet.class.getClassLoader());
            Object mapping = applicationContext.getAutowireCapableBeanFactory().createBean(clazz);
            result.add((HandlerMapping) mapping);
        } catch (ClassNotFoundException ex) {
            throw new IllegalStateException("Could not find default HandlerMapping [" + name + "]");
        }
    }
    return result;
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource) HandlerMapping(org.springframework.web.servlet.HandlerMapping)

Example 3 with HandlerMapping

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

the class WebMvcConfigurationSupportTests method emptyHandlerMappings.

@Test
public void emptyHandlerMappings() {
    ApplicationContext context = initContext(WebConfig.class);
    Map<String, HandlerMapping> handlerMappings = context.getBeansOfType(HandlerMapping.class);
    assertThat(handlerMappings.containsKey("viewControllerHandlerMapping")).isFalse();
    assertThat(handlerMappings.containsKey("resourceHandlerMapping")).isFalse();
    assertThat(handlerMappings.containsKey("defaultServletHandlerMapping")).isFalse();
    Object nullBean = context.getBean("viewControllerHandlerMapping");
    assertThat(nullBean.equals(null)).isTrue();
    nullBean = context.getBean("resourceHandlerMapping");
    assertThat(nullBean.equals(null)).isTrue();
    nullBean = context.getBean("defaultServletHandlerMapping");
    assertThat(nullBean.equals(null)).isTrue();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) Test(org.junit.jupiter.api.Test)

Example 4 with HandlerMapping

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

the class BeanNameUrlHandlerMappingTests method asteriskMatches.

@Test
public void asteriskMatches() throws Exception {
    HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
    Object bean = wac.getBean("godCtrl");
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html");
    HandlerExecutionChain hec = hm.getHandler(req);
    assertThat(hec != null && hec.getHandler() == bean).as("Handler is correct bean").isTrue();
    req = new MockHttpServletRequest("GET", "/mypath/testarossa");
    hec = hm.getHandler(req);
    assertThat(hec != null && hec.getHandler() == bean).as("Handler is correct bean").isTrue();
    req = new MockHttpServletRequest("GET", "/mypath/tes");
    hec = hm.getHandler(req);
    assertThat(hec == null).as("Handler is correct bean").isTrue();
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 5 with HandlerMapping

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

the class TestValidator method stompBrokerRelay.

@Test
public void stompBrokerRelay() {
    loadBeanDefinitions("websocket-config-broker-relay.xml");
    HandlerMapping hm = this.appContext.getBean(HandlerMapping.class);
    assertThat(hm).isNotNull();
    assertThat(hm).isInstanceOf(SimpleUrlHandlerMapping.class);
    SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
    assertThat(suhm.getUrlMap()).hasSize(1);
    assertThat(suhm.getOrder()).isEqualTo(2);
    HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo/**");
    assertThat(httpRequestHandler).isNotNull();
    assertThat(httpRequestHandler).isInstanceOf(SockJsHttpRequestHandler.class);
    SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
    WebSocketHandler wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
    assertThat(wsHandler).isNotNull();
    assertThat(wsHandler).isInstanceOf(SubProtocolWebSocketHandler.class);
    assertThat(sockJsHttpRequestHandler.getSockJsService()).isNotNull();
    UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
    assertThat(userDestResolver).isNotNull();
    assertThat(userDestResolver).isInstanceOf(DefaultUserDestinationResolver.class);
    DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
    assertThat(defaultUserDestResolver.getDestinationPrefix()).isEqualTo("/user/");
    StompBrokerRelayMessageHandler messageBroker = this.appContext.getBean(StompBrokerRelayMessageHandler.class);
    assertThat(messageBroker).isNotNull();
    assertThat(messageBroker.getClientLogin()).isEqualTo("clientlogin");
    assertThat(messageBroker.getClientPasscode()).isEqualTo("clientpass");
    assertThat(messageBroker.getSystemLogin()).isEqualTo("syslogin");
    assertThat(messageBroker.getSystemPasscode()).isEqualTo("syspass");
    assertThat(messageBroker.getRelayHost()).isEqualTo("relayhost");
    assertThat(messageBroker.getRelayPort()).isEqualTo(1234);
    assertThat(messageBroker.getVirtualHost()).isEqualTo("spring.io");
    assertThat(messageBroker.getSystemHeartbeatReceiveInterval()).isEqualTo(5000);
    assertThat(messageBroker.getSystemHeartbeatSendInterval()).isEqualTo(5000);
    assertThat(messageBroker.getDestinationPrefixes()).containsExactlyInAnyOrder("/topic", "/queue");
    assertThat(messageBroker.isPreservePublishOrder()).isTrue();
    List<Class<? extends MessageHandler>> subscriberTypes = Arrays.asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, StompBrokerRelayMessageHandler.class);
    testChannel("clientInboundChannel", subscriberTypes, 2);
    testExecutor("clientInboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Collections.singletonList(SubProtocolWebSocketHandler.class);
    testChannel("clientOutboundChannel", subscriberTypes, 2);
    testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Arrays.asList(StompBrokerRelayMessageHandler.class, UserDestinationMessageHandler.class);
    testChannel("brokerChannel", subscriberTypes, 1);
    assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class));
    String destination = "/topic/unresolved-user-destination";
    UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
    assertThat(userDestHandler.getBroadcastDestination()).isEqualTo(destination);
    assertThat(messageBroker.getSystemSubscriptions()).isNotNull();
    assertThat(messageBroker.getSystemSubscriptions().get(destination)).isSameAs(userDestHandler);
    destination = "/topic/simp-user-registry";
    UserRegistryMessageHandler userRegistryHandler = this.appContext.getBean(UserRegistryMessageHandler.class);
    assertThat(userRegistryHandler.getBroadcastDestination()).isEqualTo(destination);
    assertThat(messageBroker.getSystemSubscriptions()).isNotNull();
    assertThat(messageBroker.getSystemSubscriptions().get(destination)).isSameAs(userRegistryHandler);
    SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
    assertThat(userRegistry.getClass()).isEqualTo(MultiServerUserRegistry.class);
    String name = "webSocketMessageBrokerStats";
    WebSocketMessageBrokerStats stats = this.appContext.getBean(name, WebSocketMessageBrokerStats.class);
    String actual = stats.toString();
    String expected = "WebSocketSession\\[0 current WS\\(0\\)-HttpStream\\(0\\)-HttpPoll\\(0\\), " + "0 total, 0 closed abnormally \\(0 connect failure, 0 send limit, 0 transport error\\)], " + "stompSubProtocol\\[processed CONNECT\\(0\\)-CONNECTED\\(0\\)-DISCONNECT\\(0\\)], " + "stompBrokerRelay\\[0 sessions, relayhost:1234 \\(not available\\), " + "processed CONNECT\\(0\\)-CONNECTED\\(0\\)-DISCONNECT\\(0\\)], " + "inboundChannel\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d], " + "outboundChannel\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d], " + "sockJsScheduler\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d]";
    assertThat(actual.matches(expected)).as("\nExpected: " + expected.replace("\\", "") + "\n  Actual: " + actual).isTrue();
}
Also used : UserDestinationResolver(org.springframework.messaging.simp.user.UserDestinationResolver) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpUserRegistry(org.springframework.messaging.simp.user.SimpUserRegistry) DefaultSimpUserRegistry(org.springframework.web.socket.messaging.DefaultSimpUserRegistry) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) HandlerMapping(org.springframework.web.servlet.HandlerMapping) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) Test(org.junit.jupiter.api.Test)

Aggregations

HandlerMapping (org.springframework.web.servlet.HandlerMapping)17 Test (org.junit.jupiter.api.Test)11 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)5 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)5 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)4 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)3 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)3 Properties (java.util.Properties)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)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