Search in sources :

Example 1 with MatchResult

use of java.util.regex.MatchResult in project jersey by jersey.

the class UriRoutingContext method getPathParameterBounds.

private int[] getPathParameterBounds(final String name) {
    final Iterator<UriTemplate> templatesIterator = templates.iterator();
    final Iterator<MatchResult> matchResultsIterator = matchResults.iterator();
    while (templatesIterator.hasNext()) {
        MatchResult mr = matchResultsIterator.next();
        // Find the index of path parameter
        final int pIndex = getLastPathParameterIndex(name, templatesIterator.next());
        if (pIndex != -1) {
            int pathLength = mr.group().length();
            int segmentIndex = mr.end(pIndex + 1);
            final int groupLength = segmentIndex - mr.start(pIndex + 1);
            // capturing group in the request path
            while (matchResultsIterator.hasNext()) {
                mr = matchResultsIterator.next();
                segmentIndex += mr.group().length() - pathLength;
                pathLength = mr.group().length();
            }
            return new int[] { segmentIndex - groupLength, segmentIndex };
        }
    }
    return null;
}
Also used : UriTemplate(org.glassfish.jersey.uri.UriTemplate) MatchResult(java.util.regex.MatchResult) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint)

Example 2 with MatchResult

use of java.util.regex.MatchResult in project jersey by jersey.

the class PathPatternTest method testLastElementOfMatchIsRestOfPath.

@Test
public void testLastElementOfMatchIsRestOfPath() throws Exception {
    PathPattern path = new PathPattern("{a: (\\d)(\\d*)}-{b: (\\d)(\\d*)}-{c: (\\d)(\\d*)}");
    MatchResult m = path.match("/123-456-789/d");
    String value = m.group(m.groupCount());
    assertEquals("Last value should match all of the trailing part", "/d", value);
}
Also used : MatchResult(java.util.regex.MatchResult) Test(org.junit.Test)

Example 3 with MatchResult

use of java.util.regex.MatchResult in project jersey by jersey.

the class PathPatternTest method testTerminalPathPatterMatching.

@Test
public void testTerminalPathPatterMatching() {
    final String[] rootPaths = new String[] { "/a/b/c", "/a/b/c/" };
    final String[] subPaths = new String[] { "d/e", "d/e/", "/d/e", "/d/e/" };
    final String path1 = "/a/b/c";
    final String path2 = "/a/b/c/";
    final PathPattern[] patterns = new PathPattern[] { new PathPattern("a"), new PathPattern("b"), new PathPattern("c"), new PathPattern("", PathPattern.RightHandPath.capturingZeroSegments) };
    String rhp = path1;
    MatchResult matchResult;
    for (PathPattern pattern : patterns) {
        matchResult = pattern.match(rhp);
        assertNotNull("No match of " + rhp + " for pattern " + pattern, matchResult);
        rhp = matchResult.group(matchResult.groupCount());
        rhp = (rhp == null) ? "" : rhp;
    }
    Assert.assertEquals("", rhp);
    rhp = path2;
    for (PathPattern pattern : patterns) {
        matchResult = pattern.match(rhp);
        rhp = matchResult.group(matchResult.groupCount());
    }
    assertEquals("/", rhp);
}
Also used : MatchResult(java.util.regex.MatchResult) Test(org.junit.Test)

Example 4 with MatchResult

use of java.util.regex.MatchResult in project jersey by jersey.

the class UriTemplateTest method _testMatching.

void _testMatching(final String template, final String uri, final String... values) {
    final UriTemplate t = new UriTemplate(template);
    final Map<String, String> m = new HashMap<String, String>();
    boolean isMatch = t.match(uri, m);
    assertTrue(isMatch);
    assertEquals(values.length, t.getTemplateVariables().size());
    final Iterator<String> names = t.getTemplateVariables().iterator();
    for (final String value : values) {
        final String mapValue = m.get(names.next());
        assertEquals(value, mapValue);
    }
    final List<String> matchedValues = new ArrayList<String>();
    isMatch = t.match(uri, matchedValues);
    assertTrue(isMatch);
    assertEquals(values.length, matchedValues.size());
    for (int i = 0; i < values.length; i++) {
        assertEquals(values[i], matchedValues.get(i));
    }
    final MatchResult mr = t.getPattern().match(uri);
    assertNotNull(mr);
    assertEquals(values.length, mr.groupCount());
    assertEquals(uri, mr.group());
    assertEquals(uri, mr.group(0));
    assertEquals(0, mr.start());
    assertEquals(uri.length(), mr.end());
    assertEquals(0, mr.start(0));
    assertEquals(uri.length(), mr.end(0));
    for (int i = 0; i < mr.groupCount(); i++) {
        assertEquals(values[i], mr.group(i + 1));
        assertEquals(values[i], uri.substring(mr.start(i + 1), mr.end(i + 1)));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MatchResult(java.util.regex.MatchResult)

Example 5 with MatchResult

use of java.util.regex.MatchResult in project jersey by jersey.

the class PathMatchingRouter method apply.

@Override
public Router.Continuation apply(final RequestProcessingContext context) {
    final RoutingContext rc = context.routingContext();
    // Peek at matching information to obtain path to match
    String path = rc.getFinalMatchingGroup();
    final TracingLogger tracingLogger = TracingLogger.getInstance(context.request());
    tracingLogger.log(ServerTraceEvent.MATCH_PATH_FIND, path);
    Router.Continuation result = null;
    final Iterator<Route> iterator = acceptedRoutes.iterator();
    while (iterator.hasNext()) {
        final Route acceptedRoute = iterator.next();
        final PathPattern routePattern = acceptedRoute.routingPattern();
        final MatchResult m = routePattern.match(path);
        if (m != null) {
            // Push match result information and rest of path to match
            rc.pushMatchResult(m);
            result = Router.Continuation.of(context, acceptedRoute.next());
            //tracing
            tracingLogger.log(ServerTraceEvent.MATCH_PATH_SELECTED, routePattern.getRegex());
            break;
        } else {
            tracingLogger.log(ServerTraceEvent.MATCH_PATH_NOT_MATCHED, routePattern.getRegex());
        }
    }
    if (tracingLogger.isLogEnabled(ServerTraceEvent.MATCH_PATH_SKIPPED)) {
        while (iterator.hasNext()) {
            tracingLogger.log(ServerTraceEvent.MATCH_PATH_SKIPPED, iterator.next().routingPattern().getRegex());
        }
    }
    if (result == null) {
        // No match
        return Router.Continuation.of(context);
    }
    return result;
}
Also used : PathPattern(org.glassfish.jersey.uri.PathPattern) TracingLogger(org.glassfish.jersey.message.internal.TracingLogger) MatchResult(java.util.regex.MatchResult)

Aggregations

MatchResult (java.util.regex.MatchResult)62 Matcher (java.util.regex.Matcher)26 Pattern (java.util.regex.Pattern)16 Scanner (java.util.Scanner)11 Point (java.awt.Point)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 MatcherState (com.github.anba.es6draft.regexp.MatcherState)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 InputStream (java.io.InputStream)2 Fault (org.apache.cxf.interceptor.Fault)2 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)2 IterableMatchResult (com.github.anba.es6draft.regexp.IterableMatchResult)1 MatcherResult (com.github.anba.es6draft.regexp.MatcherResult)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1