software.amazon.awssdk.http.AbortableInputStream Java Examples

The following examples show how to use software.amazon.awssdk.http.AbortableInputStream. 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: UrlConnectionHttpClient.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public HttpExecuteResponse call() throws IOException {
    connection.connect();

    request.contentStreamProvider().ifPresent(provider ->
            invokeSafely(() -> IoUtils.copy(provider.newStream(), connection.getOutputStream())));

    int responseCode = connection.getResponseCode();
    boolean isErrorResponse = HttpStatusFamily.of(responseCode).isOneOf(CLIENT_ERROR, SERVER_ERROR);
    InputStream content = !isErrorResponse ? connection.getInputStream() : connection.getErrorStream();
    AbortableInputStream responseBody = content != null ?
                                        AbortableInputStream.create(content) : null;

    return HttpExecuteResponse.builder()
                              .response(SdkHttpResponse.builder()
                                                   .statusCode(responseCode)
                                                   .statusText(connection.getResponseMessage())
                                                   // TODO: Don't ignore abort?
                                                   .headers(extractHeaders(connection))
                                                   .build())
                              .responseBody(responseBody)
                              .build();
}
 
Example #2
Source File: XmlResponseParserUtils.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Parse an XML response if one is expected and available. If we are not expecting a payload, but the HTTP response
 * code shows an error then we will parse it anyway, as it should contain a serialized error.
 * @param sdkPojo the SDK builder object associated with the final response
 * @param response the HTTP response
 * @return A parsed XML document or an empty XML document if no payload/contents were found in the response.
 */
public static XmlElement parse(SdkPojo sdkPojo, SdkHttpFullResponse response) {

    try {
        Optional<AbortableInputStream> responseContent = response.content();

        // In some cases the responseContent is present but empty, so when we are not expecting a body we should
        // not attempt to parse it even if the body appears to be present.
        if ((!response.isSuccessful() || hasPayloadMembers(sdkPojo)) && responseContent.isPresent()) {
            return XmlDomParser.parse(responseContent.get());
        } else {
            return XmlElement.empty();
        }
    } catch (RuntimeException e) {
        if (response.isSuccessful()) {
            throw e;
        }

        return XmlElement.empty();
    }
}
 
Example #3
Source File: Crc32Validation.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private static AbortableInputStream process(boolean calculateCrc32FromCompressedData,
                                            SdkHttpFullResponse httpResponse,
                                            AbortableInputStream content) {
    Optional<Long> crc32Checksum = getCrc32Checksum(httpResponse);

    if (shouldDecompress(httpResponse)) {
        if (calculateCrc32FromCompressedData && crc32Checksum.isPresent()) {
            return decompressing(crc32Validating(content, crc32Checksum.get()));
        }

        if (crc32Checksum.isPresent()) {
            return crc32Validating(decompressing(content), crc32Checksum.get());
        }

        return decompressing(content);

    }

    return crc32Checksum.map(aLong -> crc32Validating(content, aLong)).orElse(content);
}
 
Example #4
Source File: AsyncResponseHandler.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<T> prepare() {
    streamFuture = new CompletableFuture<>();
    return streamFuture.thenCompose(baos -> {
        ByteArrayInputStream content = new ByteArrayInputStream(baos.toByteArray());
        // Ignore aborts - we already have all of the content.
        AbortableInputStream abortableContent = AbortableInputStream.create(content);
        httpResponse.content(abortableContent);
        try {
            return CompletableFuture.completedFuture(responseHandler.handle(crc32Validator.apply(httpResponse.build()),
                                                                            executionAttributes));
        } catch (Exception e) {
            return CompletableFutureUtils.failedFuture(e);
        }
    });
}
 
Example #5
Source File: ResponseTransformer.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Static helper method to create a response transformer that allows the connection to be left open. Useful for creating a
 * {@link ResponseTransformer} with a lambda or method reference rather than an anonymous inner class.
 *
 * @param transformer     Transformer to wrap.
 * @param <ResponseT> Type of unmarshalled response POJO.
 * @param <ReturnT>   Return type of transformer.
 * @return New {@link ResponseTransformer} which does not close the connection afterwards.
 */
