org.springframework.util.PathMatcher Java Examples

The following examples show how to use org.springframework.util.PathMatcher. 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: MessageBrokerConfigurationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void customPathMatcher() {
	ApplicationContext context = loadConfig(CustomConfig.class);

	SimpleBrokerMessageHandler broker = context.getBean(SimpleBrokerMessageHandler.class);
	DefaultSubscriptionRegistry registry = (DefaultSubscriptionRegistry) broker.getSubscriptionRegistry();
	assertEquals("a.a", registry.getPathMatcher().combine("a", "a"));

	PathMatcher pathMatcher =
			context.getBean(SimpAnnotationMethodMessageHandler.class).getPathMatcher();

	assertEquals("a.a", pathMatcher.combine("a", "a"));

	DefaultUserDestinationResolver resolver = context.getBean(DefaultUserDestinationResolver.class);
	assertNotNull(resolver);
	assertEquals(false, resolver.isRemoveLeadingSlash());
}
 
Example #2
Source File: EventRouter.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
@Autowired
@Qualifier("simpleBrokerMessageHandler")
public void onSimpleBrockedMessageChannel(AbstractBrokerMessageHandler handler) {
    // here we try to inherit matcher from subscription registry
    if (!(handler instanceof SimpleBrokerMessageHandler)) {
        return;
    }
    SubscriptionRegistry registry = ((SimpleBrokerMessageHandler) handler).getSubscriptionRegistry();
    if (!(registry instanceof DefaultSubscriptionRegistry)) {
        return;
    }
    PathMatcher pathMatcher = ((DefaultSubscriptionRegistry) registry).getPathMatcher();
    if(pathMatcher != null) {
        this.pathMatcher = pathMatcher;
    }
}
 
Example #3
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 #4
Source File: MessageBrokerConfigurationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void customPathMatcher() {
	ApplicationContext context = loadConfig(CustomConfig.class);

	SimpleBrokerMessageHandler broker = context.getBean(SimpleBrokerMessageHandler.class);
	DefaultSubscriptionRegistry registry = (DefaultSubscriptionRegistry) broker.getSubscriptionRegistry();
	assertEquals("a.a", registry.getPathMatcher().combine("a", "a"));

	PathMatcher pathMatcher =
			context.getBean(SimpAnnotationMethodMessageHandler.class).getPathMatcher();

	assertEquals("a.a", pathMatcher.combine("a", "a"));

	DefaultUserDestinationResolver resolver = context.getBean(DefaultUserDestinationResolver.class);
	assertNotNull(resolver);
	assertEquals(false, resolver.isRemoveLeadingSlash());
}
 
Example #5
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 #6
Source File: PatternsRequestCondition.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Private constructor accepting a collection of patterns.
 */
private PatternsRequestCondition(Collection<String> patterns, UrlPathHelper urlPathHelper,
		PathMatcher pathMatcher, boolean useSuffixPatternMatch, boolean useTrailingSlashMatch,
		List<String> fileExtensions) {

	this.patterns = Collections.unmodifiableSet(prependLeadingSlash(patterns));
	this.pathHelper = (urlPathHelper != null ? urlPathHelper : new UrlPathHelper());
	this.pathMatcher = (pathMatcher != null ? pathMatcher : new AntPathMatcher());
	this.useSuffixPatternMatch = useSuffixPatternMatch;
	this.useTrailingSlashMatch = useTrailingSlashMatch;
	if (fileExtensions != null) {
		for (String fileExtension : fileExtensions) {
			if (fileExtension.charAt(0) != '.') {
				fileExtension = "." + fileExtension;
			}
			this.fileExtensions.add(fileExtension);
		}
	}
}
 
Example #7
Source File: DynamicSecurityMetadataSource.java    From mall-swarm with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
    if (configAttributeMap == null) this.loadDataSource();
    List<ConfigAttribute>  configAttributes = new ArrayList<>();
    //获取当前访问的路径
    String url = ((FilterInvocation) o).getRequestUrl();
    String path = URLUtil.getPath(url);
    PathMatcher pathMatcher = new AntPathMatcher();
    Iterator<String> iterator = configAttributeMap.keySet().iterator();
    //获取访问该路径所需资源
    while (iterator.hasNext()) {
        String pattern = iterator.next();
        if (pathMatcher.match(pattern, path)) {
            configAttributes.add(configAttributeMap.get(pattern));
        }
    }
    // 未设置操作请求权限,返回空集合
    return configAttributes;
}
 
