Skip to content

Commit 633dd51

Browse files
committed
moving appId to client override config
1 parent 3819b2d commit 633dd51

File tree

5 files changed

+58
-34
lines changed

5 files changed

+58
-34
lines changed

core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkClientBuilder.java

-20
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import software.amazon.awssdk.annotations.SdkPublicApi;
2323
import software.amazon.awssdk.core.SdkPlugin;
2424
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
25-
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
2625
import software.amazon.awssdk.endpoints.EndpointProvider;
2726
import software.amazon.awssdk.http.auth.spi.scheme.AuthScheme;
2827
import software.amazon.awssdk.utils.builder.SdkBuilder;
@@ -97,23 +96,4 @@ default List<SdkPlugin> plugins() {
9796
throw new UnsupportedOperationException();
9897
}
9998

100-
/**
101-
* Configure an optional identification value to be appended to the user agent header.
102-
* The value should be less than 50 characters in length and is null by default.
103-
* <p>
104-
* Users can additionally supply the appId value through environment and JVM settings, and
105-
* it will be resolved using the following order of precedence (highest first):
106-
* <ol>
107-
* <li>This client builder configuration </li>
108-
* <li>The {@code AWS_SDK_UA_APP_ID} environment variable</li>
109-
* <li>The {@code sdk.ua.appId} JVM system property</li>
110-
* <li>The {@code sdk_ua_app_id} setting in the profile file for the active profile</li>
111-
* </ol>
112-
* <p>
113-
* This configuration option supersedes {@link SdkAdvancedClientOption#USER_AGENT_PREFIX} and
114-
* {@link SdkAdvancedClientOption#USER_AGENT_SUFFIX} and should be used instead of those options.
115-
*/
116-
default B appId(String appId) {
117-
throw new UnsupportedOperationException();
118-
}
11999
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkDefaultClientBuilder.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_STRATEGY;
4949
import static software.amazon.awssdk.core.client.config.SdkClientOption.SCHEDULED_EXECUTOR_SERVICE;
5050
import static software.amazon.awssdk.core.client.config.SdkClientOption.SYNC_HTTP_CLIENT;
51+
import static software.amazon.awssdk.core.client.config.SdkClientOption.USER_AGENT_APP_ID;
5152
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.APP_ID;
5253
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.HTTP;
5354
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.INTERNAL_METADATA_MARKER;
@@ -151,7 +152,6 @@ public abstract class SdkDefaultClientBuilder<B extends SdkClientBuilder<B, C>,
151152
private final SdkHttpClient.Builder defaultHttpClientBuilder;
152153
private final SdkAsyncHttpClient.Builder defaultAsyncHttpClientBuilder;
153154
private final List<SdkPlugin> plugins = new ArrayList<>();
154-
private String appId;
155155

156156

