software.amazon.awssdk.core.async.AsyncResponseTransformer Java Examples

The following examples show how to use software.amazon.awssdk.core.async.AsyncResponseTransformer. 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: S3Resource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@GET
@Path("async")
@Produces(TEXT_PLAIN)
public CompletionStage<String> testAsyncS3() {
    LOG.info("Testing Async S3 client with bucket: " + ASYNC_BUCKET);
    String keyValue = UUID.randomUUID().toString();

    return S3Utils.createBucketAsync(s3AsyncClient, ASYNC_BUCKET)
            .thenCompose(bucket -> s3AsyncClient.putObject(S3Utils.createPutRequest(ASYNC_BUCKET, keyValue),
                    AsyncRequestBody.fromString(SAMPLE_S3_OBJECT)))
            .thenCompose(resp -> s3AsyncClient.getObject(S3Utils.createGetRequest(ASYNC_BUCKET, keyValue),
                    AsyncResponseTransformer.toBytes()))
            .thenApply(resp -> metadata(resp.response()) + "+" + resp.asUtf8String())
            .exceptionally(th -> {
                LOG.error("Error during async S3 operations", th.getCause());
                return "ERROR";
            });
}
 
Example #2
Source File: S3AsyncStabilityTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private void getObject() {
    LOGGER.info(() -> "Starting to test getObject");
    IntFunction<CompletableFuture<?>> future = i -> {
        String keyName = computeKeyName(i);
        Path path = RandomTempFile.randomUncreatedFile().toPath();
        return s3NettyClient.getObject(b -> b.bucket(bucketName).key(keyName), AsyncResponseTransformer.toFile(path));
    };

    StabilityTestRunner.newRunner()
                       .testName("S3AsyncStabilityTest.getObject")
                       .futureFactory(future)
                       .requestCountPerRun(CONCURRENCY)
                       .totalRuns(TOTAL_RUNS)
                       .delaysBetweenEachRun(Duration.ofMillis(100))
                       .run();
}
 
Example #3
Source File: EventStreamAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void prepareReturnsNewFuture() {
    AsyncResponseTransformer<SdkResponse, Void> transformer =
            EventStreamAsyncResponseTransformer.builder()
                    .eventStreamResponseHandler(
                            onEventStream(p -> {}))
                    .eventResponseHandler((r, e) -> null)
                    .executor(Executors.newFixedThreadPool(2))
                    .future(new CompletableFuture<>())
                    .build();

    CompletableFuture<?> cf1 = transformer.prepare();

    transformer.exceptionOccurred(new RuntimeException("Boom!"));

    assertThat(cf1.isCompletedExceptionally()).isTrue();
    assertThat(transformer.prepare()).isNotEqualTo(cf1);
}
 
Example #4
Source File: Downloader.java    From hedera-mirror-node with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a PendingDownload for which the caller can waitForCompletion() to wait for the download to complete. This
 * either queues or begins the download (depending on the AWS TransferManager).
 *
 * @param s3ObjectKey
 * @param localFile
 * @return
 */
private PendingDownload saveToLocalAsync(String s3ObjectKey, Path localFile) {
    File file = localFile.toFile();
    // If process stops abruptly and is restarted, it's possible we try to re-download some of the files which
    // already exist on disk because lastValidFileName wasn't updated. AsyncFileResponseTransformer throws
    // exceptions if a file already exists, rather then silently overwrite it. So following check are to avoid
    // log spam of java.nio.file.FileAlreadyExistsException, not for optimization purpose. We can't use old file
    // because it may be half written or it maybe a data file from a node which doesn't match the hash.
    // Overwriting is the only way forward.
    // This is okay for now since we are moving away from storing downloaded S3 data in files.
    if (file.exists()) {
        boolean success = file.delete();
        if (!success) {
            log.error("Failed to delete the file {}. Expect long stack trace with FileAlreadyExistsException below",
                    file);
        }
    }
    var future = s3Client.getObject(
            GetObjectRequest.builder().bucket(downloaderProperties.getCommon().getBucketName()).key(s3ObjectKey)
                    .requestPayer(RequestPayer.REQUESTER)
                    .build(),
            AsyncResponseTransformer.toFile(file));
    return new PendingDownload(future, file, s3ObjectKey);
}
 
