You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2010/08/11 19:28:53 UTC

svn commit: r984484 - in /cxf/branches/2.2.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rt/frontend/jaxrs/...

Author: dkulp
Date: Wed Aug 11 17:28:52 2010
New Revision: 984484

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

........
  r983419 | sergeyb | 2010-08-08 10:53:25 -0400 (Sun, 08 Aug 2010) | 1 line
  
  Enabling some of disabled UriBuilderImpl tests
........

Modified:
    cxf/branches/2.2.x-fixes/   (props changed)
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java

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

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java?rev=984484&r1=984483&r2=984484&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java (original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java Wed Aug 11 17:28:52 2010
@@ -45,9 +45,10 @@ public class UriBuilderImpl extends UriB
 
     private String scheme;
     private String userInfo;
-    private int port;
+    private int port = -1;
     private String host;
     private List<PathSegment> paths = new ArrayList<PathSegment>();
+    private boolean leadingSlash;
     private String fragment;
     private String schemeSpecificPart; 
     private MultivaluedMap<String, String> query = new MetadataMap<String, String>();
@@ -88,7 +89,6 @@ public class UriBuilderImpl extends UriB
                     theQuery = substituteVarargs(queryTempl, values, values.length - lengthDiff);
                 }
             }
-            
             return buildURI(fromEncoded, thePath, theQuery);
         } catch (URISyntaxException ex) {
             throw new UriBuilderException("URI can not be built", ex);
@@ -126,7 +126,13 @@ public class UriBuilderImpl extends UriB
             }
             return new URI(b.toString());
         } else if (!isSchemeOpaque()) {
-            return new URI(scheme, userInfo, host, port, thePath, theQuery, fragment);
+            if ((scheme != null || host != null || userInfo != null)
+                && thePath.length() != 0 && !thePath.startsWith("/")) {
+                thePath = "/" + thePath;
+            }
+            
+            return new URI(scheme, userInfo, host, port, 
+                           thePath, theQuery, fragment);
         } else {
             return new URI(scheme, schemeSpecificPart, fragment);
         }
@@ -149,6 +155,9 @@ public class UriBuilderImpl extends UriB
         int idx = ind;
         for (String var : uniqueVars) {
             Object oval = values[idx++];
+            if (oval == null) {
+                throw new IllegalArgumentException("No object for " + var);
+            }
             varValueMap.put(var, oval.toString());
         }
         return templ.substitute(varValueMap);
@@ -249,6 +258,9 @@ public class UriBuilderImpl extends UriB
 
     @Override
     public UriBuilder host(String theHost) throws IllegalArgumentException {
+        if ("".equals(theHost)) {
+            throw new IllegalArgumentException("Host cannot be empty");
+        }
         this.host = theHost;
         return this;
     }
@@ -314,7 +326,16 @@ 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")) {
+            uri(URI.create(path));
+            return this;
+        }
         
