|
| 1 | +package io.avaje.http.api.context; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.concurrent.Callable; |
| 5 | +import java.util.function.Supplier; |
| 6 | + |
| 7 | +/** |
| 8 | + * The holder for the current request context that is bound to instrumented threads. Allowing lookup |
| 9 | + * of the current request if it is present. The Default implementation uses ThreadLocal. If you are |
| 10 | + * able, you should provide an implementation using ScopedValues. |
| 11 | + */ |
| 12 | +public interface RequestContextResolver { |
| 13 | + |
| 14 | + /** |
| 15 | + * Wraps the execution of the given callable in request context processing. |
| 16 | + * |
| 17 | + * @param ctx The request context |
| 18 | + * @param callable The callable |
| 19 | + * @param <T> The return type of the callable |
| 20 | + * @return The return value of the callable |
| 21 | + * @throws Exception if the callable throws an exception |
| 22 | + */ |
| 23 | + <T> T callWith(ServerContext ctx, Callable<T> callable) throws Exception; |
| 24 | + |
| 25 | + /** |
| 26 | + * Wraps the execution of the given supplier in request context processing. |
| 27 | + * |
| 28 | + * @param ctx The request context |
| 29 | + * @param supplier The supplier |
| 30 | + * @param <T> The return type of the supplier |
| 31 | + * @return The return value of the supplier |
| 32 | + */ |
| 33 | + <T> T supplyWith(ServerContext ctx, Supplier<T> supplier); |
| 34 | + |
| 35 | + /** |
| 36 | + * Wraps the execution of the given runnable in request context processing. |
| 37 | + * |
| 38 | + * @param ctx The request context |
| 39 | + * @param runnable The runnable |
| 40 | + */ |
| 41 | + void runWith(ServerContext request, Runnable runnable); |
| 42 | + |
| 43 | + /** |
| 44 | + * Retrieve the current server context. |
| 45 | + * |
| 46 | + * @return The request context if it is present |
| 47 | + */ |
| 48 | + Optional<ServerContext> currentRequest(); |
| 49 | +} |
0 commit comments