Java Code Examples for software.amazon.awssdk.core.interceptor.Context#ModifyHttpResponse

The following examples show how to use software.amazon.awssdk.core.interceptor.Context#ModifyHttpResponse . 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: 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 2
Source File: AsyncChecksumValidationInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<Publisher<ByteBuffer>> modifyAsyncHttpResponseContent(Context.ModifyHttpResponse context,
                                                                      ExecutionAttributes executionAttributes) {
    if (getObjectChecksumEnabledPerResponse(context.request(), context.httpResponse())
        && context.responsePublisher().isPresent()) {
        long contentLength = context.httpResponse()
                                    .firstMatchingHeader(CONTENT_LENGTH_HEADER)
                                    .map(Long::parseLong)
                                    .orElse(0L);

        SdkChecksum checksum = new Md5Checksum();
        executionAttributes.putAttribute(CHECKSUM, checksum);
        if (contentLength > 0) {
            return Optional.of(new ChecksumValidatingPublisher(context.responsePublisher().get(), checksum, contentLength));
        }
    }

    return context.responsePublisher();
}
 
Example 3
Source File: SyncChecksumValidationInterceptor.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 (getObjectChecksumEnabledPerResponse(context.request(), context.httpResponse())
        && context.responseBody().isPresent()) {

        SdkChecksum checksum = new Md5Checksum();

        long contentLength = context.httpResponse()
                                    .firstMatchingHeader(CONTENT_LENGTH_HEADER)
                                    .map(Long::parseLong)
                                    .orElse(0L);

        if (contentLength > 0) {
            return Optional.of(new ChecksumValidatingInputStream(context.responseBody().get(), checksum, contentLength));
        }
    }

    return context.responseBody();
}
 
Example 4
Source File: AsyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyAsyncHttpResponseContent_getObjectRequest_responseDoesNotContainChecksum_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributesWithChecksumDisabled();
    SdkHttpResponse sdkHttpResponse = SdkHttpResponse.builder()
                                                     .putHeader(CONTENT_LENGTH_HEADER, "100")
                                                     .build();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(GetObjectRequest.builder().build(), sdkHttpResponse);
    Optional<Publisher<ByteBuffer>> publisher = interceptor.modifyAsyncHttpResponseContent(modifyHttpResponse,
                                                                                           executionAttributes);
    assertThat(publisher).isEqualTo(modifyHttpResponse.responsePublisher());
}
 
Example 5
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyHttpResponseContent_nonGetObjectRequest_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributes();
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
    sdkHttpResponse.toBuilder().clearHeaders();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(PutObjectRequest.builder().build(), sdkHttpResponse);
    Optional<InputStream> publisher = interceptor.modifyHttpResponseContent(modifyHttpResponse,
                                                                            executionAttributes);
    assertThat(publisher).isEqualTo(modifyHttpResponse.responseBody());

}
 
Example 6
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyHttpResponseContent_getObjectRequest_responseDoesNotContainChecksum_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributesWithChecksumDisabled();
    SdkHttpResponse sdkHttpResponse = SdkHttpResponse.builder()
                                                     .putHeader(CONTENT_LENGTH_HEADER, "100")
                                                     .build();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(GetObjectRequest.builder().build(), sdkHttpResponse);
    Optional<InputStream> publisher = interceptor.modifyHttpResponseContent(modifyHttpResponse,
                                                                            executionAttributes);
    assertThat(publisher).isEqualTo(modifyHttpResponse.responseBody());
}
 
Example 7
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyHttpResponseContent_getObjectRequest_checksumEnabled_shouldWrapChecksumValidatingPublisher() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(GetObjectRequest.builder().build(), sdkHttpResponse);
    Optional<InputStream> publisher = interceptor.modifyHttpResponseContent(modifyHttpResponse,
                                                                            getExecutionAttributes());
    assertThat(publisher.get()).isExactlyInstanceOf(ChecksumValidatingInputStream.class);
}
 
Example 8
Source File: GetBucketPolicyInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void errorResponseShouldNotModifyResponse() {
    GetBucketPolicyRequest request = GetBucketPolicyRequest.builder().build();
    Context.ModifyHttpResponse context = modifyHttpResponseContent(request, SdkHttpResponse.builder().statusCode(404).build());
    Optional<InputStream> inputStream = interceptor.modifyHttpResponseContent(context, new ExecutionAttributes());
    assertThat(inputStream).isEqualTo(context.responseBody());
}
 
Example 9
Source File: GetBucketPolicyInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void nonGetBucketPolicyResponse_ShouldNotModifyResponse() {
    GetObjectRequest request = GetObjectRequest.builder().build();
    Context.ModifyHttpResponse context = modifyHttpResponseContent(request, SdkHttpResponse.builder().statusCode(200).build());
    Optional<InputStream> inputStream = interceptor.modifyHttpResponseContent(context, new ExecutionAttributes());
    assertThat(inputStream).isEqualTo(context.responseBody());
}
 
