cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r739990 - 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 Mon, 02 Feb 2009 14:18:15 GMT
Author: sergeyb
Date: Mon Feb  2 14:18:15 2009
New Revision: 739990

URL: http://svn.apache.org/viewvc?rev=739990&view=rev
Log:
CXF-1991 : 3rd patch from Andrzej Michalec, thanks !

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=739990&r1=739989&r2=739990&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
Mon Feb  2 14:18:15 2009
@@ -53,6 +53,7 @@
     private List<PathSegment> paths = new ArrayList<PathSegment>();
     private String fragment;
     private MultivaluedMap<String, String> query = new MetadataMap<String, String>();
+    private MultivaluedMap<String, String> matrix = new MetadataMap<String, String>();
 
     /**
      * Creates builder with empty URI.
@@ -149,6 +150,7 @@
     public UriBuilder clone() {
         return new UriBuilderImpl(build());
     }
+
     // CHECKSTYLE:ON
 
     @Override
@@ -178,6 +180,7 @@
         return path(ann.value());
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public UriBuilder path(Class resource, String method) throws IllegalArgumentException
{
         if (resource == null) {
@@ -275,6 +278,9 @@
         port = uri.getPort();
         host = uri.getHost();
         paths = JAXRSUtils.getPathSegments(uri.getPath(), false);
+        if (!paths.isEmpty()) {
+            matrix = paths.get(paths.size() - 1).getMatrixParameters();
+        }
         fragment = uri.getFragment();
         query = JAXRSUtils.getStructuredParams(uri.getQuery(), "&", true);
         userInfo = uri.getUserInfo();
@@ -292,47 +298,62 @@
                 sb.append(p);
             }
         }
+        if (!matrix.isEmpty()) {
+            sb.append(';');
+            sb.append(buildParams(matrix, ';'));
+        }
         return sb.toString();
     }
 
     private String buildQuery() {
-        StringBuilder b = new StringBuilder();
-        for (Iterator<Map.Entry<String, List<String>>> it = query.entrySet().iterator();
it.hasNext();) {
-            Map.Entry<String, List<String>> entry = it.next();
-            b.append(entry.getKey()).append('=').append(entry.getValue().get(0));
-            if (it.hasNext()) {
-                b.append('&');
-            }
-        }
-        return b.length() > 0 ? b.toString() : null;
+        return buildParams(query, '&');
     }
 
     @Override
     public UriBuilder matrixParam(String name, Object... values) throws IllegalArgumentException
{
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException("Not implemented :/");
+        if (name == null) {
+            throw new IllegalArgumentException("name is null");
+        }
+        List<String> list = matrix.get(name);
+        if (list == null) {
+            matrix.put(name, toStringList(values));
+        } else {
+            list.addAll(toStringList(values));
+        }
+        return this;
     }
 
     @Override
     public UriBuilder queryParam(String name, Object... values) throws IllegalArgumentException
{
-        List<String> queryList = new ArrayList<String>();
-        for (Object value : values) {
-            queryList.add(value.toString());
+        if (name == null) {
+            throw new IllegalArgumentException("name is null");
+        }
+        List<String> list = query.get(name);
+        if (list == null) {
+            query.put(name, toStringList(values));
+        } else {
+            list.addAll(toStringList(values));
         }
-        query.put(name, queryList);
         return this;
     }
 
     @Override
-    public UriBuilder replaceMatrix(String matrix) throws IllegalArgumentException {
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException("Not implemented :/");
+    public UriBuilder replaceMatrix(String matrixValues) throws IllegalArgumentException
{
+        this.matrix = JAXRSUtils.getStructuredParams(matrixValues, ";", true);
+        return this;
     }
 
     @Override
     public UriBuilder replaceMatrixParam(String name, Object... values) throws IllegalArgumentException
{
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException("Not implemented :/");
+        if (name == null) {
+            throw new IllegalArgumentException("name is null");
+        }
+        if (values != null && values.length >= 1 && values[0] != null)
{
+            matrix.put(name, toStringList(values));
+        } else {
+            matrix.remove(name);
+        }
+        return this;
     }
 
     @Override
@@ -348,17 +369,24 @@
     }
 
     @Override
-    public UriBuilder segment(String... segments) throws IllegalArgumentException {
-        for (String segment : segments) {
-            path(segment);
+    public UriBuilder replaceQueryParam(String name, Object... values) throws IllegalArgumentException
{
+        if (name == null) {
+            throw new IllegalArgumentException("name is null");
+        }
+        if (values != null && values.length >= 1 && values[0] != null)
{
+            query.put(name, toStringList(values));
+        } else {
+            query.remove(name);
         }
         return this;
     }
 
     @Override
-    public UriBuilder replaceQueryParam(String name, Object... values) throws IllegalArgumentException
{
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException("Not implemented :/");
+    public UriBuilder segment(String... segments) throws IllegalArgumentException {
+        for (String segment : segments) {
+            path(segment);
+        }
+        return this;
     }
 
     /**
@@ -378,4 +406,46 @@
         m.appendTail(sb);
         return sb.toString();
     }
+
+    /**
+     * Query or matrix params convertion from object values vararg to list of strings. No
encoding is
+     * provided.
+     * 
+     * @param values entry vararg values
+     * @return list of strings
+     * @throws IllegalArgumentException when one of values is null
+     */
+    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");
+            }
+            list.add(value.toString());
+        }
+        return list;
+    }
+
+    /**
+     * Builds param string for query part or matrix part of URI.
+     * 
+     * @param map query or matrix multivalued map
+     * @param separator params separator, '&' for query ';' for matrix
+     * @return stringified params.
+     */
+    private String buildParams(MultivaluedMap<String, String> map, char separator)
{
+        StringBuilder b = new StringBuilder();
+        for (Iterator<Map.Entry<String, List<String>>> it = map.entrySet().iterator();
it.hasNext();) {
+            Map.Entry<String, List<String>> entry = it.next();
+            for (Iterator<String> sit = entry.getValue().iterator(); sit.hasNext();)
{
+                String val = sit.next();
+                b.append(entry.getKey()).append('=').append(val);
+                if (sit.hasNext() || it.hasNext()) {
+                    b.append(separator);
+                }
+            }
+        }
+        return b.length() > 0 ? b.toString() : 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=739990&r1=739989&r2=739990&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
Mon Feb  2 14:18:15 2009
@@ -83,7 +83,7 @@
         URI newUri = new UriBuilderImpl(uri).path("/{a}").build("{foo}");
         assertEquals("URI is not built correctly", new URI("http://zzz/%7Bfoo%7D"), newUri);
     }