Example #5
Source File: AsyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Generate a simple method for operations with streaming input and output members.
 * Streaming input member takes a {@link Path} containing the data to upload and
 * the streaming output member takes a {@link Path} where data will be downloaded to.
 */
private MethodSpec streamingInputOutputFileSimpleMethod(OperationModel opModel) {
    ClassName requestType = ClassName.get(modelPackage, opModel.getInput().getVariableType());
    return interfaceMethodSignature(opModel)
        .returns(completableFutureType(getPojoResponseType(opModel)))
        .addJavadoc(opModel.getDocs(model, ClientType.ASYNC, SimpleMethodOverload.FILE))
        .addParameter(requestType, opModel.getInput().getVariableName())
        .addParameter(ClassName.get(Path.class), "sourcePath")
        .addParameter(ClassName.get(Path.class), "destinationPath")
        .addStatement("return $L($L, $T.fromFile(sourcePath), $T.toFile(destinationPath))",
                      opModel.getMethodName(),
                      opModel.getInput().getVariableName(),
                      ClassName.get(AsyncRequestBody.class),
                      ClassName.get(AsyncResponseTransformer.class))
        .build();
}
 
Example #6
Source File: AsyncResponseThreadingTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completionWithNioThreadWorksCorrectly() {
    stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).willReturn(aResponse().withStatus(200).withBody("test")));

    Executor mockExecutor = Mockito.spy(new SpyableExecutor());

    ProtocolRestJsonAsyncClient client =
            ProtocolRestJsonAsyncClient.builder()
                                       .region(Region.US_WEST_1)
                                       .endpointOverride(URI.create("http://localhost:" + wireMock.port()))
                                       .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid"))
                                       .asyncConfiguration(c -> c.advancedOption(FUTURE_COMPLETION_EXECUTOR, mockExecutor))
                                       .build();

    ResponseBytes<StreamingOutputOperationResponse> response =
            client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build(),
                                            AsyncResponseTransformer.toBytes()).join();

    verify(mockExecutor).execute(any());

    byte[] arrayCopy = response.asByteArray();
    assertThat(arrayCopy).containsExactly('t', 'e', 's', 't');
}
 
Example #7
Source File: test-query-async-client-class.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * 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>QueryException Base class for all service exceptions. Unknown exceptions will be thrown as an
 *         instance of this type.</li>
 *         </ul>
 * @sample QueryAsyncClient.StreamingOutputOperation
 * @see <a href="http://docs.aws.amazon.com/goto/WebAPI/query-service-2010-05-08/StreamingOutputOperation"
 *      target="_top">AWS API Documentation</a>
 */
@Override
public <ReturnT> CompletableFuture<ReturnT> streamingOutputOperation(
    StreamingOutputOperationRequest streamingOutputOperationRequest,
    AsyncResponseTransformer<StreamingOutputOperationResponse, ReturnT> asyncResponseTransformer) {
    try {

        HttpResponseHandler<StreamingOutputOperationResponse> responseHandler = protocolFactory
            .createResponseHandler(StreamingOutputOperationResponse::builder);

        HttpResponseHandler<AwsServiceException> errorResponseHandler = protocolFactory.createErrorResponseHandler();

        CompletableFuture<ReturnT> executeFuture = clientHandler.execute(
            new ClientExecutionParams<StreamingOutputOperationRequest, StreamingOutputOperationResponse>()
                .withOperationName("StreamingOutputOperation")
                .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory))
                .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
                .withInput(streamingOutputOperationRequest), asyncResponseTransformer);
        executeFuture.whenComplete((r, e) -> {
            if (e != null) {
                runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring",
                               () -> asyncResponseTransformer.exceptionOccurred(e));
            }
        });
        return executeFuture;
    } catch (Throwable t) {
        runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring",
                       () -> asyncResponseTransformer.exceptionOccurred(t));
        return CompletableFutureUtils.failedFuture(t);
    }
}
 
