/** * Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped * resource handlers. To configure resource handling, override * {@link #addResourceHandlers}. */ @Bean @Nullable public HandlerMapping resourceHandlerMapping() { Assert.state(this.applicationContext != null, "No ApplicationContext set"); Assert.state(this.servletContext != null, "No ServletContext set"); ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext, this.servletContext, mvcContentNegotiationManager(), mvcUrlPathHelper()); addResourceHandlers(registry); AbstractHandlerMapping handlerMapping = registry.getHandlerMapping(); if (handlerMapping == null) { return null; } handlerMapping.setPathMatcher(mvcPathMatcher()); handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); handlerMapping.setInterceptors(getInterceptors()); handlerMapping.setCorsConfigurations(getCorsConfigurations()); return handlerMapping; }
/** * Return a handler mapping ordered at 1 to map URL paths directly to * view names. To configure view controllers, override * {@link #addViewControllers}. */ @Bean @Nullable public HandlerMapping viewControllerHandlerMapping() { ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext); addViewControllers(registry); AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping(); if (handlerMapping == null) { return null; } handlerMapping.setPathMatcher(mvcPathMatcher()); handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); handlerMapping.setInterceptors(getInterceptors()); handlerMapping.setCorsConfigurations(getCorsConfigurations()); return handlerMapping; }
@Test public void orderedInterceptors() throws Exception { HandlerInterceptor i1 = Mockito.mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor1 = new MappedInterceptor(new String[]{"/**"}, i1); HandlerInterceptor i2 = Mockito.mock(HandlerInterceptor.class); HandlerInterceptor i3 = Mockito.mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor3 = new MappedInterceptor(new String[]{"/**"}, i3); HandlerInterceptor i4 = Mockito.mock(HandlerInterceptor.class); this.handlerMapping.setInterceptors(mappedInterceptor1, i2, mappedInterceptor3, i4); this.handlerMapping.setApplicationContext(this.context); HandlerExecutionChain chain = this.handlerMapping.getHandlerExecutionChain(new SimpleHandler(), this.request); Assert.assertThat(chain.getInterceptors(), Matchers.arrayContaining( mappedInterceptor1.getInterceptor(), i2, mappedInterceptor3.getInterceptor(), i4)); }
/** * Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped * resource handlers. To configure resource handling, override * {@link #addResourceHandlers}. */ @Bean @Nullable public HandlerMapping resourceHandlerMapping() { Assert.state(this.applicationContext != null, "No ApplicationContext set"); Assert.state(this.servletContext != null, "No ServletContext set"); ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext, this.servletContext, mvcContentNegotiationManager(), mvcUrlPathHelper()); addResourceHandlers(registry); AbstractHandlerMapping handlerMapping = registry.getHandlerMapping(); if (handlerMapping == null) { return null; } handlerMapping.setPathMatcher(mvcPathMatcher()); handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); handlerMapping.setInterceptors(getInterceptors()); handlerMapping.setCorsConfigurations(getCorsConfigurations()); return handlerMapping; }
/** * Return a handler mapping ordered at 1 to map URL paths directly to * view names. To configure view controllers, override * {@link #addViewControllers}. */ @Bean @Nullable public HandlerMapping viewControllerHandlerMapping() { ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext); addViewControllers(registry); AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping(); if (handlerMapping == null) { return null; } handlerMapping.setPathMatcher(mvcPathMatcher()); handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); handlerMapping.setInterceptors(getInterceptors()); handlerMapping.setCorsConfigurations(getCorsConfigurations()); return handlerMapping; }
@Override @Bean public HandlerMapping resourceHandlerMapping() { ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext, this.servletContext); addResourceHandlers(registry); AbstractHandlerMapping handlerMapping = registry.getHandlerMapping(); if (handlerMapping != null) { handlerMapping.setPathMatcher(mvcPathMatcher()); handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); handlerMapping.setInterceptors(new HandlerInterceptor[] { new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider())}); //this line!!!: ((ResourceHttpRequestHandler)handlerMapping).setUseExpiresHeader(false); } else { handlerMapping = new EmptyHandlerMapping(); } return handlerMapping; }
@Override public Object processAfterInstantiation( final ApplicationContext context, final Object bean) { AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) bean; List<HandlerInterceptor> candidates = Beans.lookFor(context, HandlerInterceptor.class); OrderComparator.sort(candidates); Object[] interceptors = new Object[candidates.size()]; for (int i = interceptors.length - 1, j = 0; i >= 0; i--, j++) { HandlerInterceptor interceptor = candidates.get(i); RequestMapping mapping = AnnotationUtils.findAnnotation(interceptor.getClass(), RequestMapping.class); if (mapping != null && mapping.value().length > 0) { interceptors[j] = new MappedInterceptor(mapping.value(), interceptor); } else { interceptors[j] = interceptor; } } handlerMapping.setInterceptors(interceptors); return bean; } },