org.springframework.web.servlet.handler.MappedInterceptor Java Examples

The following examples show how to use org.springframework.web.servlet.handler.MappedInterceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourcesBeanDefinitionParser.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void registerUrlProvider(ParserContext context, @Nullable Object source) {
	if (!context.getRegistry().containsBeanDefinition(RESOURCE_URL_PROVIDER)) {
		RootBeanDefinition urlProvider = new RootBeanDefinition(ResourceUrlProvider.class);
		urlProvider.setSource(source);
		urlProvider.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		context.getRegistry().registerBeanDefinition(RESOURCE_URL_PROVIDER, urlProvider);
		context.registerComponent(new BeanComponentDefinition(urlProvider, RESOURCE_URL_PROVIDER));

		RootBeanDefinition interceptor = new RootBeanDefinition(ResourceUrlProviderExposingInterceptor.class);
		interceptor.setSource(source);
		interceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, urlProvider);

		RootBeanDefinition mappedInterceptor = new RootBeanDefinition(MappedInterceptor.class);
		mappedInterceptor.setSource(source);
		mappedInterceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
		mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(1, interceptor);
		String mappedInterceptorName = context.getReaderContext().registerWithGeneratedName(mappedInterceptor);
		context.registerComponent(new BeanComponentDefinition(mappedInterceptor, mappedInterceptorName));
	}
}
 
Example #2
Source File: InterceptorRegistration.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: InterceptorRegistryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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;
}
 
Example #4
Source File: ResourcesBeanDefinitionParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void registerUrlProvider(ParserContext parserContext, Object source) {
	if (!parserContext.getRegistry().containsBeanDefinition(RESOURCE_URL_PROVIDER)) {
		RootBeanDefinition urlProvider = new RootBeanDefinition(ResourceUrlProvider.class);
		urlProvider.setSource(source);
		urlProvider.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		parserContext.getRegistry().registerBeanDefinition(RESOURCE_URL_PROVIDER, urlProvider);
		parserContext.registerComponent(new BeanComponentDefinition(urlProvider, RESOURCE_URL_PROVIDER));

		RootBeanDefinition interceptor = new RootBeanDefinition(ResourceUrlProviderExposingInterceptor.class);
		interceptor.setSource(source);
		interceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, urlProvider);

		RootBeanDefinition mappedInterceptor = new RootBeanDefinition(MappedInterceptor.class);
		mappedInterceptor.setSource(source);
		mappedInterceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
		mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(1, interceptor);
		String mappedInterceptorName = parserContext.getReaderContext().registerWithGeneratedName(mappedInterceptor);
		parserContext.registerComponent(new BeanComponentDefinition(mappedInterceptor, mappedInterceptorName));
	}
}
 
Example #5
Source File: InterceptorRegistration.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: RequestMappingInfoHandlerMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #7
Source File: ResourcesBeanDefinitionParser.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void registerUrlProvider(ParserContext context, @Nullable Object source) {
	if (!context.getRegistry().containsBeanDefinition(RESOURCE_URL_PROVIDER)) {
		RootBeanDefinition urlProvider = new RootBeanDefinition(ResourceUrlProvider.class);
		urlProvider.setSource(source);
		urlProvider.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		context.getRegistry().registerBeanDefinition(RESOURCE_URL_PROVIDER, urlProvider);
		context.registerComponent(new BeanComponentDefinition(urlProvider, RESOURCE_URL_PROVIDER));

		RootBeanDefinition interceptor = new RootBeanDefinition(ResourceUrlProviderExposingInterceptor.class);
		interceptor.setSource(source);
		interceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, urlProvider);

		RootBeanDefinition mappedInterceptor = new RootBeanDefinition(MappedInterceptor.class);
		mappedInterceptor.setSource(source);
		mappedInterceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
		mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(1, interceptor);
		String mappedInterceptorName = context.getReaderContext().registerWithGeneratedName(mappedInterceptor);
		context.registerComponent(new BeanComponentDefinition(mappedInterceptor, mappedInterceptorName));
	}
}
 
