aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core
diff options
context:
space:
mode:
Diffstat (limited to 'jdisc_core')
-rw-r--r--jdisc_core/abi-spec.json4
-rw-r--r--jdisc_core/src/main/java/com/yahoo/jdisc/handler/DelegatedRequestHandler.java22
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);
+ }
+
}