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.loader;
17
18 import software.amazon.awssdk.annotations.SdkInternalApi;
19 import software.amazon.awssdk.core.exception.SdkClientException;
20 import software.amazon.awssdk.http.SdkHttpClient;
21 import software.amazon.awssdk.http.SdkHttpService;
22 import software.amazon.awssdk.utils.AttributeMap;
23
24 /**
25  * Utility to load the default HTTP client factory and create an instance of {@link SdkHttpClient}.
26  */

27 @SdkInternalApi
28 public final class DefaultSdkHttpClientBuilder implements SdkHttpClient.Builder {
29
30     private static final SdkHttpServiceProvider<SdkHttpService> DEFAULT_CHAIN = new CachingSdkHttpServiceProvider<>(
31             new SdkHttpServiceProviderChain<>(
32                     SystemPropertyHttpServiceProvider.syncProvider(),
33                     ClasspathSdkHttpServiceProvider.syncProvider()
34             ));
35
36     @Override
37     public SdkHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
38         // TODO We create and build every time. Do we want to cache it instead of the service binding?
39         return DEFAULT_CHAIN
40                 .loadService()
41                 .map(SdkHttpService::createHttpClientBuilder)
42                 .map(f -> f.buildWithDefaults(serviceDefaults))
43                 .orElseThrow(
44                     () -> SdkClientException.builder()
45                                             .message("Unable to load an HTTP implementation from any provider in the " +
46                                                      "chain. You must declare a dependency on an appropriate HTTP " +
47                                                      "implementation or pass in an SdkHttpClient explicitly to the " +
48                                                      "client builder.")
49                                             .build());
50     }
51
52 }
53