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.async.SdkAsyncHttpClient;
22 import software.amazon.awssdk.http.async.SdkAsyncHttpService;
23 import software.amazon.awssdk.utils.AttributeMap;
24
25 /**
26  * Utility to load the default HTTP client factory and create an instance of {@link SdkHttpClient}.
27  */

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