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

The following examples show how to use software.amazon.awssdk.core.interceptor.Context#ModifyResponse . 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: DecodeUrlEncodedResponseInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context,
                                  ExecutionAttributes executionAttributes) {
    SdkResponse response = context.response();
    if (shouldHandle(response)) {
        if (response instanceof ListObjectsResponse) {
            return modifyListObjectsResponse((ListObjectsResponse) response);
        }

        if (response instanceof ListObjectsV2Response) {
            return modifyListObjectsV2Response((ListObjectsV2Response) response);
        }

        if (response instanceof ListObjectVersionsResponse) {
            return modifyListObjectVersionsResponse((ListObjectVersionsResponse) response);
        }

        if (response instanceof ListMultipartUploadsResponse) {
            return modifyListMultipartUploadsResponse((ListMultipartUploadsResponse) response);
        }
    }
    return response;
}
 
Example 2
Source File: EnableTrailingChecksumInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Subtract the contentLength of {@link GetObjectResponse} if trailing checksums is enabled.
 */
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
    SdkResponse response = context.response();
    SdkHttpResponse httpResponse = context.httpResponse();

    if (getObjectChecksumEnabledPerResponse(context.request(), httpResponse)) {
        GetObjectResponse getResponse = (GetObjectResponse) response;
        Long contentLength = getResponse.contentLength();
        Validate.notNull(contentLength, "Service returned null 'Content-Length'.");
        return getResponse.toBuilder()
                          .contentLength(contentLength - S3_MD5_CHECKSUM_LENGTH)
                          .build();
    }

    return response;
}
 
Example 3
Source File: EnableTrailingChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void modifyResponse_getObjectResponseContainsChecksumHeader_shouldModifyResponse() {
    long contentLength = 50;
    GetObjectResponse response = GetObjectResponse.builder().contentLength(contentLength).build();
    Context.ModifyResponse modifyResponseContext = modifyResponseContext(
        GetObjectRequest.builder().build(),
        response,
        SdkHttpFullResponse.builder()
                           .putHeader(CHECKSUM_ENABLED_RESPONSE_HEADER, ENABLE_MD5_CHECKSUM_HEADER_VALUE).build());

    GetObjectResponse actualResponse = (GetObjectResponse) interceptor.modifyResponse(modifyResponseContext,
                                                                                      new ExecutionAttributes());

    assertThat(actualResponse).isNotEqualTo(response);
    assertThat(actualResponse.contentLength()).isEqualTo(contentLength - S3_MD5_CHECKSUM_LENGTH);
}
 
Example 4
Source File: ExecutionInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
    MembersInHeadersResponse response = (MembersInHeadersResponse) context.response();
    return response.toBuilder()
            .stringMember("4")
            .build();
}
 
Example 5
Source File: MessageAttributesIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
    if (context.response() instanceof SendMessageResponse) {
        return ((SendMessageResponse) context.response()).toBuilder()
                                                         .md5OfMessageBody("invalid-md5")
                                                         .build();
    }
    return context.response();
}
 
Example 6
Source File: EnableTrailingChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyResponse_nonGetObjectResponse_shouldNotModifyResponse() {
    GetObjectAclResponse getObjectAclResponse = GetObjectAclResponse.builder().build();
    Context.ModifyResponse modifyResponseContext = modifyResponseContext(
        GetObjectAclRequest.builder().build(),
        getObjectAclResponse,
        SdkHttpFullResponse.builder().build());

    GetObjectAclResponse actualResponse = (GetObjectAclResponse) interceptor.modifyResponse(modifyResponseContext,
                                                                                            new ExecutionAttributes());
    assertThat(actualResponse).isEqualTo(getObjectAclResponse);
}
 
Example 7
Source File: EnableTrailingChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyResponse_getObjectResponseNoChecksumHeader_shouldNotModifyResponse() {
    long contentLength = 50;
    GetObjectResponse response = GetObjectResponse.builder().contentLength(contentLength).build();
    Context.ModifyResponse modifyResponseContext = modifyResponseContext(
        GetObjectRequest.builder().build(),
        response,
        SdkHttpFullResponse.builder().build());

    GetObjectResponse actualResponse = (GetObjectResponse) interceptor.modifyResponse(modifyResponseContext,
                                                                                      new ExecutionAttributes());
    assertThat(actualResponse).isEqualTo(response);
}
 
Example 8
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void otherResponses_shouldNotModifyResponse() {
    HeadObjectResponse original = HeadObjectResponse.builder().build();
    Context.ModifyResponse ctx = newContext(original);
    SdkResponse sdkResponse = INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());
    assertThat(original.hashCode()).isEqualTo(sdkResponse.hashCode());
}
 