Example #8
Source File: AbstractMessageBrokerConfiguration.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Bean
public SimpAnnotationMethodMessageHandler simpAnnotationMethodMessageHandler() {
	SimpAnnotationMethodMessageHandler handler = createAnnotationMethodMessageHandler();
	handler.setDestinationPrefixes(getBrokerRegistry().getApplicationDestinationPrefixes());
	handler.setMessageConverter(brokerMessageConverter());
	handler.setValidator(simpValidator());

	List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
	addArgumentResolvers(argumentResolvers);
	handler.setCustomArgumentResolvers(argumentResolvers);

	List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
	addReturnValueHandlers(returnValueHandlers);
	handler.setCustomReturnValueHandlers(returnValueHandlers);

	PathMatcher pathMatcher = getBrokerRegistry().getPathMatcher();
	if (pathMatcher != null) {
		handler.setPathMatcher(pathMatcher);
	}
	return handler;
}
 
Example #9
Source File: PatternsRequestCondition.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Private constructor accepting a collection of patterns.
 */
private PatternsRequestCondition(Collection<String> patterns, @Nullable UrlPathHelper urlPathHelper,
		@Nullable PathMatcher pathMatcher, boolean useSuffixPatternMatch,
		boolean useTrailingSlashMatch, @Nullable List<String> fileExtensions) {

	this.patterns = Collections.unmodifiableSet(prependLeadingSlash(patterns));
	this.pathHelper = urlPathHelper != null ? urlPathHelper : new UrlPathHelper();
	this.pathMatcher = pathMatcher != null ? pathMatcher : new AntPathMatcher();
	this.useSuffixPatternMatch = useSuffixPatternMatch;
	this.useTrailingSlashMatch = useTrailingSlashMatch;

	if (fileExtensions != null) {
		for (String fileExtension : fileExtensions) {
			if (fileExtension.charAt(0) != '.') {
				fileExtension = "." + fileExtension;
			}
			this.fileExtensions.add(fileExtension);
		}
	}
}
 
Example #10
Source File: PathMatchingResourcePatternResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static Set<Resource> findMatchingResources(
		URL rootDirURL, String locationPattern, PathMatcher pathMatcher) throws IOException {

	Object root = VfsPatternUtils.findRoot(rootDirURL);
	PatternVirtualFileVisitor visitor =
			new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), locationPattern, pathMatcher);
	VfsPatternUtils.visit(root, visitor);
	return visitor.getResources();
}
 
Example #11
Source File: RequestMatchResult.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an instance with a matching pattern.
 * @param matchingPattern the matching pattern, possibly not the same as the
 * input pattern, e.g. inputPattern="/foo" and matchingPattern="/foo/".
 * @param lookupPath the lookup path extracted from the request
 * @param pathMatcher the PathMatcher used
 */
public RequestMatchResult(String matchingPattern, String lookupPath, PathMatcher pathMatcher) {
	Assert.hasText(matchingPattern, "'matchingPattern' is required");
	Assert.hasText(lookupPath, "'lookupPath' is required");
	Assert.notNull(pathMatcher, "'pathMatcher' is required");
	this.matchingPattern = matchingPattern;
	this.lookupPath = lookupPath;
	this.pathMatcher = pathMatcher;
}
 
Example #12
Source File: PathMatchingResourcePatternResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public static Set<Resource> findMatchingResources(
		URL rootDirURL, String locationPattern, PathMatcher pathMatcher) throws IOException {

	Object root = VfsPatternUtils.findRoot(rootDirURL);
	PatternVirtualFileVisitor visitor =
			new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), locationPattern, pathMatcher);
	VfsPatternUtils.visit(root, visitor);
	return visitor.getResources();
}
 
Example #13
Source File: ResourceServlet.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean isAllowed(String resourcePath) {
	if (resourcePath.matches(PROTECTED_PATH)) {
		return false;
	}
	PathMatcher pathMatcher = new AntPathMatcher();
	for (String pattern : allowedResourcePaths) {
		if (pathMatcher.match(pattern, resourcePath)) {
			return true;
		}
	}
	return false;
}
 
Example #14
Source File: WebMvcConfigurationSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * A {@link ResourceUrlProvider} bean for use with the MVC dispatcher.
 * @since 4.1
 */
@Bean
public ResourceUrlProvider mvcResourceUrlProvider() {
	ResourceUrlProvider urlProvider = new ResourceUrlProvider();
	UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper();
	if (pathHelper != null) {
		urlProvider.setUrlPathHelper(pathHelper);
	}
	PathMatcher pathMatcher = getPathMatchConfigurer().getPathMatcher();
	if (pathMatcher != null) {
		urlProvider.setPathMatcher(pathMatcher);
	}
	return urlProvider;
}
 
Example #15
Source File: DestinationPatternsMessageCondition.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static Set<String> prependLeadingSlash(Collection<String> patterns, PathMatcher pathMatcher) {
	boolean slashSeparator = pathMatcher.combine("a", "a").equals("a/a");
	Set<String> result = new LinkedHashSet<>(patterns.size());
	for (String pattern : patterns) {
		if (slashSeparator && StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
			pattern = "/" + pattern;
		}
		result.add(pattern);
	}
	return result;
}
 
