Package pl.gsmservice.gateway.utils
Class AsyncHooks
- java.lang.Object
-
- pl.gsmservice.gateway.utils.AsyncHooks
-
- All Implemented Interfaces:
AsyncHook.AfterError,AsyncHook.AfterSuccess,AsyncHook.BeforeRequest
public class AsyncHooks extends java.lang.Object implements AsyncHook.BeforeRequest, AsyncHook.AfterSuccess, AsyncHook.AfterError
Async hook registry for runtime request/response processing.Example usage:
asyncHooks.registerBeforeRequest((context, request) -> CompletableFuture.completedFuture( Helpers.copy(request) .header("transaction-id", UUID.randomUUID().toString()) .build()));
-
-
Field Summary
-
Fields inherited from interface pl.gsmservice.gateway.utils.AsyncHook.AfterError
DEFAULT
-
Fields inherited from interface pl.gsmservice.gateway.utils.AsyncHook.AfterSuccess
DEFAULT
-
Fields inherited from interface pl.gsmservice.gateway.utils.AsyncHook.BeforeRequest
DEFAULT
-
-
Constructor Summary
Constructors Constructor Description AsyncHooks()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<Blob>>afterError(Hook.AfterErrorContext context, java.net.http.HttpResponse<Blob> response, java.lang.Throwable error)Either returns an HttpResponse or throws an Exception.java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<Blob>>afterSuccess(Hook.AfterSuccessContext context, java.net.http.HttpResponse<Blob> response)Transforms the given response before response processing occurs.java.util.concurrent.CompletableFuture<java.net.http.HttpRequest>beforeRequest(Hook.BeforeRequestContext context, java.net.http.HttpRequest request)Transforms the givenHttpRequestbefore sending.AsyncHooksregisterAfterError(AsyncHook.AfterError afterError)Registers an async after-error hook.AsyncHooksregisterAfterSuccess(AsyncHook.AfterSuccess afterSuccess)Registers an async after-success hook.AsyncHooksregisterBeforeRequest(AsyncHook.BeforeRequest beforeRequest)Registers an async before-request hook.
-
-
-
Method Detail
-
registerBeforeRequest
public AsyncHooks registerBeforeRequest(AsyncHook.BeforeRequest beforeRequest)
Registers an async before-request hook. Hooks are chained in registration order.- Parameters:
beforeRequest- async hook returning CompletableFuture<HttpRequest>- Returns:
- this
-
registerAfterSuccess
public AsyncHooks registerAfterSuccess(AsyncHook.AfterSuccess afterSuccess)
Registers an async after-success hook. Hooks are chained in registration order.- Parameters:
afterSuccess- async hook returning CompletableFuture<HttpResponse>- Returns:
- this
-
registerAfterError
public AsyncHooks registerAfterError(AsyncHook.AfterError afterError)
Registers an async after-error hook. Hooks are chained in registration order.- Parameters:
afterError- async hook for error handling- Returns:
- this
-
beforeRequest
public java.util.concurrent.CompletableFuture<java.net.http.HttpRequest> beforeRequest(Hook.BeforeRequestContext context, java.net.http.HttpRequest request)
Description copied from interface:AsyncHook.BeforeRequestTransforms the givenHttpRequestbefore sending.Note that
HttpRequestis immutable. To modify the request you can useHttpRequest#newBuilder(HttpRequest, BiPredicate<String, String>)with JDK 16 and later (which will copy the request for modification in a builder). If that method is not available then useHelpers.copy(java.net.http.HttpRequest)(which also returns a builder).- Specified by:
beforeRequestin interfaceAsyncHook.BeforeRequest- Parameters:
context- context for the hook callrequest- request to be transformed- Returns:
- transformed request
-
afterSuccess
public java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<Blob>> afterSuccess(Hook.AfterSuccessContext context, java.net.http.HttpResponse<Blob> response)
Description copied from interface:AsyncHook.AfterSuccessTransforms the given response before response processing occurs.- Specified by:
afterSuccessin interfaceAsyncHook.AfterSuccess- Parameters:
context- context for the hook callresponse- response to be transformed- Returns:
- transformed response
-
afterError
public java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<Blob>> afterError(Hook.AfterErrorContext context, java.net.http.HttpResponse<Blob> response, java.lang.Throwable error)
Description copied from interface:AsyncHook.AfterErrorEither returns an HttpResponse or throws an Exception. Must be passed either a response or an error (both can't be absent).- Specified by:
afterErrorin interfaceAsyncHook.AfterError- Parameters:
context- context for the errorresponse- response information if available.error- the optional exception. If response present then the error is for-info only, it was the last error in the chain of AfterError hook calls leading to this one- Returns:
- HTTP response if method decides that an exception is not to be thrown
-
-