use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.
the class CrossOriginTests method getCorsConfiguration.
@Nullable
private CorsConfiguration getCorsConfiguration(@Nullable HandlerExecutionChain chain, boolean isPreFlightRequest) {
assertThat(chain).isNotNull();
if (isPreFlightRequest) {
Object handler = chain.getHandler();
assertThat(handler.getClass().getSimpleName()).isEqualTo("PreFlightHandler");
DirectFieldAccessor accessor = new DirectFieldAccessor(handler);
return (CorsConfiguration) accessor.getPropertyValue("config");
} else {
for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
if (interceptor.getClass().getSimpleName().equals("CorsInterceptor")) {
DirectFieldAccessor accessor = new DirectFieldAccessor(interceptor);
return (CorsConfiguration) accessor.getPropertyValue("config");
}
}
}
return null;
}
use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.
the class RequestMappingInfoHandlerMappingTests method getHandlerMappedInterceptors.
@Test
void getHandlerMappedInterceptors() throws Exception {
String path = "/foo";
HandlerInterceptor interceptor = new HandlerInterceptor() {
};
MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { path }, interceptor);
TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping();
mapping.registerHandler(new TestController());
mapping.setInterceptors(mappedInterceptor);
mapping.setApplicationContext(new StaticWebApplicationContext());
HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path));
assertThat(chain).isNotNull();
assertThat(chain.getInterceptorList().get(0)).isSameAs(interceptor);
chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid"));
assertThat(chain).isNull();
}
use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.
the class MvcNamespaceTests method testResources.
@Test
@SuppressWarnings("deprecation")
public void testResources() throws Exception {
loadBeanDefinitions("mvc-config-resources.xml");
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
assertThat(adapter).isNotNull();
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
assertThat(handler).isNotNull();
assertThat(handler.getContentNegotiationManager()).isSameAs(manager);
SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertThat(resourceMapping).isNotNull();
assertThat(resourceMapping.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 1);
BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
assertThat(beanNameMapping).isNotNull();
assertThat(beanNameMapping.getOrder()).isEqualTo(2);
ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
assertThat(urlProvider).isNotNull();
Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
List<Class<?>> interceptors = beans.values().stream().map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass()).collect(Collectors.toList());
assertThat(interceptors).contains(ConversionServiceExposingInterceptor.class, ResourceUrlProviderExposingInterceptor.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/resources/foo.css");
request.setMethod("GET");
HandlerExecutionChain chain = resourceMapping.getHandler(request);
assertThat(chain).isNotNull();
assertThat(chain.getHandler() instanceof ResourceHttpRequestHandler).isTrue();
MockHttpServletResponse response = new MockHttpServletResponse();
for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
interceptor.preHandle(request, response, chain.getHandler());
}
ModelAndView mv = adapter.handle(request, response, chain.getHandler());
assertThat((Object) mv).isNull();
}
use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportExtensionTests method handlerMappings.
@Test
public void handlerMappings() throws Exception {
RequestMappingHandlerMapping rmHandlerMapping = this.config.requestMappingHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
rmHandlerMapping.setApplicationContext(this.context);
rmHandlerMapping.afterPropertiesSet();
assertThat(rmHandlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(rmHandlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
HandlerExecutionChain chain = rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
assertThat(chain).isNotNull();
HandlerInterceptor[] interceptors = chain.getInterceptors();
assertThat(interceptors).isNotNull();
assertThat(interceptors.length).isEqualTo(4);
assertThat(interceptors[0].getClass().getSimpleName()).isEqualTo("CorsInterceptor");
assertThat(interceptors[1].getClass()).isEqualTo(LocaleChangeInterceptor.class);
assertThat(interceptors[2].getClass()).isEqualTo(ConversionServiceExposingInterceptor.class);
assertThat(interceptors[3].getClass()).isEqualTo(ResourceUrlProviderExposingInterceptor.class);
Map<RequestMappingInfo, HandlerMethod> map = rmHandlerMapping.getHandlerMethods();
assertThat(map.size()).isEqualTo(2);
RequestMappingInfo info = map.entrySet().stream().filter(entry -> entry.getValue().getBeanType().equals(UserController.class)).findFirst().orElseThrow(() -> new AssertionError("UserController bean not found")).getKey();
assertThat(info.getPatternValues()).isEqualTo(Collections.singleton("/api/user/{id}"));
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) this.config.viewControllerHandlerMapping(this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo(1);
assertThat(handlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(handlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/bad"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/old"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
handlerMapping = (AbstractHandlerMapping) this.config.resourceHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo((Integer.MAX_VALUE - 1));
assertThat(handlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(handlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
interceptors = chain.getInterceptors();
assertThat(interceptors.length).as(Arrays.toString(interceptors)).isEqualTo(5);
assertThat(interceptors[0].getClass().getSimpleName()).isEqualTo("CorsInterceptor");
// PathExposingHandlerInterceptor at interceptors[1]
assertThat(interceptors[2].getClass()).isEqualTo(LocaleChangeInterceptor.class);
assertThat(interceptors[3].getClass()).isEqualTo(ConversionServiceExposingInterceptor.class);
assertThat(interceptors[4].getClass()).isEqualTo(ResourceUrlProviderExposingInterceptor.class);
handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping();
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo(Integer.MAX_VALUE);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
}
use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.
the class HandlerMappingTests method orderedInterceptors.
@PathPatternsParameterizedTest
void orderedInterceptors(TestHandlerMapping mapping, Function<String, MockHttpServletRequest> requestFactory) throws Exception {
MappedInterceptor i1 = new MappedInterceptor(new String[] { "/**" }, mock(HandlerInterceptor.class));
HandlerInterceptor i2 = mock(HandlerInterceptor.class);
MappedInterceptor i3 = new MappedInterceptor(new String[] { "/**" }, mock(HandlerInterceptor.class));
HandlerInterceptor i4 = mock(HandlerInterceptor.class);
mapping.setInterceptors(i1, i2, i3, i4);
mapping.setApplicationContext(new StaticWebApplicationContext());
HandlerExecutionChain chain = mapping.getHandler(requestFactory.apply("/"));
assertThat(chain).isNotNull();
assertThat(chain.getInterceptorList()).contains(i1.getInterceptor(), i2, i3.getInterceptor(), i4);
}
Aggregations