Example 10
Source File: GetBucketPolicyInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getBucketPolicy_shouldModifyResponseContent() {
    GetBucketPolicyRequest request = GetBucketPolicyRequest.builder().build();
    Context.ModifyHttpResponse context = modifyHttpResponseContent(request, SdkHttpResponse.builder()
                                                                                           .statusCode(200)
                                                                                           .build());
    Optional<InputStream> inputStream = interceptor.modifyHttpResponseContent(context, new ExecutionAttributes());
    assertThat(inputStream).isNotEqualTo(context.responseBody());
}
 
Example 11
Source File: AsyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyAsyncHttpResponseContent_nonGetObjectRequest_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributes();
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
    sdkHttpResponse.toBuilder().clearHeaders();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(PutObjectRequest.builder().build(), sdkHttpResponse);
    Optional<Publisher<ByteBuffer>> publisher = interceptor.modifyAsyncHttpResponseContent(modifyHttpResponse,
                                                                                           executionAttributes);
    assertThat(publisher).isEqualTo(modifyHttpResponse.responsePublisher());

}
 
Example 12
Source File: MetricsExecutionInterceptor.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Publisher<ByteBuffer>> modifyAsyncHttpResponseContent(Context.ModifyHttpResponse context,
                                                                      ExecutionAttributes executionAttributes) {
    return context.responsePublisher().map(publisher -> subscriber -> {
        ResponseSizeSubscriber responseSizeSubscriber = new ResponseSizeSubscriber(subscriber);
        executionAttributes.putAttributeIfAbsent(SIZE, responseSizeSubscriber);
        publisher.subscribe(responseSizeSubscriber);
    });
}
 
Example 13
Source File: AsyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyAsyncHttpResponseContent_getObjectRequest_checksumEnabled_shouldWrapChecksumValidatingPublisher() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(GetObjectRequest.builder().build(), sdkHttpResponse);
    Optional<Publisher<ByteBuffer>> publisher = interceptor.modifyAsyncHttpResponseContent(modifyHttpResponse,
                                                                                           getExecutionAttributes());
    assertThat(publisher.get()).isExactlyInstanceOf(ChecksumValidatingPublisher.class);
}
 
Example 14
Source File: GetBucketPolicyInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Publisher<ByteBuffer>> modifyAsyncHttpResponseContent(Context.ModifyHttpResponse context,
                                                                      ExecutionAttributes executionAttributes) {
    if (INTERCEPTOR_CONTEXT_PREDICATE.test(context)) {
        return context.responsePublisher().map(
            body -> SdkPublishers.envelopeWrappedPublisher(body, XML_ENVELOPE_PREFIX, XML_ENVELOPE_SUFFIX));
    }

    return context.responsePublisher();
}
 
Example 15
Source File: InterceptorTestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static Context.ModifyHttpResponse modifyHttpResponseContent(SdkRequest request, SdkHttpResponse sdkHttpResponse) {
    InputStream stream = new StringInputStream("hello world");

    return new Context.ModifyResponse() {
        @Override
        public SdkResponse response() {
            return null;
        }

        @Override
        public SdkHttpResponse httpResponse() {
            return sdkHttpResponse;
        }

        @Override
        public Optional<Publisher<ByteBuffer>> responsePublisher() {
            return Optional.empty();
        }

        @Override
        public Optional<InputStream> responseBody() {
            return Optional.of(stream);
        }

        @Override
        public SdkHttpRequest httpRequest() {
            return null;
        }

        @Override
        public Optional<RequestBody> requestBody() {
            return Optional.empty();
        }

        @Override
        public Optional<AsyncRequestBody> asyncRequestBody() {
            return Optional.empty();
        }

        @Override
        public SdkRequest request() {
            return request;
        }
    };
}
 
Example 16
Source File: InterceptorTestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static Context.ModifyHttpResponse modifyHttpResponse(SdkRequest request, SdkHttpResponse sdkHttpResponse) {

        Publisher<ByteBuffer> publisher = new EmptyPublisher<>();
        InputStream responseBody = new StringInputStream("helloworld");

        return new Context.ModifyResponse() {
            @Override
            public SdkResponse response() {
                return null;
            }

            @Override
            public SdkHttpResponse httpResponse() {
                return sdkHttpResponse;
            }

            @Override
            public Optional<Publisher<ByteBuffer>> responsePublisher() {
                return Optional.of(publisher);
            }

            @Override
            public Optional<InputStream> responseBody() {
                return Optional.of(responseBody);
            }

            @Override
            public SdkHttpRequest httpRequest() {
                return SdkHttpRequest.builder().build();
            }

            @Override
            public Optional<RequestBody> requestBody() {
                return Optional.empty();
            }

            @Override
            public Optional<AsyncRequestBody> asyncRequestBody() {
                return Optional.empty();
            }

            @Override
            public SdkRequest request() {
                return request;
            }
        };
    }
 
Example 17
Source File: ExecutionInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public SdkHttpResponse modifyHttpResponse(Context.ModifyHttpResponse context,
                                          ExecutionAttributes executionAttributes) {
    SdkHttpResponse httpResponse = context.httpResponse();
    return httpResponse.copy(b -> b.putHeader("x-amz-integer", Collections.singletonList("3")));
}