-    
+
     @Test
     public void testBuildValuesPct() throws Exception {
         URI uri = new URI("http://zzz");
@@ -127,13 +127,13 @@
         URI newUri = new UriBuilderImpl(uri).path("/{a}").buildFromMap(immutable);
         assertEquals("URI is not built correctly", new URI("http://zzz/%7Bfoo%7D"), newUri);
     }
-    
+
     @Test
     public void testBuildFromMapValuesPct() throws Exception {
         URI uri = new URI("http://zzz");
         Map<String, String> map = new HashMap<String, String>();
         map.put("a", "foo%25/bar%");
-        Map<String, String> immutable = Collections.unmodifiableMap(map);        
+        Map<String, String> immutable = Collections.unmodifiableMap(map);
         URI newUri = new UriBuilderImpl(uri).path("/{a}").buildFromMap(immutable);
         assertEquals("URI is not built correctly", new URI("http://zzz/foo%2525/bar%25"),
newUri);
     }
@@ -144,11 +144,11 @@
         Map<String, String> map = new HashMap<String, String>();
         map.put("a", "foo%25");
         map.put("b", "bar%");
-        Map<String, String> immutable = Collections.unmodifiableMap(map);        
+        Map<String, String> immutable = Collections.unmodifiableMap(map);
         URI newUri = new UriBuilderImpl(uri).path("/{a}/{b}").buildFromEncodedMap(immutable);
         assertEquals("URI is not built correctly", new URI("http://zzz/foo%25/bar%25"), newUri);
     }
-    
+
     @Test
     public void testAddPath() throws Exception {
         URI uri = new URI("http://foo/bar");
@@ -255,4 +255,210 @@
             .queryParam("n2", "v2").fragment("fragment").build();
         assertEquals("URI is not built correctly", uri, newUri);
     }
