NIFI-13810 Handled Trailing Separator for Paths in URI Builder

This closes #9319.

Signed-off-by: Peter Turcsanyi <turcsanyi@apache.org>
This commit is contained in:
exceptionfactory 2024-09-27 08:52:53 -05:00 committed by Peter Turcsanyi
parent e3e4dd5047
commit 6761de3901
No known key found for this signature in database
GPG Key ID: 55A813F1C3E553DC
2 changed files with 56 additions and 0 deletions

View File

@ -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);
}

View File

@ -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()