summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Wrobel <tomasz.wrobel@nokia.com>2020-11-19 14:22:14 +0100
committerTomasz Wrobel <tomasz.wrobel@nokia.com>2020-11-20 12:13:14 +0100
commita2f9f66c8971e14b117b49940946d565d0d118f7 (patch)
tree91c2f1fc26f1879ba4b8f4248c9f8a92ace3743b
parent75ee4a9d489b53b2abd6b44b1a1a46635a703d44 (diff)
[OOM CERT-SERVICE-CLIENT] Improve readability of URI regex
Issue-ID: OOM-2632 Signed-off-by: Tomasz Wrobel <tomasz.wrobel@nokia.com> Change-Id: Ic016c788d4dfde95ecd5eca955508385cd144bb4
-rw-r--r--certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/UriValidator.java49
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/UriValidatorTest.java4
2 files changed, 36 insertions, 17 deletions
diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/UriValidator.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/UriValidator.java
index 6971d8ea..7800d739 100644
--- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/UriValidator.java
+++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/UriValidator.java
@@ -28,21 +28,40 @@ public final class UriValidator {
private UriValidator() {
}
- private static final String RFC3986_URI_MATCH_PATTERN = "([A-Za-z][A-Za-z0-9+\\-.]*):(?:(//)(?:((?:[A-Za-z0-9\\-"
- + "._~!$&'()*+,;=:]|%[0-9A-Fa-f]{2})*)@)?((?:\\[(?:(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|::(?:[0-9A-Fa-f]{1,4}:){5}|"
- + "(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::"
- + "(?:[0-9A-Fa-f]{1,4}:){3}|(?:(?:[0-9A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+,;"
- + "=:@/?]|%[0-9A-Fa-f]{2})*))?";
+ private static final String SCHEME = "([A-Za-z][A-Za-z0-9+\\-.]*):";
+
+ private static final String OR = "|";
+
+ private static final String AUTHORITY_WITH_PATH = "?:(//)(?:((?:[A-Za-z0-9\\-._~!$&'()*+,;=:]|%[0-9A-Fa-f]{2})*)"
+ + "@)?((?:\\[(?:(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|::(?:[0-9A-Fa-f]{1,4}:){5}|(?:[0-9A-Fa-f]{1,4})?::"
+ + "(?:[0-9A-Fa-f]{1,4}:){4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}|(?:"
+ + "(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}|(?:(?:[0-9A-Fa-f]{1,4}:){0,"
+ + "3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:|(?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)"
+ + "(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
+ + "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,"
+ + "4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)|[Vv][0-9A-Fa-f]+\\.[A-Za-z0-9\\-._~!$&'()*+,;=:]+)"
+ + "\\]|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|"
+ + "(?:[A-Za-z0-9\\-._~!$&'()*+,;=]|%[0-9A-Fa-f]{2})*))(?::([0-9]*))?((?:/(?:[A-Za-z0-9\\-._~!$&'()*+,;"
+ + "=:@]|%[0-9A-Fa-f]{2})*)*)";
+
+ private static final String PATH_BEGIN_WITH_SLASH = "/((?:(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:/"
+ + "(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)?)";
+
+ private static final String PATH_WITHOUT_SLASH = "((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:/"
+ + "(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)";
+
+ private static final String QUERY = "(?:\\?((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@/?]|%[0-9A-Fa-f]{2})*))?";
+
+ private static final String FRAGMENT = "(?:\\#((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@/?]|%[0-9A-Fa-f]{2})*))?";
+
+ /**
+ * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
+ * <p>
+ * hier-part = "//" authority path-abempty / path-absolute / path-rootless / path-empty
+ */
+ private static final String RFC3986_URI_MATCH_PATTERN =
+ SCHEME + "(" + AUTHORITY_WITH_PATH + OR + PATH_BEGIN_WITH_SLASH + OR + PATH_WITHOUT_SLASH + OR + "" + ")"
+ + QUERY + FRAGMENT;
public static boolean isValidUri(String uri) {
return uri.matches(RFC3986_URI_MATCH_PATTERN);
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/UriValidatorTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/UriValidatorTest.java
index ed358b6f..d7024d4e 100644
--- a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/UriValidatorTest.java
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/UriValidatorTest.java
@@ -32,7 +32,7 @@ class UriValidatorTest {
*/
@ParameterizedTest
- @ValueSource(strings = {"http:/", "http:", "http://"})
+ @ValueSource(strings = {"http:/", "http:", "http://", "h4ttp://"})
void shouldTrueForValidScheme(String uri) {
assertThat(UriValidator.isValidUri(uri)).isTrue();
}
@@ -44,7 +44,7 @@ class UriValidatorTest {
}
@ParameterizedTest
- @ValueSource(strings = {"*http://", "_http://", "?http://"})
+ @ValueSource(strings = {"*http://", "_http://", "?http://", "4http://"})
void shouldFalseForUriWithInvalidScheme(String uri) {
assertThat(UriValidator.isValidUri(uri)).isFalse();
}