Search in sources :

Example 36 with HandlerExecutionChain

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

the class AbstractUrlHandlerMapping method buildPathExposingHandler.

/**
	 * Build a handler object for the given raw handler, exposing the actual
	 * handler, the {@link #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE}, as well as
	 * the {@link #URI_TEMPLATE_VARIABLES_ATTRIBUTE} before executing the handler.
	 * <p>The default implementation builds a {@link HandlerExecutionChain}
	 * with a special interceptor that exposes the path attribute and uri template variables
	 * @param rawHandler the raw handler to expose
	 * @param pathWithinMapping the path to expose before executing the handler
	 * @param uriTemplateVariables the URI template variables, can be {@code null} if no variables found
	 * @return the final handler object
	 */
protected Object buildPathExposingHandler(Object rawHandler, String bestMatchingPattern, String pathWithinMapping, Map<String, String> uriTemplateVariables) {
    HandlerExecutionChain chain = new HandlerExecutionChain(rawHandler);
    chain.addInterceptor(new PathExposingHandlerInterceptor(bestMatchingPattern, pathWithinMapping));
    if (!CollectionUtils.isEmpty(uriTemplateVariables)) {
        chain.addInterceptor(new UriTemplateVariablesHandlerInterceptor(uriTemplateVariables));
    }
    return chain;
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain)

Example 37 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain 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)

Example 38 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain 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 39 with HandlerExecutionChain

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

the class WebMvcConfigurationSupportTests method requestMappingHandlerMapping.

@Test
public void requestMappingHandlerMapping() throws Exception {
    ApplicationContext context = initContext(WebConfig.class, ScopedController.class, ScopedProxyController.class);
    RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
    assertEquals(0, handlerMapping.getOrder());
    HandlerExecutionChain chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
    assertNotNull(chain);
    assertNotNull(chain.getInterceptors());
    assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[0].getClass());
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scoped"));
    assertNotNull("HandlerExecutionChain for '/scoped' mapping should not be null.", chain);
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scopedProxy"));
    assertNotNull("HandlerExecutionChain for '/scopedProxy' mapping should not be null.", chain);
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) Test(org.junit.Test)

Example 40 with HandlerExecutionChain

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

the class WebMvcConfigurationSupportTests method beanNameHandlerMapping.

@Test
public void beanNameHandlerMapping() throws Exception {
    ApplicationContext context = initContext(WebConfig.class);
    BeanNameUrlHandlerMapping handlerMapping = context.getBean(BeanNameUrlHandlerMapping.class);
    assertEquals(2, handlerMapping.getOrder());
    HttpServletRequest request = new MockHttpServletRequest("GET", "/testController");
    HandlerExecutionChain chain = handlerMapping.getHandler(request);
    assertNotNull(chain);
    assertNotNull(chain.getInterceptors());
    assertEquals(3, chain.getInterceptors().length);
    assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
    assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)61 Test (org.junit.Test)47 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)28 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)16 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)11 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)9 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)8 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)7 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)7 HandlerMethod (org.springframework.web.method.HandlerMethod)6 ModelAndView (org.springframework.web.servlet.ModelAndView)6 LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)5 ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)5 Mock (org.jmock.Mock)4 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 HttpRequestHandlerAdapter (org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ApplicationContext (org.springframework.context.ApplicationContext)2