From 488a3986b3271e49c56945f84bca6bc68bf289bc Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Thu, 9 Feb 2023 15:07:44 +0100 Subject: 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. --- .../jdisc/handler/DelegatedRequestHandler.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'jdisc_core/src/main/java') 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 Optional resolve(Class 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); + } + } -- cgit v1.2.3