static <ResponseT, ReturnT> ResponseTransformer<ResponseT, ReturnT> unmanaged(
    ResponseTransformer<ResponseT, ReturnT> transformer) {
    return new ResponseTransformer<ResponseT, ReturnT>() {
        @Override
        public ReturnT transform(ResponseT response, AbortableInputStream inputStream) throws Exception {
            InterruptMonitor.checkInterrupted();
            return transformer.transform(response, inputStream);
        }

        @Override
        public boolean needsConnectionLeftOpen() {
            return true;
        }
    };

}
 
Example #6
Source File: S3MockUtils.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * @return A mocked result for the ListBuckets operation.
 */
public static HttpExecuteResponse mockListBucketsResponse() throws UnsupportedEncodingException {
    return HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(200).build()).responseBody(
        AbortableInputStream.create(new StringInputStream(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<ListAllMyBucketsResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01\">\n" +
            "  <Owner>\n" +
            "    <ID>bcaf1ffd86f461ca5fb16fd081034f</ID>\n" +
            "    <DisplayName>webfile</DisplayName>\n" +
            "  </Owner>\n" +
            "  <Buckets>\n" +
            "    <Bucket>\n" +
            "      <Name>quotes</Name>\n" +
            "      <CreationDate>2006-02-03T16:45:09.000Z</CreationDate>\n" +
            "    </Bucket>\n" +
            "  </Buckets>\n" +
            "</ListAllMyBucketsResult>")))
                              .build();
}
 
Example #7
Source File: S3MockUtils.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * @return A mocked result for the ListObjects operation.
 */
public static HttpExecuteResponse mockListObjectsResponse() throws UnsupportedEncodingException {
    return HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(200).build()).responseBody(
        AbortableInputStream.create(new StringInputStream(
            "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\n" +
            "  <Name>example-bucket</Name>\n" +
            "  <Prefix>photos/2006/</Prefix>\n" +
            "  <Marker></Marker>\n" +
            "  <MaxKeys>1000</MaxKeys>\n" +
            "  <Delimiter>/</Delimiter>\n" +
            "  <IsTruncated>false</IsTruncated>\n" +
            "\n" +
            "  <CommonPrefixes>\n" +
            "    <Prefix>photos/2006/February/</Prefix>\n" +
            "  </CommonPrefixes>\n" +
            "  <CommonPrefixes>\n" +
            "    <Prefix>photos/2006/January/</Prefix>\n" +
            "  </CommonPrefixes>\n" +
            "</ListBucketResult>")))
                              .build();
}
 
Example #8
Source File: SubscribeToShardUnmarshallingTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void exceptionWithMessage_UnmarshalledCorrectly() throws Throwable {
    String errorCode = "ResourceNotFoundException";
    AbortableInputStream content = new MessageWriter()
        .writeInitialResponse(new byte[0])
        .writeException("{\"message\": \"foo\"}", errorCode)
        .toInputStream();

    stubResponse(SdkHttpFullResponse.builder()
                                    .statusCode(200)
                                    .content(content)
                                    .putHeader("x-amzn-requestid", REQUEST_ID)
                                    .build());

    try {
        subscribeToShard();
        fail("Expected ResourceNotFoundException exception");
    } catch (ResourceNotFoundException e) {
        assertThat(e.requestId()).isEqualTo(REQUEST_ID);
        assertThat(e.statusCode()).isEqualTo(500);
        assertThat(e.awsErrorDetails().errorCode()).isEqualTo(errorCode);
        assertThat(e.awsErrorDetails().errorMessage()).isEqualTo("foo");
        assertThat(e.awsErrorDetails().serviceName()).isEqualTo("kinesis");
    }
}
 