157157
protected SdkDefaultClientBuilder() {
@@ -415,7 +415,7 @@ private String resolveClientUserAgent(LazyValueSource config) {
415415
SdkClientUserAgentProperties clientProperties = new SdkClientUserAgentProperties();
416416

417417
ClientType clientType = config.get(CLIENT_TYPE);
418-
ClientType resolvedClientType = clientType == null ? ClientType.UNKNOWN : config.get(CLIENT_TYPE);
418+
ClientType resolvedClientType = clientType == null ? ClientType.UNKNOWN : clientType;
419419

420420
clientProperties.putProperty(RETRY_MODE, StringUtils.lowerCase(resolveRetryMode(config.get(RETRY_POLICY),
421421
config.get(RETRY_STRATEGY))));
@@ -424,7 +424,9 @@ private String resolveClientUserAgent(LazyValueSource config) {
424424
clientProperties.putProperty(HTTP, SdkHttpUtils.urlEncode(clientName(resolvedClientType,
425425
config.get(SYNC_HTTP_CLIENT),
426426
config.get(ASYNC_HTTP_CLIENT))));
427-
clientProperties.putProperty(APP_ID, appId().orElseGet(() -> resolveAppId(config)));
427+
String appId = config.get(USER_AGENT_APP_ID);
428+
String resolvedAppId = appId == null ? resolveAppId(config) : appId;
429+
clientProperties.putProperty(APP_ID, resolvedAppId);
428430
return SdkUserAgentBuilder.buildClientUserAgentString(SystemUserAgent.getOrCreate(), clientProperties);
429431
}
430432

@@ -457,10 +459,6 @@ private RetryStrategy resolveRetryStrategy(LazyValueSource config) {
457459
return SdkDefaultRetryStrategy.forRetryMode(retryMode);
458460
}
459461

460-
public Optional<String> appId() {
461-
return Optional.ofNullable(appId);
462-
}
463-
464462
/**
465463
* Finalize which sync HTTP client will be used for the created client.
466464
*/
@@ -658,12 +656,6 @@ public final List<SdkPlugin> plugins() {
658656
return Collections.unmodifiableList(plugins);
659657
}
660658

661-
@Override
662-
public final B appId(String appId) {
663-
this.appId = appId;
664-
return thisBuilder();
665-
}
666-
667659
/**
668660
* Return "this" for method chaining.
669661
*/

core/sdk-core/src/main/java/software/amazon/awssdk/core/client/config/ClientOverrideConfiguration.java

+32
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_POLICY;
3535
import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_STRATEGY;
3636
import static software.amazon.awssdk.core.client.config.SdkClientOption.SCHEDULED_EXECUTOR_SERVICE;
37+
import static software.amazon.awssdk.core.client.config.SdkClientOption.USER_AGENT_APP_ID;
3738
import static software.amazon.awssdk.utils.ScheduledExecutorUtils.unmanagedScheduledExecutor;
3839
import static software.amazon.awssdk.utils.ScheduledExecutorUtils.unwrapUnmanagedScheduledExecutor;
3940

@@ -120,6 +121,7 @@ public final class ClientOverrideConfiguration
120121
options.add(CONFIGURED_RETRY_STRATEGY);
121122
options.add(CONFIGURED_RETRY_CONFIGURATOR);
122123
options.add(CONFIGURED_RETRY_MODE);
124+
options.add(USER_AGENT_APP_ID);
123125
CLIENT_OVERRIDE_OPTIONS = Collections.unmodifiableSet(options);
124126

125127
Set<ClientOption<?>> resolvedOptions = new HashSet<>();
@@ -381,6 +383,14 @@ public Optional<CompressionConfiguration> compressionConfiguration() {
381383
return Optional.ofNullable(compressionConfig);
382384
}
383385

386+
/**
387+
* An optional user specified identification value to be appended to the user agent header.
388+
* For more information, see {@link SdkClientOption#USER_AGENT_APP_ID}.
389+
*/
390+
public Optional<String> appId() {
391+
return Optional.ofNullable(config.option(USER_AGENT_APP_ID));
392+
}
393+
384394
@Override
385395
public String toString() {
386396
return ToString.builder("ClientOverrideConfiguration")
@@ -395,6 +405,7 @@ public String toString() {
395405
.add("profileName", defaultProfileName().orElse(null))
396406
.add("scheduledExecutorService", scheduledExecutorService().orElse(null))
397407
.add("compressionConfiguration", compressionConfiguration().orElse(null))
408+
.add("appId", appId().orElse(null))
398409
.build();
399410
}
400411

@@ -757,6 +768,16 @@ default Builder compressionConfiguration(Consumer<CompressionConfiguration.Build
757768
}
758769

759770
CompressionConfiguration compressionConfiguration();
771+
772+
/**
773+
* Sets the appId for this client. See {@link SdkClientOption#USER_AGENT_APP_ID}.
774+
*/
775+
Builder appId(String appId);
776+
777+
/**
778+
* The appId for this client. See {@link SdkClientOption#USER_AGENT_APP_ID}.
779+
*/
780+
String appId();
760781
}
761782

762783
/**
@@ -1089,6 +1110,17 @@ public CompressionConfiguration compressionConfiguration() {
10891110
return config.option(CONFIGURED_COMPRESSION_CONFIGURATION);
10901111
}
10911112

1113+
@Override
1114+
public String appId() {
1115+
return config.option(USER_AGENT_APP_ID);
1116+
}
1117+
1118+
@Override
1119+
public Builder appId(String appId) {
1120+
config.option(USER_AGENT_APP_ID, appId);
1121+
return this;
1122+
}
1123+
10921124
@Override
10931125
public ClientOverrideConfiguration build() {
10941126
return new ClientOverrideConfiguration(config.build(), resolvedConfig.build());

core/sdk-core/src/main/java/software/amazon/awssdk/core/client/config/SdkClientOption.java

+18
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,24 @@ public final class SdkClientOption<T> extends ClientOption<T> {
319319
public static final SdkClientOption<CompressionConfiguration> COMPRESSION_CONFIGURATION =
320320
new SdkClientOption<>(CompressionConfiguration.class);
321321

322+
/**
323+
* An optional identification value to be appended to the user agent header.
324+
* The value should be less than 50 characters in length and is null by default.
325+
* <p>
326+
* Users can additionally supply the appId value through environment and JVM settings, and
327+
* it will be resolved using the following order of precedence (highest first):
328+
* <ol>
329+
* <li>This client option configuration </li>
330+
* <li>The {@code AWS_SDK_UA_APP_ID} environment variable</li>
331+
* <li>The {@code sdk.ua.appId} JVM system property</li>
332+
* <li>The {@code sdk_ua_app_id} setting in the profile file for the active profile</li>
333+
* </ol>
334+
* <p>
335+
* This configuration option supersedes {@link SdkAdvancedClientOption#USER_AGENT_PREFIX} and
336+
* {@link SdkAdvancedClientOption#USER_AGENT_SUFFIX} and should be used instead of those options.
337+
*/
338+
public static final SdkClientOption<String> USER_AGENT_APP_ID = new SdkClientOption<>(String.class);
339+
322340
/**
323341
* Option to specify a reference to the SDK client in use.
324342
*/

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/useragent/AppIdUserAgentTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
3030
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
3131
import software.amazon.awssdk.core.SdkSystemSetting;
32+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
3233
import software.amazon.awssdk.core.interceptor.Context;
3334
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
3435
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
@@ -62,7 +63,8 @@ void resolveAppIdFromEnvironment(String description, String clientAppId, String
6263
RestJsonEndpointProvidersClientBuilder clientBuilder = syncClientBuilder();
6364

6465
if (!StringUtils.isEmpty(clientAppId)) {
65-
clientBuilder.appId(clientAppId);
66+
ClientOverrideConfiguration config = clientBuilder.overrideConfiguration().toBuilder().appId(clientAppId).build();
67+
clientBuilder.overrideConfiguration(config);
6668
}
6769

6870
assertThatThrownBy(() -> clientBuilder.build().allTypes(r -> {}))

0 commit comments

Comments
 (0)