@Test public void testSuffixPatternOverPrefixPatternMatch() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.register("/ma.*", h1); matcher.register(".*tch", h2); final Object h = matcher.lookup("/match"); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); }
@Test public void testRegisterUnregister() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final Object h3 = new Object(); final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.register("/h1", h1); matcher.register("/h2", h2); matcher.register("/h3", h3); Object h; h = matcher.lookup("/h1"); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); h = matcher.lookup("/h2"); Assert.assertNotNull(h); Assert.assertTrue(h2 == h); h = matcher.lookup("/h3"); Assert.assertNotNull(h); Assert.assertTrue(h3 == h); matcher.unregister("/h1"); h = matcher.lookup("/h1"); Assert.assertNull(h); }
public void register(final String hostname, final String uriPattern, final T object) { Args.notBlank(uriPattern, "URI pattern"); if (object == null) { return; } final String key = hostname != null ? hostname.toLowerCase(Locale.ROOT) : null; if (hostname == null || hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST)) { primary.register(uriPattern, object); } else { LookupRegistry<T> patternMatcher = virtualMap.get(key); if (patternMatcher == null) { final LookupRegistry<T> newPatternMatcher = registrySupplier.get(); patternMatcher = virtualMap.putIfAbsent(key, newPatternMatcher); if (patternMatcher == null) { patternMatcher = newPatternMatcher; } } patternMatcher.register(uriPattern, object); } }
@Override public T resolve(final HttpRequest request, final HttpContext context) throws MisdirectedRequestException { final URIAuthority authority = request.getAuthority(); final String key = authority != null ? authority.getHostName().toLowerCase(Locale.ROOT) : null; final LookupRegistry<T> patternMatcher = getPatternMatcher(key); if (patternMatcher == null) { throw new MisdirectedRequestException("Not authoritative"); } String path = request.getPath(); final int i = path.indexOf('?'); if (i != -1) { path = path.substring(0, i); } return patternMatcher.lookup(path); }
@Test public void testRegisterUnregister() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final Object h3 = new Object(); final LookupRegistry<Object> matcher = new UriPatternMatcher<>(); matcher.register("/h1", h1); matcher.register("/h2", h2); matcher.register("/h3", h3); Object h; h = matcher.lookup("/h1"); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); h = matcher.lookup("/h2"); Assert.assertNotNull(h); Assert.assertTrue(h2 == h); h = matcher.lookup("/h3"); Assert.assertNotNull(h); Assert.assertTrue(h3 == h); matcher.unregister("/h1"); h = matcher.lookup("/h1"); Assert.assertNull(h); }
public void register(final String hostname, final String uriPattern, final T object) { Args.notBlank(uriPattern, "URI pattern"); if (object == null) { return; } final String key = hostname != null ? hostname.toLowerCase(Locale.ROOT) : null; if (hostname == null || hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST)) { primary.register(uriPattern, object); } else { LookupRegistry<T> patternMatcher = virtualMap.get(key); if (patternMatcher == null) { final LookupRegistry<T> newPatternMatcher = registrySupplier.get(); patternMatcher = virtualMap.putIfAbsent(key, newPatternMatcher); if (patternMatcher == null) { patternMatcher = newPatternMatcher; } } patternMatcher.register(uriPattern, object); } }
@Override public T resolve(final HttpRequest request, final HttpContext context) throws MisdirectedRequestException { final URIAuthority authority = request.getAuthority(); final String key = authority != null ? authority.getHostName().toLowerCase(Locale.ROOT) : null; final LookupRegistry<T> patternMatcher = getPatternMatcher(key); if (patternMatcher == null) { throw new MisdirectedRequestException("Not authoritative"); } String path = request.getPath(); final int i = path.indexOf('?'); if (i != -1) { path = path.substring(0, i); } return patternMatcher.lookup(path); }
@Test public void testSuffixPatternOverPrefixPatternMatch() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final LookupRegistry<Object> matcher = new UriPatternMatcher<>(); matcher.register("/ma*", h1); matcher.register("*tch", h2); final Object h = matcher.lookup("/match"); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); }
@Test(expected = IllegalArgumentException.class) public void testRegisterInvalidInput() throws Exception { final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.register(null, null); }
@Test(expected = IllegalArgumentException.class) public void testLookupInvalidInput() throws Exception { final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.lookup(null); }
@Test public void testWildCardMatching2a() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final Object def = new Object(); final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.register(".*", def); matcher.register(".*\\.view", h1); matcher.register(".*\\.form", h2); Object h; h = matcher.lookup("/that.view"); Assert.assertNotNull(h); Assert.assertTrue(def == h); h = matcher.lookup("/that.form"); Assert.assertNotNull(h); Assert.assertTrue(def == h); h = matcher.lookup("/whatever"); Assert.assertNotNull(h); Assert.assertTrue(def == h); }
@Test(expected = IllegalArgumentException.class) public void testRegisterNull() throws Exception { final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.register(null, null); }
@Test(expected=IllegalArgumentException.class) public void testLookupInvalidInput() throws Exception { final LookupRegistry<Object> matcher = new UriPatternMatcher<>(); matcher.lookup(null); }
@Test public void testWildCardMatching2b() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final Object def = new Object(); final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.register(".*\\.form", h2); matcher.register(".*\\.view", h1); matcher.register(".*", def); Object h; h = matcher.lookup("/that.view"); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); h = matcher.lookup("/that.form"); Assert.assertNotNull(h); Assert.assertTrue(h2 == h); h = matcher.lookup("/whatever"); Assert.assertNotNull(h); Assert.assertTrue(def == h); }
@Test(expected=IllegalArgumentException.class) public void testRegisterInvalidInput() throws Exception { final LookupRegistry<Object> matcher = new UriPatternMatcher<>(); matcher.register(null, null); }
@Test public void testWildCardMatching2() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final Object def = new Object(); final LookupRegistry<Object> matcher = new UriPatternMatcher<>(); matcher.register("*", def); matcher.register("*.view", h1); matcher.register("*.form", h2); Object h; h = matcher.lookup("/that.view"); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); h = matcher.lookup("/that.form"); Assert.assertNotNull(h); Assert.assertTrue(h2 == h); h = matcher.lookup("/whatever"); Assert.assertNotNull(h); Assert.assertTrue(def == h); }
@Test(expected=IllegalArgumentException.class) public void testRegisterNull() throws Exception { final LookupRegistry<Object> matcher = new UriPatternMatcher<>(); matcher.register(null, null); }
@Test public void testWildCardMatching1() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final Object h3 = new Object(); final Object def = new Object(); final LookupRegistry<Object> matcher = new UriPatternMatcher<>(); matcher.register("*", def); matcher.register("/one/*", h1); matcher.register("/one/two/*", h2); matcher.register("/one/two/three/*", h3); Object h; h = matcher.lookup("/one/request"); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); h = matcher.lookup("/one/two/request"); Assert.assertNotNull(h); Assert.assertTrue(h2 == h); h = matcher.lookup("/one/two/three/request"); Assert.assertNotNull(h); Assert.assertTrue(h3 == h); h = matcher.lookup("default/request"); Assert.assertNotNull(h); Assert.assertTrue(def == h); }
@Test public void testWildCardMatching1a() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final Object h3 = new Object(); final Object def = new Object(); final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.register(".*", def); matcher.register("/one/.*", h1); matcher.register("/one/two/.*", h2); matcher.register("/one/two/three/.*", h3); Object h; h = matcher.lookup("/one/request"); Assert.assertNotNull(h); Assert.assertTrue(def == h); h = matcher.lookup("/one/two/request"); Assert.assertNotNull(h); Assert.assertTrue(def == h); h = matcher.lookup("/one/two/three/request"); Assert.assertNotNull(h); Assert.assertTrue(def == h); h = matcher.lookup("default/request"); Assert.assertNotNull(h); Assert.assertTrue(def == h); }
@Test public void testWildCardMatching1b() throws Exception { final Object h1 = new Object(); final Object h2 = new Object(); final Object h3 = new Object(); final Object def = new Object(); final LookupRegistry<Object> matcher = new UriRegexMatcher<>(); matcher.register("/one/two/three/.*", h3); matcher.register("/one/two/.*", h2); matcher.register("/one/.*", h1); matcher.register(".*", def); Object h; h = matcher.lookup("/one/request"); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); h = matcher.lookup("/one/two/request"); Assert.assertNotNull(h); Assert.assertTrue(h2 == h); h = matcher.lookup("/one/two/three/request"); Assert.assertNotNull(h); Assert.assertTrue(h3 == h); h = matcher.lookup("default/request"); Assert.assertNotNull(h); Assert.assertTrue(def == h); }