Example #9
Source File: GetBucketPolicyInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<InputStream> modifyHttpResponseContent(Context.ModifyHttpResponse context,
                                                       ExecutionAttributes executionAttributes) {
    if (INTERCEPTOR_CONTEXT_PREDICATE.test(context)) {

        String policy = context.responseBody()
                               .map(r -> invokeSafely(() -> IoUtils.toUtf8String(r)))
                               .orElse(null);

        if (policy != null) {
            String xml = XML_ENVELOPE_PREFIX + policy + XML_ENVELOPE_SUFFIX;
            return Optional.of(AbortableInputStream.create(new StringInputStream(xml)));
        }
    }

    return context.responseBody();
}
 
Example #10
Source File: SubscribeToShardUnmarshallingTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void errorWithMessage_UnmarshalledCorrectly() throws Throwable {
    String errorCode = "InternalError";
    String message = "error message";
    AbortableInputStream content = new MessageWriter()
        .writeInitialResponse(new byte[0])
        .writeError(errorCode, message)
        .toInputStream();

    stubResponse(SdkHttpFullResponse.builder()
                                    .statusCode(200)
                                    .content(content)
                                    .putHeader("x-amzn-requestid", REQUEST_ID)
                                    .build());

    try {
        subscribeToShard();
        fail("Expected ResourceNotFoundException exception");
    } catch (KinesisException e) {
        assertThat(e.requestId()).isEqualTo(REQUEST_ID);
        assertThat(e.statusCode()).isEqualTo(500);
        assertThat(e.awsErrorDetails().errorCode()).isEqualTo(errorCode);
        assertThat(e.awsErrorDetails().errorMessage()).isEqualTo(message);
        assertThat(e.awsErrorDetails().serviceName()).isEqualTo("kinesis");
    }
}
 
Example #11
Source File: Crc32ValidationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void adapt_InputStreamWithGzipEncoding_WrappedWithDecompressingStream() throws IOException {
    try (InputStream content = getClass().getResourceAsStream("/resources/compressed_json_body.gz")) {
        SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder()
                                                              .statusCode(200)
                                                              .putHeader("Content-Encoding", "gzip")
                                                              .content(AbortableInputStream.create(content))
                                                              .build();
        SdkHttpFullResponse adapted = adapt(httpResponse);
        InputStream in = getField(adapted.content().get(), "in");
        assertThat(in).isInstanceOf((GZIPInputStream.class));
    }
}
 
Example #12
Source File: AwsStructuredIonFactoryTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static SdkHttpFullResponse createResponse(IonStruct payload) throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    IonWriter writer = system.newBinaryWriter(bytes);
    payload.writeTo(writer);
    writer.close();

    return ValidSdkObjects.sdkHttpFullResponse()
                          .content(AbortableInputStream.create(new ByteArrayInputStream(bytes.toByteArray())))
                          .build();
}
 
Example #13
Source File: AfterTransmissionExecutionInterceptorsStage.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<SdkHttpFullRequest, SdkHttpFullResponse> execute(Pair<SdkHttpFullRequest, SdkHttpFullResponse> input,
                                                             RequestExecutionContext context) throws Exception {
    InterruptMonitor.checkInterrupted();
    // Update interceptor context
    InterceptorContext interceptorContext =
            context.executionContext().interceptorContext().copy(b -> b.httpResponse(input.right())
                                                                       .responseBody(input.right()
                                                                                          .content()
                                                                                          .orElse(null)));

    // interceptors.afterTransmission
    context.interceptorChain().afterTransmission(interceptorContext, context.executionAttributes());

    // interceptors.modifyHttpResponse
    interceptorContext = context.interceptorChain().modifyHttpResponse(interceptorContext, context.executionAttributes());

    // Store updated context
    context.executionContext().interceptorContext(interceptorContext);

    InterruptMonitor.checkInterrupted((SdkHttpFullResponse) interceptorContext.httpResponse());

    SdkHttpFullResponse response = (SdkHttpFullResponse) interceptorContext.httpResponse();

    if (interceptorContext.responseBody().isPresent()) {
        response = response.toBuilder().content(AbortableInputStream.create(interceptorContext.responseBody().get())).build();
    }

    return Pair.of(input.left(), response);
}
 
