Search in sources :

Example 16 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping 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);
    assertNotNull(hm);
    assertThat(hm, Matchers.instanceOf(SimpleUrlHandlerMapping.class));
    SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
    assertThat(suhm.getUrlMap().keySet(), Matchers.hasSize(1));
    assertThat(suhm.getUrlMap().values(), Matchers.hasSize(1));
    assertEquals(2, suhm.getOrder());
    HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo/**");
    assertNotNull(httpRequestHandler);
    assertThat(httpRequestHandler, Matchers.instanceOf(SockJsHttpRequestHandler.class));
    SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
    WebSocketHandler wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
    assertNotNull(wsHandler);
    assertThat(wsHandler, Matchers.instanceOf(SubProtocolWebSocketHandler.class));
    assertNotNull(sockJsHttpRequestHandler.getSockJsService());
    UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
    assertNotNull(userDestResolver);
    assertThat(userDestResolver, Matchers.instanceOf(DefaultUserDestinationResolver.class));
    DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
    assertEquals("/user/", defaultUserDestResolver.getDestinationPrefix());
    StompBrokerRelayMessageHandler messageBroker = this.appContext.getBean(StompBrokerRelayMessageHandler.class);
    assertNotNull(messageBroker);
    assertEquals("clientlogin", messageBroker.getClientLogin());
    assertEquals("clientpass", messageBroker.getClientPasscode());
    assertEquals("syslogin", messageBroker.getSystemLogin());
    assertEquals("syspass", messageBroker.getSystemPasscode());
    assertEquals("relayhost", messageBroker.getRelayHost());
    assertEquals(1234, messageBroker.getRelayPort());
    assertEquals("spring.io", messageBroker.getVirtualHost());
    assertEquals(5000, messageBroker.getSystemHeartbeatReceiveInterval());
    assertEquals(5000, messageBroker.getSystemHeartbeatSendInterval());
    assertThat(messageBroker.getDestinationPrefixes(), Matchers.containsInAnyOrder("/topic", "/queue"));
    List<Class<? extends MessageHandler>> subscriberTypes = Arrays.<Class<? extends MessageHandler>>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, 1);
    testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(StompBrokerRelayMessageHandler.class, UserDestinationMessageHandler.class);
    testChannel("brokerChannel", subscriberTypes, 1);
    try {
        this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class);
        fail("expected exception");
    } catch (NoSuchBeanDefinitionException ex) {
    // expected
    }
    String destination = "/topic/unresolved-user-destination";
    UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
    assertEquals(destination, userDestHandler.getBroadcastDestination());
    assertNotNull(messageBroker.getSystemSubscriptions());
    assertSame(userDestHandler, messageBroker.getSystemSubscriptions().get(destination));
    destination = "/topic/simp-user-registry";
    UserRegistryMessageHandler userRegistryHandler = this.appContext.getBean(UserRegistryMessageHandler.class);
    assertEquals(destination, userRegistryHandler.getBroadcastDestination());
    assertNotNull(messageBroker.getSystemSubscriptions());
    assertSame(userRegistryHandler, messageBroker.getSystemSubscriptions().get(destination));
    SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
    assertEquals(MultiServerUserRegistry.class, userRegistry.getClass());
    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\\], " + "outboundChannelpool size = \\d, active threads = \\d, queued tasks = \\d, completed tasks = \\d\\], " + "sockJsScheduler\\[pool size = \\d, active threads = \\d, queued tasks = \\d, completed tasks = \\d\\]";
    assertTrue("\nExpected: " + expected.replace("\\", "") + "\n  Actual: " + actual, actual.matches(expected));
}
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) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) Test(org.junit.Test)

Example 17 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project cas by apereo.

the class CasWebAppConfiguration method handlerMapping.

@Bean
public SimpleUrlHandlerMapping handlerMapping() {
    final SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    final Controller root = rootController();
    mapping.setOrder(1);
    mapping.setAlwaysUseFullPath(true);
    mapping.setRootHandler(root);
    final Map urls = new HashMap();
    urls.put("/", root);
    mapping.setUrlMap(urls);
    return mapping;
}
Also used : HashMap(java.util.HashMap) Controller(org.springframework.web.servlet.mvc.Controller) ParameterizableViewController(org.springframework.web.servlet.mvc.ParameterizableViewController) HashMap(java.util.HashMap) Map(java.util.Map) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) ServletListenerRegistrationBean(org.springframework.boot.web.servlet.ServletListenerRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 18 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project cas by apereo.

the class CasManagementWebAppConfiguration method handlerMappingC.

@Bean
public SimpleUrlHandlerMapping handlerMappingC() {
    final SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setOrder(1);
    mapping.setAlwaysUseFullPath(true);
    mapping.setRootHandler(rootController());
    final Properties properties = new Properties();
    properties.put("/*.html", new UrlFilenameViewController());
    mapping.setMappings(properties);
    return mapping;
}
Also used : UrlFilenameViewController(org.springframework.web.servlet.mvc.UrlFilenameViewController) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) ServerProperties(org.springframework.boot.autoconfigure.web.ServerProperties) Properties(java.util.Properties) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 19 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project gocd by gocd.

the class InterceptorInjectorTest method testShouldNotChangeHandler.

public void testShouldNotChangeHandler() throws Throwable {
    SimpleUrlHandlerMapping handler = new SimpleUrlHandlerMapping();
    Mock proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    proceedingJoinPoint.expects(once()).method("proceed").will(returnValue(new HandlerExecutionChain(handler, null)));
    InterceptorInjector injector = new InterceptorInjector();
    HandlerExecutionChain handlers = injector.mergeInterceptorsToTabs((ProceedingJoinPoint) proceedingJoinPoint.proxy());
    assertSame(handler, handlers.getHandler());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Mock(org.jmock.Mock)

Example 20 with SimpleUrlHandlerMapping

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

the class MvcNamespaceTests method testViewControllersOnWebSphere.

/** WebSphere gives trailing servlet path slashes by default!! */
@Test
public void testViewControllersOnWebSphere() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml", 19);
    SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
    SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/myapp/app/bar");
    request.setContextPath("/myapp");
    request.setServletPath("/app/");
    request.setAttribute("com.ibm.websphere.servlet.uri_non_decoded", "/myapp/app/bar");
    HandlerExecutionChain chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("baz", mv2.getViewName());
    request.setRequestURI("/myapp/app/");
    request.setContextPath("/myapp");
    request.setServletPath("/app/");
    chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    ModelAndView mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("root", mv3.getViewName());
    request.setRequestURI("/myapp/");
    request.setContextPath("/myapp");
    request.setServletPath("/");
    chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("root", mv3.getViewName());
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

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