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.core.internal.http.pipeline.stages;
17
18 import software.amazon.awssdk.annotations.SdkInternalApi;
19 import software.amazon.awssdk.core.client.config.SdkClientOption;
20 import software.amazon.awssdk.core.internal.http.HttpClientDependencies;
21 import software.amazon.awssdk.core.internal.http.InterruptMonitor;
22 import software.amazon.awssdk.core.internal.http.RequestExecutionContext;
23 import software.amazon.awssdk.core.internal.http.pipeline.RequestPipeline;
24 import software.amazon.awssdk.http.ExecutableHttpRequest;
25 import software.amazon.awssdk.http.HttpExecuteRequest;
26 import software.amazon.awssdk.http.HttpExecuteResponse;
27 import software.amazon.awssdk.http.SdkHttpClient;
28 import software.amazon.awssdk.http.SdkHttpFullRequest;
29 import software.amazon.awssdk.http.SdkHttpFullResponse;
30 import software.amazon.awssdk.utils.Pair;
31
32 /**
33  * Delegate to the HTTP implementation to make an HTTP request and receive the response.
34  */

35 @SdkInternalApi
36 public class MakeHttpRequestStage
37     implements RequestPipeline<SdkHttpFullRequest, Pair<SdkHttpFullRequest, SdkHttpFullResponse>> {
38
39     private final SdkHttpClient sdkHttpClient;
40
41     public MakeHttpRequestStage(HttpClientDependencies dependencies) {
42         this.sdkHttpClient = dependencies.clientConfiguration().option(SdkClientOption.SYNC_HTTP_CLIENT);
43     }
44
45     /**
46      * Returns the response from executing one httpClientSettings request; or null for retry.
47      */

48     public Pair<SdkHttpFullRequest, SdkHttpFullResponse> execute(SdkHttpFullRequest request,
49                                                                  RequestExecutionContext context) throws Exception {
50         InterruptMonitor.checkInterrupted();
51         HttpExecuteResponse executeResponse = executeHttpRequest(request, context);
52         // TODO: Plumb through ExecuteResponse instead
53         SdkHttpFullResponse httpResponse = (SdkHttpFullResponse) executeResponse.httpResponse();
54         return Pair.of(request, httpResponse.toBuilder().content(executeResponse.responseBody().orElse(null)).build());
55     }
56
57     private HttpExecuteResponse executeHttpRequest(SdkHttpFullRequest request, RequestExecutionContext context) throws Exception {
58         ExecutableHttpRequest requestCallable = sdkHttpClient
59             .prepareRequest(HttpExecuteRequest.builder()
60                                               .request(request)
61                                               .contentStreamProvider(request.contentStreamProvider().orElse(null))
62                                               .build());
63
64         context.apiCallTimeoutTracker().abortable(requestCallable);
65         context.apiCallAttemptTimeoutTracker().abortable(requestCallable);
66         return requestCallable.call();
67     }
68 }
69