cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1333910 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
Date Fri, 04 May 2012 12:35:39 GMT
Author: sergeyb
Date: Fri May  4 12:35:39 2012
New Revision: 1333910

URL: http://svn.apache.org/viewvc?rev=1333910&view=rev
Log:
[CXF-4281] Update to UriBuilder.replacePath implementation

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java?rev=1333910&r1=1333909&r2=1333910&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
Fri May  4 12:35:39 2012
@@ -387,9 +387,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;
         }
@@ -578,14 +576,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/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java?rev=1333910&r1=1333909&r2=1333910&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
Fri May  4 12:35:39 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() {



Mime
View raw message