Search in sources :

Example 1 with HandlerExecutionChain

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

the class StandaloneMockMvcBuilderTests method placeHoldersInRequestMapping.

// SPR-10825
@Test
public void placeHoldersInRequestMapping() throws Exception {
    TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
    builder.addPlaceholderValue("sys.login.ajax", "/foo");
    builder.build();
    RequestMappingHandlerMapping hm = builder.wac.getBean(RequestMappingHandlerMapping.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    HandlerExecutionChain chain = hm.getHandler(request);
    assertNotNull(chain);
    assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 2 with HandlerExecutionChain

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

the class CloudFoundryEndpointHandlerMappingTests method registersCloudFoundryHealthEndpoint.

@Test
public void registersCloudFoundryHealthEndpoint() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    HealthEndpoint delegate = new HealthEndpoint(new OrderedHealthAggregator(), Collections.<String, HealthIndicator>emptyMap());
    CloudFoundryEndpointHandlerMapping handlerMapping = new CloudFoundryEndpointHandlerMapping(Collections.singleton(new TestHealthMvcEndpoint(delegate)), null, null);
    handlerMapping.setPrefix("/test");
    handlerMapping.setApplicationContext(context);
    handlerMapping.afterPropertiesSet();
    HandlerExecutionChain handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/test/health"));
    HandlerMethod handlerMethod = (HandlerMethod) handler.getHandler();
    Object handlerMethodBean = handlerMethod.getBean();
    assertThat(handlerMethodBean).isInstanceOf(CloudFoundryHealthMvcEndpoint.class);
}
Also used : HealthEndpoint(org.springframework.boot.actuate.endpoint.HealthEndpoint) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OrderedHealthAggregator(org.springframework.boot.actuate.health.OrderedHealthAggregator) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 3 with HandlerExecutionChain

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

the class AbstractHandlerMapping method getHandler.

/**
	 * Look up a handler for the given request, falling back to the default
	 * handler if no specific one is found.
	 * @param request current HTTP request
	 * @return the corresponding handler instance, or the default handler
	 * @see #getHandlerInternal
	 */
@Override
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    Object handler = getHandlerInternal(request);
    if (handler == null) {
        handler = getDefaultHandler();
    }
    if (handler == null) {
        return null;
    }
    // Bean name or resolved handler?
    if (handler instanceof String) {
        String handlerName = (String) handler;
        handler = getApplicationContext().getBean(handlerName);
    }
    HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
    if (CorsUtils.isCorsRequest(request)) {
        CorsConfiguration globalConfig = this.globalCorsConfigSource.getCorsConfiguration(request);
        CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
        CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
        executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
    }
    return executionChain;
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain)

Example 4 with HandlerExecutionChain

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

the class WebMvcConfigurationSupportExtensionTests method contentNegotiation.

@Test
public void contentNegotiation() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
    NativeWebRequest webRequest = new ServletWebRequest(request);
    RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping();
    ContentNegotiationManager manager = mapping.getContentNegotiationManager();
    assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
    request.setRequestURI("/foo.xml");
    assertEquals(Collections.singletonList(APPLICATION_XML), manager.resolveMediaTypes(webRequest));
    request.setRequestURI("/foo.rss");
    assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));
    request.setRequestURI("/foo.atom");
    assertEquals(Collections.singletonList(APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest));
    request.setRequestURI("/foo");
    request.setParameter("f", "json");
    assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
    request.setRequestURI("/resources/foo.gif");
    SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping();
    handlerMapping.setApplicationContext(this.context);
    HandlerExecutionChain chain = handlerMapping.getHandler(request);
    assertNotNull(chain);
    ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
    assertNotNull(handler);
    assertSame(manager, handler.getContentNegotiationManager());
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 5 with HandlerExecutionChain

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

the class WebMvcConfigurationSupportExtensionTests method handlerMappings.

@Test
public void handlerMappings() throws Exception {
    RequestMappingHandlerMapping rmHandlerMapping = this.config.requestMappingHandlerMapping();
    rmHandlerMapping.setApplicationContext(this.context);
    rmHandlerMapping.afterPropertiesSet();
    assertEquals(TestPathHelper.class, rmHandlerMapping.getUrlPathHelper().getClass());
    assertEquals(TestPathMatcher.class, rmHandlerMapping.getPathMatcher().getClass());
    HandlerExecutionChain chain = rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
    assertNotNull(chain);
    assertNotNull(chain.getInterceptors());
    assertEquals(3, chain.getInterceptors().length);
    assertEquals(LocaleChangeInterceptor.class, chain.getInterceptors()[0].getClass());
    assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
    assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
    AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) this.config.viewControllerHandlerMapping();
    handlerMapping.setApplicationContext(this.context);
    assertNotNull(handlerMapping);
    assertEquals(1, handlerMapping.getOrder());
    assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
    assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
    assertNotNull(chain);
    assertNotNull(chain.getHandler());
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/bad"));
    assertNotNull(chain);
    assertNotNull(chain.getHandler());
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/old"));
    assertNotNull(chain);
    assertNotNull(chain.getHandler());
    handlerMapping = (AbstractHandlerMapping) this.config.resourceHandlerMapping();
    handlerMapping.setApplicationContext(this.context);
    assertNotNull(handlerMapping);
    assertEquals(Integer.MAX_VALUE - 1, handlerMapping.getOrder());
    assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
    assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
    assertNotNull(chain);
    assertNotNull(chain.getHandler());
    assertEquals(Arrays.toString(chain.getInterceptors()), 2, chain.getInterceptors().length);
    // PathExposingHandlerInterceptor at chain.getInterceptors()[0]
    assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[1].getClass());
    handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping();
    handlerMapping.setApplicationContext(this.context);
    assertNotNull(handlerMapping);
    assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath"));
    assertNotNull(chain);
    assertNotNull(chain.getHandler());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) 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