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.client.config.AwsClientOptionValidation;
22 import software.amazon.awssdk.core.SdkRequest;
23 import software.amazon.awssdk.core.SdkResponse;
24 import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
25 import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
26 import software.amazon.awssdk.core.client.handler.SdkSyncClientHandler;
27 import software.amazon.awssdk.core.client.handler.SyncClientHandler;
28 import software.amazon.awssdk.core.http.Crc32Validation;
29 import software.amazon.awssdk.core.http.ExecutionContext;
30 import software.amazon.awssdk.core.http.HttpResponseHandler;
31 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
32 import software.amazon.awssdk.core.sync.ResponseTransformer;
33 import software.amazon.awssdk.http.SdkHttpFullResponse;
34
35 /**
36  * Client handler for AWS SDK clients.
37  */

38 @ThreadSafe
39 @Immutable
40 @SdkProtectedApi
41 public final class AwsSyncClientHandler extends SdkSyncClientHandler implements SyncClientHandler {
42
43     private final SdkClientConfiguration clientConfiguration;
44
45     public AwsSyncClientHandler(SdkClientConfiguration clientConfiguration) {
46         super(clientConfiguration);
47         this.clientConfiguration = clientConfiguration;
48         AwsClientOptionValidation.validateSyncClientOptions(clientConfiguration);
49     }
50
51     @Override
52     public <InputT extends SdkRequest, OutputT extends SdkResponse> OutputT execute(
53         ClientExecutionParams<InputT, OutputT> executionParams) {
54         ClientExecutionParams<InputT, OutputT> clientExecutionParams = addCrc32Validation(executionParams);
55         return super.execute(clientExecutionParams);
56     }
57
58     @Override
59     public <InputT extends SdkRequest, OutputT extends SdkResponse, ReturnT> ReturnT execute(
60         ClientExecutionParams<InputT, OutputT> executionParams,
61         ResponseTransformer<OutputT, ReturnT> responseTransformer) {
62         return super.execute(executionParams, responseTransformer);
63     }
64
65     @Override
66     protected <InputT extends SdkRequest, OutputT extends SdkResponse> ExecutionContext createExecutionContext(
67         ClientExecutionParams<InputT, OutputT> executionParams, ExecutionAttributes executionAttributes) {
68         return AwsClientHandlerUtils.createExecutionContext(executionParams, clientConfiguration, executionAttributes);
69     }
70
71     private <InputT extends SdkRequest, OutputT> ClientExecutionParams<InputT, OutputT> addCrc32Validation(
72         ClientExecutionParams<InputT, OutputT> executionParams) {
73         if (executionParams.getCombinedResponseHandler() != null) {
74             return executionParams.withCombinedResponseHandler(
75                 new Crc32ValidationResponseHandler<>(executionParams.getCombinedResponseHandler()));
76         } else {
77             return executionParams.withResponseHandler(
78                 new Crc32ValidationResponseHandler<>(executionParams.getResponseHandler()));
79         }
80     }
81
82     /**
83      * Decorate {@link HttpResponseHandler} to validate CRC32 if needed.
84      */

85     private class Crc32ValidationResponseHandler<T> implements HttpResponseHandler<T> {
86         private final HttpResponseHandler<T> delegate;
87
88         private Crc32ValidationResponseHandler(HttpResponseHandler<T> delegate) {
89             this.delegate = delegate;
90         }
91
92         @Override
93         public T handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
94             return delegate.handle(Crc32Validation.validate(isCalculateCrc32FromCompressedData(), response), executionAttributes);
95         }
96     }
97 }
98