Allow separator configuration in PathPatternParser

This commit allows to configure a custom path separator when parsing and
matching path patterns with `PathPatternParser`, but also when parsing
incoming paths as `PathContainer` instances.

Closes gh-23092
This commit is contained in:
Brian Clozel
2019-06-06 11:39:53 +02:00
parent ec91934730
commit 33becd8258
5 changed files with 63 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -89,11 +89,13 @@ final class DefaultPathContainer implements PathContainer {
}
static PathContainer createFromUrlPath(String path) {
static PathContainer createFromUrlPath(String path, String separator) {
if (path.equals("")) {
return EMPTY_PATH;
}
String separator = "/";
if (separator.length() == 0) {
throw new IllegalArgumentException("separator should not be empty");
}
Separator separatorElement = separator.equals(SEPARATOR.value()) ? SEPARATOR : () -> separator;
List<Element> elements = new ArrayList<>();
int begin;

View File

@@ -66,13 +66,25 @@ public interface PathContainer {
/**
* Parse the path value into a sequence of {@link Separator Separator} and
* {@link PathSegment PathSegment} elements.
* @param path the encoded, raw URL path value to parse
* Parse the path value into a sequence of {@code "/"} {@link Separator Separator}
* and {@link PathSegment PathSegment} elements.
* @param path the encoded, raw path value to parse
* @return the parsed path
*/
static PathContainer parsePath(String path) {
return DefaultPathContainer.createFromUrlPath(path);
return DefaultPathContainer.createFromUrlPath(path, "/");
}
/**
* Parse the path value into a sequence of {@link Separator Separator} and
* {@link PathSegment PathSegment} elements.
* @param path the encoded, raw path value to parse
* @param separator the decoded separator for parsing patterns
* @return the parsed path
* @since 5.2
*/
static PathContainer parsePath(String path, String separator) {
return DefaultPathContainer.createFromUrlPath(path, separator);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,6 +36,8 @@ public class PathPatternParser {
private boolean caseSensitive = true;
private char separator = '/';
/**
* Whether a {@link PathPattern} produced by this parser should should
@@ -75,14 +77,20 @@ public class PathPatternParser {
}
/**
* Accessor used for the separator to use.
* <p>Currently not exposed for configuration with URI path patterns and
* mainly for use in InternalPathPatternParser and PathPattern. If required
* in the future, a similar option would also need to be exposed in
* {@link org.springframework.http.server.PathContainer PathContainer}.
* Char that represents the separator to use in the patterns.
* <p>The default is {@code '/'}.
* @since 5.2
*/
char getSeparator() {
return '/';
public void setSeparator(char separator) {
this.separator = separator;
}
/**
* Char that represents the separator to use in the patterns.
* @since 5.2
*/
public char getSeparator() {
return this.separator;
}