Search in sources :

Example 21 with SimpleUrlHandlerMapping

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

the class MvcNamespaceTests method testDefaultServletHandler.

@Test
public void testDefaultServletHandler() throws Exception {
    loadBeanDefinitions("mvc-config-default-servlet.xml", 6);
    HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
    assertNotNull(adapter);
    DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
    assertNotNull(handler);
    SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(mapping);
    assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/foo.css");
    request.setMethod("GET");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mv = adapter.handle(request, response, chain.getHandler());
    assertNull(mv);
}
Also used : HttpRequestHandlerAdapter(org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) DefaultServletHttpRequestHandler(org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 22 with SimpleUrlHandlerMapping

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

the class MvcNamespaceTests method testViewControllers.

@Test
public void testViewControllers() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml", 19);
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertNotNull(mapping);
    mapping.setDefaultHandler(handlerMethod);
    BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
    assertNotNull(beanNameMapping);
    assertEquals(2, beanNameMapping.getOrder());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertEquals(3, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
    SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(mapping2);
    SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
    assertNotNull(adapter);
    request = new MockHttpServletRequest("GET", "/foo");
    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 mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertNull(mv.getViewName());
    request = new MockHttpServletRequest("GET", "/myapp/app/bar");
    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);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("baz", mv.getViewName());
    request = new MockHttpServletRequest("GET", "/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);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("root", mv.getViewName());
    request = new MockHttpServletRequest("GET", "/myapp/app/old");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    request.setQueryString("a=b");
    chain = mapping2.getHandler(request);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertNotNull(mv.getView());
    assertEquals(RedirectView.class, mv.getView().getClass());
    RedirectView redirectView = (RedirectView) mv.getView();
    MockHttpServletResponse response = new MockHttpServletResponse();
    redirectView.render(Collections.emptyMap(), request, response);
    assertEquals("/new?a=b", response.getRedirectedUrl());
    assertEquals(308, response.getStatus());
    request = new MockHttpServletRequest("GET", "/bad");
    chain = mapping2.getHandler(request);
    response = new MockHttpServletResponse();
    mv = adapter.handle(request, response, chain.getHandler());
    assertNull(mv);
    assertEquals(404, response.getStatus());
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 23 with SimpleUrlHandlerMapping

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

the class MvcNamespaceTests method testResourcesWithResolversTransformersCustom.

@Test
public void testResourcesWithResolversTransformersCustom() throws Exception {
    loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml", 12);
    SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(mapping);
    assertNotNull(mapping.getUrlMap().get("/resources/**"));
    ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"), ResourceHttpRequestHandler.class);
    assertNotNull(handler);
    assertThat(handler.getCacheControl().getHeaderValue(), Matchers.equalTo(CacheControl.maxAge(1, TimeUnit.HOURS).sMaxAge(30, TimeUnit.MINUTES).cachePublic().getHeaderValue()));
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers, Matchers.hasSize(3));
    assertThat(resolvers.get(0), Matchers.instanceOf(VersionResourceResolver.class));
    assertThat(resolvers.get(1), Matchers.instanceOf(GzipResourceResolver.class));
    assertThat(resolvers.get(2), Matchers.instanceOf(PathResourceResolver.class));
    VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(0);
    assertThat(versionResolver.getStrategyMap().get("/**/*.js"), Matchers.instanceOf(FixedVersionStrategy.class));
    assertThat(versionResolver.getStrategyMap().get("/**"), Matchers.instanceOf(ContentVersionStrategy.class));
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers, Matchers.hasSize(2));
    assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
    assertThat(transformers.get(1), Matchers.instanceOf(AppCacheManifestTransformer.class));
}
Also used : FixedVersionStrategy(org.springframework.web.servlet.resource.FixedVersionStrategy) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) AppCacheManifestTransformer(org.springframework.web.servlet.resource.AppCacheManifestTransformer) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver) ContentVersionStrategy(org.springframework.web.servlet.resource.ContentVersionStrategy) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) Test(org.junit.Test)

Example 24 with SimpleUrlHandlerMapping

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

the class ViewControllerRegistryTests method order.

@Test
public void order() {
    this.registry.addViewController("/path");
    SimpleUrlHandlerMapping handlerMapping = getHandlerMapping();
    assertEquals(1, handlerMapping.getOrder());
    this.registry.setOrder(2);
    handlerMapping = getHandlerMapping();
    assertEquals(2, handlerMapping.getOrder());
}
Also used : SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 25 with SimpleUrlHandlerMapping

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

the class MvcNamespaceTests method testResourcesWithResolversTransformers.

@Test
public void testResourcesWithResolversTransformers() throws Exception {
    loadBeanDefinitions("mvc-config-resources-chain.xml", 11);
    SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(mapping);
    assertNotNull(mapping.getUrlMap().get("/resources/**"));
    ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"), ResourceHttpRequestHandler.class);
    assertNotNull(handler);
    List<ResourceResolver> resolvers = handler.getResourceResolvers();
    assertThat(resolvers, Matchers.hasSize(4));
    assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
    assertThat(resolvers.get(1), Matchers.instanceOf(VersionResourceResolver.class));
    assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
    assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));
    CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
    assertThat(cachingResolver.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
    assertEquals("test-resource-cache", cachingResolver.getCache().getName());
    VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(1);
    assertThat(versionResolver.getStrategyMap().get("/**/*.js"), Matchers.instanceOf(FixedVersionStrategy.class));
    assertThat(versionResolver.getStrategyMap().get("/**"), Matchers.instanceOf(ContentVersionStrategy.class));
    List<ResourceTransformer> transformers = handler.getResourceTransformers();
    assertThat(transformers, Matchers.hasSize(3));
    assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
    assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
    assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
    CachingResourceTransformer cachingTransformer = (CachingResourceTransformer) transformers.get(0);
    assertThat(cachingTransformer.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
    assertEquals("test-resource-cache", cachingTransformer.getCache().getName());
}
Also used : FixedVersionStrategy(org.springframework.web.servlet.resource.FixedVersionStrategy) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) AppCacheManifestTransformer(org.springframework.web.servlet.resource.AppCacheManifestTransformer) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver) ContentVersionStrategy(org.springframework.web.servlet.resource.ContentVersionStrategy) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) 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