+
+    @Test
+    public void testReplaceQueryNull() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1&p2=v2");
+        URI newUri = new UriBuilderImpl(uri).replaceQuery(null).build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar"), newUri);
+    }
+
+    @Test
+    public void testReplaceQueryEmpty() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1&p2=v2");
+        URI newUri = new UriBuilderImpl(uri).replaceQuery("").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar"), newUri);
+    }
+
+    @Test
+    public void testReplaceQuery() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1");
+        URI newUri = new UriBuilderImpl(uri).replaceQuery("p1=nv1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=nv1"), newUri);
+    }
+
+    @Test
+    public void testReplaceQuery2() throws Exception {
+        URI uri = new URI("http://foo/bar");
+        URI newUri = new UriBuilderImpl(uri).replaceQuery("p1=nv1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=nv1"), newUri);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testQueryParamNameNull() throws Exception {
+        new UriBuilderImpl().queryParam(null, "baz");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testQueryParamNullVal() throws Exception {
+        new UriBuilderImpl().queryParam("foo", "bar", null, "baz");
+    }
+
+    @Test
+    public void testQueryParamSameNameAndVal() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1");
+        URI newUri = new UriBuilderImpl(uri).queryParam("p1", "v1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=v1&p1=v1"),
newUri);
+    }
+
+    @Test
+    public void testQueryParamVal() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1");
+        URI newUri = new UriBuilderImpl(uri).queryParam("p2", "v2").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=v1&p2=v2"),
newUri);
+    }
+
+    @Test
+    public void testQueryParamSameNameDiffVal() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1");
+        URI newUri = new UriBuilderImpl(uri).queryParam("p1", "v2").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=v1&p1=v2"),
newUri);
+    }
+
+    @Test
+    public void testQueryParamMultiVal() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1");
+        URI newUri = new UriBuilderImpl(uri).queryParam("p1", "v2", "v3").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=v1&p1=v2&p1=v3"),
newUri);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testReplaceQueryParamNameNull() throws Exception {
+        new UriBuilderImpl().replaceQueryParam(null, "baz");
+    }
+
+    @Test
+    public void testReplaceQueryParamValNull() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1&p2=v2&p1=v3");
+        URI newUri = new UriBuilderImpl(uri).replaceQueryParam("p1", (Object)null).build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p2=v2"), newUri);
+    }
+
+    @Test
+    public void testReplaceQueryParamValEmpty() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1&p2=v2&p1=v3");
+        URI newUri = new UriBuilderImpl(uri).replaceQueryParam("p1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p2=v2"), newUri);
+    }
+
+    @Test
+    public void testReplaceQueryParamExisting() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1");
+        URI newUri = new UriBuilderImpl(uri).replaceQueryParam("p1", "nv1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=nv1"), newUri);
+    }
+
+    @Test
+    public void testReplaceQueryParamExistingMulti() throws Exception {
+        URI uri = new URI("http://foo/bar?p1=v1&p2=v2");
+        URI newUri = new UriBuilderImpl(uri).replaceQueryParam("p1", "nv1", "nv2").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=nv1&p1=nv2&p2=v2"),
newUri);
+    }
+
+    @Test
+    public void testReplaceMatrixNull() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1;p2=v2");
+        URI newUri = new UriBuilderImpl(uri).replaceMatrix(null).build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar"), newUri);
+    }
+
+    @Test
+    public void testReplaceMatrixEmpty() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1;p2=v2");
+        URI newUri = new UriBuilderImpl(uri).replaceMatrix("").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar"), newUri);
+    }
+
+    @Test
+    public void testReplaceMatrix() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1;p2=v2");
+        URI newUri = new UriBuilderImpl(uri).replaceMatrix("p1=nv1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=nv1"), newUri);
+    }
+
+    @Test
+    public void testReplaceMatrix2() throws Exception {
+        URI uri = new URI("http://foo/bar/");
+        URI newUri = new UriBuilderImpl(uri).replaceMatrix("p1=nv1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar/;p1=nv1"), newUri);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testMatrixParamNameNull() throws Exception {
+        new UriBuilderImpl().matrixParam(null, "baz");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testMatrixParamNullVal() throws Exception {
+        new UriBuilderImpl().matrixParam("foo", "bar", null, "baz");
+    }
+
+    @Test
+    public void testMatrixParamSameNameAndVal() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1");
+        URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p1=v1"),
newUri);
+    }
+
+    @Test
+    public void testMatrixParamNewNameAndVal() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1");
+        URI newUri = new UriBuilderImpl(uri).matrixParam("p2", "v2").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p2=v2"),
newUri);
+    }
+
+    @Test
+    public void testMatrixParamSameNameDiffVal() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1");
+        URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v2").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p1=v2"),
newUri);
+    }
+
+    @Test
+    public void testMatrixParamMultiSameNameNewVals() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1");
+        URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v2", "v3").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p1=v2;p1=v3"),
newUri);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testReplaceMatrixParamNameNull() throws Exception {
+        new UriBuilderImpl().replaceMatrixParam(null, "baz");
+    }
+
+    @Test
+    public void testReplaceMatrixParamValNull() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1;p2=v2;p1=v3?noise=bazzz");
+        URI newUri = new UriBuilderImpl(uri).replaceMatrixParam("p1", (Object)null).build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p2=v2?noise=bazzz"),
newUri);
+    }
+
+    @Test
+    public void testReplaceMatrixParamValEmpty() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1;p2=v2;p1=v3?noise=bazzz");
+        URI newUri = new UriBuilderImpl(uri).replaceMatrixParam("p1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p2=v2?noise=bazzz"),
newUri);
+    }
+
+    @Test
+    public void testReplaceMatrixParamExisting() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1");
+        URI newUri = new UriBuilderImpl(uri).replaceMatrixParam("p1", "nv1").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=nv1"), newUri);
+    }
+
+    @Test
+    public void testReplaceMatrixParamExistingMulti() throws Exception {
+        URI uri = new URI("http://foo/bar;p1=v1;p2=v2");
+        URI newUri = new UriBuilderImpl(uri).replaceMatrixParam("p1", "nv1", "nv2").build();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=nv1;p1=nv2;p2=v2"),
newUri);
+    }
+
+    @Test
+    public void testMatrixNonFinalPathSegment() throws Exception {
+        URI uri = new URI("http://blah/foo;p1=v1/bar");
+        URI newUri = new UriBuilderImpl(uri).build();
+        assertEquals("URI is not built correctly", new URI("http://blah/foo/bar"), newUri);
+    }
+    
 }



Mime
View raw message