Example 9
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void encodingTypeNotSet_doesNotDecodeListObjectsV2ResponseParts() {
    ListObjectsV2Response original = V2_TEST_ENCODED_RESPONSE.toBuilder()
            .encodingType((String) null)
            .build();

    Context.ModifyResponse ctx = newContext(original);

    ListObjectsV2Response fromInterceptor = (ListObjectsV2Response) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());

    assertThat(fromInterceptor).isEqualTo(original);
}
 
Example 10
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void encodingTypeNotSet_doesNotDecodeListObjectsResponseParts() {
    ListObjectsResponse original = V1_TEST_ENCODED_RESPONSE.toBuilder()
            .encodingType((String) null)
            .build();

    Context.ModifyResponse ctx = newContext(original);

    ListObjectsResponse fromInterceptor = (ListObjectsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());

    assertThat(fromInterceptor).isEqualTo(original);
}
 
Example 11
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void encodingTypeSet_decodesListMultipartUploadsResponse() {
    Context.ModifyResponse ctx = newContext(TEST_LIST_MULTIPART_UPLOADS_RESPONSE);

    ListMultipartUploadsResponse decoded = (ListMultipartUploadsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());

    assertDecoded(decoded::delimiter, " delimiter");
    assertDecoded(decoded::prefix, " prefix");
    assertDecoded(decoded::keyMarker, " keyMarker");
    assertDecoded(decoded::nextKeyMarker, " nextKeyMarker");
    assertCommonPrefixesAreDecoded(decoded.commonPrefixes());
    assertUploadsAreDecoded(decoded.uploads());
    assertCommonPrefixesAreDecoded(decoded.commonPrefixes());
}
 
Example 12
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void encodingTypeSet_decodesListObjectVersionsResponse() {
    Context.ModifyResponse ctx = newContext(TEST_LIST_OBJECT_VERSION_RESPONSE);

    ListObjectVersionsResponse decoded = (ListObjectVersionsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());

    assertDecoded(decoded::delimiter, " delimiter");
    assertDecoded(decoded::prefix, " prefix");
    assertDecoded(decoded::keyMarker, " keyMarker");
    assertDecoded(decoded::nextKeyMarker, " nextKeyMarker");
    assertCommonPrefixesAreDecoded(decoded.commonPrefixes());
    assertVersionsAreDecoded(decoded.versions());
}
 
Example 13
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void encodingTypeSet_decodesListObjectsV2ResponseParts() {
    Context.ModifyResponse ctx = newContext(V2_TEST_ENCODED_RESPONSE);

    ListObjectsV2Response decoded = (ListObjectsV2Response) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());

    assertDecoded(decoded::delimiter, " delimiter");
    assertDecoded(decoded::prefix, " prefix");
    assertDecoded(decoded::startAfter, " startafter");
    assertKeysAreDecoded(decoded.contents());
    assertCommonPrefixesAreDecoded(decoded.commonPrefixes());
}
 
Example 14
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void encodingTypeSet_decodesListObjectsResponseParts() {
    Context.ModifyResponse ctx = newContext(V1_TEST_ENCODED_RESPONSE);

    ListObjectsResponse decoded = (ListObjectsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());

    assertDecoded(decoded::delimiter, " delimiter");
    assertDecoded(decoded::nextMarker, " nextmarker");
    assertDecoded(decoded::prefix, " prefix");
    assertDecoded(decoded::marker, " marker");
    assertKeysAreDecoded(decoded.contents());
    assertCommonPrefixesAreDecoded(decoded.commonPrefixes());
}
 
Example 15
Source File: GetObjectAsyncIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
    return ((GetObjectResponse) context.response())
            .toBuilder()
            .metadata(ImmutableMap.of("x-amz-assert", "injected-value"))
            .build();
}
 
Example 16
Source File: GetObjectInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
    SdkResponse response = context.response();
    if (!(response instanceof GetObjectResponse)) {
        return response;
    }

    return fixContentRange(response, context.httpResponse());
}
 
Example 17
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 18
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private static Context.ModifyResponse newContext(SdkResponse response) {
    return new Context.ModifyResponse() {
        @Override
        public Optional<Publisher<ByteBuffer>> responsePublisher() {
            return null;
        }

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

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

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

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

        @Override
        public SdkResponse response() {
            return response;
        }

        @Override
        public SdkHttpFullResponse httpResponse() {
            return null;
        }

        @Override
        public SdkRequest request() {
            return null;
        }
    };
}
 
Example 19
Source File: InterceptorTestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static Context.ModifyResponse modifyResponseContext(SdkRequest request, SdkResponse response, SdkHttpResponse sdkHttpResponse) {
    return new Context.ModifyResponse() {
        @Override
        public SdkResponse response() {
            return response;
        }

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

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

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

        @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 20
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;
            }
        };
    }