tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1416397 - in /tomcat/trunk: java/org/apache/tomcat/websocket/LocalStrings.properties java/org/apache/tomcat/websocket/UriTemplate.java test/org/apache/tomcat/websocket/TestUriTemplate.java
Date Mon, 03 Dec 2012 09:48:00 GMT
Author: markt
Date: Mon Dec  3 09:47:56 2012
New Revision: 1416397

URL: http://svn.apache.org/viewvc?rev=1416397&view=rev
Log:
Use Pattern.quote() as suggested by kkolinko
Tighten up implementation. WS spec (draft 8, section 3.1.1.) states "if the relative path
is a URI template (level-1) the request URI matches if and only if the request URI is an expanded
version of the URI-template" so every path segment must be present - none can be optional

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties
    tomcat/trunk/java/org/apache/tomcat/websocket/UriTemplate.java
    tomcat/trunk/test/org/apache/tomcat/websocket/TestUriTemplate.java

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties?rev=1416397&r1=1416396&r2=1416397&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties Mon Dec  3 09:47:56
2012
@@ -17,4 +17,5 @@ serverContainer.endpointDeploy=Endpoint 
 serverContainer.missingEndpoint=An Endpoint instance has been request for path [{0}] but
no matching Endpoint class was found
 serverContainer.pojoDeploy=POJO class [{0}] deploying to path [{1}] in ServletContext [{2}]
 serverContainer.servletContextMismatch=Attempted to register a POJO annotated for WebSocket
at path [{0}] in the ServletContext with context path [{1}] when the WebSocket ServerContainer
is allocated to the ServletContext with context path [{2}]
-serverContainer.servletContextMissing=No ServletContext was specified
\ No newline at end of file
+serverContainer.servletContextMissing=No ServletContext was specified
+uriTemplate.noMatch=The input template [{0}] generated the pattern [{1}] which did not match
the supplied pathInfo [{2}]
\ No newline at end of file

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/UriTemplate.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/UriTemplate.java?rev=1416397&r1=1416396&r2=1416397&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/UriTemplate.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/UriTemplate.java Mon Dec  3 09:47:56 2012
@@ -23,28 +23,36 @@ import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.tomcat.util.res.StringManager;
+
 /**
  * Extracts path parameters from URIs used to create web socket connections
  * using the URI template defined for the associated Endpoint.
  */
 public class UriTemplate {
 
+    private static StringManager sm = StringManager.getManager(
+            Constants.PACKAGE_NAME);
+
+    private final String template;
     private final Pattern pattern;
     private final List<String> names = new ArrayList<>();
 
     public UriTemplate(String template) {
+
+        this.template = template;
+
         // +10 is just a guess at this point
         StringBuilder pattern = new StringBuilder(template.length() + 10);
 
-
         int pos = 0;
         int end = 0;
         int start = template.indexOf('{');
         while (start > -1) {
             end = template.indexOf('}', start);
             pattern.append('(');
-            pattern.append(template.substring(pos, start));
-            pattern.append(")?([^/]*)");
+            pattern.append(Pattern.quote(template.substring(pos, start)));
+            pattern.append(")([^/]*)");
             names.add(template.substring(start + 1, end));
             pos = end + 1;
             start = template.indexOf('{', pos);
@@ -77,7 +85,11 @@ public class UriTemplate {
     public Map<String,String> match(String pathInfo) {
         Map<String,String> result = new HashMap<>();
         Matcher m = pattern.matcher(pathInfo);
-        m.matches();
+
+        if (!m.matches()) {
+            throw new IllegalArgumentException(sm.getString(
+                    "uriTemplate.noMatch", template, pattern, pathInfo));
+        }
 
         int group = 2;
         for (String name : names) {

Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TestUriTemplate.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TestUriTemplate.java?rev=1416397&r1=1416396&r2=1416397&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/websocket/TestUriTemplate.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/websocket/TestUriTemplate.java Mon Dec  3 09:47:56
2012
@@ -37,14 +37,10 @@ public class TestUriTemplate {
     }
 
 
-    @Test
+    @Test(expected=java.lang.IllegalArgumentException.class)
     public void testOneOfTwo() throws Exception {
         UriTemplate t = new UriTemplate("/{a}/{b}");
-        Map<String,String> result = t.match("/foo");
-
-        Assert.assertEquals(1, result.size());
-        Assert.assertTrue(result.containsKey("a"));
-        Assert.assertEquals("foo", result.get("a"));
+        t.match("/foo");
     }
 
 
@@ -61,24 +57,34 @@ public class TestUriTemplate {
     }
 
 
-    @Test
+    @Test(expected=java.lang.IllegalArgumentException.class)
     public void testPrefixOneOfTwo() throws Exception {
         UriTemplate t = new UriTemplate("/x{a}/y{b}");
-        Map<String,String> result = t.match("/xfoo");
-
-        Assert.assertEquals(1, result.size());
-        Assert.assertTrue(result.containsKey("a"));
-        Assert.assertEquals("foo", result.get("a"));
+        t.match("/xfoo");
     }
 
 
-    @Test
+    @Test(expected=java.lang.IllegalArgumentException.class)
     public void testPrefixTwoOfTwo() throws Exception {
         UriTemplate t = new UriTemplate("/x{a}/y{b}");
-        Map<String,String> result = t.match("/ybar");
+        t.match("/ybar");
+    }
+
+
+    @Test(expected=java.lang.IllegalArgumentException.class)
+    public void testQuote1() throws Exception {
+        UriTemplate t = new UriTemplate("/.{a}");
+        t.match("/yfoo");
+    }
+
+
+    @Test
+    public void testQuote2() throws Exception {
+        UriTemplate t = new UriTemplate("/.{a}");
+        Map<String,String> result = t.match("/.foo");
 
         Assert.assertEquals(1, result.size());
-        Assert.assertTrue(result.containsKey("b"));
-        Assert.assertEquals("bar", result.get("b"));
+        Assert.assertTrue(result.containsKey("a"));
+        Assert.assertEquals("foo", result.get("a"));
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message