public HttpPredicate(String uri, @Nullable HttpVersion protocol, @Nullable HttpMethod method) { this.protocol = protocol; this.uri = uri; this.method = method; this.template = uri != null ? new UriPathTemplate(uri) : null; }
@Override public Map<String, String> apply(Object key) { if (template == null) { return null; } Map<String, String> headers = template.match(key.toString()); if (null != headers && !headers.isEmpty()) { return headers; } return null; }
String s = "^" + filterQueryParams(uriPattern);
String s = "^" + filterQueryParams(uriPattern);
/** * Matches the template against the given {@code uri} returning a map of path * parameters extracted from the uri, keyed by the names in the template. If the * uri does not match, or there are no path parameters, an empty map is returned. * * @param uri The uri to match * * @return the path parameters from the uri. Never {@code null}. */ final Map<String, String> match(String uri) { Map<String, String> pathParameters = vars.get(uri); if (null != pathParameters) { return pathParameters; } pathParameters = new HashMap<>(); Matcher m = matcher(uri); if (m.matches()) { int i = 1; for (String name : pathVariables) { String val = m.group(i++); pathParameters.put(name, val); } } synchronized (vars) { vars.put(uri, pathParameters); } return pathParameters; }
/** * Matches the template against the given {@code uri} returning a map of path * parameters extracted from the uri, keyed by the names in the template. If the * uri does not match, or there are no path parameters, an empty map is returned. * * @param uri The uri to match * * @return the path parameters from the uri. Never {@code null}. */ final Map<String, String> match(String uri) { Map<String, String> pathParameters = vars.get(uri); if (null != pathParameters) { return pathParameters; } pathParameters = new HashMap<>(); Matcher m = matcher(uri); if (m.matches()) { int i = 1; for (String name : pathVariables) { String val = m.group(i++); pathParameters.put(name, val); } } synchronized (vars) { vars.put(uri, pathParameters); } return pathParameters; }
/** * Tests the given {@code uri} against this template, returning {@code true} if * the uri matches the template, {@code false} otherwise. * * @param uri The uri to match * * @return {@code true} if there's a match, {@code false} otherwise */ public boolean matches(String uri) { return matcher(uri).matches(); }
private Matcher matcher(String uri) { uri = filterQueryParams(uri); Matcher m = matchers.get(uri); if (null == m) { m = uriPattern.matcher(uri); synchronized (matchers) { matchers.put(uri, m); } } return m; }
@Override public final boolean test(HttpServerRequest key) { return (protocol == null || protocol.equals(key.version())) && (method == null || method.equals( key.method())) && (template == null || template.matches(key.uri())); }
@Override public final boolean test(HttpServerRequest key) { return (protocol == null || protocol.equals(key.version())) && (method == null || method.equals( key.method())) && (template == null || template.matches(key.uri())); }
private Matcher matcher(String uri) { uri = filterQueryParams(uri); Matcher m = matchers.get(uri); if (null == m) { m = uriPattern.matcher(uri); synchronized (matchers) { matchers.put(uri, m); } } return m; }
@Test public void patternShouldMatchPathWithDots() { UriPathTemplate uriPathTemplate = new UriPathTemplate("/test/{order}"); // does not match, the dot in the segment parameter breaks matching // expected: a map containing {"order": "2.0"}, found: empty map assertThat(uriPathTemplate.match("/test/2.0").get("order"), is("2.0")); }
/** * Tests the given {@code uri} against this template, returning {@code true} if * the uri matches the template, {@code false} otherwise. * * @param uri The uri to match * * @return {@code true} if there's a match, {@code false} otherwise */ public boolean matches(String uri) { return matcher(uri).matches(); }
@Test public void parameterizedPatternShouldMatchPathWithQueryParams() { UriPathTemplate uriPathTemplate = new UriPathTemplate("/test/{order}"); // does not match, the query parameter breaks matching // expected: a map containing {"order": "3"}, found: a map containing {"order": "3?q=reactor"} assertEquals("3", uriPathTemplate.match("/test/3?q=reactor") .get("order")); // assertThat(uriPathTemplate.match("/test/3?q=reactor").get("order"), is("3")); }
@Test public void staticPatternShouldMatchPathWithQueryParams() { UriPathTemplate uriPathTemplate = new UriPathTemplate("/test/3"); // does not match, the query parameter breaks matching // expected: true, found: false assertTrue(uriPathTemplate.matches("/test/3?q=reactor")); // assertThat(uriPathTemplate.matches("/test/3?q=reactor"), is(true)); }
@Test public void parametrizedPathWithMultipleParametersShouldBeMatched() { UriPathTemplate template = new UriPathTemplate("/{collection}/{id}"); assertThat(template.matches("/comments/1"), is(true)); assertThat(template.match("/comments/1"), allOf(Arrays.asList(hasEntry("id", "1"), hasEntry("collection", "comments")))); }
@Test public void staticPathWithDotShouldBeMatched() { UriPathTemplate template = new UriPathTemplate("/1.0/comments"); assertThat(template.matches("/1.0/comments"), is(true)); assertThat(template.match("/1.0/comments").entrySet(), empty()); }
@Test public void parametrizedPathWithStaticSuffixShouldBeMatched() { UriPathTemplate template = new UriPathTemplate("/comments/{id}/author"); assertThat(template.matches("/comments/1/author"), is(true)); assertThat(template.match("/comments/1/author"), hasEntry("id", "1")); }
@Test public void staticPathShouldBeMatched() { UriPathTemplate template = new UriPathTemplate("/comments"); assertThat(template.matches("/comments"), is(true)); assertThat(template.match("/comments").entrySet(), empty()); } @Test
@Test public void parametrizedPathShouldBeMatched() { UriPathTemplate template = new UriPathTemplate("/comments/{id}"); assertThat(template.matches("/comments/1"), is(true)); assertThat(template.match("/comments/1"), hasEntry("id", "1")); }