software.amazon.awssdk.core.sync.ResponseTransformer Java Examples

The following examples show how to use software.amazon.awssdk.core.sync.ResponseTransformer. 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: SyncClientInterface.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 that reads data from a file and a streaming output member that write response content to a file.
 */
private MethodSpec streamingInputOutputFileSimpleMethod(OperationModel opModel,
                                                        TypeName responseType,
                                                        ClassName requestType) {
    return MethodSpec.methodBuilder(opModel.getMethodName())
                     .returns(responseType)
                     .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
                     .addParameter(requestType, opModel.getInput().getVariableName())
                     .addParameter(ClassName.get(Path.class), "sourcePath")
                     .addParameter(ClassName.get(Path.class), "destinationPath")
                     .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE))
                     .addExceptions(getExceptionClasses(model, opModel))
                     .addStatement("return $L($L, $T.fromFile(sourcePath), $T.toFile(destinationPath))",
                                   opModel.getMethodName(),
                                   opModel.getInput().getVariableName(),
                                   ClassName.get(RequestBody.class),
                                   ClassName.get(ResponseTransformer.class))
                     .build();
}
 
Example #2
Source File: SyncServerSideEncryptionIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void sse_AWSKMS_succeeds() throws Exception {
    String key = UUID.randomUUID().toString();
    PutObjectRequest request = PutObjectRequest.builder()
                                               .key(key)
                                               .bucket(BUCKET)
                                               .serverSideEncryption(ServerSideEncryption.AWS_KMS)
                                               .build();

    s3.putObject(request, file.toPath());

    GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                                                        .key(key)
                                                        .bucket(BUCKET)
                                                        .build();

    String response = s3.getObject(getObjectRequest, ResponseTransformer.toBytes()).asUtf8String();
    SdkAsserts.assertStringEqualsStream(response, new FileInputStream(file));
}
 
Example #3
Source File: SyncServerSideEncryptionIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void sse_onBucket_succeeds() throws FileNotFoundException {
    String key = UUID.randomUUID().toString();

    PutObjectRequest request = PutObjectRequest.builder()
                                               .key(key)
                                               .bucket(BUCKET_WITH_SSE)
                                               .build();

    s3.putObject(request, file.toPath());

    GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                                                        .key(key)
                                                        .bucket(BUCKET_WITH_SSE)
                                                        .build();

    String response = s3.getObject(getObjectRequest, ResponseTransformer.toBytes()).asUtf8String();
    SdkAsserts.assertStringEqualsStream(response, new FileInputStream(file));
}
 
Example #4
Source File: SyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static void streamingMethod(MethodSpec.Builder methodBuilder, OperationModel opModel, TypeName responseType) {
    if (opModel.hasStreamingInput()) {
        methodBuilder.addParameter(ClassName.get(RequestBody.class), "requestBody");
    }
    if (opModel.hasStreamingOutput()) {
        methodBuilder.addTypeVariable(STREAMING_TYPE_VARIABLE);
        ParameterizedTypeName streamingResponseHandlerType = ParameterizedTypeName
                .get(ClassName.get(ResponseTransformer.class), responseType, STREAMING_TYPE_VARIABLE);
        methodBuilder.addParameter(streamingResponseHandlerType, "responseTransformer");
    }
}
 
Example #5
Source File: S3PinotFS.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Override
public void copyToLocalFile(URI srcUri, File dstFile)
    throws Exception {
  LOGGER.info("Copy {} to local {}", srcUri, dstFile.getAbsolutePath());
  URI base = getBase(srcUri);
  String prefix = sanitizePath(base.relativize(srcUri).getPath());
  GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(srcUri.getHost()).key(prefix).build();

  _s3Client.getObject(getObjectRequest, ResponseTransformer.toFile(dstFile));
}
 
Example #6
Source File: BaseSyncClientHandler.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public <InputT extends SdkRequest, OutputT extends SdkResponse, ReturnT> ReturnT execute(
    ClientExecutionParams<InputT, OutputT> executionParams,
    ResponseTransformer<OutputT, ReturnT> responseTransformer) {

    validateExecutionParams(executionParams);

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

    ExecutionContext executionContext = createExecutionContext(executionParams, createInitialExecutionAttributes());

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

    HttpResponseHandler<ReturnT> httpResponseHandler =
        new HttpResponseHandlerAdapter<>(decoratedResponseHandlers, responseTransformer);

    return doExecute(
        executionParams,
        executionContext,
        new CombinedResponseHandler<>(httpResponseHandler, executionParams.getErrorResponseHandler()));
}
 