Example #14
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
private SdkHttpClient mockSdkHttpClient(SdkHttpResponse response, String body) throws Exception {
    ExecutableHttpRequest abortableCallable = Mockito.mock(ExecutableHttpRequest.class);
    SdkHttpClient mockClient = Mockito.mock(SdkHttpClient.class);

    Mockito.when(mockClient.prepareRequest(Mockito.any())).thenReturn(abortableCallable);
    Mockito.when(abortableCallable.call()).thenReturn(HttpExecuteResponse.builder()
            .response(response)
            .responseBody(AbortableInputStream.create(
                    new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8))
            ))
            .build()
    );
    return mockClient;
}
 
Example #15
Source File: LocalS3Client.java    From synapse with Apache License 2.0 5 votes vote down vote up
private AbortableInputStream toAbortableInputStream(final BucketItem bucketItem) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    final Constructor<AbortableInputStream> constructor = AbortableInputStream.class.getDeclaredConstructor(InputStream.class, Abortable.class);
    constructor.setAccessible(true);
    return constructor.newInstance(
            new ByteArrayInputStream(bucketItem.getData()),
            (Abortable) () -> {}
    );
}
 
Example #16
Source File: Crc32ValidationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void adapt_InputStreamWithNoGzipOrCrc32_NotWrappedWhenAdapted() {
    InputStream content = new StringInputStream("content");
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder()
                                                          .statusCode(200)
                                                          .content(AbortableInputStream.create(content))
                                                          .build();

    SdkHttpFullResponse adapted = adapt(httpResponse);

    InputStream in = getField(adapted.content().get(), "in");
    assertThat(in).isEqualTo(content);
}
 
Example #17
Source File: Crc32ValidationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void adapt_InputStreamWithCrc32Header_WrappedWithValidatingStream() throws UnsupportedEncodingException {
    InputStream content = new StringInputStream("content");
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder()
                                                          .statusCode(200)
                                                          .putHeader("x-amz-crc32", "1234")
                                                          .content(AbortableInputStream.create(content))
                                                          .build();

    SdkHttpFullResponse adapted = adapt(httpResponse);

    InputStream in = getField(adapted.content().get(), "in");
    assertThat(in).isInstanceOf((Crc32ChecksumValidatingInputStream.class));
}
 
Example #18
Source File: SyncClientHandlerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void mockSuccessfulApiCall() throws Exception {
    expectRetrievalFromMocks();
    when(httpClientCall.call()).thenReturn(HttpExecuteResponse.builder()
                                                              .responseBody(AbortableInputStream.create(new ByteArrayInputStream("TEST".getBytes())))
                                                              .response(SdkHttpResponse.builder().statusCode(200).build())
                                                              .build());
    when(responseHandler.handle(any(), any())).thenReturn(VoidSdkResponse.builder().build());
}
 
Example #19
Source File: Crc32ValidationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void adapt_CalculateCrcFromCompressed_WrapsWithCrc32ThenGzip() throws IOException {
    try (InputStream content = getClass().getResourceAsStream("/resources/compressed_json_body.gz")) {
        SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder()
                                                              .statusCode(200)
                                                              .putHeader("Content-Encoding", "gzip")
                                                              .putHeader("x-amz-crc32", "1234")
                                                              .content(AbortableInputStream.create(content))
                                                              .build();

        SdkHttpFullResponse adapted = Crc32Validation.validate(true, httpResponse);
        InputStream in = getField(adapted.content().get(), "in");
        assertThat(in).isInstanceOf((GZIPInputStream.class));
    }
}
 