Example #8
Source File: AsyncOperationCancelTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamingOutputOperation() {
    CompletableFuture<ResponseBytes<StreamingOutputOperationResponse>> responseFuture = client.streamingOutputOperation(r -> {
    }, AsyncResponseTransformer.toBytes());
    responseFuture.cancel(true);
    assertThat(executeFuture.isCompletedExceptionally()).isTrue();
    assertThat(executeFuture.isCancelled()).isTrue();
}
 
Example #9
Source File: S3AsyncStabilityTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void downloadLargeObjectToFile() {
    File randomTempFile = RandomTempFile.randomUncreatedFile();
    StabilityTestRunner.newRunner()
                       .testName("S3AsyncStabilityTest.downloadLargeObjectToFile")
                       .futures(s3NettyClient.getObject(b -> b.bucket(bucketName).key(LARGE_KEY_NAME),
                                                        AsyncResponseTransformer.toFile(randomTempFile)))
                       .run();
    randomTempFile.delete();
}
 
Example #10
Source File: AsyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec traditionalMethod(OperationModel opModel) {
    ClassName responsePojoType = getPojoResponseType(opModel);
    ClassName requestType = ClassName.get(modelPackage, opModel.getInput().getVariableType());

    MethodSpec.Builder builder = methodSignatureWithReturnType(opModel)
            .addParameter(requestType, opModel.getInput().getVariableName())
            .addJavadoc(opModel.getDocs(model, ClientType.ASYNC));

    if (opModel.hasStreamingInput()) {
        builder.addParameter(ClassName.get(AsyncRequestBody.class), "requestBody");
    } else if (opModel.hasEventStreamInput()) {
        String eventStreamShapeName = EventStreamUtils.getEventStreamInRequest(opModel.getInputShape())
                                                      .getShapeName();
        ClassName shapeClass = ClassName.get(modelPackage, eventStreamShapeName);
        ParameterizedTypeName requestPublisher = ParameterizedTypeName.get(ClassName.get(Publisher.class), shapeClass);
        builder.addParameter(requestPublisher, EVENT_PUBLISHER_PARAM_NAME);
    }

    if (opModel.hasStreamingOutput()) {
        builder.addTypeVariable(STREAMING_TYPE_VARIABLE);
        ParameterizedTypeName asyncResponseHandlerType = ParameterizedTypeName
            .get(ClassName.get(AsyncResponseTransformer.class), responsePojoType, STREAMING_TYPE_VARIABLE);
        builder.addParameter(asyncResponseHandlerType, "asyncResponseTransformer");
    } else if (opModel.hasEventStreamOutput()) {
        builder.addParameter(poetExtensions.eventStreamResponseHandlerType(opModel), "asyncResponseHandler");
    }
    return operationBody(builder, opModel).build();
}
 
Example #11
Source File: test-async-client-class.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * 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>JsonException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
 *         of this type.</li>
 *         </ul>
 * @sample JsonAsyncClient.StreamingOutputOperation
 * @see <a href="http://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/StreamingOutputOperation"
 *      target="_top">AWS API Documentation</a>
 */