Example #7
Source File: SyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return Simple method for streaming output operations to write response content to a file.
 */
private MethodSpec downloadToFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
    return MethodSpec.methodBuilder(opModel.getMethodName())
                     .returns(responseType)
                     .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
                     .addParameter(requestType, opModel.getInput().getVariableName())
                     .addParameter(ClassName.get(Path.class), "filePath")
                     .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE))
                     .addExceptions(getExceptionClasses(model, opModel))
                     .addStatement("return $L($L, $T.toFile($L))", opModel.getMethodName(),
                                   opModel.getInput().getVariableName(),
                                   ClassName.get(ResponseTransformer.class),
                                   "filePath")
                     .build();
}
 
Example #8
Source File: SyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return Simple method for streaming output operations to get the content as a byte buffer or other in-memory types.
 */
private MethodSpec bytesSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
    TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseBytes.class), responseType);
    return MethodSpec.methodBuilder(opModel.getMethodName() + "AsBytes")
                     .returns(returnType)
                     .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
                     .addParameter(requestType, opModel.getInput().getVariableName())
                     .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.BYTES))
                     .addExceptions(getExceptionClasses(model, opModel))
                     .addStatement("return $L($L, $T.toBytes())", opModel.getMethodName(),
                                   opModel.getInput().getVariableName(),
                                   ClassName.get(ResponseTransformer.class))
                     .build();
}
 
Example #9
Source File: SyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return Simple method for streaming output operations to get content as an input stream.
 */
private MethodSpec inputStreamSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
    TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseInputStream.class), responseType);
    return MethodSpec.methodBuilder(opModel.getMethodName())
                     .returns(returnType)
                     .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
                     .addParameter(requestType, opModel.getInput().getVariableName())
                     .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.INPUT_STREAM))
                     .addExceptions(getExceptionClasses(model, opModel))
                     .addStatement("return $L($L, $T.toInputStream())", opModel.getMethodName(),
                                   opModel.getInput().getVariableName(),
                                   ClassName.get(ResponseTransformer.class))
                     .build();
}
 
Example #10
Source File: S3SyncClientResource.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 Response downloadFile(@PathParam("objectKey") String objectKey) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GetObjectResponse object = s3.getObject(buildGetRequest(objectKey), ResponseTransformer.toOutputStream(baos));

    ResponseBuilder response = Response.ok((StreamingOutput) output -> baos.writeTo(output));
    response.header("Content-Disposition", "attachment;filename=" + objectKey);
    response.header("Content-Type", object.contentType());
    return response.build();
}
 
Example #11
Source File: ClientReflector.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Call the operation (with a streaming output) method on the client with the given request.
 *
 * @param requestObject   POJO request object.
 * @param responseHandler Response handler for an operation with a streaming output.
 * @return Unmarshalled result
 */
public Object invokeStreamingMethod(TestCase testCase,
                                    Object requestObject,
                                    ResponseTransformer<?, ?> responseHandler) throws Exception {
    String operationName = testCase.getWhen().getOperationName();
    Method operationMethod = getOperationMethod(operationName, requestObject.getClass(), ResponseTransformer.class);
    return operationMethod.invoke(client, requestObject, responseHandler);
}
 
Example #12
Source File: CustomResponseMetadataTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void syncStreaming_shouldContainResponseMetadata() {
    stubResponseWithHeaders();
    ResponseBytes<StreamingOutputOperationResponse> streamingOutputOperationResponseResponseBytes =
        client.streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes());
    verifyResponseMetadata(streamingOutputOperationResponseResponseBytes.response());
}
 
Example #13
Source File: SyncClientConnectionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void streamingOut_toInputStream_closeResponseStreamShouldCloseUnderlyingStream() throws IOException {
    ClosableStringInputStream inputStream = new ClosableStringInputStream("{}");
    mockHttpClient.stubNextResponse(mockResponse(inputStream, 200));

    ResponseInputStream<StreamingOutputOperationResponse> responseInputStream =
        client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toInputStream());

    assertThat(inputStream.isClosed()).isFalse();
    responseInputStream.close();
    assertThat(inputStream.isClosed()).isTrue();
}
 
