From 633dd51a221ebe03ded75d9c4c1c35d56ffeccdb Mon Sep 17 00:00:00 2001 From: Anna-Karin Salander Date: Wed, 9 Oct 2024 10:34:07 -0700 Subject: [PATCH] moving appId to client override config --- .../core/client/builder/SdkClientBuilder.java | 20 ------------ .../builder/SdkDefaultClientBuilder.java | 18 +++-------- .../config/ClientOverrideConfiguration.java | 32 +++++++++++++++++++ .../core/client/config/SdkClientOption.java | 18 +++++++++++ .../useragent/AppIdUserAgentTest.java | 4 ++- 5 files changed, 58 insertions(+), 34 deletions(-) diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkClientBuilder.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkClientBuilder.java index f984105f898b..b798724bde04 100644 --- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkClientBuilder.java +++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkClientBuilder.java @@ -22,7 +22,6 @@ import software.amazon.awssdk.annotations.SdkPublicApi; import software.amazon.awssdk.core.SdkPlugin; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; -import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption; import software.amazon.awssdk.endpoints.EndpointProvider; import software.amazon.awssdk.http.auth.spi.scheme.AuthScheme; import software.amazon.awssdk.utils.builder.SdkBuilder; @@ -97,23 +96,4 @@ default List plugins() { throw new UnsupportedOperationException(); } - /** - * Configure an optional identification value to be appended to the user agent header. - * The value should be less than 50 characters in length and is null by default. - *

- * Users can additionally supply the appId value through environment and JVM settings, and - * it will be resolved using the following order of precedence (highest first): - *

    - *
  1. This client builder configuration
  2. - *
  3. The {@code AWS_SDK_UA_APP_ID} environment variable
  4. - *
  5. The {@code sdk.ua.appId} JVM system property
  6. - *
  7. The {@code sdk_ua_app_id} setting in the profile file for the active profile
  8. - *
- *

- * This configuration option supersedes {@link SdkAdvancedClientOption#USER_AGENT_PREFIX} and - * {@link SdkAdvancedClientOption#USER_AGENT_SUFFIX} and should be used instead of those options. - */ - default B appId(String appId) { - throw new UnsupportedOperationException(); - } } diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkDefaultClientBuilder.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkDefaultClientBuilder.java index 965b060e1eda..6596a7065a7a 100644 --- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkDefaultClientBuilder.java +++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkDefaultClientBuilder.java @@ -48,6 +48,7 @@ import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_STRATEGY; import static software.amazon.awssdk.core.client.config.SdkClientOption.SCHEDULED_EXECUTOR_SERVICE; import static software.amazon.awssdk.core.client.config.SdkClientOption.SYNC_HTTP_CLIENT; +import static software.amazon.awssdk.core.client.config.SdkClientOption.USER_AGENT_APP_ID; import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.APP_ID; import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.HTTP; import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.INTERNAL_METADATA_MARKER; @@ -151,7 +152,6 @@ public abstract class SdkDefaultClientBuilder, private final SdkHttpClient.Builder defaultHttpClientBuilder; private final SdkAsyncHttpClient.Builder defaultAsyncHttpClientBuilder; private final List plugins = new ArrayList<>(); - private String appId; protected SdkDefaultClientBuilder() { @@ -415,7 +415,7 @@ private String resolveClientUserAgent(LazyValueSource config) { SdkClientUserAgentProperties clientProperties = new SdkClientUserAgentProperties(); ClientType clientType = config.get(CLIENT_TYPE); - ClientType resolvedClientType = clientType == null ? ClientType.UNKNOWN : config.get(CLIENT_TYPE); + ClientType resolvedClientType = clientType == null ? ClientType.UNKNOWN : clientType; clientProperties.putProperty(RETRY_MODE, StringUtils.lowerCase(resolveRetryMode(config.get(RETRY_POLICY), config.get(RETRY_STRATEGY)))); @@ -424,7 +424,9 @@ private String resolveClientUserAgent(LazyValueSource config) { clientProperties.putProperty(HTTP, SdkHttpUtils.urlEncode(clientName(resolvedClientType, config.get(SYNC_HTTP_CLIENT), config.get(ASYNC_HTTP_CLIENT)))); - clientProperties.putProperty(APP_ID, appId().orElseGet(() -> resolveAppId(config))); + String appId = config.get(USER_AGENT_APP_ID); + String resolvedAppId = appId == null ? resolveAppId(config) : appId; + clientProperties.putProperty(APP_ID, resolvedAppId); return SdkUserAgentBuilder.buildClientUserAgentString(SystemUserAgent.getOrCreate(), clientProperties); } @@ -457,10 +459,6 @@ private RetryStrategy resolveRetryStrategy(LazyValueSource config) { return SdkDefaultRetryStrategy.forRetryMode(retryMode); } - public Optional appId() { - return Optional.ofNullable(appId); - } - /** * Finalize which sync HTTP client will be used for the created client. */ @@ -658,12 +656,6 @@ public final List plugins() { return Collections.unmodifiableList(plugins); } - @Override - public final B appId(String appId) { - this.appId = appId; - return thisBuilder(); - } - /** * Return "this" for method chaining. */ diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/config/ClientOverrideConfiguration.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/config/ClientOverrideConfiguration.java index 204a695397fc..04c418c1b08d 100644 --- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/config/ClientOverrideConfiguration.java +++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/config/ClientOverrideConfiguration.java @@ -34,6 +34,7 @@ import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_POLICY; import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_STRATEGY; import static software.amazon.awssdk.core.client.config.SdkClientOption.SCHEDULED_EXECUTOR_SERVICE; +import static software.amazon.awssdk.core.client.config.SdkClientOption.USER_AGENT_APP_ID; import static software.amazon.awssdk.utils.ScheduledExecutorUtils.unmanagedScheduledExecutor; import static software.amazon.awssdk.utils.ScheduledExecutorUtils.unwrapUnmanagedScheduledExecutor; @@ -120,6 +121,7 @@ public final class ClientOverrideConfiguration options.add(CONFIGURED_RETRY_STRATEGY); options.add(CONFIGURED_RETRY_CONFIGURATOR); options.add(CONFIGURED_RETRY_MODE); + options.add(USER_AGENT_APP_ID); CLIENT_OVERRIDE_OPTIONS = Collections.unmodifiableSet(options); Set> resolvedOptions = new HashSet<>(); @@ -381,6 +383,14 @@ public Optional compressionConfiguration() { return Optional.ofNullable(compressionConfig); } + /** + * An optional user specified identification value to be appended to the user agent header. + * For more information, see {@link SdkClientOption#USER_AGENT_APP_ID}. + */ + public Optional appId() { + return Optional.ofNullable(config.option(USER_AGENT_APP_ID)); + } + @Override public String toString() { return ToString.builder("ClientOverrideConfiguration") @@ -395,6 +405,7 @@ public String toString() { .add("profileName", defaultProfileName().orElse(null)) .add("scheduledExecutorService", scheduledExecutorService().orElse(null)) .add("compressionConfiguration", compressionConfiguration().orElse(null)) + .add("appId", appId().orElse(null)) .build(); } @@ -757,6 +768,16 @@ default Builder compressionConfiguration(Consumer extends ClientOption { public static final SdkClientOption COMPRESSION_CONFIGURATION = new SdkClientOption<>(CompressionConfiguration.class); + /** + * An optional identification value to be appended to the user agent header. + * The value should be less than 50 characters in length and is null by default. + *

+ * Users can additionally supply the appId value through environment and JVM settings, and + * it will be resolved using the following order of precedence (highest first): + *

    + *
  1. This client option configuration
  2. + *
  3. The {@code AWS_SDK_UA_APP_ID} environment variable
  4. + *
  5. The {@code sdk.ua.appId} JVM system property
  6. + *
  7. The {@code sdk_ua_app_id} setting in the profile file for the active profile
  8. + *
+ *

+ * This configuration option supersedes {@link SdkAdvancedClientOption#USER_AGENT_PREFIX} and + * {@link SdkAdvancedClientOption#USER_AGENT_SUFFIX} and should be used instead of those options. + */ + public static final SdkClientOption USER_AGENT_APP_ID = new SdkClientOption<>(String.class); + /** * Option to specify a reference to the SDK client in use. */ diff --git a/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/useragent/AppIdUserAgentTest.java b/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/useragent/AppIdUserAgentTest.java index 07b413c1848d..58ff279b1a30 100644 --- a/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/useragent/AppIdUserAgentTest.java +++ b/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/useragent/AppIdUserAgentTest.java @@ -29,6 +29,7 @@ import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.SdkSystemSetting; +import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.core.interceptor.Context; import software.amazon.awssdk.core.interceptor.ExecutionAttributes; import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; @@ -62,7 +63,8 @@ void resolveAppIdFromEnvironment(String description, String clientAppId, String RestJsonEndpointProvidersClientBuilder clientBuilder = syncClientBuilder(); if (!StringUtils.isEmpty(clientAppId)) { - clientBuilder.appId(clientAppId); + ClientOverrideConfiguration config = clientBuilder.overrideConfiguration().toBuilder().appId(clientAppId).build(); + clientBuilder.overrideConfiguration(config); } assertThatThrownBy(() -> clientBuilder.build().allTypes(r -> {}))