diff options
author | Bjørn Christian Seime <bjorncs@yahooinc.com> | 2023-02-09 15:07:44 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@yahooinc.com> | 2023-02-09 15:18:43 +0100 |
commit | 488a3986b3271e49c56945f84bca6bc68bf289bc (patch) | |
tree | 70c6e7f8dd08a4c223c1889a6b0023eac039020b /jdisc_core | |
parent | 6ef87df07fa0c0ffdd595e39647e8253cb558d92 (diff) |
Introduce capability support for jdisc request handlers
Add trait like interface to define capability mapping based on HTTP method and uri path.
Enforce required capabilities through wrapper handler through existing filtering request handler.
Diffstat (limited to 'jdisc_core')
-rw-r--r-- | jdisc_core/abi-spec.json | 4 | ||||
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/handler/DelegatedRequestHandler.java | 22 |
2 files changed, 25 insertions, 1 deletions
diff --git a/jdisc_core/abi-spec.json b/jdisc_core/abi-spec.json index 0213d382894..acacccb4241 100644 --- a/jdisc_core/abi-spec.json +++ b/jdisc_core/abi-spec.json @@ -521,7 +521,9 @@ ], "methods" : [ "public abstract com.yahoo.jdisc.handler.RequestHandler getDelegate()", - "public com.yahoo.jdisc.handler.RequestHandler getDelegateRecursive()" + "public com.yahoo.jdisc.handler.RequestHandler getDelegateRecursive()", + "public static com.yahoo.jdisc.handler.RequestHandler resolve(com.yahoo.jdisc.handler.RequestHandler)", + "public static java.util.Optional resolve(java.lang.Class, com.yahoo.jdisc.handler.RequestHandler)" ], "fields" : [ ] }, diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/DelegatedRequestHandler.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/DelegatedRequestHandler.java index d021eb5a79c..f79bc5fee58 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/DelegatedRequestHandler.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/DelegatedRequestHandler.java @@ -2,6 +2,8 @@ package com.yahoo.jdisc.handler; +import java.util.Optional; + public interface DelegatedRequestHandler extends RequestHandler { RequestHandler getDelegate(); @@ -12,4 +14,24 @@ public interface DelegatedRequestHandler extends RequestHandler { } return delegate; } + + /** Find delegated request handler recursively */ + static RequestHandler resolve(RequestHandler h) { + if (h instanceof DelegatedRequestHandler dh) return dh.getDelegateRecursive(); + return h; + } + + /** + * Find delegated request handler of specified type recursively + * Note that the returned handler may not be the innermost handler. + */ + static <T extends RequestHandler> Optional<T> resolve(Class<T> type, RequestHandler h) { + T candidate = type.isInstance(h) ? type.cast(h) : null; + while (h instanceof DelegatedRequestHandler) { + h = ((DelegatedRequestHandler) h).getDelegate(); + if (type.isInstance(h)) candidate = type.cast(h); + } + return Optional.ofNullable(candidate); + } + } |