cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1133363 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/model/URITemplate.java test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
Date Wed, 08 Jun 2011 12:14:08 GMT
Author: sergeyb
Date: Wed Jun  8 12:14:08 2011
New Revision: 1133363

URL: http://svn.apache.org/viewvc?rev=1133363&view=rev
Log:
[CXF-3573] URI starting with '/;' match '/' thus some checks are needed to prevent matrix
params escaping

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java?rev=1133363&r1=1133362&r2=1133363&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
Wed Jun  8 12:14:08 2011
@@ -41,7 +41,9 @@ public final class URITemplate {
     public static final String FINAL_MATCH_GROUP = "FINAL_MATCH_GROUP";
     private static final String DEFAULT_PATH_VARIABLE_REGEX = "([^/]+?)";
     private static final String CHARACTERS_TO_ESCAPE = ".*+";
-
+    private static final String SLASH = "/";
+    private static final String SLASH_QUOTE = "/;";
+    
     private final String template;
     private final List<String> variables = new ArrayList<String>();
     private final List<String> customVariables = new ArrayList<String>();
@@ -141,7 +143,7 @@ public final class URITemplate {
         }
 
         Matcher m = templateRegexPattern.matcher(uri);
-        if (!m.matches()) {
+        if (!m.matches() || template.equals(SLASH) && uri.startsWith(SLASH_QUOTE))
{
             if (uri.contains(";")) {
                 // we might be trying to match one or few path segments
                 // containing matrix
@@ -150,14 +152,21 @@ public final class URITemplate {
                 List<PathSegment> uList = JAXRSUtils.getPathSegments(uri, false);
                 StringBuilder sb = new StringBuilder();
                 for (int i = 0; i < uList.size(); i++) {
-                    sb.append('/');
+                    String segment = null;
                     if (pList.size() > i && pList.get(i).getPath().indexOf('{')
== -1) {
-                        sb.append(uList.get(i).getPath());
+                        segment = uList.get(i).getPath();
                     } else {
-                        sb.append(HttpUtils.fromPathSegment(uList.get(i)));
+                        segment = HttpUtils.fromPathSegment(uList.get(i));
+                    }
+                    if (segment.length() > 0) {
+                        sb.append(SLASH);    
                     }
+                    sb.append(segment);
                 }
                 uri = sb.toString();
+                if (uri.length() == 0) {
+                    uri = SLASH;
+                }
                 m = templateRegexPattern.matcher(uri);
                 if (!m.matches()) {
                     return false;
@@ -184,8 +193,11 @@ public final class URITemplate {
         // The right hand side value, might be used to further resolve
         // sub-resources.
         
-        String finalGroup = i > groupCount ? "/" : m.group(groupCount);
-        templateVariableToValue.putSingle(FINAL_MATCH_GROUP, finalGroup == null ? "/" : finalGroup);
+        String finalGroup = i > groupCount ? SLASH : m.group(groupCount);
+        if (finalGroup == null) {
+            finalGroup = SLASH;
+        }
+        templateVariableToValue.putSingle(FINAL_MATCH_GROUP, finalGroup);
 
         return true;
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java?rev=1133363&r1=1133362&r2=1133363&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
Wed Jun  8 12:14:08 2011
@@ -62,6 +62,28 @@ public class URITemplateTest extends Ass
     }
 
     @Test
+    public void testMatchWithMatrixWithEmptyPath() throws Exception {
+        URITemplate uriTemplate = new URITemplate("/");
+        MultivaluedMap<String, String> values = new MetadataMap<String, String>();
+
+        boolean match = uriTemplate.match("/;a=b", values);
+        assertTrue(match);
+        String finalGroup = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
+        assertEquals("/", finalGroup);
+    }
+    
+    @Test
+    public void testMatchWithMatrixWithEmptyPath2() throws Exception {
+        URITemplate uriTemplate = new URITemplate("/");
+        MultivaluedMap<String, String> values = new MetadataMap<String, String>();
+
+        boolean match = uriTemplate.match("/;a=b/b/c", values);
+        assertTrue(match);
+        String finalGroup = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
+        assertEquals("/b/c", finalGroup);
+    }
+    
+    @Test
     public void testMatchWithMatrixOnClearPath1() throws Exception {
         URITemplate uriTemplate = new URITemplate("/customers/{id}");
         MultivaluedMap<String, String> values = new MetadataMap<String, String>();
@@ -123,6 +145,16 @@ public class URITemplateTest extends Ass
 
         assertTrue(uriTemplate.match("/customers;123456/123/orders;456/3", values));
     }
+    
+    @Test
+    public void testMatchWithMatrixOnClearPath5() throws Exception {
+        URITemplate uriTemplate = new URITemplate("/customers");
+        MultivaluedMap<String, String> values = new MetadataMap<String, String>();
+
+        assertTrue(uriTemplate.match("/customers;a=b", values));
+        String finalGroup = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
+        assertEquals("/", finalGroup);
+    }
 
     @Test
     public void testMatchBasicTwoParametersVariation1() throws Exception {
@@ -188,6 +220,17 @@ public class URITemplateTest extends Ass
         String subResourcePath = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
         assertEquals("/chapter/1", subResourcePath);
     }
+    
+    @Test
+    public void testURITemplateWithSubResourceVariation4() throws Exception {
+        URITemplate uriTemplate = new URITemplate("/");
+        MultivaluedMap<String, String> values = new MetadataMap<String, String>();
+
+        boolean match = uriTemplate.match("/books/123/chapter/1", values);
+        assertTrue(match);
+        String subResourcePath = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
+        assertEquals("/books/123/chapter/1", subResourcePath);
+    }
 
     @Test
     public void testBasicCustomExpression() throws Exception {



Mime
View raw message