You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2012/05/04 14:40:33 UTC

svn commit: r1333913 - in /cxf/branches/2.5.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java

Author: sergeyb
Date: Fri May  4 12:40:33 2012
New Revision: 1333913

URL: http://svn.apache.org/viewvc?rev=1333913&view=rev
Log:
Merged revisions 1333910 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1333910 | sergeyb | 2012-05-04 13:35:39 +0100 (Fri, 04 May 2012) | 1 line
  
  [CXF-4281] Update to UriBuilder.replacePath implementation
........

Modified:
    cxf/branches/2.5.x-fixes/   (props changed)
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1333910

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java?rev=1333913&r1=1333912&r2=1333913&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java (original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java Fri May  4 12:40:33 2012
@@ -386,9 +386,7 @@ public class UriBuilderImpl extends UriB
         if (path == null) {
             throw new IllegalArgumentException("path is null");
         }
-        // this is the cheapest way to figure out if a given path is a full-fledged 
-        // URI with the http(s) scheme but a more formal approach may be needed 
-        if (path.startsWith("http")) {
+        if (isAbsoluteUriPath(path)) {
             uri(URI.create(path));
             return this;
         }
@@ -577,14 +575,34 @@ public class UriBuilderImpl extends UriB
     @Override
     public UriBuilder replacePath(String path) {
         if (path == null) {
-            paths.clear();
-            matrix.clear();
+            clearPathAndMatrix();
+        } else if (isAbsoluteUriPath(path)) {
+            clearPathAndMatrix();
+            uri(URI.create(path));
         } else {
             setPathAndMatrix(path);
         }
         return this;
     }
 
+    private void clearPathAndMatrix() {
+        paths.clear();
+        matrix.clear();
+    }
+    
+    private boolean isAbsoluteUriPath(String path) {
+        // This is the cheapest way to figure out if a given path is an absolute 
+        // URI with the http(s) scheme, more expensive way is to always convert 
+        // a path to URI and check if it starts from some scheme or not
+        
+        // Given that the list of schemes can be open-ended it is recommended that
+        // UriBuilder.fromUri is called instead for schemes like 'file', 'jms', etc
+        // be supported though the use of non-http schemes for *building* new URIs
+        // is pretty limited in the context of working with JAX-RS services
+         
+        return path.startsWith("http");
+    }
+    
     @Override
     public UriBuilder replaceQuery(String queryValue) throws IllegalArgumentException {
         if (queryValue != null) {

Modified: cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java?rev=1333913&r1=1333912&r2=1333913&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java (original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java Fri May  4 12:40:33 2012
@@ -994,6 +994,26 @@ public class UriBuilderImplTest extends 
 
         assertEquals(expected, uri.toString());        
     }
+    
+    @Test
+    public void testFromEncodedDuplicateVarReplacePath() {
+        String expected = "http://localhost:8080/1/2/3/1";
+        URI uri = UriBuilder.fromPath("")
+                            .replacePath("http://localhost:8080")
+                            .path("/{a}/{b}/{c}/{a}")
+                            .buildFromEncoded("1", "2", "3");
+
+        assertEquals(expected, uri.toString());        
+    }
+    
+    @Test
+    public void testNullScheme() {
+        String expected = "//localhost:8080";
+        URI uri = UriBuilder.fromUri("http://localhost:8080")
+                            .scheme(null)
+                            .build();
+        assertEquals(expected, uri.toString());        
+    }
 
     @Test
     public void testNullMapValue() {