Package com.styra.opa.openapi.utils
Class Hooks
java.lang.Object
com.styra.opa.openapi.utils.Hooks
- All Implemented Interfaces:
Hook.AfterError,Hook.AfterSuccess,Hook.BeforeRequest,Hook.SdkInit
public class Hooks
extends 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 -
Field Summary
Fields inherited from interface com.styra.opa.openapi.utils.Hook.AfterError
DEFAULTFields inherited from interface com.styra.opa.openapi.utils.Hook.AfterSuccess
DEFAULTFields inherited from interface com.styra.opa.openapi.utils.Hook.BeforeRequest
DEFAULTFields inherited from interface com.styra.opa.openapi.utils.Hook.SdkInit
DEFAULT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionafterError(Hook.AfterErrorContext context, Optional<HttpResponse<InputStream>> response, Optional<Exception> error) Either returns an HttpResponse or throws an Exception.afterSuccess(Hook.AfterSuccessContext context, HttpResponse<InputStream> response) Transforms the given response before response processing occurs.beforeRequest(Hook.BeforeRequestContext context, HttpRequest request) Transforms the givenHttpRequestbefore sending.registerAfterError(Hook.AfterError afterError) Registers anHook.AfterErrorhook to be applied in order of registration (multiple can be registered).registerAfterSuccess(Hook.AfterSuccess afterSuccess) Registers anHook.AfterSuccesshook to be applied in order of registration (multiple can be registered).registerBeforeRequest(Hook.BeforeRequest beforeRequest) Registers aHook.BeforeRequesthook to be applied in order of registration.registerSdkInit(Hook.SdkInit SdkInit) Registers aHook.SdkInithook to be applied in order of registration (multiple can be registered).sdkInit(Hook.SdkInitData data) Returns a transformedHTTPClientandbaseUrlfor use in requests.
-
Constructor Details
-
Hooks
public Hooks()Constructor.
-
-
Method Details
-
registerBeforeRequest
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
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
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
Registers aHook.SdkInithook to be applied in order of registration (multiple can be registered).- Parameters:
SdkInit- hook to be registered- Returns:
- this
-
beforeRequest
public HttpRequest beforeRequest(Hook.BeforeRequestContext context, HttpRequest request) throws 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:
Exception- on error
-
afterSuccess
public HttpResponse<InputStream> afterSuccess(Hook.AfterSuccessContext context, HttpResponse<InputStream> response) throws 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:
Exception- on error
-
afterError
public HttpResponse<InputStream> afterError(Hook.AfterErrorContext context, Optional<HttpResponse<InputStream>> response, Optional<Exception> error) throws 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:
Exception- if error to be propagated
-
sdkInit
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
-