use of software.amazon.awssdk.core.Response in project aws-sdk-java-v2 by aws.
the class MakeAsyncHttpRequestStage method executeHttpRequest.
private CompletableFuture<Response<OutputT>> executeHttpRequest(SdkHttpFullRequest request, RequestExecutionContext context) {
CompletableFuture<Response<OutputT>> responseFuture = new CompletableFuture<>();
CompletableFuture<Response<OutputT>> responseHandlerFuture = responseHandler.prepare();
SdkHttpContentPublisher requestProvider = context.requestProvider() == null ? new SimpleHttpContentPublisher(request) : new SdkHttpContentPublisherAdapter(context.requestProvider());
// Set content length if it hasn't been set already.
SdkHttpFullRequest requestWithContentLength = getRequestWithContentLength(request, requestProvider);
MetricCollector httpMetricCollector = MetricUtils.createHttpMetricsCollector(context);
AsyncExecuteRequest.Builder executeRequestBuilder = AsyncExecuteRequest.builder().request(requestWithContentLength).requestContentPublisher(requestProvider).responseHandler(responseHandler).fullDuplex(isFullDuplex(context.executionAttributes())).metricCollector(httpMetricCollector);
if (context.executionAttributes().getAttribute(SDK_HTTP_EXECUTION_ATTRIBUTES) != null) {
executeRequestBuilder.httpExecutionAttributes(context.executionAttributes().getAttribute(SDK_HTTP_EXECUTION_ATTRIBUTES));
}
CompletableFuture<Void> httpClientFuture = doExecuteHttpRequest(context, executeRequestBuilder.build());
TimeoutTracker timeoutTracker = setupAttemptTimer(responseFuture, context);
context.apiCallAttemptTimeoutTracker(timeoutTracker);
// Forward the cancellation
responseFuture.whenComplete((r, t) -> {
if (t != null) {
httpClientFuture.completeExceptionally(t);
}
});
// Offload the completion of the future returned from this stage onto
// the future completion executor
responseHandlerFuture.whenCompleteAsync((r, t) -> {
if (t == null) {
responseFuture.complete(r);
} else {
responseFuture.completeExceptionally(t);
}
}, futureCompletionExecutor);
return responseFuture;
}
use of software.amazon.awssdk.core.Response in project aws-sdk-java-v2 by aws.
the class CombinedResponseAsyncHttpResponseHandlerTest method errorResponse_shouldCompleteHeaderFuture.
@Test
void errorResponse_shouldCompleteHeaderFuture() {
when(errorResponseHandler.prepare()).thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<Response<Void>> future = responseHandler.prepare();
SdkHttpFullResponse sdkHttpFullResponse = SdkHttpFullResponse.builder().statusCode(400).build();
Flowable<ByteBuffer> publisher = publisher();
responseHandler.onHeaders(sdkHttpFullResponse);
responseHandler.onStream(publisher);
verify(errorResponseHandler).prepare();
verify(errorResponseHandler).onStream(publisher);
assertThat(future).isDone();
assertThat(future.join().httpResponse()).isEqualTo(sdkHttpFullResponse);
}
use of software.amazon.awssdk.core.Response in project aws-sdk-java-v2 by aws.
the class DefaultXmlAsyncClient method streamingOutputOperation.
/**
* Some operation with a streaming output
*
* @param streamingOutputOperationRequest
* @param asyncResponseTransformer
* The response transformer for processing the streaming response in a non-blocking manner. See
* {@link AsyncResponseTransformer} for details on how this callback should be implemented and for links to
* precanned implementations for common scenarios like downloading to a file. The service documentation for
* the response content is as follows 'This be a stream'.
* @return A future to the transformed result of the AsyncResponseTransformer.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>XmlException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
* of this type.</li>
* </ul>
* @sample XmlAsyncClient.StreamingOutputOperation
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/xml-service-2010-05-08/StreamingOutputOperation"
* target="_top">AWS API Documentation</a>
*/
@Override
public <ReturnT> CompletableFuture<ReturnT> streamingOutputOperation(StreamingOutputOperationRequest streamingOutputOperationRequest, AsyncResponseTransformer<StreamingOutputOperationResponse, ReturnT> asyncResponseTransformer) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Xml Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingOutputOperation");
Pair<AsyncResponseTransformer<StreamingOutputOperationResponse, ReturnT>, CompletableFuture<Void>> pair = AsyncResponseTransformerUtils.wrapWithEndOfStreamFuture(asyncResponseTransformer);
asyncResponseTransformer = pair.left();
CompletableFuture<Void> endOfStreamFuture = pair.right();
HttpResponseHandler<StreamingOutputOperationResponse> responseHandler = protocolFactory.createResponseHandler(StreamingOutputOperationResponse::builder, new XmlOperationMetadata().withHasStreamingSuccessResponse(true));
HttpResponseHandler<AwsServiceException> errorResponseHandler = protocolFactory.createErrorResponseHandler();
CompletableFuture<ReturnT> executeFuture = clientHandler.execute(new ClientExecutionParams<StreamingOutputOperationRequest, StreamingOutputOperationResponse>().withOperationName("StreamingOutputOperation").withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector).withInput(streamingOutputOperationRequest), asyncResponseTransformer);
CompletableFuture<ReturnT> whenCompleteFuture = null;
AsyncResponseTransformer<StreamingOutputOperationResponse, ReturnT> finalAsyncResponseTransformer = asyncResponseTransformer;
whenCompleteFuture = executeFuture.whenComplete((r, e) -> {
if (e != null) {
runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", () -> finalAsyncResponseTransformer.exceptionOccurred(e));
}
endOfStreamFuture.whenComplete((r2, e2) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
});
return CompletableFutureUtils.forwardExceptionTo(whenCompleteFuture, executeFuture);
} catch (Throwable t) {
AsyncResponseTransformer<StreamingOutputOperationResponse, ReturnT> finalAsyncResponseTransformer = asyncResponseTransformer;
runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", () -> finalAsyncResponseTransformer.exceptionOccurred(t));
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.core.Response in project aws-sdk-java-v2 by aws.
the class DefaultXmlAsyncClient method aPostOperation.
/**
* <p>
* Performs a post operation to the xml service and has no output
* </p>
*
* @param aPostOperationRequest
* @return A Java Future containing the result of the APostOperation operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>InvalidInputException The request was rejected because an invalid or out-of-range value was supplied
* for an input parameter.</li>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>XmlException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
* of this type.</li>
* </ul>
* @sample XmlAsyncClient.APostOperation
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/xml-service-2010-05-08/APostOperation" target="_top">AWS
* API Documentation</a>
*/
@Override
public CompletableFuture<APostOperationResponse> aPostOperation(APostOperationRequest aPostOperationRequest) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Xml Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation");
HttpResponseHandler<Response<APostOperationResponse>> responseHandler = protocolFactory.createCombinedResponseHandler(APostOperationResponse::builder, new XmlOperationMetadata().withHasStreamingSuccessResponse(false));
String hostPrefix = "foo-";
String resolvedHostExpression = "foo-";
CompletableFuture<APostOperationResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<APostOperationRequest, APostOperationResponse>().withOperationName("APostOperation").withMarshaller(new APostOperationRequestMarshaller(protocolFactory)).withCombinedResponseHandler(responseHandler).hostPrefixExpression(resolvedHostExpression).withMetricCollector(apiCallMetricCollector).withInput(aPostOperationRequest));
CompletableFuture<APostOperationResponse> whenCompleteFuture = null;
whenCompleteFuture = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
CompletableFutureUtils.forwardExceptionTo(whenCompleteFuture, executeFuture);
return whenCompleteFuture;
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
use of software.amazon.awssdk.core.Response in project aws-sdk-java-v2 by aws.
the class DefaultXmlAsyncClient method streamingInputOperation.
/**
* Some operation with a streaming input
*
* @param streamingInputOperationRequest
* @param requestBody
* Functional interface that can be implemented to produce the request content in a non-blocking manner. The
* size of the content is expected to be known up front. See {@link AsyncRequestBody} for specific details on
* implementing this interface as well as links to precanned implementations for common scenarios like
* uploading from a file. The service documentation for the request content is as follows 'This be a stream'
* @return A Java Future containing the result of the StreamingInputOperation operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>XmlException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
* of this type.</li>
* </ul>
* @sample XmlAsyncClient.StreamingInputOperation
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/xml-service-2010-05-08/StreamingInputOperation"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<StreamingInputOperationResponse> streamingInputOperation(StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Xml Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOperation");
if (!isSignerOverridden(clientConfiguration)) {
streamingInputOperationRequest = applySignerOverride(streamingInputOperationRequest, AsyncAws4Signer.create());
}
HttpResponseHandler<Response<StreamingInputOperationResponse>> responseHandler = protocolFactory.createCombinedResponseHandler(StreamingInputOperationResponse::builder, new XmlOperationMetadata().withHasStreamingSuccessResponse(false));
CompletableFuture<StreamingInputOperationResponse> executeFuture = clientHandler.execute(new ClientExecutionParams<StreamingInputOperationRequest, StreamingInputOperationResponse>().withOperationName("StreamingInputOperation").withMarshaller(AsyncStreamingRequestMarshaller.builder().delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)).asyncRequestBody(requestBody).build()).withCombinedResponseHandler(responseHandler).withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody).withInput(streamingInputOperationRequest));
CompletableFuture<StreamingInputOperationResponse> whenCompleteFuture = null;
whenCompleteFuture = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
CompletableFutureUtils.forwardExceptionTo(whenCompleteFuture, executeFuture);
return whenCompleteFuture;
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}
Aggregations