aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2018-04-03 14:27:43 +0200
committerSonarTech <sonartech@sonarsource.com>2018-04-04 15:18:42 +0200
commit6ab3e013a2cc0f3c90c6cff8f89ab015977a561f (patch)
tree3c754c98e96f3e6e35519628b3c6785a4069799c /sonar-plugin-api
parente3ab105183d7b7678340b2cca8902929fda8200d (diff)
downloadsonarqube-6ab3e013a2cc0f3c90c6cff8f89ab015977a561f.tar.gz
sonarqube-6ab3e013a2cc0f3c90c6cff8f89ab015977a561f.zip
Fix WS routing
When a regular WS path is a suffix of a "ServletFilter" path, the regular WS can't be reached.
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java15
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java11
2 files changed, 23 insertions, 3 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java
index 5fd7540ac1d..93e549358dd 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java
@@ -35,6 +35,7 @@ import org.sonar.api.server.ServerSide;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
+import static org.apache.commons.lang.StringUtils.substringBeforeLast;
/**
* @since 3.1
@@ -160,7 +161,8 @@ public abstract class ServletFilter implements Filter {
/**
* Add inclusion patterns. Supported formats are:
* <ul>
- * <li>path prefixed by / and ended by *, for example "/api/foo/*", to match all paths "/api/foo" and "api/api/foo/something/else"</li>
+ * <li>path prefixed by / and ended by * or /*, for example "/api/foo/*", to match all paths "/api/foo" and "api/api/foo/something/else"</li>
+ * <li>path prefixed by / and ended by .*, for example "/api/foo.*", to match exact path "/api/foo" with any suffix like "/api/foo.protobuf"</li>
* <li>path prefixed by *, for example "*\/foo", to match all paths "/api/foo" and "something/else/foo"</li>
* <li>path with leading slash and no wildcard, for example "/api/foo", to match exact path "/api/foo"</li>
* </ul>
@@ -206,8 +208,15 @@ public abstract class ServletFilter implements Filter {
checkArgument(countStars == 1, "URL pattern accepts only zero or one wildcard character '*': %s", pattern);
if (pattern.charAt(0) == '/') {
checkArgument(pattern.endsWith(WILDCARD_CHAR), "URL pattern must end with wildcard character '*': %s", pattern);
- // remove the ending /* or *
- String path = pattern.replaceAll("/?\\*", "");
+ if (pattern.endsWith("/*")) {
+ String path = pattern.substring(0, pattern.length() - "/*".length());
+ return url -> url.startsWith(path);
+ }
+ if (pattern.endsWith(".*")) {
+ String path = pattern.substring(0, pattern.length() - ".*".length());
+ return url -> substringBeforeLast(url, ".").equals(path);
+ }
+ String path = pattern.substring(0, pattern.length() - "*".length());
return url -> url.startsWith(path);
}
checkArgument(pattern.startsWith(WILDCARD_CHAR), "URL pattern must start with wildcard character '*': %s", pattern);
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java
index 6d98e78e2b0..43e3379cf57 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java
@@ -159,6 +159,17 @@ public class ServletFilterTest {
}
@Test
+ public void use_include_and_exclude_prefix() {
+ ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder()
+ .includes("/foo_2")
+ .excludes("/foo")
+ .build();
+ assertThat(pattern.matches("/")).isFalse();
+ assertThat(pattern.matches("/foo_2")).isTrue();
+ assertThat(pattern.matches("/foo")).isFalse();
+ }
+
+ @Test
public void exclude_pattern_has_higher_priority_than_include_pattern() {
ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder()
.includes("/foo")