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 2009/04/29 15:35:47 UTC

svn commit: r769784 - 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

Author: sergeyb
Date: Wed Apr 29 13:35:46 2009
New Revision: 769784

URL: http://svn.apache.org/viewvc?rev=769784&view=rev
Log:
JAXRS: updating UriBuilderImpl to support opaque URIs

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=769784&r1=769783&r2=769784&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 Wed Apr 29 13:35:46 2009
@@ -49,9 +49,10 @@
     private String host;
     private List<PathSegment> paths = new ArrayList<PathSegment>();
     private String fragment;
+    private String schemeSpecificPart; 
     private MultivaluedMap<String, String> query = new MetadataMap<String, String>();
     private MultivaluedMap<String, String> matrix = new MetadataMap<String, String>();
-
+    
     /**
      * Creates builder with empty URI.
      */
@@ -91,29 +92,40 @@
         // again if fromEncoded is set
         if (fromEncoded) {
             StringBuilder b = new StringBuilder();
-            b.append(scheme).append("://");
-            if (userInfo != null) {
-                b.append(userInfo).append('@');
-            }
-            b.append(host);
-            if (port != -1) {
-                b.append(':').append(port);    
-            }
-            if (thePath != null && thePath.length() > 0) {
-                b.append(thePath.startsWith("/") ? thePath : '/' + thePath);
-            }
-            if (theQuery != null && theQuery.length() != 0) {
-                b.append('?').append(theQuery);
+            b.append(scheme).append(":");
+            if (!isSchemeOpaque()) {
+                b.append("//");
+                if (userInfo != null) {
+                    b.append(userInfo).append('@');
+                }
+                b.append(host);
+                if (port != -1) {
+                    b.append(':').append(port);    
+                }
+                if (thePath != null && thePath.length() > 0) {
+                    b.append(thePath.startsWith("/") ? thePath : '/' + thePath);
+                }
+                if (theQuery != null && theQuery.length() != 0) {
+                    b.append('?').append(theQuery);
+                }
+            } else {
+                b.append(schemeSpecificPart);
             }
             if (fragment != null) {
                 b.append('#').append(fragment);
             }
             return new URI(b.toString());
-        } else {
+        } else if (!isSchemeOpaque()) {
             return new URI(scheme, userInfo, host, port, thePath, theQuery, fragment);
+        } else {
+            return new URI(scheme, schemeSpecificPart, fragment);
         }
     }
     
+    private boolean isSchemeOpaque() {
+        return schemeSpecificPart != null;
+    }
+    
     private String substituteVarargs(String path, Object... values) {
         Map<String, String> varValueMap = new HashMap<String, String>();
         URITemplate templ = new URITemplate(path);
@@ -311,7 +323,7 @@
             if (scheme == null) {
                 scheme = "http";
             }
-            URI uri = new URI(scheme + "://" + ssp);
+            URI uri = new URI(scheme, ssp, fragment);
             setUriParts(uri);
         } catch (URISyntaxException e) {
             throw new IllegalArgumentException("Wrong syntax of scheme-specific part", e);
@@ -336,12 +348,22 @@
             throw new IllegalArgumentException("uri is null");
         }
         scheme = uri.getScheme();
-        port = uri.getPort();
-        host = uri.getHost();
-        setPathAndMatrix(uri.getRawPath());
+        if (!uri.isOpaque()) {
+            port = uri.getPort();
+            host = uri.getHost();
+            String rawPath = uri.getRawPath();
+            if (rawPath != null) {
+                setPathAndMatrix(uri.getRawPath());
+            }
+            String rawQuery = uri.getRawQuery();
+            if (rawQuery != null) {
+                query = JAXRSUtils.getStructuredParams(rawQuery, "&", false);
+            }
+            userInfo = uri.getUserInfo();
+        } else {
+            schemeSpecificPart = uri.getSchemeSpecificPart();
+        }
         fragment = uri.getFragment();
-        query = JAXRSUtils.getStructuredParams(uri.getRawQuery(), "&", false);
-        userInfo = uri.getUserInfo();
     }
 
     private void setPathAndMatrix(String path) {
@@ -502,7 +524,10 @@
                 if (fromEncoded) {
                     val = HttpUtils.encodePartiallyEncoded(val, isQuery);
                 }
-                b.append(entry.getKey()).append('=').append(val);
+                b.append(entry.getKey());
+                if (val.length() != 0) {
+                    b.append('=').append(val);
+                }
                 if (sit.hasNext() || it.hasNext()) {
                     b.append(separator);
                 }

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=769784&r1=769783&r2=769784&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 Wed Apr 29 13:35:46 2009
@@ -26,6 +26,7 @@
 import java.util.Map;
 
 import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.UriBuilder;
 
 import org.apache.cxf.jaxrs.resources.Book;
 import org.apache.cxf.jaxrs.resources.BookStore;
@@ -119,8 +120,17 @@
     @Test
     public void testSchemeSpecificPart() throws Exception {
         URI uri = new URI("http://bar");
-        URI newUri = new UriBuilderImpl(uri).scheme("https").schemeSpecificPart("foo/bar").build();
-        assertEquals("URI is not built correctly", "https://foo/bar", newUri.toString());
+        URI newUri = new UriBuilderImpl(uri).scheme("https").schemeSpecificPart("//localhost:8080/foo/bar")
+            .build();
+        assertEquals("URI is not built correctly", "https://localhost:8080/foo/bar", newUri.toString());
+    }
+    
+    @Test
+    public void testOpaqueSchemeSpecificPart() throws Exception {
+        URI expectedUri = new URI("mailto:javanet@java.net.com");
+        URI newUri = new UriBuilderImpl().scheme("mailto")
+            .schemeSpecificPart("javanet@java.net.com").build();
+        assertEquals("URI is not built correctly", expectedUri, newUri);
     }
     
     @Test
@@ -570,6 +580,27 @@
         assertEquals("URI is not built correctly", new URI("http://blah/foo/bar;p1=v1/baz;p2=v2"), newUri);
     }
     
+    @Test
+    public void testNonHttpSchemes() {
+        String[] uris = {"ftp://ftp.is.co.za/rfc/rfc1808.txt",
+                         "mailto:java-net@java.sun.com",
+                         "news:comp.lang.java",
+                         "urn:isbn:096139212y",
+                         "ldap://[2001:db8::7]/c=GB?objectClass?one",
+                         "telnet://194.1.2.17:81/",
+                         "tel:+1-816-555-1212",
+                         "foo://bar.com:8042/there/here?name=baz#brr"};
+
+        int expectedCount = 0; 
+         
+        for (int i = 0; i < uris.length; i++) {
+            URI uri = UriBuilder.fromUri(uris[i]).build();
+            assertEquals("Strange", uri.toString(), uris[i]);
+            expectedCount++;
+        }
+        assertEquals(8, expectedCount);
+    }
+    
     private void compareURIs(URI uri1, URI uri2) {
         
         assertEquals("Unexpected scheme", uri1.getScheme(), uri2.getScheme());