cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r740015 - 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 15:51:54 GMT
Author: sergeyb
Date: Mon Feb  2 15:51:53 2009
New Revision: 740015

URL: http://svn.apache.org/viewvc?rev=740015&view=rev
Log:
CXF-1991: making sure matrix params can be added as part of path(String)

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=740015&r1=740014&r2=740015&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 15:51:53 2009
@@ -224,7 +224,15 @@
     @Override
     public UriBuilder path(String path) throws IllegalArgumentException {
         List<PathSegment> segments = JAXRSUtils.getPathSegments(path, false);
+        if (!paths.isEmpty() && !matrix.isEmpty()) {
+            PathSegment ps = paths.remove(paths.size() - 1);
+            paths.add(replacePathSegment(ps));
+        }
         paths.addAll(segments);
+        matrix.clear();
+        if (!paths.isEmpty()) {
+            matrix = paths.get(paths.size() - 1).getMatrixParameters();        
+        }
         return this;
     }
 
@@ -290,18 +298,19 @@
         StringBuilder sb = new StringBuilder();
         Iterator<PathSegment> iter = paths.iterator();
         while (iter.hasNext()) {
-            String p = iter.next().getPath();
+            PathSegment ps = iter.next();
+            String p = ps.getPath();
             if (p.length() != 0 || !iter.hasNext()) {
                 if (!p.startsWith("/")) {
                     sb.append('/');
                 }
                 sb.append(p);
+                if (iter.hasNext()) {
+                    buildMatrix(sb, ps.getMatrixParameters());
+                }
             }
         }
-        if (!matrix.isEmpty()) {
-            sb.append(';');
-            sb.append(buildParams(matrix, ';'));
-        }
+        buildMatrix(sb, matrix);
         return sb.toString();
     }
 
@@ -434,7 +443,7 @@
      * @param separator params separator, '&' for query ';' for matrix
      * @return stringified params.
      */
-    private String buildParams(MultivaluedMap<String, String> map, char separator)
{
+    private static 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();
@@ -448,4 +457,24 @@
         }
         return b.length() > 0 ? b.toString() : null;
     }
+    
+    /**
+     * Builds param string for matrix part of URI.
+     * 
+     * @param sb buffer to add the matrix part to, will get ';' added if map is not empty

+     * @param map matrix multivalued map
+     */    
+    private static void buildMatrix(StringBuilder sb, MultivaluedMap<String, String>
map) {
+        if (!map.isEmpty()) {
+            sb.append(';');
+            sb.append(buildParams(map, ';'));
+        }
+    }
+    
+    private PathSegment replacePathSegment(PathSegment ps) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(ps.getPath());
+        buildMatrix(sb, matrix);
+        return new PathSegmentImpl(sb.toString());
+    }
 }

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=740015&r1=740014&r2=740015&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 15:51:53 2009
@@ -458,7 +458,21 @@
     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);
+        assertEquals("URI is not built correctly", new URI("http://blah/foo;p1=v1/bar"),
newUri);
+    }
+    
+    @Test
+    public void testMatrixFinalPathSegment() throws Exception {
+        URI uri = new URI("http://blah/foo;p1=v1/bar;p2=v2");
+        URI newUri = new UriBuilderImpl(uri).build();
+        assertEquals("URI is not built correctly", new URI("http://blah/foo;p1=v1/bar;p2=v2"),
newUri);
+    }
+    
+    @Test
+    public void testAddPathWithMatrix() throws Exception {
+        URI uri = new URI("http://blah/foo/bar;p1=v1");
+        URI newUri = new UriBuilderImpl(uri).path("baz;p2=v2").build();
+        assertEquals("URI is not built correctly", new URI("http://blah/foo/bar;p1=v1/baz;p2=v2"),
newUri);
     }
     
 }



Mime
View raw message