Search in sources :

Example 1 with MappedInterceptor

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;
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor)

Example 2 with 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);
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) Validator(org.springframework.validation.Validator) ResourceUrlProvider(org.springframework.web.servlet.resource.ResourceUrlProvider) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) MockRequestDispatcher(org.springframework.mock.web.test.MockRequestDispatcher) CacheControl(org.springframework.http.CacheControl) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Map(java.util.Map) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) SpringBeanPreparerFactory(org.springframework.web.servlet.view.tiles3.SpringBeanPreparerFactory) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) RequestDispatcher(javax.servlet.RequestDispatcher) NotNull(javax.validation.constraints.NotNull) TypeMismatchException(org.springframework.beans.TypeMismatchException) StandardCharsets(java.nio.charset.StandardCharsets) NumberFormat(org.springframework.format.annotation.NumberFormat) MockServletContext(org.springframework.mock.web.test.MockServletContext) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) DeferredResultProcessingInterceptor(org.springframework.web.context.request.async.DeferredResultProcessingInterceptor) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) TilesConfigurer(org.springframework.web.servlet.view.tiles3.TilesConfigurer) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Ordered(org.springframework.core.Ordered) Errors(org.springframework.validation.Errors) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) BindingResult(org.springframework.validation.BindingResult) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) CacheManager(org.springframework.cache.CacheManager) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) ConversionService(org.springframework.core.convert.ConversionService) Before(org.junit.Before) Validated(org.springframework.validation.annotation.Validated) ViewResolver(org.springframework.web.servlet.ViewResolver) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) PathMatcher(org.springframework.util.PathMatcher) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) Target(java.lang.annotation.Target) Test(org.junit.Test) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) GroovyMarkupViewResolver(org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) UrlPathHelper(org.springframework.web.util.UrlPathHelper) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Assert(org.junit.Assert) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) Date(java.util.Date) AppCacheManifestTransformer(org.springframework.web.servlet.resource.AppCacheManifestTransformer) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) InternalResourceView(org.springframework.web.servlet.view.InternalResourceView) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) RedirectView(org.springframework.web.servlet.view.RedirectView) Locale(java.util.Locale) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Method(java.lang.reflect.Method) UnresolvingLocaleDefinitionsFactory(org.apache.tiles.definition.UnresolvingLocaleDefinitionsFactory) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) Collectors(java.util.stream.Collectors) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) UserRoleAuthorizationInterceptor(org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ConcurrentTaskExecutor(org.springframework.scheduling.concurrent.ConcurrentTaskExecutor) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) DeferredResultProcessingInterceptorAdapter(org.springframework.web.context.request.async.DeferredResultProcessingInterceptorAdapter) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) CallableProcessingInterceptorAdapter(org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter) ContentVersionStrategy(org.springframework.web.servlet.resource.ContentVersionStrategy) GroovyMarkupConfigurer(org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer) MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) Cache(org.springframework.cache.Cache) ClassPathResource(org.springframework.core.io.ClassPathResource) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Controller(org.springframework.stereotype.Controller) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Retention(java.lang.annotation.Retention) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) DefaultServletHttpRequestHandler(org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) ScriptTemplateConfigurer(org.springframework.web.servlet.view.script.ScriptTemplateConfigurer) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) HttpRequestHandlerAdapter(org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter) CallableProcessingInterceptor(org.springframework.web.context.request.async.CallableProcessingInterceptor) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) GzipResourceResolver(org.springframework.web.servlet.resource.GzipResourceResolver) TilesViewResolver(org.springframework.web.servlet.view.tiles3.TilesViewResolver) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Matchers(org.hamcrest.Matchers) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) ParameterizableViewController(org.springframework.web.servlet.mvc.ParameterizableViewController) ElementType(java.lang.annotation.ElementType) TimeUnit(java.util.concurrent.TimeUnit) LocalDate(org.joda.time.LocalDate) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) FixedVersionStrategy(org.springframework.web.servlet.resource.FixedVersionStrategy) Comparator(java.util.Comparator) Collections(java.util.Collections) RetentionPolicy(java.lang.annotation.RetentionPolicy) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) HttpRequestHandlerAdapter(org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) ResourceUrlProvider(org.springframework.web.servlet.resource.ResourceUrlProvider) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with MappedInterceptor

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;
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) PathMatcher(org.springframework.util.PathMatcher) AntPathMatcher(org.springframework.util.AntPathMatcher) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) ArrayList(java.util.ArrayList) AntPathMatcher(org.springframework.util.AntPathMatcher)

Example 4 with MappedInterceptor

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());
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) PathMatcher(org.springframework.util.PathMatcher) AntPathMatcher(org.springframework.util.AntPathMatcher) Test(org.junit.Test)

Example 5 with MappedInterceptor

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);
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Aggregations

MappedInterceptor (org.springframework.web.servlet.handler.MappedInterceptor)5 Test (org.junit.Test)3 PathMatcher (org.springframework.util.PathMatcher)3 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 AntPathMatcher (org.springframework.util.AntPathMatcher)2 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 ElementType (java.lang.annotation.ElementType)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Target (java.lang.annotation.Target)1 Method (java.lang.reflect.Method)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1