aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/handler/DelegatedRequestHandler.java
blob: f79bc5fee588c5e2c0e78d0a083bb6f7a7d1e5dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.jdisc.handler;

import java.util.Optional;

public interface DelegatedRequestHandler extends RequestHandler {
    RequestHandler getDelegate();

    default RequestHandler getDelegateRecursive() {
        RequestHandler delegate = getDelegate();
        while(delegate instanceof DelegatedRequestHandler) {
            delegate = ((DelegatedRequestHandler) delegate).getDelegate();
        }
        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);
    }

}