@Override
public <ReturnT> CompletableFuture<ReturnT> streamingOutputOperation(
        StreamingOutputOperationRequest streamingOutputOperationRequest,
        AsyncResponseTransformer<StreamingOutputOperationResponse, ReturnT> asyncResponseTransformer) {
    try {
        JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true)
                .isPayloadJson(false).build();

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

        HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory,
                operationMetadata);

        CompletableFuture<ReturnT> executeFuture = clientHandler.execute(
                new ClientExecutionParams<StreamingOutputOperationRequest, StreamingOutputOperationResponse>()
                        .withOperationName("StreamingOutputOperation")
                        .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory))
                        .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
                        .withInput(streamingOutputOperationRequest), asyncResponseTransformer);
        executeFuture.whenComplete((r, e) -> {
            if (e != null) {
                runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring",
                               () -> asyncResponseTransformer.exceptionOccurred(e));
            }
        });
        return executeFuture;
    } catch (Throwable t) {
        runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring",
                () -> asyncResponseTransformer.exceptionOccurred(t));
        return CompletableFutureUtils.failedFuture(t);
    }
}
 
Example #12
Source File: AsyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a simple method for operations with a streaming output member that takes a {@link Path} where data
 * will be downloaded to.
 */
private MethodSpec streamingOutputFileSimpleMethod(OperationModel opModel) {
    ClassName requestType = ClassName.get(modelPackage, opModel.getInput().getVariableType());
    return interfaceMethodSignature(opModel)
            .returns(completableFutureType(getPojoResponseType(opModel)))
            .addJavadoc(opModel.getDocs(model, ClientType.ASYNC, SimpleMethodOverload.FILE))
            .addParameter(requestType, opModel.getInput().getVariableName())
            .addParameter(ClassName.get(Path.class), "destinationPath")
            .addStatement("return $L($L, $T.toFile(destinationPath))", opModel.getMethodName(),
                          opModel.getInput().getVariableName(),
                          ClassName.get(AsyncResponseTransformer.class))
            .build();
}
 
Example #13
Source File: AsyncClientHandlerTransformerVerificationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void executeAndWaitError(AsyncResponseTransformer<SdkResponse, ?> transformer) {
    try {
        execute(transformer);
        fail("Client execution should have completed exceptionally");
    } catch (CompletionException e) {
        // ignored
    }
}
 
Example #14
Source File: AsyncClientHandlerExceptionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void streamingRequest_marshallingException_shouldInvokeExceptionOccurred() throws Exception {
    AsyncResponseTransformer asyncResponseTransformer = mock(AsyncResponseTransformer.class);
    CompletableFuture<?> future = new CompletableFuture<>();
    when(asyncResponseTransformer.prepare()).thenReturn(future);

    SdkClientException exception = SdkClientException.create("Could not handle response");
    when(marshaller.marshall(any(SdkRequest.class))).thenThrow(exception);

    doVerify(() -> clientHandler.execute(executionParams, asyncResponseTransformer), exception);

    verify(asyncResponseTransformer, times(1)).prepare();
    verify(asyncResponseTransformer, times(1)).exceptionOccurred(exception);
}
 
Example #15
Source File: EventStreamAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 2000)
public void prepareResetsSubscriberRef() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(2);
    AtomicBoolean exceptionThrown = new AtomicBoolean(false);

    AsyncResponseTransformer<SdkResponse, Void> transformer =
            EventStreamAsyncResponseTransformer.builder()
                    .eventStreamResponseHandler(
                            onEventStream(p -> {
                                try {
                                    p.subscribe(e -> {});
                                } catch (Throwable t) {
                                    exceptionThrown.set(true);
                                } finally {
                                    latch.countDown();
                                }
                            }))
                    .eventResponseHandler((r, e) -> null)
                    .executor(Executors.newFixedThreadPool(2))
                    .future(new CompletableFuture<>())
                    .build();

    Flowable<ByteBuffer> bytePublisher = Flowable.empty();

    CompletableFuture<Void> transformFuture = transformer.prepare();
    transformer.onStream(SdkPublisher.adapt(bytePublisher));
    transformFuture.join();

    transformFuture = transformer.prepare();
    transformer.onStream(SdkPublisher.adapt(bytePublisher));
    transformFuture.join();

    latch.await();
    assertThat(exceptionThrown).isFalse();
}
 
