Package pl.gsmservice.gateway.utils
Class Hooks
- java.lang.Object
-
- pl.gsmservice.gateway.utils.Hooks
-
- All Implemented Interfaces:
Hook.AfterError,Hook.AfterSuccess,Hook.BeforeRequest,Hook.SdkInit
public class Hooks extends java.lang.Object implements Hook.BeforeRequest, Hook.AfterSuccess, Hook.AfterError, Hook.SdkInit
Registers hooks for use at runtime by an end-user or for use by a customer that may edit the SDKHooks.java file.For example, this code will add a transaction id header to every request:
hooks.registerBeforeRequest((context, request) -> { request.headers().map().put("acme-transaction-id", nextTransactionId()); return request; });
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHooks.FailEarlyException
-
Field Summary
-
Fields inherited from interface pl.gsmservice.gateway.utils.Hook.AfterError
DEFAULT
-
Fields inherited from interface pl.gsmservice.gateway.utils.Hook.AfterSuccess
DEFAULT
-
Fields inherited from interface pl.gsmservice.gateway.utils.Hook.BeforeRequest
DEFAULT
-
Fields inherited from interface pl.gsmservice.gateway.utils.Hook.SdkInit
DEFAULT
-
-
Constructor Summary
Constructors Constructor Description Hooks()Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.net.http.HttpResponse<java.io.InputStream>afterError(Hook.AfterErrorContext context, java.util.Optional<java.net.http.HttpResponse<java.io.InputStream>> response, java.util.Optional<java.lang.Exception> error)Either returns an HttpResponse or throws an Exception.java.net.http.HttpResponse<java.io.InputStream>afterSuccess(Hook.AfterSuccessContext context, java.net.http.HttpResponse<java.io.InputStream> response)Transforms the given response before response processing occurs.java.net.http.HttpRequestbeforeRequest(Hook.BeforeRequestContext context, java.net.http.HttpRequest request)Transforms the givenHttpRequestbefore sending.HooksregisterAfterError(Hook.AfterError afterError)Registers anHook.AfterErrorhook to be applied in order of registration (multiple can be registered).HooksregisterAfterSuccess(Hook.AfterSuccess afterSuccess)Registers anHook.AfterSuccesshook to be applied in order of registration (multiple can be registered).HooksregisterBeforeRequest(Hook.BeforeRequest beforeRequest)Registers aHook.BeforeRequesthook to be applied in order of registration.HooksregisterSdkInit(Hook.SdkInit SdkInit)Registers aHook.SdkInithook to be applied in order of registration (multiple can be registered).Hook.SdkInitDatasdkInit(Hook.SdkInitData data)Returns a transformedHTTPClientandbaseUrlfor use in requests.
-
-
-
Method Detail
-
registerBeforeRequest
public Hooks registerBeforeRequest(Hook.BeforeRequest beforeRequest)
Registers aHook.BeforeRequesthook to be applied in order of registration. The result of the first BeforeRequest hook will be passed to the second BeforeRequest hook and processed similarly for the rest of the registered hooks. If a BeforeRequest hook throws then that Exception will not be passed to theHook.AfterErrorhooks.- Parameters:
beforeRequest- hook to be registered- Returns:
- this
-
registerAfterSuccess
public Hooks registerAfterSuccess(Hook.AfterSuccess afterSuccess)
Registers anHook.AfterSuccesshook to be applied in order of registration (multiple can be registered). The result of the first AfterSuccess hook will be passed to the second AfterSuccess hook and processed similarly for the rest of the registered hooks. If an AfterSuccess hook throws then that Exception will not be passed to theHook.AfterErrorhooks.- Parameters:
afterSuccess- hook to be registered- Returns:
- this
-
registerAfterError
public Hooks registerAfterError(Hook.AfterError afterError)
Registers anHook.AfterErrorhook to be applied in order of registration (multiple can be registered). If the first AfterError hook throws then the second hook will be called with that exception (and no response object) and so on for the rest of the registered hooks. If an AfterError hook returns normally then its result will be passed through to the next AfterError hook with the latest thrown Exception.- Parameters:
afterError- hook to be registered- Returns:
- this
-
registerSdkInit
public Hooks registerSdkInit(Hook.SdkInit SdkInit)
Registers aHook.SdkInithook to be applied in order of registration (multiple can be registered).- Parameters:
SdkInit- hook to be registered- Returns:
- this
-
beforeRequest
public java.net.http.HttpRequest beforeRequest(Hook.BeforeRequestContext context, java.net.http.HttpRequest request) throws java.lang.Exception
Description copied from interface:Hook.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 interfaceHook.BeforeRequest- Parameters:
context- context for the hook callrequest- request to be transformed- Returns:
- transformed request
- Throws:
java.lang.Exception- on error
-
afterSuccess
public java.net.http.HttpResponse<java.io.InputStream> afterSuccess(Hook.AfterSuccessContext context, java.net.http.HttpResponse<java.io.InputStream> response) throws java.lang.Exception
Description copied from interface:Hook.AfterSuccessTransforms the given response before response processing occurs.- Specified by:
afterSuccessin interfaceHook.AfterSuccess- Parameters:
context- context for the hook callresponse- response to be transformed- Returns:
- transformed response
- Throws:
java.lang.Exception- on error
-
afterError
public java.net.http.HttpResponse<java.io.InputStream> afterError(Hook.AfterErrorContext context, java.util.Optional<java.net.http.HttpResponse<java.io.InputStream>> response, java.util.Optional<java.lang.Exception> error) throws java.lang.Exception
Description copied from interface:Hook.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 interfaceHook.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
- Throws:
java.lang.Exception- if error to be propagated
-
sdkInit
public Hook.SdkInitData sdkInit(Hook.SdkInitData data)
Description copied from interface:Hook.SdkInitReturns a transformedHTTPClientandbaseUrlfor use in requests.- Specified by:
sdkInitin interfaceHook.SdkInit- Parameters:
data- data to transform- Returns:
- the transformed data
-
-