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 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 class
Hooks.FailEarlyException
-
Field Summary
-
Fields inherited from interface com.styra.opa.openapi.utils.Hook.AfterError
DEFAULT
-
Fields inherited from interface com.styra.opa.openapi.utils.Hook.AfterSuccess
DEFAULT
-
Fields inherited from interface com.styra.opa.openapi.utils.Hook.BeforeRequest
DEFAULT
-
Fields inherited from interface com.styra.opa.openapi.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.HttpRequest
beforeRequest(Hook.BeforeRequestContext context, java.net.http.HttpRequest request)
Transforms the givenHttpRequest
before sending.Hooks
registerAfterError(Hook.AfterError afterError)
Registers anHook.AfterError
hook to be applied in order of registration (multiple can be registered).Hooks
registerAfterSuccess(Hook.AfterSuccess afterSuccess)
Registers anHook.AfterSuccess
hook to be applied in order of registration (multiple can be registered).Hooks
registerBeforeRequest(Hook.BeforeRequest beforeRequest)
Registers aHook.BeforeRequest
hook to be applied in order of registration.Hooks
registerSdkInit(Hook.SdkInit SdkInit)
Registers aHook.SdkInit
hook to be applied in order of registration (multiple can be registered).Hook.SdkInitData
sdkInit(Hook.SdkInitData data)
Returns a transformedHTTPClient
andbaseUrl
for use in requests.
-
-
-
Method Detail
-
registerBeforeRequest
public Hooks registerBeforeRequest(Hook.BeforeRequest beforeRequest)
Registers aHook.BeforeRequest
hook 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.AfterError
hooks.- Parameters:
beforeRequest
- hook to be registered- Returns:
- this
-
registerAfterSuccess
public Hooks registerAfterSuccess(Hook.AfterSuccess afterSuccess)
Registers anHook.AfterSuccess
hook 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.AfterError
hooks.- Parameters:
afterSuccess
- hook to be registered- Returns:
- this
-
registerAfterError
public Hooks registerAfterError(Hook.AfterError afterError)
Registers anHook.AfterError
hook 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.SdkInit
hook 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.BeforeRequest
Transforms the givenHttpRequest
before sending.Note that
HttpRequest
is immutable. To modify the request you can useHttpRequest#newBuilder(HttpRequest, BiPredicate
with JDK 16 and later (which will copy the request for modification in a builder). If that method is not available then use) Helpers.copy(java.net.http.HttpRequest)
(which also returns a builder).- Specified by:
beforeRequest
in 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.AfterSuccess
Transforms the given response before response processing occurs.- Specified by:
afterSuccess
in 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.AfterError
Either returns an HttpResponse or throws an Exception. Must be passed either a response or an error (both can't be absent).- Specified by:
afterError
in 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.SdkInit
Returns a transformedHTTPClient
andbaseUrl
for use in requests.- Specified by:
sdkInit
in interfaceHook.SdkInit
- Parameters:
data
- data to transform- Returns:
- the transformed data
-
-