Skip to content

Commit 93b91b9

Browse files
committed
feat: Wire AfterResponseHook through HandlerConfig
Add afterHooks field to HandlerConfig record, add afterHooks list and afterResponseHook(...) builder method beside interceptor(...), and pass List.copyOf(afterHooks) into HandlerConfig in build(). Hooks are stored but not yet fired — execution wiring comes in a later task.
1 parent 6b64b6c commit 93b91b9

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/main/java/com/retailsvc/http/OpenApiServer.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ record HandlerConfig(
5959
ExceptionHandler exceptionHandler,
6060
Map<String, HttpHandler> extras,
6161
Map<String, SchemeValidator> securityValidators,
62-
boolean externalAuth) {}
62+
boolean externalAuth,
63+
List<AfterResponseHook> afterHooks) {}
6364

6465
OpenApiServer(
6566
Spec spec,
@@ -175,6 +176,7 @@ public static final class Builder {
175176
private Map<String, RequestHandler> handlers;
176177
private final List<ResponseDecorator> decorators = new ArrayList<>();
177178
private final List<RequestInterceptor> interceptors = new ArrayList<>();
179+
private final List<AfterResponseHook> afterHooks = new ArrayList<>();
178180
private ExceptionHandler exceptionHandler;
179181
private int port = DEFAULT_PORT;
180182
private InetAddress bindAddress;
@@ -225,6 +227,17 @@ public Builder interceptor(RequestInterceptor interceptor) {
225227
return this;
226228
}
227229

230+
/**
231+
* Registers an {@link AfterResponseHook} invoked after each response is sent. Hooks run on the
232+
* request thread inside the library's request scope, in registration order, with all exceptions
233+
* swallowed. Hooks fire only when a {@link Request} was successfully built — pre-request
234+
* failures (404, 405, 400 validation) do not fire hooks.
235+
*/
236+
public Builder afterResponseHook(AfterResponseHook hook) {
237+
afterHooks.add(requireNonNull(hook, "hook must not be null"));
238+
return this;
239+
}
240+
228241
/**
229242
* Registers a {@link SchemeValidator} for the OpenAPI security scheme named {@code schemeName}.
230243
* The library extracts a {@link Credential} per request and hands it to this callback; return a
@@ -325,7 +338,8 @@ public OpenApiServer build() throws IOException {
325338
exceptionHandler,
326339
extras,
327340
Map.copyOf(securityValidators),
328-
externalAuth);
341+
externalAuth,
342+
List.copyOf(afterHooks));
329343
return new OpenApiServer(
330344
spec, resolved, handlerConfig, port, bindAddress, shutdownTimeoutSeconds);
331345
}

0 commit comments

Comments
 (0)