@Test public void includePatternWithMatrixVariables() { MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/foo*/*" }, this.interceptor); assertTrue(mappedInterceptor.matches("/foo;q=1/bar;s=2", pathMatcher)); }
@Override protected void addInterceptors(InterceptorRegistry registry) { for (MappedInterceptor interceptor : mappedInterceptors) { InterceptorRegistration registration = registry.addInterceptor(interceptor.getInterceptor()); if (interceptor.getPathPatterns() != null) { registration.addPathPatterns(interceptor.getPathPatterns()); } } }
@Test public void customPathMatcher() { MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/foo/[0-9]*" }, this.interceptor); mappedInterceptor.setPathMatcher(new TestPathMatcher()); assertTrue(mappedInterceptor.matches("/foo/123", pathMatcher)); assertFalse(mappedInterceptor.matches("/foo/bar", pathMatcher)); }
/** * Build 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 = StringUtils.toStringArray(this.includePatterns); String[] exclude = StringUtils.toStringArray(this.excludePatterns); MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor); if (this.pathMatcher != null) { mappedInterceptor.setPathMatcher(this.pathMatcher); } return mappedInterceptor; }
/** * Add interceptors mapped to a set of path patterns. */ public StandaloneMockMvcBuilder addMappedInterceptors( @Nullable String[] pathPatterns, HandlerInterceptor... interceptors) { for (HandlerInterceptor interceptor : interceptors) { this.mappedInterceptors.add(new MappedInterceptor(pathPatterns, interceptor)); } return this; }
if (interceptor instanceof MappedInterceptor) { MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor; if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) { chain.addInterceptor(mappedInterceptor.getInterceptor());
@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)); }
.map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass()) .collect(Collectors.toList()); assertThat(interceptors, containsInAnyOrder(ConversionServiceExposingInterceptor.class,
@Test public void preHandle() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.preHandle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null); then(interceptor).should().preHandle(any(HttpServletRequest.class), any(HttpServletResponse.class), any()); }
@Test public void postHandle() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.postHandle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(ModelAndView.class)); then(interceptor).should().postHandle(any(), any(), any(), any()); }
@Test public void afterCompletion() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.afterCompletion(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(Exception.class)); then(interceptor).should().afterCompletion(any(), any(), any(), any()); }
/** * Since in the admin we have multiple handler mappings, we need to register a {@link MappedInterceptor} * to apply to all of them. Modifying just tine {@link InterceptorRegistry} will apply those * interceptors to _only_ the default RequestMappingHandlerMapping. */ @Bean public MappedInterceptor blJsFieldNameCompatibilityInterceptor() { return new MappedInterceptor(null, new JSFieldNameCompatibilityInterceptor()); }
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; }
/** * Build 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 = StringUtils.toStringArray(this.includePatterns); String[] exclude = StringUtils.toStringArray(this.excludePatterns); MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor); if (this.pathMatcher != null) { mappedInterceptor.setPathMatcher(this.pathMatcher); } return mappedInterceptor; }
/** * Obtain the {@link FacesHandlerInterceptor} by searching all mapped interceptors. * @return the interceptor */ protected HandlerInterceptor findFacesHandlerInterceptor() { HandlerInterceptor interceptor = null; Map<String, MappedInterceptor> mappedInterceptors = BeanFactoryUtils.beansOfTypeIncludingAncestors( getApplicationContext(), MappedInterceptor.class, true, false); for (MappedInterceptor mappedInterceptor : mappedInterceptors.values()) { if (mappedInterceptor.getInterceptor() instanceof FacesHandlerInterceptor) { Assert.state(interceptor == null, "Multiple " + FacesHandlerInterceptor.class.getSimpleName() + " registered within the web context"); interceptor = mappedInterceptor.getInterceptor(); } } Assert.state(interceptor != null, "No " + FacesHandlerInterceptor.class.getSimpleName() + " registered within the web context"); return interceptor; }
@Override protected void addInterceptors(InterceptorRegistry registry) { for (MappedInterceptor interceptor : mappedInterceptors) { InterceptorRegistration registration = registry.addInterceptor(interceptor.getInterceptor()); if (interceptor.getPathPatterns() != null) { registration.addPathPatterns(interceptor.getPathPatterns()); } } }
@Test public void excludePattern() { MappedInterceptor mappedInterceptor = new MappedInterceptor(null, new String[] { "/admin/**" }, this.interceptor); assertTrue(mappedInterceptor.matches("/foo", pathMatcher)); assertFalse(mappedInterceptor.matches("/admin/foo", pathMatcher)); }
@Bean public MappedInterceptor blLocaleChangeInterceptor() { LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); localeInterceptor.setParamName("blLocaleCode"); return new MappedInterceptor(null, localeInterceptor); }
if (interceptor instanceof MappedInterceptor) { MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor; if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) { chain.addInterceptor(mappedInterceptor.getInterceptor());
/** * Build 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 = StringUtils.toStringArray(this.includePatterns); String[] exclude = StringUtils.toStringArray(this.excludePatterns); MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor); if (this.pathMatcher != null) { mappedInterceptor.setPathMatcher(this.pathMatcher); } return mappedInterceptor; }