T - defines the ErrorType enumeration.public abstract class BaseRestConnection<E extends org.mule.runtime.extension.api.exception.ModuleException,T extends org.mule.runtime.extension.api.error.ErrorTypeDefinition<? extends Enum<?>>> extends Object implements RestConnection
RestConnection which provides an implementation based on HttpClient and supports
for error handling which allow to be customized by sub classing with template methods.| Constructor and Description |
|---|
BaseRestConnection(org.mule.runtime.http.api.client.HttpClient httpClient,
org.mule.runtime.http.api.client.HttpRequestOptions httpRequestOptions,
String baseUri,
org.mule.runtime.api.el.ExpressionLanguage expressionLanguage) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
authenticate(org.mule.runtime.http.api.domain.message.request.HttpRequestBuilder httpRequestBuilder)
Allows to different type of connections to intercept the request before sending the HTTP call.
|
protected org.mule.runtime.http.api.domain.message.request.HttpRequest |
beforeRequest(org.mule.runtime.http.api.domain.message.request.HttpRequest request) |
protected abstract E |
createModuleException(org.mule.runtime.http.api.domain.message.response.HttpResponse httpResponse,
T errorTypeDefinition,
org.mule.runtime.api.metadata.MediaType mediaType)
Utility method that provides a hook for customizing the
E to be thrown when an HttpResponse is marked as
failure based on httpResponseToErrorTypeDefinition(HttpResponse, MediaType). |
String |
getBaseUri() |
protected CommonsExpressionLanguage |
getCommonsExpressionLanguage() |
protected org.mule.runtime.api.el.ExpressionLanguage |
getExpressionLanguage() |
protected org.mule.runtime.http.api.client.HttpClient |
getHttpClient() |
protected org.mule.runtime.http.api.client.HttpRequestOptions |
getHttpRequestOptions() |
protected org.mule.runtime.api.metadata.MediaType |
getMediaType(org.mule.runtime.http.api.domain.message.response.HttpResponse response,
org.mule.runtime.api.metadata.MediaType defaultResponseMediaType) |
protected abstract Optional<T> |
httpResponseToErrorTypeDefinition(org.mule.runtime.http.api.domain.message.response.HttpResponse httpResponse,
org.mule.runtime.api.metadata.MediaType mediaType)
Template method that allows usage of custom
ErrorTypeDefinition's for those scenarios where SaaS is not Restful
complaint or signal success error responses are returned with 20x responses. |
protected abstract org.mule.runtime.http.api.domain.entity.HttpEntity |
materializeHttpResponseEntity(org.mule.runtime.http.api.domain.entity.HttpEntity httpEntity)
Whenever the connection implementation needs to handle signal error responses which are 200 response status code the method
httpResponseToErrorTypeDefinition(HttpResponse, MediaType) would be implemented in order to read the entity content
response to check for the signal error flag. |
HttpResponseResult |
send(org.mule.runtime.http.api.domain.message.request.HttpRequest request,
org.mule.runtime.api.metadata.MediaType defaultResponseMediaType)
Synchronous invocation of the
HttpRequest against the connection. |
CompletableFuture<HttpResponseResult> |
sendAsync(org.mule.runtime.http.api.domain.message.request.HttpRequest request,
org.mule.runtime.api.metadata.MediaType defaultResponseMediaType)
Asynchronous invocation of the
HttpRequest against this connection. |
protected org.mule.runtime.http.api.domain.message.response.HttpResponse |
throwModuleExceptionIfErrorResponse(org.mule.runtime.http.api.domain.message.response.HttpResponse httpResponse,
org.mule.runtime.api.metadata.MediaType mediaType)
Utility method that provides a
Function responsible for checking if the
HttpResponse should be treated as error, based on httpResponseToErrorTypeDefinition(HttpResponse, MediaType)
error type definition function. |
protected abstract E |
toConnectivityErrorFunction(Throwable throwable)
Template method that allows usage of custom
ErrorTypeDefinition's for general errors like TimeoutException or
IOException which are thrown by the HttpClient API. |
protected HttpResponseResult |
toResult(org.mule.runtime.http.api.domain.message.response.HttpResponse httpResponse,
org.mule.runtime.api.metadata.MediaType mediaType) |
org.mule.runtime.api.connection.ConnectionValidationResult |
validate(org.mule.runtime.http.api.domain.message.request.HttpRequest request,
Function<org.mule.runtime.http.api.domain.message.response.HttpResponse,org.mule.runtime.api.connection.ConnectionValidationResult> whenComplete,
Function<Exception,org.mule.runtime.api.connection.ConnectionValidationResult> onError)
Executes the
HttpRequest and validates the response to generate the corresponding ConnectionValidationResult. |
public BaseRestConnection(org.mule.runtime.http.api.client.HttpClient httpClient,
org.mule.runtime.http.api.client.HttpRequestOptions httpRequestOptions,
String baseUri,
org.mule.runtime.api.el.ExpressionLanguage expressionLanguage)
protected org.mule.runtime.http.api.client.HttpClient getHttpClient()
protected org.mule.runtime.http.api.client.HttpRequestOptions getHttpRequestOptions()
public String getBaseUri()
getBaseUri in interface RestConnectionprotected org.mule.runtime.api.el.ExpressionLanguage getExpressionLanguage()
protected CommonsExpressionLanguage getCommonsExpressionLanguage()
public CompletableFuture<HttpResponseResult> sendAsync(org.mule.runtime.http.api.domain.message.request.HttpRequest request, org.mule.runtime.api.metadata.MediaType defaultResponseMediaType)
RestConnectionHttpRequest against this connection. Allows non-blocking operations to fulfil the SDK
contract for non-blocking support in connectors.sendAsync in interface RestConnectionrequest - to be executed.defaultResponseMediaType - to be used if the server doesn't specify one with the
HttpHeaders.Names.CONTENT_TYPE header.HttpResponseResult > to handle the async response. CompletableFuture will success if the
response from the server is success, case of a failure a ModuleException would be passed to the
completeExceptionally. The HttpResponseResult provides the MediaType for the output
InputStream.protected final org.mule.runtime.api.metadata.MediaType getMediaType(org.mule.runtime.http.api.domain.message.response.HttpResponse response,
org.mule.runtime.api.metadata.MediaType defaultResponseMediaType)
protected HttpResponseResult toResult(org.mule.runtime.http.api.domain.message.response.HttpResponse httpResponse, org.mule.runtime.api.metadata.MediaType mediaType)
public HttpResponseResult send(org.mule.runtime.http.api.domain.message.request.HttpRequest request, org.mule.runtime.api.metadata.MediaType defaultResponseMediaType) throws E extends org.mule.runtime.extension.api.exception.ModuleException
RestConnectionHttpRequest against the connection. Used for design operations like value providers or
even for paging operations where non-blocking is not supported by SDK.send in interface RestConnectionrequest - to be executed.defaultResponseMediaType - to be used if the server doesn't specify one with the
HttpHeaders.Names.CONTENT_TYPE header. with the response body and HTTP protocol attributes
from the server. The HttpResponseResult provides the MediaType for the output InputStream.E extends org.mule.runtime.extension.api.exception.ModuleExceptionpublic org.mule.runtime.api.connection.ConnectionValidationResult validate(org.mule.runtime.http.api.domain.message.request.HttpRequest request,
Function<org.mule.runtime.http.api.domain.message.response.HttpResponse,org.mule.runtime.api.connection.ConnectionValidationResult> whenComplete,
Function<Exception,org.mule.runtime.api.connection.ConnectionValidationResult> onError)
RestConnectionHttpRequest and validates the response to generate the corresponding ConnectionValidationResult.validate in interface RestConnectionrequest - to be executed.whenComplete - to process an HttpResponse and return a ConnectionValidationResult.onError - to process an Exception, that happened from client side before calling the server, and return a
ConnectionValidationResult.ConnectionValidationResult.protected org.mule.runtime.http.api.domain.message.request.HttpRequest beforeRequest(org.mule.runtime.http.api.domain.message.request.HttpRequest request)
protected void authenticate(org.mule.runtime.http.api.domain.message.request.HttpRequestBuilder httpRequestBuilder)
httpRequestBuilder - HttpRequestBuilder populated with the values from the original HttpRequest.protected final org.mule.runtime.http.api.domain.message.response.HttpResponse throwModuleExceptionIfErrorResponse(org.mule.runtime.http.api.domain.message.response.HttpResponse httpResponse,
org.mule.runtime.api.metadata.MediaType mediaType)
Function responsible for checking if the
HttpResponse should be treated as error, based on httpResponseToErrorTypeDefinition(HttpResponse, MediaType)
error type definition function. @Throws(RestfullErrorTypeProvider.class), if the httpResponseToErrorTypeDefinition(HttpResponse, MediaType)
has not been overridden to change the ErrorTypeProvider.
HttpResponse httpResponse = httpClient.send(HttpRequest.builder()
.uri(connection.getBaseUri() + "/accounts/" + accountId)
.build());
// Check if the HTTP response is not success based on RestfullError and throws a ModuleException
httpResponse = throwModuleExceptionIfErrorResponse().apply(httpResponse);
...
httpClient.sendAsync(HttpRequest.builder()
.uri(connection.getBaseUri() + "/accounts/" + accountId)
.build())
// Check if the HTTP response is not success based on RestfullError and throws a ModuleException.
.thenApply(throwModuleExceptionIfErrorResponse())
.thenAccept(httpResponse ->...)
mediaType - to read the entity content from server.Function to be applied after obtaining the CompletableFuture from the
HttpClient invocation as it applies the decision logic to define if the HttpResponse is a failure or
success response. If it is defined as an error a ModuleException is thrown with its associated
ErrorTypeDefinition.protected abstract org.mule.runtime.http.api.domain.entity.HttpEntity materializeHttpResponseEntity(org.mule.runtime.http.api.domain.entity.HttpEntity httpEntity)
httpResponseToErrorTypeDefinition(HttpResponse, MediaType) would be implemented in order to read the entity content
response to check for the signal error flag. If this is done without materializing the HttpEntity it won't later allow the
client which made the request to be executed by the connection to consume the response. Therefore, this method allows to
materialize the entity so it would allow to be read more than once.httpEntity - the server's response.HttpEntity that allows to be read more than once.protected abstract Optional<T> httpResponseToErrorTypeDefinition(org.mule.runtime.http.api.domain.message.response.HttpResponse httpResponse, org.mule.runtime.api.metadata.MediaType mediaType)
ErrorTypeDefinition's for those scenarios where SaaS is not Restful
complaint or signal success error responses are returned with 20x responses. By overriding this method in a custom connection
implementation allows customizing ErrorTypeDefinition's. If the function returns an Optional.empty() it means
the HttpResponse is a success. If this method is overridden most like toConnectivityErrorFunction(Throwable)
would need to be too as it uses RestError for generic errors. HttpResponse entity content with an in memory implementation such is
ByteArrayHttpEntity by overwritten
materializeHttpResponseEntity(HttpEntity) so it could be consumed more than once, to check the body content for
signal error and if it not an error by the client when processing the response.httpResponse - the response HttpResponse from the server.mediaType - to read the entity content from server.Optional to check if the response corresponds to an error.protected abstract E createModuleException(org.mule.runtime.http.api.domain.message.response.HttpResponse httpResponse, T errorTypeDefinition, org.mule.runtime.api.metadata.MediaType mediaType)
E to be thrown when an HttpResponse is marked as
failure based on httpResponseToErrorTypeDefinition(HttpResponse, MediaType). error.description based on statusCode " " reasonPhrase.
E that also implements
ErrorMessageAwareException so it associates the HttpResponse body as payload and the
attributes representation's for the response.httpResponse - the response HttpResponse from the server.errorTypeDefinition - ErrorTypeDefinition associated to this failure response.mediaType - to read the entity content from server.E with the description and the ErrorTypeDefinition associated.protected abstract E toConnectivityErrorFunction(Throwable throwable)
ErrorTypeDefinition's for general errors like TimeoutException or
IOException which are thrown by the HttpClient API. The implementation should consider that a
E may be passed to this function in which case it should be propagated as it is. By overriding this method in a
custom connection implementation it will allow customizing these ErrorTypeDefinition's.Function to handle a Throwable and return a E with its corresponding
ErrorTypeDefinition.Copyright © 2023. All rights reserved.