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/src | |
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/src')
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/handler/DelegatedRequestHandler.java | 22 |
1 files changed, 22 insertions, 0 deletions
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); + } + } |