Example #14
Source File: SyncClientConnectionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void streamingOut_errorResponse_shouldCloseConnection() {
    ClosableStringInputStream inputStream = new ClosableStringInputStream("{\"__type\":\"SomeUnknownType\"}");
    mockHttpClient.stubNextResponse(mockResponse(inputStream, 400));

    assertThatThrownBy(() -> client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toBytes()))
        .isExactlyInstanceOf(ProtocolRestJsonException.class);
    assertThat(inputStream.isClosed()).isTrue();
}
 
Example #15
Source File: SyncClientConnectionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void streamingOut_successfulResponse_shouldCloseConnection() {
    ClosableStringInputStream inputStream = new ClosableStringInputStream("{}");
    mockHttpClient.stubNextResponse(mockResponse(inputStream, 200));

    client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toBytes());
    assertThat(inputStream.isClosed()).isTrue();
}
 
Example #16
Source File: S3Resource.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@GET
@Path("blocking")
@Produces(TEXT_PLAIN)
public String testBlockingS3() {
    LOG.info("Testing S3 Blocking client with bucket: " + SYNC_BUCKET);

    String keyValue = UUID.randomUUID().toString();
    String result = null;

    try {
        if (S3Utils.createBucket(s3Client, SYNC_BUCKET)) {
            if (s3Client.putObject(S3Utils.createPutRequest(SYNC_BUCKET, keyValue),
                    RequestBody.fromString(SAMPLE_S3_OBJECT)) != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                GetObjectResponse object = s3Client.getObject(S3Utils.createGetRequest(SYNC_BUCKET, keyValue),
                        ResponseTransformer.toOutputStream(baos));

                if (object != null) {
                    result = metadata(object) + "+" + baos.toString();
                }
            }
        }
    } catch (Exception ex) {
        LOG.error("Error during S3 operations.", ex);
        return "ERROR";
    }
    return result;
}
 
Example #17
Source File: BaseTimeoutTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
public SlowFileResponseTransformer() {
    try {
        this.delegate = ResponseTransformer.toFile(File.createTempFile("ApiCallTiemoutTest", ".txt"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #18
Source File: SdkHttpResponseTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void syncStreamingShouldContainSdkHttpDate() {

    stubWithHeaders(EXPECTED_HEADERS);
    ResponseBytes<StreamingOutputOperationResponse> responseBytes = client
        .streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes());
    StreamingOutputOperationResponse response = responseBytes.response();

    verifySdkHttpResponse(response);
    verifyResponseMetadata(response);
}
 
Example #19
Source File: ResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void downloadToOutputStreamDoesNotRetry() throws IOException {
    stubForRetriesTimeoutReadingFromStreams();

    assertThatThrownBy(() -> testClient().streamingOutputOperation(StreamingOutputOperationRequest.builder().build(),
                                                                   ResponseTransformer
                                                                       .toOutputStream(new ByteArrayOutputStream())))
        .isInstanceOf(SdkClientException.class);
}
 
Example #20
Source File: ResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void downloadToExistingFileDoesNotRetry() throws IOException {
    stubForRetriesTimeoutReadingFromStreams();

    assertThatThrownBy(() -> testClient().streamingOutputOperation(StreamingOutputOperationRequest.builder().build(),
        ResponseTransformer
            .toFile(new File(".."))))
        .isInstanceOf(SdkClientException.class);
}
 
Example #21
Source File: GetObjectIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void toInputStream_loadFromProperties() throws IOException {
    s3.putObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY), RequestBody.fromString("test: test"));
    try (ResponseInputStream<GetObjectResponse> object = s3.getObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY),
                                                                      ResponseTransformer.toInputStream())) {
        Properties properties = new Properties();
        properties.load(object);
        assertThat(properties.getProperty("test")).isEqualTo("test");
    }
}
 
Example #22
Source File: SyncApiCallAttemptTimeoutTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
protected Callable streamingCallable() {
    return () -> client.streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes());
}
 
Example #23
Source File: S3GetObject.java    From piper with Apache License 2.0 4 votes vote down vote up
@Override
public Object handle (TaskExecution aTask) throws Exception {
  
  AmazonS3URI s3Uri = new AmazonS3URI(aTask.getRequiredString("uri"));
  
  String bucketName = s3Uri.getBucket();
  String key = s3Uri.getKey();
  
  S3Client s3 = S3Client.builder().build();
  
  s3.getObject(GetObjectRequest.builder().bucket(bucketName).key(key).build(),
      ResponseTransformer.toFile(Paths.get(aTask.getRequiredString("filepath"))));
  
  
  return null;
}
 
