Java Code Examples for software.amazon.awssdk.core.async.SdkPublisher#adapt()

The following examples show how to use software.amazon.awssdk.core.async.SdkPublisher#adapt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: test-async-client-class.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes the EventStreamOperationWithOnlyInput operation asynchronously.
 *
 * @param eventStreamOperationWithOnlyInputRequest
 * @return A Java Future containing the result of the EventStreamOperationWithOnlyInput 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>JsonException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
 *         of this type.</li>
 *         </ul>
 * @sample JsonAsyncClient.EventStreamOperationWithOnlyInput
 * @see <a href="http://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/EventStreamOperationWithOnlyInput"
 *      target="_top">AWS API Documentation</a>
 */
@Override
public CompletableFuture<EventStreamOperationWithOnlyInputResponse> eventStreamOperationWithOnlyInput(
        EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest,
        Publisher<InputEventStreamTwo> requestStream) {
    try {
        eventStreamOperationWithOnlyInputRequest = applySignerOverride(eventStreamOperationWithOnlyInputRequest,
                EventStreamAws4Signer.create());
        JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
                .isPayloadJson(true).build();

        HttpResponseHandler<EventStreamOperationWithOnlyInputResponse> responseHandler = protocolFactory
                .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder);

        HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory,
                operationMetadata);
        EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder()
                .putMarshaller(InputEvent.class, new InputEventMarshaller(protocolFactory))
                .putMarshaller(InputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build();
        SdkPublisher<InputEventStreamTwo> eventPublisher = SdkPublisher.adapt(requestStream);
        Publisher<ByteBuffer> adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map(
                AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer);

        CompletableFuture<EventStreamOperationWithOnlyInputResponse> executeFuture = clientHandler
                .execute(new ClientExecutionParams<EventStreamOperationWithOnlyInputRequest, EventStreamOperationWithOnlyInputResponse>()
                        .withOperationName("EventStreamOperationWithOnlyInput")
                        .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory))
                        .withAsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody.fromPublisher(adapted))
                        .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
                        .withInput(eventStreamOperationWithOnlyInputRequest));
        return executeFuture;
    } catch (Throwable t) {
        return CompletableFutureUtils.failedFuture(t);
    }
}
 
Example 2
Source File: test-async-client-class.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Invokes the EventStreamOperation operation asynchronously.
 *
 * @param eventStreamOperationRequest
 * @return A Java Future containing the result of the EventStreamOperation 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>JsonException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
 *         of this type.</li>
 *         </ul>
 * @sample JsonAsyncClient.EventStreamOperation
 * @see <a href="http://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/EventStreamOperation"
 *      target="_top">AWS API Documentation</a>
 */
@Override
public CompletableFuture<Void> eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest,
        Publisher<InputEventStream> requestStream, EventStreamOperationResponseHandler asyncResponseHandler) {
    try {
        eventStreamOperationRequest = applySignerOverride(eventStreamOperationRequest, EventStreamAws4Signer.create());
        JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
                .isPayloadJson(true).build();

        HttpResponseHandler<EventStreamOperationResponse> responseHandler = new AttachHttpMetadataResponseHandler(
                protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder));

        HttpResponseHandler<SdkResponse> voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata
                .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder);

        HttpResponseHandler<? extends EventStream> eventResponseHandler = protocolFactory.createResponseHandler(
                JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(),
                EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventOne::builder)
                        .putSdkPojoSupplier("event-two", EventTwo::builder)
                        .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN))
                        .build());

        HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory,
                operationMetadata);
        EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder()
                .putMarshaller(InputEvent.class, new InputEventMarshaller(protocolFactory)).build();
        SdkPublisher<InputEventStream> eventPublisher = SdkPublisher.adapt(requestStream);
        Publisher<ByteBuffer> adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map(
                AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer);
        CompletableFuture<Void> future = new CompletableFuture<>();
        EventStreamAsyncResponseTransformer<EventStreamOperationResponse, EventStream> asyncResponseTransformer = EventStreamAsyncResponseTransformer
                .<EventStreamOperationResponse, EventStream> builder().eventStreamResponseHandler(asyncResponseHandler)
                .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler)
                .exceptionResponseHandler(errorResponseHandler).future(future).executor(executor).serviceName(serviceName())
                .build();
        RestEventStreamAsyncResponseTransformer<EventStreamOperationResponse, EventStream> restAsyncResponseTransformer = RestEventStreamAsyncResponseTransformer
                .<EventStreamOperationResponse, EventStream> builder()
                .eventStreamAsyncResponseTransformer(asyncResponseTransformer)
                .eventStreamResponseHandler(asyncResponseHandler).build();

        CompletableFuture<Void> executeFuture = clientHandler.execute(
                new ClientExecutionParams<EventStreamOperationRequest, EventStreamOperationResponse>()
                        .withOperationName("EventStreamOperation")
                        .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory))
                        .withAsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody.fromPublisher(adapted))
                        .withFullDuplex(true).withResponseHandler(responseHandler)
                        .withErrorResponseHandler(errorResponseHandler).withInput(eventStreamOperationRequest),
                restAsyncResponseTransformer);
        executeFuture.whenComplete((r, e) -> {
            if (e != null) {
                try {
                    asyncResponseHandler.exceptionOccurred(e);
                } finally {
                    future.completeExceptionally(e);
                }
            }
        });
        return CompletableFutureUtils.forwardExceptionTo(future, executeFuture);
    } catch (Throwable t) {
        runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring",
                () -> asyncResponseHandler.exceptionOccurred(t));
        return CompletableFutureUtils.failedFuture(t);
    }
}