Example #16
Source File: EventStreamAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void erroneousExtraExceptionOccurredDoesNotSurfaceException() {
    AtomicLong numExceptions = new AtomicLong(0);
    AsyncResponseTransformer<SdkResponse, Void> transformer =
            EventStreamAsyncResponseTransformer.builder()
                    .eventStreamResponseHandler(new EventStreamResponseHandler<Object, Object>() {
                        @Override
                        public void responseReceived(Object response) {
                        }

                        @Override
                        public void onEventStream(SdkPublisher<Object> publisher) {
                        }

                        @Override
                        public void exceptionOccurred(Throwable throwable) {
                            numExceptions.incrementAndGet();
                        }

                        @Override
                        public void complete() {
                        }
                    })
                    .eventResponseHandler((r, e) -> null)
                    .executor(Executors.newFixedThreadPool(2))
                    .future(new CompletableFuture<>())
                    .build();

    transformer.prepare();
    transformer.exceptionOccurred(new RuntimeException("Boom!"));
    transformer.exceptionOccurred(new RuntimeException("Boom again!"));

    assertThat(numExceptions).hasValue(1);
}
 
Example #17
Source File: EventStreamAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void verifyExceptionThrown(Map<String, HeaderValue> headers) {
    SdkServiceException exception = SdkServiceException.builder().build();

    Message exceptionMessage = new Message(headers, new byte[0]);

    Flowable<ByteBuffer> bytePublisher = Flowable.just(exceptionMessage.toByteBuffer());

    AsyncResponseTransformer<SdkResponse, Void> transformer =
        EventStreamAsyncResponseTransformer.builder()
                                           .eventStreamResponseHandler(new SubscribingResponseHandler())
                                           .exceptionResponseHandler((response, executionAttributes) -> exception)
                                           .executor(Executors.newSingleThreadExecutor())
                                           .future(new CompletableFuture<>())
                                           .build();
    CompletableFuture<Void> cf = transformer.prepare();
    transformer.onResponse(null);
    transformer.onStream(SdkPublisher.adapt(bytePublisher));

    assertThatThrownBy(() -> {
        try {
            cf.join();
        } catch (CompletionException e) {
            if (e.getCause() instanceof SdkServiceException) {
                throw ((SdkServiceException) e.getCause());
            }
        }
    }).isSameAs(exception);
}
 
Example #18
Source File: S3AsyncStreamOps.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage:\n" +
                "    S3AsyncOps <bucketname> <objectname> <path>\n\n" +
                "Where:\n" +
                "    bucketname - the name of the bucket (i.e., bucket1)\n\n" +
                "    objectname - the name pf the object object (i.e., book.pdf)\n" +
                "    path - the local path to the file (i.e., C:\\AWS\\book.pdf)\n" +
                "Example:\n" +
                "    bucket1 book.pdf  C:\\AWS\\book.pdf\n";

         String bucketName = args[0];
         String objectKey = args[1];
         String path = args[2];

        S3AsyncClient client = S3AsyncClient.create();
        final CompletableFuture<GetObjectResponse> futureGet = client.getObject(
                GetObjectRequest.builder()
                        .bucket(bucketName)
                        .key(objectKey)
                        .build(),
                AsyncResponseTransformer.toFile(Paths.get(path)));
        futureGet.whenComplete((resp, err) -> {
            try {
                if (resp != null) {
                    System.out.println(resp);
                } else {
                    // Handle error
                    err.printStackTrace();
                }
            } finally {
                // Lets the application shut down. Only close the client when you are completely done with it
                client.close();
            }
        });

        futureGet.join();
    }
 
Example #19
Source File: AsyncOperationCancelTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamingOutputOperation() {
    CompletableFuture<ResponseBytes<StreamingOutputOperationResponse>> responseFuture = client.streamingOutputOperation(r -> {
    }, AsyncResponseTransformer.toBytes());
    responseFuture.cancel(true);
    assertThat(executeFuture.isCompletedExceptionally()).isTrue();
    assertThat(executeFuture.isCancelled()).isTrue();
}
 