Example #20
Source File: Crc32ValidationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test(expected = UncheckedIOException.class)
public void adapt_InvalidGzipContent_ThrowsException() throws UnsupportedEncodingException {
    InputStream content = new StringInputStream("this isn't GZIP");
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder()
                                                          .statusCode(200)
                                                          .putHeader("Content-Encoding", "gzip")
                                                          .content(AbortableInputStream.create(content))
                                                          .build();

    SdkHttpFullResponse adapted = adapt(httpResponse);
    InputStream in = getField(adapted.content().get(), "in");
    assertThat(in).isInstanceOf((GZIPInputStream.class));
}
 
Example #21
Source File: SyncClientHandlerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void responseTransformerThrowsRetryableException_shouldPropogate() throws Exception {
    mockSuccessfulApiCall();
    when(responseTransformer.transform(any(SdkResponse.class), any(AbortableInputStream.class))).thenThrow(
        RetryableException.create("test"));

    assertThatThrownBy(() -> syncClientHandler.execute(clientExecutionParams(), responseTransformer))
        .isInstanceOf(RetryableException.class);
}
 
Example #22
Source File: LocalS3Client.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
private AbortableInputStream toAbortableInputStream(final BucketItem bucketItem) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    final Constructor<AbortableInputStream> constructor = AbortableInputStream.class.getDeclaredConstructor(InputStream.class, Abortable.class);
    constructor.setAccessible(true);
    return constructor.newInstance(
            new ByteArrayInputStream(bucketItem.getData()),
            (Abortable) () -> {}
    );
}
 
Example #23
Source File: SyncClientHandlerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void responseTransformerThrowsOtherException_shouldWrapWithNonRetryableException() throws Exception {
    mockSuccessfulApiCall();
    when(responseTransformer.transform(any(SdkResponse.class), any(AbortableInputStream.class))).thenThrow(
        new RuntimeException());

    assertThatThrownBy(() -> syncClientHandler.execute(clientExecutionParams(), responseTransformer))
        .hasCauseInstanceOf(NonRetryableException.class);
}
 
Example #24
Source File: SyncClientHandlerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void verifyResponseTransformerPropagateException(Exception exception) throws Exception {
    mockSuccessfulApiCall();
    when(responseTransformer.transform(any(SdkResponse.class), any(AbortableInputStream.class))).thenThrow(
        exception);

    assertThatThrownBy(() -> syncClientHandler.execute(clientExecutionParams(), responseTransformer))
        .hasCauseInstanceOf(exception.getClass());
}
 
Example #25
Source File: EventStreamAsyncResponseTransformer.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Transforms an event stream message into a {@link SdkHttpFullResponse} so we can reuse our existing generated unmarshallers.
 *
 * @param message Message to transform.
 */
private SdkHttpFullResponse adaptMessageToResponse(Message message, boolean isException) {

    Map<String, List<String>> headers =
        message.getHeaders()
               .entrySet()
               .stream()
               .collect(HashMap::new, (m, e) -> m.put(e.getKey(), singletonList(e.getValue().getString())), Map::putAll);

    if (requestId != null) {
        headers.put(X_AMZN_REQUEST_ID_HEADER, singletonList(requestId));
    }

    if (extendedRequestId != null) {
        headers.put(X_AMZ_ID_2_HEADER, singletonList(extendedRequestId));
    }

    SdkHttpFullResponse.Builder builder =
        SdkHttpFullResponse.builder()
                           .content(AbortableInputStream.create(new ByteArrayInputStream(message.getPayload())))
                           .headers(headers);

    if (!isException) {
        builder.statusCode(200);
    }

    return builder.build();
}
 
Example #26
Source File: SubscribeToShardUnmarshallingTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void eventWithRecords_UnmarshalledCorrectly() throws Throwable {
    String data = BinaryUtils.toBase64("foobar".getBytes(StandardCharsets.UTF_8));
    AbortableInputStream content = new MessageWriter()
        .writeInitialResponse(new byte[0])
        .writeEvent("SubscribeToShardEvent",
                    String.format("{\"ContinuationSequenceNumber\": \"1234\","
                                  + "\"MillisBehindLatest\": 0,"
                                  + "\"Records\": [{\"Data\": \"%s\"}]"
                                  + "}", data))
        .toInputStream();
    SubscribeToShardEvent event = SubscribeToShardEvent.builder()
                                                       .continuationSequenceNumber("1234")
                                                       .millisBehindLatest(0L)
                                                       .records(Record.builder()
                                                                      .data(SdkBytes.fromUtf8String("foobar"))
                                                                      .build())
                                                       .build();

    stubResponse(SdkHttpFullResponse.builder()
                                    .statusCode(200)
                                    .content(content)
                                    .build());

    List<SubscribeToShardEventStream> events = subscribeToShard();
    assertThat(events).containsOnly(event);
}
 
