From 6761de3901a36303cd616c69ca05ecc3a1c1127d Mon Sep 17 00:00:00 2001 From: exceptionfactory Date: Fri, 27 Sep 2024 08:52:53 -0500 Subject: [PATCH] NIFI-13810 Handled Trailing Separator for Paths in URI Builder This closes #9319. Signed-off-by: Peter Turcsanyi --- .../web/client/StandardHttpUriBuilder.java | 12 +++++ .../client/StandardHttpUriBuilderTest.java | 44 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpUriBuilder.java b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpUriBuilder.java index 6b5816facd..70597239cf 100644 --- a/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpUriBuilder.java +++ b/nifi-commons/nifi-web-client/src/main/java/org/apache/nifi/web/client/StandardHttpUriBuilder.java @@ -41,6 +41,8 @@ public class StandardHttpUriBuilder implements HttpUriBuilder { private static final int MAXIMUM_PORT = 65535; + private static final char PATH_SEGMENT_SEPARATOR_CHARACTER = '/'; + private static final String PATH_SEGMENT_SEPARATOR = "/"; private static final String QUERY_PARAMETER_SEPARATOR = "&"; @@ -132,6 +134,16 @@ public class StandardHttpUriBuilder implements HttpUriBuilder { } if (!pathSegments.isEmpty()) { + final int pathBuilderLength = pathBuilder.length(); + if (pathBuilderLength > 0) { + // Append Path Segment Separator after encodedPath and before pathSegments when not found in encodedPath + final int lastIndex = pathBuilderLength - 1; + final char lastCharacter = pathBuilder.charAt(lastIndex); + if (PATH_SEGMENT_SEPARATOR_CHARACTER != lastCharacter) { + pathBuilder.append(PATH_SEGMENT_SEPARATOR_CHARACTER); + } + } + final String separatedPath = String.join(PATH_SEGMENT_SEPARATOR, pathSegments); pathBuilder.append(separatedPath); } diff --git a/nifi-commons/nifi-web-client/src/test/java/org/apache/nifi/web/client/StandardHttpUriBuilderTest.java b/nifi-commons/nifi-web-client/src/test/java/org/apache/nifi/web/client/StandardHttpUriBuilderTest.java index 24a2244873..c6b7f61079 100644 --- a/nifi-commons/nifi-web-client/src/test/java/org/apache/nifi/web/client/StandardHttpUriBuilderTest.java +++ b/nifi-commons/nifi-web-client/src/test/java/org/apache/nifi/web/client/StandardHttpUriBuilderTest.java @@ -33,8 +33,14 @@ class StandardHttpUriBuilderTest { private static final String ENCODED_PATH = "/resources/search"; + private static final String ENCODED_PATH_WITH_TRAILING_SEPARATOR = "/resources/search/"; + private static final String PATH_WITH_SPACES_ENCODED = "/resources/%20separated%20search"; + private static final String BUCKETS_PATH_SEGMENT = "buckets"; + + private static final String FILES_PATH_SEGMENT = "files"; + private static final String RESOURCES_PATH_SEGMENT = "resources"; private static final String RESOURCES_PATH_SEGMENT_SEPARATED = "resources|separated"; @@ -69,6 +75,14 @@ class StandardHttpUriBuilderTest { String.format("%s://%s:%d%s", HTTP_SCHEME, LOCALHOST, PORT, PATH_WITH_SPACES_ENCODED) ); + private static final URI HTTP_LOCALHOST_PORT_ENCODED_PATH_WITH_SPACES_AND_SEGMENTS_URI = URI.create( + String.format("%s://%s:%d%s/%s/%s", HTTP_SCHEME, LOCALHOST, PORT, PATH_WITH_SPACES_ENCODED, BUCKETS_PATH_SEGMENT, FILES_PATH_SEGMENT) + ); + + private static final URI HTTP_LOCALHOST_PORT_ENCODED_PATH_WITH_TRAILING_SEPARATOR_AND_SEGMENTS_URI = URI.create( + String.format("%s://%s:%d%s%s/%s", HTTP_SCHEME, LOCALHOST, PORT, ENCODED_PATH_WITH_TRAILING_SEPARATOR, BUCKETS_PATH_SEGMENT, FILES_PATH_SEGMENT) + ); + private static final URI HTTP_LOCALHOST_RESOURCES_URI = URI.create( String.format("%s%s", HTTP_LOCALHOST_URI, RESOURCES_PATH_SEGMENT) ); @@ -168,6 +182,36 @@ class StandardHttpUriBuilderTest { assertEquals(HTTP_LOCALHOST_PORT_ENCODED_PATH_WITH_SPACES_URI, uri); } + @Test + void testBuildSchemeHostPortEncodedPathWithSpacesAndPathSegments() { + final HttpUriBuilder builder = new StandardHttpUriBuilder() + .scheme(HTTP_SCHEME) + .host(LOCALHOST) + .port(PORT) + .encodedPath(PATH_WITH_SPACES_ENCODED) + .addPathSegment(BUCKETS_PATH_SEGMENT) + .addPathSegment(FILES_PATH_SEGMENT); + + final URI uri = builder.build(); + + assertEquals(HTTP_LOCALHOST_PORT_ENCODED_PATH_WITH_SPACES_AND_SEGMENTS_URI, uri); + } + + @Test + void testBuildSchemeHostPortEncodedPathWithTrailingSeparatorAndPathSegments() { + final HttpUriBuilder builder = new StandardHttpUriBuilder() + .scheme(HTTP_SCHEME) + .host(LOCALHOST) + .port(PORT) + .encodedPath(ENCODED_PATH_WITH_TRAILING_SEPARATOR) + .addPathSegment(BUCKETS_PATH_SEGMENT) + .addPathSegment(FILES_PATH_SEGMENT); + + final URI uri = builder.build(); + + assertEquals(HTTP_LOCALHOST_PORT_ENCODED_PATH_WITH_TRAILING_SEPARATOR_AND_SEGMENTS_URI, uri); + } + @Test void testBuildSchemeHostPathSegment() { final HttpUriBuilder builder = new StandardHttpUriBuilder()