Example #20
Source File: S3AsyncClientResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@GET
@Path("download/{objectKey}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Uni<Response> downloadFile(@PathParam("objectKey") String objectKey) throws Exception {
    File tempFile = tempFilePath();

    return Uni.createFrom()
            .completionStage(() -> s3.getObject(buildGetRequest(objectKey), AsyncResponseTransformer.toFile(tempFile)))
            .onItem()
            .apply(object -> Response.ok(tempFile)
                    .header("Content-Disposition", "attachment;filename=" + objectKey)
                    .header("Content-Type", object.contentType()).build());
}
 
Example #21
Source File: GetObjectAsyncIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void customResponseHandler_InterceptorRecievesResponsePojo() throws Exception {
    final CompletableFuture<String> cf = new CompletableFuture<>();
    try (S3AsyncClient asyncWithInterceptor = createClientWithInterceptor(new AssertingExecutionInterceptor())) {
        String result = asyncWithInterceptor
                .getObject(getObjectRequest, new AsyncResponseTransformer<GetObjectResponse, String>() {

                    @Override
                    public CompletableFuture<String> prepare() {
                        return cf;
                    }

                    @Override
                    public void onResponse(GetObjectResponse response) {
                        // POJO returned by modifyResponse should be delivered to the AsyncResponseTransformer
                        assertThat(response.metadata()).hasEntrySatisfying("x-amz-assert",
                                                                           s -> assertThat(s).isEqualTo("injected-value"));
                    }

                    @Override
                    public void onStream(SdkPublisher<ByteBuffer> publisher) {
                        publisher.subscribe(new SimpleSubscriber(b -> {
                        }) {
                            @Override
                            public void onComplete() {
                                super.onComplete();
                                cf.complete("result");
                            }
                        });
                    }

                    @Override
                    public void exceptionOccurred(Throwable throwable) {
                        cf.completeExceptionally(throwable);
                    }
                }).join();
        assertThat(result).isEqualTo("result");
    }
}
 
Example #22
Source File: EndpointOverrideTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getObjectAsync_shouldNotThrowNPE() throws IOException {
    stubFor(get(anyUrl())
                .willReturn(aResponse()
                                .withStatus(200)
                                .withBody("<?xml version=\"1.0\"?><GetObjectResult xmlns=\"http://s3"
                                          + ".amazonaws.com/doc/2006-03-01\"></GetObjectResult>")));
    assertThat(s3AsyncClient.getObject(b -> b.bucket("test").key("test").build(),
                                       AsyncResponseTransformer.toBytes()).join()).isNotNull();
}
 
Example #23
Source File: SdkHttpResponseTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void asyncStreamingMethodShouldContainSdkHttpDate() {

    stubWithHeaders(EXPECTED_HEADERS);
    ResponseBytes<StreamingOutputOperationResponse> responseBytes = asyncClient
        .streamingOutputOperation(SdkBuilder::build, AsyncResponseTransformer.toBytes()).join();
    StreamingOutputOperationResponse response = responseBytes.response();

    verifySdkHttpResponse(response);
    verifyResponseMetadata(response);
}
 
Example #24
Source File: CustomResponseMetadataTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void asyncStreaming_shouldContainResponseMetadata() {
    stubResponseWithHeaders();
    CompletableFuture<ResponseBytes<StreamingOutputOperationResponse>> response =
        asyncClient.streamingOutputOperation(SdkBuilder::build, AsyncResponseTransformer.toBytes());
    verifyResponseMetadata(response.join().response());
}
 
Example #25
Source File: AsyncOperationCancelTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamingOutputOperation() {
    CompletableFuture<ResponseBytes<StreamingOutputOperationResponse>> responseFuture = client.streamingOutputOperation(r -> {
    }, AsyncResponseTransformer.toBytes());
    responseFuture.cancel(true);
    assertThat(executeFuture.isCompletedExceptionally()).isTrue();
    assertThat(executeFuture.isCancelled()).isTrue();
}
 