Example #16
Source File: PageDescribeController.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
	BlogLanguage blogLanguage = (BlogLanguage) request.getAttribute(BlogLanguageMethodArgumentResolver.BLOG_LANGUAGE_ATTRIBUTE);
	if (blogLanguage == null) {
		Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
		blogLanguage = blog.getLanguage(blog.getDefaultLanguage());
	}

	String path = urlPathHelper.getLookupPathForRequest(request);

	PathMatcher pathMatcher = new AntPathMatcher();
	if (!pathMatcher.match(PATH_PATTERN, path)) {
		throw new HttpNotFoundException();
	}

	Map<String, String> variables = pathMatcher.extractUriTemplateVariables(PATH_PATTERN, path);
	request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, variables);

	Page page = pageService.getPageByCode(variables.get("code"), blogLanguage.getLanguage());
	if (page == null) {
		page = pageService.getPageByCode(variables.get("code"), blogLanguage.getBlog().getDefaultLanguage());
	}
	if (page == null) {
		throw new HttpNotFoundException();
	}
	if (page.getStatus() != Post.Status.PUBLISHED) {
		throw new HttpNotFoundException();
	}

	return createModelAndView(page);
}
 
Example #17
Source File: AbstractHandlerMapping.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Set the PathMatcher implementation to use for matching URL paths
 * against registered URL patterns. Default is AntPathMatcher.
 * @see org.springframework.util.AntPathMatcher
 */
public void setPathMatcher(PathMatcher pathMatcher) {
	Assert.notNull(pathMatcher, "PathMatcher must not be null");
	this.pathMatcher = pathMatcher;
	if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource) {
		((UrlBasedCorsConfigurationSource)this.corsConfigurationSource).setPathMatcher(pathMatcher);
	}
}
 
Example #18
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * A {@link ResourceUrlProvider} bean for use with the MVC dispatcher.
 * @since 4.1
 */
@Bean
public ResourceUrlProvider mvcResourceUrlProvider() {
	ResourceUrlProvider urlProvider = new ResourceUrlProvider();
	UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper();
	if (pathHelper != null) {
		urlProvider.setUrlPathHelper(pathHelper);
	}
	PathMatcher pathMatcher = getPathMatchConfigurer().getPathMatcher();
	if (pathMatcher != null) {
		urlProvider.setPathMatcher(pathMatcher);
	}
	return urlProvider;
}
 
Example #19
Source File: ServiceMatcherAutoConfiguration.java    From spring-cloud-bus with Apache License 2.0 5 votes vote down vote up
@BusPathMatcher
// There is a @Bean of type PathMatcher coming from Spring MVC
@ConditionalOnMissingBean(name = BusAutoConfiguration.BUS_PATH_MATCHER_NAME)
@Bean(name = BusAutoConfiguration.BUS_PATH_MATCHER_NAME)
public PathMatcher busPathMatcher() {
	return new DefaultBusPathMatcher(new AntPathMatcher(":"));
}
 
Example #20
Source File: AbstractHandlerMapping.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Set the PathMatcher implementation to use for matching URL paths
 * against registered URL patterns. Default is AntPathMatcher.
 * @see org.springframework.util.AntPathMatcher
 */
public void setPathMatcher(PathMatcher pathMatcher) {
	Assert.notNull(pathMatcher, "PathMatcher must not be null");
	this.pathMatcher = pathMatcher;
	if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource) {
		((UrlBasedCorsConfigurationSource) this.corsConfigurationSource).setPathMatcher(pathMatcher);
	}
}
 
Example #21
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 #22
Source File: DelegatingWebMvcConfigurationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void configurePathMatch() throws Exception {
	final PathMatcher pathMatcher = mock(PathMatcher.class);
	final UrlPathHelper pathHelper = mock(UrlPathHelper.class);

	List<WebMvcConfigurer> configurers = new ArrayList<>();
	configurers.add(new WebMvcConfigurer() {
		@Override
		public void configurePathMatch(PathMatchConfigurer configurer) {
			configurer.setUseRegisteredSuffixPatternMatch(true)
					.setUseTrailingSlashMatch(false)
					.setUrlPathHelper(pathHelper)
					.setPathMatcher(pathMatcher);
		}
	});
	delegatingConfig.setConfigurers(configurers);

	RequestMappingHandlerMapping handlerMapping = delegatingConfig.requestMappingHandlerMapping(
			delegatingConfig.mvcContentNegotiationManager(), delegatingConfig.mvcConversionService(),
			delegatingConfig.mvcResourceUrlProvider());
	assertNotNull(handlerMapping);
	assertEquals("PathMatchConfigurer should configure RegisteredSuffixPatternMatch",
			true, handlerMapping.useRegisteredSuffixPatternMatch());
	assertEquals("PathMatchConfigurer should configure SuffixPatternMatch",
			true, handlerMapping.useSuffixPatternMatch());
	assertEquals("PathMatchConfigurer should configure TrailingSlashMatch",
			false, handlerMapping.useTrailingSlashMatch());
	assertEquals("PathMatchConfigurer should configure UrlPathHelper",
			pathHelper, handlerMapping.getUrlPathHelper());
	assertEquals("PathMatchConfigurer should configure PathMatcher",
			pathMatcher, handlerMapping.getPathMatcher());
}
 
