tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Konstantin Kolinko <knst.koli...@gmail.com>
Subject Re: svn commit: r1416047 - in /tomcat/trunk: java/org/apache/tomcat/websocket/ test/org/apache/tomcat/websocket/
Date Sun, 02 Dec 2012 21:38:45 GMT
2012/12/2  <markt@apache.org>:
> Author: markt
> Date: Sat Dec  1 20:17:38 2012
> New Revision: 1416047
>
> URL: http://svn.apache.org/viewvc?rev=1416047&view=rev
> Log:
> WebSocket 1.0 implementation part 6 of many
> Path parameter processing for POJOs
> Clearer differentiation between endpoint path, servlet mapping path and path info
>
> Added:
>     tomcat/trunk/java/org/apache/tomcat/websocket/UriTemplate.java   (with props)
>     tomcat/trunk/test/org/apache/tomcat/websocket/TestUriTemplate.java   (with props)
> Modified:
>     tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java
>     tomcat/trunk/java/org/apache/tomcat/websocket/ServerContainerImpl.java
>     tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java
>     tomcat/trunk/java/org/apache/tomcat/websocket/WsServlet.java
>

> --- tomcat/trunk/java/org/apache/tomcat/websocket/UriTemplate.java (added)
> +++ tomcat/trunk/java/org/apache/tomcat/websocket/UriTemplate.java Sat Dec  1 20:17:38
2012
> @@ -0,0 +1,84 @@
> +package org.apache.tomcat.websocket;
> +
> +import java.util.ArrayList;
> +import java.util.HashMap;
> +import java.util.List;
> +import java.util.Map;
> +import java.util.regex.Matcher;
> +import java.util.regex.Pattern;
> +
> +public class UriTemplate {
> +
> +    private final Pattern pattern;
> +    private final List<String> names = new ArrayList<>();
> +
> +    public UriTemplate(String 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));

When creating a pattern as above, some quoting is needed, e.g. "." -> "\.".

There is method Pattern.quote(), though I never used it when
concatenating several patterns together.

> +            pattern.append(")?([^/]*)");
> +            names.add(template.substring(start + 1, end));
> +            pos = end + 1;
> +            start = template.indexOf('{', pos);
> +        }
> +
> +        // No more matches, append current position to end
> +        if (pos < template.length()) {
> +            pattern.append('(');
> +            pattern.append(template.substring(pos));
> +            pattern.append(")?");
> +        }
> +
> +        this.pattern = Pattern.compile(pattern.toString());
> +    }

Best regards,
Konstantin Kolinko

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


Mime
View raw message