• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.awscore.client.handler;
17 
18 import software.amazon.awssdk.annotations.Immutable;
19 import software.amazon.awssdk.annotations.SdkProtectedApi;
20 import software.amazon.awssdk.annotations.ThreadSafe;
21 import software.amazon.awssdk.awscore.internal.AwsExecutionContextBuilder;
22 import software.amazon.awssdk.awscore.internal.client.config.AwsClientOptionValidation;
23 import software.amazon.awssdk.core.SdkRequest;
24 import software.amazon.awssdk.core.SdkResponse;
25 import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
26 import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
27 import software.amazon.awssdk.core.client.handler.SdkSyncClientHandler;
28 import software.amazon.awssdk.core.client.handler.SyncClientHandler;
29 import software.amazon.awssdk.core.http.Crc32Validation;
30 import software.amazon.awssdk.core.http.ExecutionContext;
31 import software.amazon.awssdk.core.http.HttpResponseHandler;
32 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
33 import software.amazon.awssdk.core.sync.ResponseTransformer;
34 import software.amazon.awssdk.http.SdkHttpFullResponse;
35 
36 /**
37  * Client handler for AWS SDK clients.
38  */
39 @ThreadSafe
40 @Immutable
41 @SdkProtectedApi
42 public final class AwsSyncClientHandler extends SdkSyncClientHandler implements SyncClientHandler {
43 
AwsSyncClientHandler(SdkClientConfiguration clientConfiguration)44     public AwsSyncClientHandler(SdkClientConfiguration clientConfiguration) {
45         super(clientConfiguration);
46         AwsClientOptionValidation.validateSyncClientOptions(clientConfiguration);
47     }
48 
49     @Override
execute( ClientExecutionParams<InputT, OutputT> executionParams)50     public <InputT extends SdkRequest, OutputT extends SdkResponse> OutputT execute(
51         ClientExecutionParams<InputT, OutputT> executionParams) {
52         ClientExecutionParams<InputT, OutputT> clientExecutionParams = addCrc32Validation(executionParams);
53         return super.execute(clientExecutionParams);
54     }
55 
56     @Override
execute( ClientExecutionParams<InputT, OutputT> executionParams, ResponseTransformer<OutputT, ReturnT> responseTransformer)57     public <InputT extends SdkRequest, OutputT extends SdkResponse, ReturnT> ReturnT execute(
58         ClientExecutionParams<InputT, OutputT> executionParams,
59         ResponseTransformer<OutputT, ReturnT> responseTransformer) {
60         return super.execute(executionParams, responseTransformer);
61     }
62 
63     @Override
64     protected <InputT extends SdkRequest, OutputT extends SdkResponse> ExecutionContext
invokeInterceptorsAndCreateExecutionContext(ClientExecutionParams<InputT, OutputT> executionParams)65         invokeInterceptorsAndCreateExecutionContext(ClientExecutionParams<InputT, OutputT> executionParams) {
66         SdkClientConfiguration clientConfiguration = resolveRequestConfiguration(executionParams);
67         return AwsExecutionContextBuilder.invokeInterceptorsAndCreateExecutionContext(executionParams, clientConfiguration);
68     }
69 
addCrc32Validation( ClientExecutionParams<InputT, OutputT> executionParams)70     private <InputT extends SdkRequest, OutputT> ClientExecutionParams<InputT, OutputT> addCrc32Validation(
71         ClientExecutionParams<InputT, OutputT> executionParams) {
72         if (executionParams.getCombinedResponseHandler() != null) {
73             return executionParams.withCombinedResponseHandler(
74                 new Crc32ValidationResponseHandler<>(executionParams.getCombinedResponseHandler()));
75         } else {
76             return executionParams.withResponseHandler(
77                 new Crc32ValidationResponseHandler<>(executionParams.getResponseHandler()));
78         }
79     }
80 
81     /**
82      * Decorate {@link HttpResponseHandler} to validate CRC32 if needed.
83      */
84     private class Crc32ValidationResponseHandler<T> implements HttpResponseHandler<T> {
85         private final HttpResponseHandler<T> delegate;
86 
Crc32ValidationResponseHandler(HttpResponseHandler<T> delegate)87         private Crc32ValidationResponseHandler(HttpResponseHandler<T> delegate) {
88             this.delegate = delegate;
89         }
90 
91         @Override
handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes)92         public T handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
93             return delegate.handle(Crc32Validation.validate(isCalculateCrc32FromCompressedData(), response), executionAttributes);
94         }
95     }
96 }
97