use of org.springframework.web.servlet.handler.MappedInterceptor in project spring-framework by spring-projects.
the class InterceptorRegistration method getInterceptor.
/**
* Returns the underlying interceptor. If URL patterns are provided the returned type is
* {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}.
*/
protected Object getInterceptor() {
if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) {
return this.interceptor;
}
String[] include = toArray(this.includePatterns);
String[] exclude = toArray(this.excludePatterns);
MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor);
if (this.pathMatcher != null) {
mappedInterceptor.setPathMatcher(this.pathMatcher);
}
return mappedInterceptor;
}
use of org.springframework.web.servlet.handler.MappedInterceptor in project spring-framework by spring-projects.
the class MvcNamespaceTests method testResources.
@Test
public void testResources() throws Exception {
loadBeanDefinitions("mvc-config-resources.xml", 20);
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
assertNotNull(adapter);
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
assertNotNull(handler);
assertSame(manager, handler.getContentNegotiationManager());
SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertNotNull(resourceMapping);
assertEquals(Ordered.LOWEST_PRECEDENCE - 1, resourceMapping.getOrder());
BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
assertNotNull(beanNameMapping);
assertEquals(2, beanNameMapping.getOrder());
ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
assertNotNull(urlProvider);
Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
List<Class<?>> interceptors = beans.values().stream().map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass()).collect(Collectors.toList());
assertThat(interceptors, containsInAnyOrder(ConversionServiceExposingInterceptor.class, ResourceUrlProviderExposingInterceptor.class));
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/resources/foo.css");
request.setMethod("GET");
HandlerExecutionChain chain = resourceMapping.getHandler(request);
assertNotNull(chain);
assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler);
MockHttpServletResponse response = new MockHttpServletResponse();
for (HandlerInterceptor interceptor : chain.getInterceptors()) {
interceptor.preHandle(request, response, chain.getHandler());
}
ModelAndView mv = adapter.handle(request, response, chain.getHandler());
assertNull(mv);
}
use of org.springframework.web.servlet.handler.MappedInterceptor in project spring-framework by spring-projects.
the class InterceptorRegistryTests method getInterceptorsForPath.
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
PathMatcher pathMatcher = new AntPathMatcher();
List<HandlerInterceptor> result = new ArrayList<>();
for (Object interceptor : this.registry.getInterceptors()) {
if (interceptor instanceof MappedInterceptor) {
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
result.add(mappedInterceptor.getInterceptor());
}
} else if (interceptor instanceof HandlerInterceptor) {
result.add((HandlerInterceptor) interceptor);
} else {
fail("Unexpected interceptor type: " + interceptor.getClass().getName());
}
}
return result;
}
use of org.springframework.web.servlet.handler.MappedInterceptor in project spring-framework by spring-projects.
the class InterceptorRegistryTests method addInterceptorsWithCustomPathMatcher.
@Test
public void addInterceptorsWithCustomPathMatcher() {
PathMatcher pathMatcher = Mockito.mock(PathMatcher.class);
this.registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").pathMatcher(pathMatcher);
MappedInterceptor mappedInterceptor = (MappedInterceptor) this.registry.getInterceptors().get(0);
assertSame(pathMatcher, mappedInterceptor.getPathMatcher());
}
use of org.springframework.web.servlet.handler.MappedInterceptor in project spring-framework by spring-projects.
the class RequestMappingInfoHandlerMappingTests method getHandlerMappedInterceptors.
@Test
public void getHandlerMappedInterceptors() throws Exception {
String path = "/foo";
HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {
};
MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { path }, interceptor);
TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping();
mapping.registerHandler(new TestController());
mapping.setInterceptors(new Object[] { mappedInterceptor });
mapping.setApplicationContext(new StaticWebApplicationContext());
HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path));
assertNotNull(chain);
assertNotNull(chain.getInterceptors());
assertSame(interceptor, chain.getInterceptors()[0]);
chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid"));
assertNull(chain);
}
Aggregations