Example #23
Source File: DelegatingWebMvcConfigurationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void configurePathMatch() throws Exception {
	final PathMatcher pathMatcher = mock(PathMatcher.class);
	final UrlPathHelper pathHelper = mock(UrlPathHelper.class);

	List<WebMvcConfigurer> configurers = new ArrayList<>();
	configurers.add(new WebMvcConfigurer() {
		@Override
		public void configurePathMatch(PathMatchConfigurer configurer) {
			configurer.setUseRegisteredSuffixPatternMatch(true)
					.setUseTrailingSlashMatch(false)
					.setUrlPathHelper(pathHelper)
					.setPathMatcher(pathMatcher);
		}
	});
	delegatingConfig.setConfigurers(configurers);

	RequestMappingHandlerMapping handlerMapping = delegatingConfig.requestMappingHandlerMapping();
	assertNotNull(handlerMapping);
	assertEquals("PathMatchConfigurer should configure RegisteredSuffixPatternMatch",
			true, handlerMapping.useRegisteredSuffixPatternMatch());
	assertEquals("PathMatchConfigurer should configure SuffixPatternMatch",
			true, handlerMapping.useSuffixPatternMatch());
	assertEquals("PathMatchConfigurer should configure TrailingSlashMatch",
			false, handlerMapping.useTrailingSlashMatch());
	assertEquals("PathMatchConfigurer should configure UrlPathHelper",
			pathHelper, handlerMapping.getUrlPathHelper());
	assertEquals("PathMatchConfigurer should configure PathMatcher",
			pathMatcher, handlerMapping.getPathMatcher());
}
 
Example #24
Source File: WebMvcConfigurationSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Bean
public ResourceUrlProvider mvcResourceUrlProvider() {
	ResourceUrlProvider urlProvider = new ResourceUrlProvider();
	UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper();
	if (pathHelper != null) {
		urlProvider.setUrlPathHelper(pathHelper);
	}
	PathMatcher pathMatcher = getPathMatchConfigurer().getPathMatcher();
	if (pathMatcher != null) {
		urlProvider.setPathMatcher(pathMatcher);
	}
	return urlProvider;
}
 
Example #25
Source File: RequestMappingInfo.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return a custom PathMatcher to use for the PatternsRequestCondition, if any.
 */
@Nullable
public PathMatcher getPathMatcher() {
	return this.pathMatcher;
}
 
Example #26
Source File: DestinationPatternsMessageCondition.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private DestinationPatternsMessageCondition(Collection<String> patterns, PathMatcher pathMatcher) {
	this.pathMatcher = (pathMatcher != null ? pathMatcher : new AntPathMatcher());
	this.patterns = Collections.unmodifiableSet(prependLeadingSlash(patterns, this.pathMatcher));
}
 
Example #27
Source File: DestinationPatternsMessageCondition.java    From java-technology-stack with MIT License 4 votes vote down vote up
private DestinationPatternsMessageCondition(Collection<String> patterns, @Nullable PathMatcher pathMatcher) {
	this.pathMatcher = (pathMatcher != null ? pathMatcher : new AntPathMatcher());
	this.patterns = Collections.unmodifiableSet(prependLeadingSlash(patterns, this.pathMatcher));
}
 
Example #28
Source File: PathMatchingHandlerChainResolver.java    From disruptor-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public void setPathMatcher(PathMatcher pathMatcher) {
	this.pathMatcher = pathMatcher;
}
 
Example #29
Source File: PathMatchingResourcePatternResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) {
	this.subPattern = subPattern;
	this.pathMatcher = pathMatcher;
	this.rootPath = (rootPath.isEmpty() || rootPath.endsWith("/") ? rootPath : rootPath + "/");
}
 
Example #30
Source File: SimpAnnotationMethodMessageHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Set the PathMatcher implementation to use for matching destinations
 * against configured destination patterns.
 * <p>By default, {@link AntPathMatcher} is used.
 */
public void setPathMatcher(PathMatcher pathMatcher) {
	Assert.notNull(pathMatcher, "PathMatcher must not be null");
	this.pathMatcher = pathMatcher;
	this.slashPathSeparator = this.pathMatcher.combine("a", "a").equals("a/a");
}