Skip to content

Commit 25c0eba

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 40ef8ff commit 25c0eba

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
@@ -60,7 +60,8 @@ record HandlerConfig(
6060
ExceptionHandler exceptionHandler,
6161
Map<String, RequestHandler> extras,
6262
Map<String, SchemeValidator> securityValidators,
63-
boolean externalAuth) {}
63+
boolean externalAuth,
64+
List<AfterResponseHook> afterHooks) {}
6465

6566
OpenApiServer(
6667
Spec spec,
@@ -178,6 +179,7 @@ public static final class Builder {
178179
private Map<String, RequestHandler> handlers;
179180
private final List<ResponseDecorator> decorators = new ArrayList<>();
180181
private final List<RequestInterceptor> interceptors = new ArrayList<>();
182+
private final List<AfterResponseHook> afterHooks = new ArrayList<>();
181183
private ExceptionHandler exceptionHandler;
182184
private int port = DEFAULT_PORT;
183185
private InetAddress bindAddress;
@@ -228,6 +230,17 @@ public Builder interceptor(RequestInterceptor interceptor) {
228230
return this;
229231
}
230232

233+
/**
234+
* Registers an {@link AfterResponseHook} invoked after each response is sent. Hooks run on the
235+
* request thread inside the library's request scope, in registration order, with all exceptions
236+
* swallowed. Hooks fire only when a {@link Request} was successfully built — pre-request
237+
* failures (404, 405, 400 validation) do not fire hooks.
238+
*/
239+
public Builder afterResponseHook(AfterResponseHook hook) {
240+
afterHooks.add(requireNonNull(hook, "hook must not be null"));
241+
return this;
242+
}
243+
231244
/**
232245
* Registers a {@link SchemeValidator} for the OpenAPI security scheme named {@code schemeName}.
233246
* The library extracts a {@link Credential} per request and hands it to this callback; return a
@@ -332,7 +345,8 @@ public OpenApiServer build() throws IOException {
332345
effectiveExceptionHandler,
333346
extras,
334347
Map.copyOf(securityValidators),
335-
externalAuth);
348+
externalAuth,
349+
List.copyOf(afterHooks));
336350
return new OpenApiServer(
337351
spec, resolved, handlerConfig, port, bindAddress, shutdownTimeoutSeconds);
338352
}

0 commit comments

Comments
 (0)