Example #26
Source File: BaseAsyncClientHandler.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public <InputT extends SdkRequest, OutputT extends SdkResponse, ReturnT> CompletableFuture<ReturnT> execute(
    ClientExecutionParams<InputT, OutputT> executionParams,
    AsyncResponseTransformer<OutputT, ReturnT> asyncResponseTransformer) {

    validateExecutionParams(executionParams);

    if (executionParams.getCombinedResponseHandler() != null) {
        // There is no support for catching errors in a body for streaming responses. Our codegen must never
        // attempt to do this.
        throw new IllegalArgumentException("A streaming 'asyncResponseTransformer' may not be used when a "
                                           + "'combinedResponseHandler' has been specified in a "
                                           + "ClientExecutionParams object.");
    }

    ExecutionAttributes executionAttributes = createInitialExecutionAttributes();

    AsyncStreamingResponseHandler<OutputT, ReturnT> asyncStreamingResponseHandler =
        new AsyncStreamingResponseHandler<>(asyncResponseTransformer);

    // For streaming requests, prepare() should be called as early as possible to avoid NPE in client
    // See https://github.com/aws/aws-sdk-java-v2/issues/1268. We do this with a wrapper that caches the prepare
    // result until the execution attempt number changes. This guarantees that prepare is only called once per
    // execution.
    TransformingAsyncResponseHandler<ReturnT> wrappedAsyncStreamingResponseHandler =
        IdempotentAsyncResponseHandler.create(
            asyncStreamingResponseHandler,
            () -> executionAttributes.getAttribute(InternalCoreExecutionAttribute.EXECUTION_ATTEMPT),
            Integer::equals);
    wrappedAsyncStreamingResponseHandler.prepare();

    ExecutionContext context = createExecutionContext(executionParams, executionAttributes);

    HttpResponseHandler<OutputT> decoratedResponseHandlers =
        decorateResponseHandlers(executionParams.getResponseHandler(), context);

    asyncStreamingResponseHandler.responseHandler(decoratedResponseHandlers);

    TransformingAsyncResponseHandler<? extends SdkException> errorHandler =
        resolveErrorResponseHandler(executionParams.getErrorResponseHandler(), context, crc32Validator);

    TransformingAsyncResponseHandler<Response<ReturnT>> combinedResponseHandler =
        new CombinedResponseAsyncHttpResponseHandler<>(wrappedAsyncStreamingResponseHandler, errorHandler);

    return doExecute(executionParams, context, combinedResponseHandler);
}
 
Example #27
Source File: AsyncServerSideEncryptionIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private void verifyGetResponse(GetObjectRequest getObjectRequest) throws FileNotFoundException {
    String response = s3Async.getObject(getObjectRequest, AsyncResponseTransformer.toBytes()).join().asUtf8String();
    SdkAsserts.assertStringEqualsStream(response, new FileInputStream(file));
}
 
Example #28
Source File: GetObjectAsyncIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void dumpToString() throws IOException {
    String returned = s3Async.getObject(getObjectRequest, AsyncResponseTransformer.toBytes()).join().asUtf8String();
    assertThat(returned).isEqualTo(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8));
}
 
Example #29
Source File: GetObjectAsyncIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void toByteArray() throws IOException {
    byte[] returned = s3Async.getObject(getObjectRequest, AsyncResponseTransformer.toBytes()).join().asByteArray();
    assertThat(returned).isEqualTo(Files.readAllBytes(file.toPath()));
}
 
Example #30
Source File: AwsAsyncClientHandler.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public <InputT extends SdkRequest, OutputT extends SdkResponse, ReturnT> CompletableFuture<ReturnT> execute(
    ClientExecutionParams<InputT, OutputT> executionParams,
    AsyncResponseTransformer<OutputT, ReturnT> asyncResponseTransformer) {
    return super.execute(executionParams, asyncResponseTransformer);
}