You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2020/01/30 02:25:12 UTC

[cxf] branch 3.2.x-fixes updated: CXF-8201 fix regression when handling file:// URI schemes (#636)

This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.2.x-fixes by this push:
     new 5a29e9c  CXF-8201 fix regression when handling file:// URI schemes (#636)
5a29e9c is described below

commit 5a29e9c077f709f3940c6d42366306d0a4dc99e9
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Thu Jan 30 01:08:06 2020 +0000

    CXF-8201 fix regression when handling file:// URI schemes (#636)
---
 .../apache/cxf/common/util/CollectionUtils.java    |  4 ++
 .../cxf/common/util/CollectionUtilsTest.java       |  5 +++
 .../org/apache/cxf/jaxrs/impl/UriBuilderImpl.java  | 15 +++++--
 .../apache/cxf/jaxrs/impl/UriBuilderImplTest.java  | 48 ++++++++++++++++++++++
 4 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/common/util/CollectionUtils.java b/core/src/main/java/org/apache/cxf/common/util/CollectionUtils.java
index c0a7e08..f733741 100644
--- a/core/src/main/java/org/apache/cxf/common/util/CollectionUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/CollectionUtils.java
@@ -57,6 +57,10 @@ public final class CollectionUtils {
         return true;
     }
 
+    public static <K, V> boolean isEmpty(Map<K, V> m) {
+        return m == null || m.isEmpty();
+    }
+
     public static <S, T> Dictionary<S, T> singletonDictionary(S s, T t) {
         return toDictionary(Collections.singletonMap(s, t));
     }
diff --git a/core/src/test/java/org/apache/cxf/common/util/CollectionUtilsTest.java b/core/src/test/java/org/apache/cxf/common/util/CollectionUtilsTest.java
index 4947573..4dc446a 100644
--- a/core/src/test/java/org/apache/cxf/common/util/CollectionUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/CollectionUtilsTest.java
@@ -22,6 +22,8 @@ package org.apache.cxf.common.util;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
+import java.util.Dictionary;
+import java.util.Map;
 
 
 import org.junit.Assert;
@@ -53,5 +55,8 @@ public class CollectionUtilsTest extends Assert {
         List<String> l = Arrays.asList(new String[]{null, null});
         assertNotNull(l);
         assertTrue(CollectionUtils.isEmpty(l));
+
+        assertTrue(CollectionUtils.isEmpty((Collection) null));
+        assertTrue(CollectionUtils.isEmpty((Map) null));
     }
 }
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
index e23398f..23aff5e 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
@@ -39,6 +39,7 @@ import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriBuilderException;
 
+import org.apache.cxf.common.util.CollectionUtils;
 import org.apache.cxf.common.util.PropertyUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.jaxrs.model.URITemplate;
@@ -636,7 +637,7 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
         }
         String rawPath = uri.getRawPath();
         if (!uri.isOpaque() && schemeSpecificPart == null
-            && (theScheme != null || rawPath != null)) {
+                && (theScheme != null || rawPath != null)) {
             port = uri.getPort();
             host = uri.getHost();
             if (rawPath != null) {
@@ -651,8 +652,10 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
         } else {
             schemeSpecificPart = uri.getSchemeSpecificPart();
         }
-        if (scheme != null && host == null && (query == null || query.isEmpty()) && userInfo == null 
-            && uri.getSchemeSpecificPart() != null) {
+        if (scheme != null && host == null && port == -1 && userInfo == null
+                && CollectionUtils.isEmpty(query)
+                && uri.getSchemeSpecificPart() != null
+                && !schemeSpecificPartMatchesUriPath(uri)) {
             schemeSpecificPart = uri.getSchemeSpecificPart();
         }
         String theFragment = uri.getFragment();
@@ -661,6 +664,12 @@ public class UriBuilderImpl extends UriBuilder implements Cloneable {
         }
     }
 
+    private boolean schemeSpecificPartMatchesUriPath(final URI uri) {
+        return uri.getRawSchemeSpecificPart() != null
+                && uri.getPath() != null
+                && uri.getRawSchemeSpecificPart().equals("//" + uri.getPath());
+    }
+
     private void setPathAndMatrix(String path) {
         leadingSlash = !originalPathEmpty && path.startsWith("/");
         paths = JAXRSUtils.getPathSegments(path, false, false);
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
index 38b2814..5a32b3e 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
@@ -1631,6 +1631,54 @@ public class UriBuilderImplTest extends Assert {
     }
 
     @Test
+    public void testURIWithExtraPathMatchesOriginalURIPlusPath() {
+        assertEquals("mailto:bob@apache.org",
+                UriBuilder.fromUri("mailto:bob@apache.org").path("extra").build().toString());
+
+        assertEquals("news:comp.lang.java",
+                UriBuilder.fromUri("news:comp.lang.java").path("extra").build().toString());
+
+        assertEquals("urn:isbn:096139210x",
+                UriBuilder.fromUri("urn:isbn:096139210x").path("extra").build().toString());
+
+        assertEquals("docs/guide/collections/designfaq.html/extra#28",
+                UriBuilder.fromUri("docs/guide/collections/designfaq.html#28").path("extra").build().toString());
+
+        assertEquals("../../../demo/jfc/SwingSet2/src/SwingSet2.java/extra",
+                UriBuilder.fromUri("../../../demo/jfc/SwingSet2/src/SwingSet2.java").path("extra").build().toString());
+
+        assertEquals("file:///~/calendar/extra",
+                UriBuilder.fromUri("file:///~/calendar").path("extra").build().toString());
+
+        assertEquals("bob@somehost.com/extra",
+                UriBuilder.fromUri("bob@somehost.com").path("extra").build().toString());
+
+        assertEquals("http://localhost/somePath/extra",
+                UriBuilder.fromUri("http://localhost/somePath").path("extra").build().toString());
+
+        assertEquals("http://localhost:1234/someOtherPath/extra",
+                UriBuilder.fromUri("http://localhost:1234/someOtherPath").path("extra").build().toString());
+
+        assertEquals("http://127.0.0.1/extra",
+                UriBuilder.fromUri("http://127.0.0.1").path("extra").build().toString());
+
+        assertEquals("http://127.0.0.1/extra",
+                UriBuilder.fromUri("http://127.0.0.1/").path("extra").build().toString());
+
+        assertEquals("http://127.0.0.1/index.html/extra",
+                UriBuilder.fromUri("http://127.0.0.1/index.html").path("extra").build().toString());
+
+        assertEquals("myscheme://a.host:7575/extra",
+                UriBuilder.fromUri("myscheme://a.host:7575/").path("extra").build().toString());
+
+        // note that this will use the scheme specific part of the URI, as opposed to host, port and path,
+        // and therefore the extra path will not be appended. URI uses an int for the port, and therefore
+        // will not parse the "fakePort" part of this URI as a port.
+        assertEquals("myscheme://not.really.a.host:fakePort/",
+                UriBuilder.fromUri("myscheme://not.really.a.host:fakePort/").path("extra").build().toString());
+    }
+
+    @Test
     public void testURIWithNonIntegerPort() {
         String url = "myscheme://not.really.a.host:port/";
         UriBuilder builder = UriBuilder.fromUri(url);