Example #8
Source File: ResourcesBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void registerUrlProvider(ParserContext parserContext, Object source) {
	if (!parserContext.getRegistry().containsBeanDefinition(RESOURCE_URL_PROVIDER)) {
		RootBeanDefinition urlProvider = new RootBeanDefinition(ResourceUrlProvider.class);
		urlProvider.setSource(source);
		urlProvider.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		parserContext.getRegistry().registerBeanDefinition(RESOURCE_URL_PROVIDER, urlProvider);
		parserContext.registerComponent(new BeanComponentDefinition(urlProvider, RESOURCE_URL_PROVIDER));

		RootBeanDefinition interceptor = new RootBeanDefinition(ResourceUrlProviderExposingInterceptor.class);
		interceptor.setSource(source);
		interceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, urlProvider);

		RootBeanDefinition mappedInterceptor = new RootBeanDefinition(MappedInterceptor.class);
		mappedInterceptor.setSource(source);
		mappedInterceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
		mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(1, interceptor);
		String mappedInterceptorName = parserContext.getReaderContext().registerWithGeneratedName(mappedInterceptor);
		parserContext.registerComponent(new BeanComponentDefinition(mappedInterceptor, mappedInterceptorName));
	}
}
 
Example #9
Source File: RequestMappingInfoHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void mappedInterceptors() throws Exception {
	String path = "/foo";
	HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {};
	MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor);

	TestRequestMappingInfoHandlerMapping hm = new TestRequestMappingInfoHandlerMapping();
	hm.registerHandler(new TestController());
	hm.setInterceptors(new Object[] { mappedInterceptor });
	hm.setApplicationContext(new StaticWebApplicationContext());

	HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path));
	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertSame(interceptor, chain.getInterceptors()[0]);

	chain = hm.getHandler(new MockHttpServletRequest("GET", "/invalid"));
	assertNull(chain);
}
 
Example #10
Source File: InterceptorRegistryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
	PathMatcher pathMatcher = new AntPathMatcher();
	List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>();
	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;
}
 
Example #11
Source File: InterceptorRegistryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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;
}
 
Example #12
Source File: RequestMappingInfoHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #13
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml", 10);

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE - 1, mapping.getOrder());

	BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());

	ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
	assertNotNull(urlProvider);

	MappedInterceptor mappedInterceptor = appContext.getBean(MappedInterceptor.class);
	assertNotNull(urlProvider);
	assertEquals(ResourceUrlProviderExposingInterceptor.class, mappedInterceptor.getInterceptor().getClass());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/resources/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	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);
}
 
Example #14
Source File: InterceptorRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #15
Source File: StandaloneMockMvcBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Add interceptors mapped to a set of path patterns.
 */
public StandaloneMockMvcBuilder addMappedInterceptors(String[] pathPatterns, HandlerInterceptor... interceptors) {
	for (HandlerInterceptor interceptor : interceptors) {
		this.mappedInterceptors.add(new MappedInterceptor(pathPatterns, interceptor));
	}
	return this;
}
 
Example #16
Source File: StandaloneMockMvcBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void addInterceptors(InterceptorRegistry registry) {
	for (MappedInterceptor interceptor : mappedInterceptors) {
		InterceptorRegistration registration = registry.addInterceptor(interceptor.getInterceptor());
		if (interceptor.getPathPatterns() != null) {
			registration.addPathPatterns(interceptor.getPathPatterns());
		}
	}
}
 
Example #17
Source File: StandaloneMockMvcBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #18
Source File: StandaloneMockMvcBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void addInterceptors(InterceptorRegistry registry) {
	for (MappedInterceptor interceptor : mappedInterceptors) {
		InterceptorRegistration registration = registry.addInterceptor(interceptor.getInterceptor());
		if (interceptor.getPathPatterns() != null) {
			registration.addPathPatterns(interceptor.getPathPatterns());
		}
	}
}
 
