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

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


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

commit eb0855b8613823682cf267e1de457b8670be9b7b
Author: Jonathan Gallimore <[email protected]>
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       |  4 +-
 .../org/apache/cxf/jaxrs/impl/UriBuilderImpl.java  | 15 +++++--
 .../apache/cxf/jaxrs/impl/UriBuilderImplTest.java  | 48 ++++++++++++++++++++++
 4 files changed, 67 insertions(+), 4 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 e4711d2..ebd91e1 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
@@ -56,6 +56,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 cb7b173..f2d7a8e 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,7 @@ package org.apache.cxf.common.util;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Dictionary;
+import java.util.Map;
 
 import org.junit.Test;
 
@@ -52,7 +53,8 @@ public class CollectionUtilsTest {
 
     @Test
     public void testIsEmpty() throws Exception {
-        assertTrue(CollectionUtils.isEmpty(null));
+        assertTrue(CollectionUtils.isEmpty((Collection) null));
+        assertTrue(CollectionUtils.isEmpty((Map) null));
 
         Collection<String> l = Arrays.asList(null, null);
         assertTrue(CollectionUtils.isEmpty(l));
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 8de34fb..01531bd 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 261bf5f..784da6e 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
@@ -1633,6 +1633,54 @@ public class UriBuilderImplTest {
     }
 
     @Test
+    public void testURIWithExtraPathMatchesOriginalURIPlusPath() {
+        assertEquals("mailto:[email protected]";,
+                
UriBuilder.fromUri("mailto:[email protected]";).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("[email protected]/extra",
+                
UriBuilder.fromUri("[email protected]").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);

Reply via email to