• 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.core;
17 
18 import java.net.URI;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.Optional;
22 import java.util.function.Consumer;
23 import software.amazon.awssdk.annotations.SdkPublicApi;
24 import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
25 import software.amazon.awssdk.endpoints.EndpointProvider;
26 import software.amazon.awssdk.http.auth.spi.scheme.AuthScheme;
27 
28 /**
29  * Class to expose SDK service client settings to the user, e.g., ClientOverrideConfiguration
30  */
31 @SdkPublicApi
32 public abstract class SdkServiceClientConfiguration {
33 
34     private final ClientOverrideConfiguration overrideConfiguration;
35     private final URI endpointOverride;
36     private final EndpointProvider endpointProvider;
37     private final Map<String, AuthScheme<?>> authSchemes;
38 
SdkServiceClientConfiguration(Builder builder)39     protected SdkServiceClientConfiguration(Builder builder) {
40         this.overrideConfiguration = builder.overrideConfiguration();
41         this.endpointOverride = builder.endpointOverride();
42         this.endpointProvider = builder.endpointProvider();
43         this.authSchemes = builder.authSchemes();
44     }
45 
46     /**
47      *
48      * @return The ClientOverrideConfiguration of the SdkClient. If this is not set, an ClientOverrideConfiguration object will
49      * still be returned, with empty fields
50      */
overrideConfiguration()51     public ClientOverrideConfiguration overrideConfiguration() {
52         return this.overrideConfiguration;
53     }
54 
55     /**
56      *
57      * @return The configured endpoint override of the SdkClient. If the endpoint was not overridden, an empty Optional will be
58      * returned
59      */
endpointOverride()60     public Optional<URI> endpointOverride() {
61         return Optional.ofNullable(this.endpointOverride);
62     }
63 
64     /**
65      *
66      * @return The configured endpoint provider of the SdkClient. If the endpoint provider was not configured, the default
67      * endpoint provider will be returned.
68      */
endpointProvider()69     public Optional<EndpointProvider> endpointProvider() {
70         return Optional.ofNullable(this.endpointProvider);
71     }
72 
73     /**
74      * @return The configured map of auth schemes.
75      */
authSchemes()76     public Map<String, AuthScheme<?>> authSchemes() {
77         return authSchemes;
78     }
79 
80     @Override
equals(Object o)81     public boolean equals(Object o) {
82         if (this == o) {
83             return true;
84         }
85         if (o == null || getClass() != o.getClass()) {
86             return false;
87         }
88 
89         SdkServiceClientConfiguration serviceClientConfiguration = (SdkServiceClientConfiguration) o;
90         return Objects.equals(overrideConfiguration, serviceClientConfiguration.overrideConfiguration())
91                && Objects.equals(endpointOverride, serviceClientConfiguration.endpointOverride().orElse(null))
92                && Objects.equals(endpointProvider, serviceClientConfiguration.endpointProvider().orElse(null))
93                && Objects.equals(authSchemes, serviceClientConfiguration.authSchemes);
94     }
95 
96     @Override
hashCode()97     public int hashCode() {
98         int result = overrideConfiguration != null ? overrideConfiguration.hashCode() : 0;
99         result = 31 * result + (endpointOverride != null ? endpointOverride.hashCode() : 0);
100         result = 31 * result + (endpointProvider != null ? endpointProvider.hashCode() : 0);
101         result = 31 * result + (authSchemes != null ? authSchemes.hashCode() : 0);
102         return result;
103     }
104 
105     /**
106      * The base interface for all SDK service client configuration builders
107      */
108     public interface Builder {
109         /**
110          * Return the client override configuration
111          */
overrideConfiguration()112         default ClientOverrideConfiguration overrideConfiguration() {
113             throw new UnsupportedOperationException();
114         }
115 
116         /**
117          * Return the endpoint override
118          */
endpointOverride()119         default URI endpointOverride() {
120             throw new UnsupportedOperationException();
121         }
122 
endpointProvider()123         default EndpointProvider endpointProvider() {
124             throw new UnsupportedOperationException();
125         }
126 
127         /**
128          * Configure the client override configuration
129          */
overrideConfiguration(ClientOverrideConfiguration clientOverrideConfiguration)130         default Builder overrideConfiguration(ClientOverrideConfiguration clientOverrideConfiguration) {
131             throw new UnsupportedOperationException();
132         }
133 
overrideConfiguration(Consumer<ClientOverrideConfiguration.Builder> consumer)134         default Builder overrideConfiguration(Consumer<ClientOverrideConfiguration.Builder> consumer) {
135             ClientOverrideConfiguration overrideConfiguration = overrideConfiguration();
136             ClientOverrideConfiguration.Builder builder;
137             if (overrideConfiguration != null) {
138                 builder = overrideConfiguration.toBuilder();
139             } else {
140                 builder = ClientOverrideConfiguration.builder();
141             }
142             consumer.accept(builder);
143             return overrideConfiguration(builder.build());
144         }
145 
146         /**
147          * Configure the endpoint override
148          */
endpointOverride(URI endpointOverride)149         default Builder endpointOverride(URI endpointOverride) {
150             throw new UnsupportedOperationException();
151         }
152 
153 
endpointProvider(EndpointProvider endpointProvider)154         default Builder endpointProvider(EndpointProvider endpointProvider) {
155             throw new UnsupportedOperationException();
156         }
157 
158         /**
159          * Adds the given auth scheme. Replaces an existing auth scheme with the same id.
160          */
putAuthScheme(AuthScheme<?> authScheme)161         default Builder putAuthScheme(AuthScheme<?> authScheme) {
162             throw new UnsupportedOperationException();
163         }
164 
165         /**
166          * Returns the configured map of auth schemes.
167          */
authSchemes()168         default Map<String, AuthScheme<?>> authSchemes() {
169             throw new UnsupportedOperationException();
170         }
171 
172         /**
173          * Build the service client configuration using the configuration on this builder
174          */
build()175         SdkServiceClientConfiguration build();
176     }
177 }
178