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;
}
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);
}
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);
}
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)));
}
}
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;
}
Aggregations