+        if (paths.isEmpty()) {
+            leadingSlash = path.startsWith("/");
+        }
         List<PathSegment> segments = JAXRSUtils.getPathSegments(path, false, false);
         if (!paths.isEmpty() && !matrix.isEmpty()) {
             PathSegment ps = paths.remove(paths.size() - 1);
@@ -330,6 +351,9 @@ public class UriBuilderImpl extends UriB
 
     @Override
     public UriBuilder port(int thePort) throws IllegalArgumentException {
+        if (thePort < 0 && thePort != -1) {
+            throw new IllegalArgumentException("Port cannot be negative");
+        }
         this.port = thePort;
         return this;
     }
@@ -392,6 +416,13 @@ public class UriBuilderImpl extends UriB
     }
 
     private void setPathAndMatrix(String path) {
+        if (path.startsWith("http://")
+            || path.startsWith("https://")) {
+            setUriParts(URI.create(path));
+            return;
+        }
+        
+        leadingSlash = path.startsWith("/");
         paths = JAXRSUtils.getPathSegments(path, false, false);
         if (!paths.isEmpty()) {
             matrix = paths.get(paths.size() - 1).getMatrixParameters();
@@ -408,7 +439,9 @@ public class UriBuilderImpl extends UriB
             String p = ps.getPath();
             if (p.length() != 0 || !iter.hasNext()) {
                 p = fromEncoded ? new URITemplate(p).encodeLiteralCharacters() : p;
-                if (!p.startsWith("/")) {
+                if (sb.length() == 0 && leadingSlash) {
+                    sb.append('/');
+                } else if (!p.startsWith("/") && sb.length() > 0) {
                     sb.append('/');
                 }
                 sb.append(p);
@@ -485,7 +518,7 @@ public class UriBuilderImpl extends UriB
 
     @Override
     public UriBuilder replaceQuery(String queryValue) throws IllegalArgumentException {
-        query = JAXRSUtils.getStructuredParams(queryValue, "&", true);
+        query = JAXRSUtils.getStructuredParams(queryValue, "&", false);
         return this;
     }
 
@@ -504,6 +537,9 @@ public class UriBuilderImpl extends UriB
 
     @Override
     public UriBuilder segment(String... segments) throws IllegalArgumentException {
+        if (segments == null) {
+            throw new IllegalArgumentException("Segments should not be null");
+        }
         for (String segment : segments) {
             path(segment);
         }
@@ -520,12 +556,14 @@ public class UriBuilderImpl extends UriB
      */
     private List<String> toStringList(Object... values) throws IllegalArgumentException {
         List<String> list = new ArrayList<String>();
-        for (int i = 0; i < values.length; i++) {
-            Object value = values[i];
-            if (value == null) {
-                throw new IllegalArgumentException("Null value on " + i + " position");
+        if (values != null) {
+            for (int i = 0; i < values.length; i++) {
+                Object value = values[i];
+                if (value == null) {
+                    throw new IllegalArgumentException("Null value on " + i + " position");
+                }
+                list.add(value.toString());
             }
-            list.add(value.toString());
         }
         if (list.isEmpty()) {
             list.add("");

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java?rev=984484&r1=984483&r2=984484&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java (original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java Wed Aug 11 17:28:52 2010
@@ -255,7 +255,8 @@ public final class URITemplate {
                     }
                     sb.append(value);
                 } else {
-                    sb.append(var);
+                    throw new IllegalArgumentException("Template variable " + var.getName() 
+                        + " has no matching value"); 
                 }
             } else {
                 sb.append(chunk);

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java?rev=984484&r1=984483&r2=984484&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java (original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java Wed Aug 11 17:28:52 2010
@@ -60,7 +60,9 @@ public final class HttpUtils {
     private static final String LOCAL_HOST = "localhost";
     private static final Pattern ENCODE_PATTERN = Pattern.compile("%[0-9a-fA-F][0-9a-fA-F]");
     private static final String CHARSET_PARAMETER = "charset";
-        
+    // there are more of such characters, ex, '*' but '*' is not affected by UrlEncode
+    private static final String PATH_RESERVED_DELIMETERS = "=";
+    
     private HttpUtils() {
     }
     
@@ -84,7 +86,9 @@ public final class HttpUtils {
     }
     
     public static String pathEncode(String value) {
-        
+        if (isReservedPathSequence(value)) {
+            return value;
+        }
         String result = urlEncode(value);
         // URLEncoder will encode '+' to %2B but will turn ' ' into '+'
         // We need to retain '+' and encode ' ' as %20
@@ -98,6 +102,11 @@ public final class HttpUtils {
         return result;
     }
     
+    private static boolean isReservedPathSequence(String sequence) {
+        // realistically, we'd probably need to check every character
+        return PATH_RESERVED_DELIMETERS.equals(sequence);
+    }
+    
     /**
      * Encodes partially encoded string. Encode all values but those matching pattern 
      * "percent char followed by two hexadecimal digits".

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java?rev=984484&r1=984483&r2=984484&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java (original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java Wed Aug 11 17:28:52 2010
@@ -34,6 +34,7 @@ import org.apache.cxf.jaxrs.resources.Ur
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 
 import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class UriBuilderImplTest extends Assert {
@@ -730,4 +731,209 @@ public class UriBuilderImplTest extends 
             JAXRSUtils.getStructuredParams(uri2.getRawQuery(), "&", false);
         assertEquals("Unexpected queries", queries1, queries2);
     }
+    
+    
+    @Test
+    public void testTck1() {
+        String value = "test1#test2";
+        String expected = "test1%23test2";
+        String path = "{arg1}";
+        URI uri = UriBuilder.fromPath(path).build(value);
+        assertEquals(expected, uri.toString());        
+    }
+    @Test
+    public void testNullValue() {
+        String value = null;
+        String path = "{arg1}";
+        try {
+            UriBuilder.fromPath(path).build(value);
+            fail("Should be IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+    @Test
+    public void testFragment() {
+        String expected = "test#abc";
+        String path = "test";
+        URI uri = UriBuilder.fromPath(path).fragment("abc").build();
+        assertEquals(expected, uri.toString());        
+    }
+    @Test
+    @Ignore
+    public void testFragmentTemplate() {
+        String expected = "abc#xyz";
+        URI uri = UriBuilder
+            .fromPath("{arg1}")
+            .fragment("{arg2}")
+            .build("abc", "xyx");
+        assertEquals(expected, uri.toString());        
+    }
+    @Test
+    @Ignore
+    public void testSegments() {
+        String path1 = "ab";
+        String[] path2 = {"a1", "x/y", "3b "};
+        String expected = "ab/a1/x%2Fy/3b%20";
+
+        URI uri = UriBuilder.fromPath(path1).segment(path2).build();
+        assertEquals(expected, uri.toString());        
+    }
+    @Test
+    @Ignore
+    public void testSegments2() {
+        String path1 = "";
+        String[] path2 = {"a1", "/", "3b "};
+        String expected = "a1/%2F/3b%20";
+
+        URI uri = UriBuilder.fromPath(path1).segment(path2).build();
+        assertEquals(expected, uri.toString());        
+    }
+    @Test
+    @Ignore
+    public void testReplaceQuery3() {
+        String expected = "http://localhost:8080?name1=xyz";
+
+        URI uri = UriBuilder.fromPath("http://localhost:8080")
+            .queryParam("name", "x=", "y?", "x y", "&").replaceQuery("name1=xyz").build();
+        assertEquals(expected, uri.toString());        
+    }
+    
+    @Test
+    public void testNullSegment() {
+        try {
+            UriBuilder.fromPath("/").segment((String)null).build();
+            fail("Should be IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+    
+    @Test
+    public void testNullQueryParam() {
+        try {
+            UriBuilder.fromPath("http://localhost:8080").queryParam("hello", (String)null);
+            fail("Should be IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+
+    @Test
+    public void testReplaceQuery4() {
+        String expected = "http://localhost:8080";
+
+        URI uri = UriBuilder.fromPath("http://localhost:8080")
+            .queryParam("name", "x=", "y?", "x y", "&").replaceQuery(null).build();
+        assertEquals(expected, uri.toString());        
+    }
+    
+    
+    @Test
+    public void testInvalidPort() {
+        try {
+            UriBuilder.fromUri("http://localhost:8080/some/path?name=foo").port(-10).build();
+            fail("Should be IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+    
+    @Test
+    public void testResetPort() {
+        URI uri = UriBuilder.fromUri("http://localhost:8080/some/path").port(-1).build();
+        assertEquals("http://localhost/some/path", uri.toString());
+    }
+    
+    @Test
+    public void testInvalidHost() {
+        try {
+            UriBuilder.fromUri("http://localhost:8080/some/path?name=foo").host("").build();
+            fail("Should be IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+    
+    
+    @Test
+    public void testFromEncodedDuplicateVar2() {
+        String expected = "http://localhost:8080/xy/%20/%25/xy";
+        URI uri = UriBuilder.fromPath("http://localhost:8080")
+            .path("/{x}/{y}/{z}/{x}")
+            .buildFromEncoded("xy", " ", "%");
+        assertEquals(expected, uri.toString());        
+    }
+
+    @Test
+    public void testNullMapValue() {
+        try {
+            Map<String, String> maps = new HashMap<String, String>();
+            maps.put("x", null);
+            maps.put("y", "/path-absolute/test1");
+            maps.put("z", "fred@example.com");
+            maps.put("w", "path-rootless/test2");
+            maps.put("u", "extra");
+
+            URI uri = UriBuilder.fromPath("")
+                .path("{w}/{x}/{y}/{z}/{x}")
+                .buildFromMap(maps);
+            
+            fail("Should be IllegalArgumentException.  Not return " + uri.toString());
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+
+    @Test
+    public void testMissingMapValue() {
+        try {
+            Map<String, String> maps = new HashMap<String, String>();
+            maps.put("x", null);
+            maps.put("y", "/path-absolute/test1");
+            maps.put("z", "fred@example.com");
+            maps.put("w", "path-rootless/test2");
+            maps.put("u", "extra");
+
+            URI uri = UriBuilder.fromPath("")
+                .path("{w}/{v}/{x}/{y}/{z}/{x}")
+                .buildFromMap(maps);
+            
+            fail("Should be IllegalArgumentException.  Not return " + uri.toString());
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+    
+    @Test
+    @Ignore("This may need to be challenged, '23' overrides '=' for the 2nd occurence of x")
+    public void testFromEncodedDuplicateVar() {
+        String expected = "http://localhost:8080/a/%25/=/%25G0/%25/=";
+
+        URI uri = UriBuilder.fromPath("http://localhost:8080")
+            .path("/{v}/{w}/{x}/{y}/{z}/{x}")
+            .buildFromEncoded("a", "%25", "=", "%G0", "%", "23"); 
+        assertEquals(expected, uri.toString());        
+    }
+    
+    @Test
+    @Ignore("name2=%20 is double encoded after the replacement -> name2=%2520")
+    public void testReplaceQuery5() {
+        String expected = "http://localhost:8080?name1=x&name2=%20&name3=x+y&name4=23&name5=x%20y";
+
+        URI uri = UriBuilder.fromPath("http://localhost:8080")
+            .queryParam("name", "x=", "y?", "x y", "&")
+            .replaceQuery("name1=x&name2=%20&name3=x+y&name4=23&name5=x y").build();
+        assertEquals(expected, uri.toString());        
+    }
+    
+    @Test
+    @Ignore("query parameters are not encoded due to build() being called")
+    public void testQueryParam() {
+        String expected = "http://localhost:8080?name=x%3D&name=y?&name=x+y&name=%26";
+
+        URI uri =  UriBuilder.fromPath("http://localhost:8080")
+            .queryParam("name", "x=", "y?", "x y", "&").build();
+        assertEquals(expected, uri.toString());        
+    }
 }

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java?rev=984484&r1=984483&r2=984484&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java (original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java Wed Aug 11 17:28:52 2010
@@ -367,12 +367,12 @@ public class URITemplateTest extends Ass
         assertEquals("Wrong substitution", "/foo/bar/bar/bar", ut.substitute(map));
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public void testSubstituteMapIncomplete() throws Exception {
         URITemplate ut = new URITemplate("/foo/{a}/{b}/{a:\\d}");
         Map<String, String> map = new HashMap<String, String>();
         map.put("b", "bar");
-        assertEquals("Wrong substitution", "/foo/{a}/bar/{a:\\d}", ut.substitute(map));
+        ut.substitute(map);
     }
 
     @Test