Example #27
Source File: MockHttpClient.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private HttpExecuteResponse errorResponse() {

        AbortableInputStream inputStream =
            AbortableInputStream.create(new ByteArrayInputStream(errorResponseContent.getBytes()));

        return HttpExecuteResponse.builder()
                                  .response(SdkHttpResponse.builder()
                                                           .statusCode(500)
                                                           .build())
                                  .responseBody(inputStream)
                                  .build();
    }
 
Example #28
Source File: MockHttpClient.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private HttpExecuteResponse successResponse() {

        AbortableInputStream inputStream =
            AbortableInputStream.create(new ByteArrayInputStream(successResponseContent.getBytes()));

        return HttpExecuteResponse.builder()
                                  .response(SdkHttpResponse.builder()
                                                           .statusCode(200)
                                                           .build())
                                  .responseBody(inputStream)
                                  .build();
    }
 
Example #29
Source File: S3PresignerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void putObject_PresignedHttpRequestCanBeInvokedDirectlyBySdk() throws IOException {
    String objectKey = generateRandomObjectKey();
    S3TestUtils.addCleanupTask(S3PresignerIntegrationTest.class,
                               () -> client.deleteObject(r -> r.bucket(testBucket).key(objectKey)));

    PresignedPutObjectRequest presigned =
        presigner.presignPutObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .putObjectRequest(por -> por.bucket(testBucket).key(objectKey)));

    assertThat(presigned.isBrowserExecutable()).isFalse();

    SdkHttpClient httpClient = ApacheHttpClient.builder().build(); // or UrlConnectionHttpClient.builder().build()

    ContentStreamProvider requestPayload = () -> new StringInputStream(testObjectContent);

    HttpExecuteRequest request = HttpExecuteRequest.builder()
                                                   .request(presigned.httpRequest())
                                                   .contentStreamProvider(requestPayload)
                                                   .build();

    HttpExecuteResponse response = httpClient.prepareRequest(request).call();

    assertThat(response.responseBody()).isPresent();
    assertThat(response.httpResponse().isSuccessful()).isTrue();
    response.responseBody().ifPresent(AbortableInputStream::abort);
    String content = client.getObjectAsBytes(r -> r.bucket(testBucket).key(objectKey)).asUtf8String();
    assertThat(content).isEqualTo(testObjectContent);
}
 
Example #30
Source File: ApacheHttpClient.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and initializes an HttpResponse object suitable to be passed to an HTTP response
 * handler object.
 *
 * @return The new, initialized HttpResponse object ready to be passed to an HTTP response handler object.
 * @throws IOException If there were any problems getting any response information from the
 *                     HttpClient method object.
 */
private HttpExecuteResponse createResponse(org.apache.http.HttpResponse apacheHttpResponse,
                                           HttpRequestBase apacheRequest) throws IOException {
    SdkHttpResponse response = SdkHttpResponse.builder()
                                              .statusCode(apacheHttpResponse.getStatusLine().getStatusCode())
                                              .statusText(apacheHttpResponse.getStatusLine().getReasonPhrase())
                                              .headers(transformHeaders(apacheHttpResponse))
                                              .build();
    AbortableInputStream responseBody = apacheHttpResponse.getEntity() != null ?
                               toAbortableInputStream(apacheHttpResponse, apacheRequest) : null;

    return HttpExecuteResponse.builder().response(response).responseBody(responseBody).build();

}