diff options
author | gjoranv <gjoranv@gmail.com> | 2017-11-10 14:04:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-10 14:04:01 +0100 |
commit | f1bfbeeb69c9825c1790f820558e0de075590675 (patch) | |
tree | 66d3ec16efbc7687baff2a65f1532c2ac9705ba7 /jdisc_core | |
parent | 69fd4be01a02864c86b861f7cb13fd81a2d1c947 (diff) | |
parent | e2cea88e5be7e86625426d28336aaf35f710721b (diff) |
Merge pull request #4073 from vespa-engine/arnej/add-uripattern-to-bindingmatch
Arnej/add uripattern to bindingmatch
Diffstat (limited to 'jdisc_core')
4 files changed, 42 insertions, 10 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingMatch.java b/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingMatch.java index 5d4974f2dc4..7318b1b38ae 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingMatch.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingMatch.java @@ -15,6 +15,7 @@ public class BindingMatch<T> { private final UriPattern.Match match; private final T target; + private final UriPattern matched; /** * <p>Constructs a new instance of this class.</p> @@ -22,12 +23,27 @@ public class BindingMatch<T> { * @param match The match information for this instance. * @param target The target of this match. * @throws NullPointerException If any argument is null. + * @deprecated use BindingMatch(UriPattern.Match match, T target, UriPattern matched) */ + @Deprecated public BindingMatch(UriPattern.Match match, T target) { + this(match, target, null); + } + + /** + * <p>Constructs a new instance of this class.</p> + * + * @param match The match information for this instance. + * @param target The target of this match. + * @param matched The matched URI pattern + * @throws NullPointerException If any argument is null. + */ + public BindingMatch(UriPattern.Match match, T target, UriPattern matched) { Objects.requireNonNull(match, "match"); Objects.requireNonNull(target, "target"); this.match = match; this.target = target; + this.matched = matched; } /** @@ -61,4 +77,14 @@ public class BindingMatch<T> { public T target() { return target; } + + /** + * <p>Returns the URI pattern that was matched.</p> + * + * @return The matched pattern. + */ + public UriPattern matched() { + return matched; + } + } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingSet.java b/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingSet.java index 7a21e204dd3..1e25846f63c 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingSet.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingSet.java @@ -38,9 +38,10 @@ public class BindingSet<T> implements Iterable<Map.Entry<UriPattern, T>> { */ public BindingMatch<T> match(URI uri) { for (Map.Entry<UriPattern, T> entry : bindings) { - UriPattern.Match match = entry.getKey().match(uri); + UriPattern pattern = entry.getKey(); + UriPattern.Match match = pattern.match(uri); if (match != null) { - return new BindingMatch<>(match, entry.getValue()); + return new BindingMatch<>(match, entry.getValue(), pattern); } } return null; diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/RequestTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/RequestTestCase.java index e00bdae153d..cd5e07f1224 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/RequestTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/RequestTestCase.java @@ -297,9 +297,10 @@ public class RequestTestCase { public RequestHandler resolveHandler(Request request) { this.asServer = request.isServerRequest(); RequestHandler requestHandler = new MyRequestHandler(); - request.setBindingMatch(new BindingMatch<>( - new UriPattern("http://*/*").match(request.getUri()), - requestHandler)); + UriPattern pattern = new UriPattern("http://*/*"); + request.setBindingMatch(new BindingMatch<>(pattern.match(request.getUri()), + requestHandler, + pattern)); return requestHandler; } diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/application/BindingMatchTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/application/BindingMatchTestCase.java index 582a1c6685e..21a3ae08c49 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/application/BindingMatchTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/application/BindingMatchTestCase.java @@ -18,32 +18,36 @@ public class BindingMatchTestCase { @Test public void requireThatAccessorsWork() { Object obj = new Object(); + UriPattern pattern = new UriPattern("http://*/*"); BindingMatch<Object> match = new BindingMatch<>( - new UriPattern("http://*/*").match(URI.create("http://localhost:69/status.html")), - obj); + pattern.match(URI.create("http://localhost:69/status.html")), + obj, pattern); assertSame(obj, match.target()); assertEquals(3, match.groupCount()); assertEquals("localhost", match.group(0)); assertEquals("69", match.group(1)); assertEquals("status.html", match.group(2)); + assertEquals(pattern, match.matched()); } @Test public void requireThatConstructorArgumentsCanNotBeNull() { try { - new BindingMatch<>(null, null); + new BindingMatch<>(null, null, null); fail(); } catch (NullPointerException e) { } try { - new BindingMatch<>(new UriPattern("http://*/*").match(URI.create("http://localhost/")), null); + UriPattern pattern = new UriPattern("http://*/*"); + new BindingMatch<>(pattern.match(URI.create("http://localhost/")), null, pattern); fail(); } catch (NullPointerException e) { } try { - new BindingMatch<>(null, new Object()); + UriPattern pattern = new UriPattern("http://*/*"); + new BindingMatch<>(null, new Object(), pattern); fail(); } catch (NullPointerException e) { |