Example #19
Source File: InterceptorRegistration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #20
Source File: InterceptorRegistryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #21
Source File: InterceptorRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #22
Source File: SpringMvcPolyfill.java    From Milkomeda with MIT License 5 votes vote down vote up
/**
 * 动态添加拦截器
 * @param interceptor       拦截器
 * @param order             排序
 * @param includeURLs       需要拦截的URL
 * @param excludeURLs       排除拦截的URL
 * @param handlerMapping    AbstractHandlerMapping实现类
 */
@SuppressWarnings("all")
public static void addDynamicInterceptor(HandlerInterceptor interceptor, int order, List<String> includeURLs, List<String> excludeURLs, AbstractHandlerMapping handlerMapping) {
    String[] include = StringUtils.toStringArray(includeURLs);
    String[] exclude = StringUtils.toStringArray(excludeURLs);
    // HandlerInterceptor -> MappedInterceptor -> HydrogenMappedInterceptor
    HydrogenMappedInterceptor hmi = new HydrogenMappedInterceptor(new MappedInterceptor(include, exclude, interceptor));
    // 内部的处理流程会设置,然而不是最终采纳的拦截器列表
    // handlerMapping.setInterceptors(mappedInterceptor);
    hmi.setOrder(order);
    try {
        findAdaptedInterceptorsField(handlerMapping);
        // 添加到可采纳的拦截器列表,让拦截器处理器Chain流程获取得到这个拦截器
        List<HandlerInterceptor> handlerInterceptors = (List<HandlerInterceptor>) adaptedInterceptorsField.get(handlerMapping);
        // 过滤添加过的拦截器
        boolean mapped = handlerInterceptors.stream().anyMatch(itor -> {
            // 只判断HydrogenMappedInterceptor拦截器类型
            if (itor instanceof HydrogenMappedInterceptor) {
                return itor.equals(hmi);
            }
            return false;
        });
        if (mapped) {
            return;
        }
        handlerInterceptors.add(hmi);
        // 仿Spring MVC源码对拦截器排序
        handlerInterceptors = handlerInterceptors.stream()
                .sorted(OrderComparator.INSTANCE.withSourceProvider(itor -> {
                    if (itor instanceof HydrogenMappedInterceptor) {
                        return (Ordered) ((HydrogenMappedInterceptor) itor)::getOrder;
                    }
                    return null;
                })).collect(Collectors.toList());
        adaptedInterceptorsField.set(handlerMapping, handlerInterceptors);
    } catch (Exception e) {
        log.error("SpringMvcPolyfill invoke AbstractHandlerMapping.adaptedInterceptors error with msg: {}",  e.getMessage(), e);
    }
}
 
Example #23
Source File: StandaloneMockMvcBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void addInterceptors(InterceptorRegistry registry) {
	for (MappedInterceptor interceptor : mappedInterceptors) {
		InterceptorRegistration registration = registry.addInterceptor(interceptor.getInterceptor());
		if (interceptor.getPathPatterns() != null) {
			registration.addPathPatterns(interceptor.getPathPatterns());
		}
	}
}
 
Example #24
Source File: StandaloneMockMvcBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #25
Source File: InterceptorRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example #26
Source File: WebMvcConfig.java    From carts with Apache License 2.0 4 votes vote down vote up
@Bean
public MappedInterceptor myMappedInterceptor(HTTPMonitoringInterceptor interceptor) {
    return new MappedInterceptor(new String[]{"/**"}, interceptor);
}
 
Example #27
Source File: WebMvcConfig.java    From shipping with Apache License 2.0 4 votes vote down vote up
@Bean
public MappedInterceptor myMappedInterceptor(HTTPMonitoringInterceptor interceptor) {
    return new MappedInterceptor(new String[]{"/**"}, interceptor);
}
 
Example #28
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml");

	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);
}
 
Example #29
Source File: HydrogenMappedInterceptor.java    From Milkomeda with MIT License 4 votes vote down vote up
public HydrogenMappedInterceptor(MappedInterceptor mappedInterceptor) {
    this.mappedInterceptor = mappedInterceptor;
}
 
Example #30
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml");

	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);
}