From 313cc368d9ac46767fa84e3864b6fa1382373c2b Mon Sep 17 00:00:00 2001 From: jansupol Date: Tue, 19 Sep 2023 13:07:44 +0200 Subject: [PATCH] Fix normalizing URIs with percent encoded symbols Signed-off-by: jansupol --- .../java/org/glassfish/jersey/uri/UriTemplate.java | 2 +- .../e2e/common/uri/internal/JerseyUriBuilderTest.java | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core-common/src/main/java/org/glassfish/jersey/uri/UriTemplate.java b/core-common/src/main/java/org/glassfish/jersey/uri/UriTemplate.java index bbd1240661..003a4b2f9f 100644 --- a/core-common/src/main/java/org/glassfish/jersey/uri/UriTemplate.java +++ b/core-common/src/main/java/org/glassfish/jersey/uri/UriTemplate.java @@ -367,7 +367,7 @@ public static URI normalize(final URI uri) { final StringBuilder pathBuilder = new StringBuilder(); for (final String segment : resolvedSegments) { - pathBuilder.append('/').append(segment); + pathBuilder.append('/').append(UriComponent.encode(segment, UriComponent.Type.PATH)); } String resultString = createURIWithStringValues(uri.getScheme(), diff --git a/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/uri/internal/JerseyUriBuilderTest.java b/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/uri/internal/JerseyUriBuilderTest.java index 7233c8d29d..c5acb8845e 100644 --- a/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/uri/internal/JerseyUriBuilderTest.java +++ b/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/uri/internal/JerseyUriBuilderTest.java @@ -30,6 +30,7 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.core.Link; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.PathSegment; import javax.ws.rs.core.UriBuilder; @@ -1659,12 +1660,19 @@ public void testQueryParamStyleMultiPairs() { } @Test - void testFragment2569() throws URISyntaxException { + void testFragment5269() throws URISyntaxException { final URI uri = new URI("http://www.example.org/foo.xml#xpointer(//Rube)").normalize(); Assertions.assertEquals(uri, UriBuilder.fromUri(uri).build()); // prints "http://www.example.org/foo.xml#xpointer(//Rube)" Assertions.assertEquals(uri, UriBuilder.fromUri(uri).fragment("xpointer(//{type})").build("Rube")); } + @Test + public void test5416() { + URI uri = UriBuilder.fromUri("http://host.com/path%20path/.test.jpg").build(); + Link link = Link.fromUri(uri).build(); + Assertions.assertEquals(uri, link.getUri()); + } + private void checkQueryFormat(String fromUri, JerseyQueryParamStyle queryParamStyle, String expected) { final URI uri = ((JerseyUriBuilder) UriBuilder.fromUri(fromUri)) .setQueryParamStyle(queryParamStyle)