Example #24
Source File: AwsSyncClientHandler.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> ReturnT execute(
    ClientExecutionParams<InputT, OutputT> executionParams,
    ResponseTransformer<OutputT, ReturnT> responseTransformer) {
    return super.execute(executionParams, responseTransformer);
}
 
Example #25
Source File: BaseSyncClientHandler.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private HttpResponseHandlerAdapter(HttpResponseHandler<OutputT> httpResponseHandler,
                                   ResponseTransformer<OutputT, ReturnT> responseTransformer) {
    this.httpResponseHandler = httpResponseHandler;
    this.responseTransformer = responseTransformer;
}
 
Example #26
Source File: SdkSyncClientHandler.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> ReturnT execute(
    ClientExecutionParams<InputT, OutputT> executionParams,
    ResponseTransformer<OutputT, ReturnT> responseTransformer) {
    return super.execute(executionParams, responseTransformer);
}
 
Example #27
Source File: test-query-client-class.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Some operation with a streaming output
 *
 * @param streamingOutputOperationRequest
 * @param responseTransformer
 *        Functional interface for processing the streamed response content. The unmarshalled
 *        StreamingOutputOperationResponse and an InputStream to the response content are provided as parameters to
 *        the callback. The callback may return a transformed type which will be the return value of this method.
 *        See {@link software.amazon.awssdk.core.sync.ResponseTransformer} for details on implementing this
 *        interface and for links to pre-canned implementations for common scenarios like downloading to a file. The
 *        service documentation for the response content is as follows 'This be a stream'.
 * @return The transformed result of the ResponseTransformer.
 * @throws SdkException
 *         Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
 *         catch all scenarios.
 * @throws SdkClientException
 *         If any client side error occurs such as an IO related failure, failure to get credentials, etc.
 * @throws QueryException
 *         Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
 * @sample QueryClient.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> ReturnT streamingOutputOperation(StreamingOutputOperationRequest streamingOutputOperationRequest,
                                                  ResponseTransformer<StreamingOutputOperationResponse, ReturnT> responseTransformer) throws AwsServiceException,
                                                                                                                                             SdkClientException, QueryException {

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

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

    return clientHandler.execute(
        new ClientExecutionParams<StreamingOutputOperationRequest, StreamingOutputOperationResponse>()
            .withOperationName("StreamingOutputOperation").withResponseHandler(responseHandler)
            .withErrorResponseHandler(errorResponseHandler).withInput(streamingOutputOperationRequest)
            .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)), responseTransformer);
}
 
Example #28
Source File: test-json-client-class.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Some operation with a streaming output
 *
 * @param streamingOutputOperationRequest
 * @param responseTransformer
 *        Functional interface for processing the streamed response content. The unmarshalled
 *        StreamingOutputOperationResponse and an InputStream to the response content are provided as parameters
 *        to the callback. The callback may return a transformed type which will be the return value of this method.
 *        See {@link software.amazon.awssdk.core.sync.ResponseTransformer} for details on implementing this
 *        interface and for links to pre-canned implementations for common scenarios like downloading to a file. The
 *        service documentation for the response content is as follows 'This be a stream'.
 * @return The transformed result of the ResponseTransformer.
 * @throws SdkException
 *         Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
 *         catch all scenarios.
 * @throws SdkClientException
 *         If any client side error occurs such as an IO related failure, failure to get credentials, etc.
 * @throws JsonException
 *         Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
 * @sample JsonClient.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> ReturnT streamingOutputOperation(StreamingOutputOperationRequest streamingOutputOperationRequest,
                                                  ResponseTransformer<StreamingOutputOperationResponse, ReturnT> responseTransformer) throws AwsServiceException,
                                                                                                                                             SdkClientException, JsonException {
    JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true)
                                                                   .isPayloadJson(false).build();

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

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

    return clientHandler.execute(
        new ClientExecutionParams<StreamingOutputOperationRequest, StreamingOutputOperationResponse>()
            .withOperationName("StreamingOutputOperation")
            .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
            .withInput(streamingOutputOperationRequest)
            .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)), responseTransformer);
}
 
Example #29
Source File: GetObjectIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void toOutputStream() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GetObjectResponse response = s3.getObject(getObjectRequest, ResponseTransformer.toOutputStream(baos));
}
 
Example #30
Source File: SyncApiCallTimeoutTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
protected Callable streamingCallable() {
    return () -> client.streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes());
}