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());
}
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);
}
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;
}
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());
}
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());
}
Aggregations