/** * Provide access to the shared handler interceptors used to configure * {@link HandlerMapping} instances with. * <p>This method cannot be overridden; use {@link #addInterceptors} instead. */ protected final Object[] getInterceptors() { if (this.interceptors == null) { InterceptorRegistry registry = new InterceptorRegistry(); addInterceptors(registry); registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService())); registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider())); this.interceptors = registry.getInterceptors(); } return this.interceptors.toArray(); }
@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()); }
@Test public void orderedInterceptors() throws Exception { this.registry.addInterceptor(this.interceptor1).order(Ordered.LOWEST_PRECEDENCE); this.registry.addInterceptor(this.interceptor2).order(Ordered.HIGHEST_PRECEDENCE); List<Object> interceptors = this.registry.getInterceptors(); assertEquals(2, interceptors.size()); assertSame(this.interceptor2, interceptors.get(0)); assertSame(this.interceptor1, interceptors.get(1)); }
@Test public void nonOrderedInterceptors() throws Exception { this.registry.addInterceptor(this.interceptor1).order(0); this.registry.addInterceptor(this.interceptor2).order(0); List<Object> interceptors = this.registry.getInterceptors(); assertEquals(2, interceptors.size()); assertSame(this.interceptor1, interceptors.get(0)); assertSame(this.interceptor2, interceptors.get(1)); } }
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; }
/** * Provide access to the shared handler interceptors used to configure * {@link HandlerMapping} instances with. * <p>This method cannot be overridden; use {@link #addInterceptors} instead. */ protected final Object[] getInterceptors() { if (this.interceptors == null) { InterceptorRegistry registry = new InterceptorRegistry(); addInterceptors(registry); registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService())); registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider())); this.interceptors = registry.getInterceptors(); } return this.interceptors.toArray(); }
@Override public List<Object> getInterceptors() { return super.getInterceptors(); } }