From 76f0bf3a98e5ed190d748ae79bec3b2b0bdbdfea Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Mon, 13 Jul 2020 17:09:42 +0800 Subject: [PATCH 1/7] update user-agent --- sdk/management/README.md | 4 +- sdk/management/pom.xml | 9 +-- sdk/resources/mgmt/pom.xml | 4 -- .../fluentcore/AzureServiceClient.java | 28 +--------- .../fluentcore/policy/UserAgentPolicy.java | 55 +------------------ 5 files changed, 9 insertions(+), 91 deletions(-) diff --git a/sdk/management/README.md b/sdk/management/README.md index 647a346676d1..e9c69857cac1 100644 --- a/sdk/management/README.md +++ b/sdk/management/README.md @@ -46,7 +46,7 @@ Azure Management Libraries require a `TokenCredential` implementation for authen com.azure azure-identity - 1.1.0-beta.5 + 1.0.8 ``` [//]: # ({x-version-update-end}) @@ -58,7 +58,7 @@ Azure Management Libraries require a `TokenCredential` implementation for authen com.azure azure-core-http-netty - 1.5.2 + 1.5.3 ``` [//]: # ({x-version-update-end}) diff --git a/sdk/management/pom.xml b/sdk/management/pom.xml index a290e23ca8e7..3e1c72a73bdb 100644 --- a/sdk/management/pom.xml +++ b/sdk/management/pom.xml @@ -55,11 +55,6 @@ - - com.azure - azure-core - 1.5.0 - com.azure azure-core-management @@ -113,13 +108,13 @@ com.azure azure-core-http-netty - 1.4.0 + 1.5.3 test com.azure azure-identity - 1.1.0-beta.3 + 1.0.8 test diff --git a/sdk/resources/mgmt/pom.xml b/sdk/resources/mgmt/pom.xml index 0894610293ef..2e7e2628ede5 100644 --- a/sdk/resources/mgmt/pom.xml +++ b/sdk/resources/mgmt/pom.xml @@ -47,10 +47,6 @@ - - com.azure - azure-core - com.azure azure-core-management diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java index 35c6193d74c4..36e6a93b9b92 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java @@ -12,7 +12,6 @@ import com.azure.core.management.polling.PollResult; import com.azure.core.management.serializer.AzureJacksonAdapter; import com.azure.core.util.Context; -import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; import com.azure.core.util.polling.AsyncPollResponse; import com.azure.core.util.polling.LongRunningOperationStatus; @@ -50,20 +49,8 @@ protected AzureServiceClient(HttpPipeline httpPipeline, AzureEnvironment environ ((AzureJacksonAdapter) serializerAdapter).serializer().registerModule(DateTimeDeserializer.getModule()); } - private static final String OS; - private static final String OS_NAME; - private static final String OS_VERSION; - private static final String JAVA_VERSION; private static final String SDK_VERSION = "2.0.0-SNAPSHOT"; - static { - OS_NAME = System.getProperty("os.name"); - OS_VERSION = System.getProperty("os.version"); - OS = OS_NAME + "/" + OS_VERSION; - String version = System.getProperty("java.version"); - JAVA_VERSION = version != null ? version : "Unknown"; - } - private final SerializerAdapter serializerAdapter = new AzureJacksonAdapter(); private String sdkName; @@ -83,20 +70,9 @@ public SerializerAdapter getSerializerAdapter() { * @return the default client context. */ public Context getContext() { - Context context = new Context("java.version", JAVA_VERSION); - if (!CoreUtils.isNullOrEmpty(OS_NAME)) { - context = context.addData("os.name", OS_NAME); - } - if (!CoreUtils.isNullOrEmpty(OS_VERSION)) { - context = context.addData("os.version", OS_VERSION); - } + Context context = Context.NONE; if (sdkName == null) { - String packageName = this.getClass().getPackage().getName(); - if (packageName.endsWith(".models")) { - sdkName = packageName.substring(0, packageName.length() - ".models".length()); - } else { - sdkName = packageName; - } + sdkName = this.getClass().getPackage().getName(); } context = context.addData("Sdk-Name", sdkName); context = context.addData("Sdk-Version", SDK_VERSION); diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java index fb907e2bebf9..440da636b175 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java @@ -10,6 +10,7 @@ import com.azure.core.http.policy.HttpPipelinePolicy; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; +import com.azure.core.util.UserAgentUtil; import reactor.core.publisher.Mono; /** @@ -21,36 +22,12 @@ public class UserAgentPolicy implements HttpPipelinePolicy { private static final String SDK_VERSION_KEY = "Sdk-Version"; private static final String APPLICATION_ID_KEY = "Application-Id"; - private static final String OS_NAME = System.getProperty("os.name"); - private static final String OS_VERSION = System.getProperty("os.version"); - private static final String JAVA_VERSION = System.getProperty("java.version"); - - /* - * The base User-Agent header format is azsdk-java-/. Additional information such as the - * application ID will be prepended and platform telemetry will be appended, a fully configured User-Agent header - * format is azsdk-java-/ . - */ - private static final String DEFAULT_USER_AGENT_FORMAT = "azsdk-java-%s/%s"; - - // From the design guidelines, the platform info format is: - // ; - private static final String PLATFORM_INFO_FORMAT = "%s; %s %s"; - private final String defaultSdkName = this.getClass().getPackage().getName(); private final String defaultSdkVersion = this.getClass().getPackage().getSpecificationVersion(); private final HttpLogOptions httpLogOptions; private final Configuration configuration; - private static final UserAgentPolicy DEFAULT_USER_AGENT_POLICY = new UserAgentPolicy(null, null); - - /** - * @return default user agent policy - */ - public static UserAgentPolicy getDefaultUserAgentPolicy() { - return DEFAULT_USER_AGENT_POLICY; - } - /** * @param httpLogOptions used for get application id * @param configuration used for check telemetry enable or not @@ -69,33 +46,6 @@ public UserAgentPolicy(HttpLogOptions httpLogOptions, Configuration configuratio } } - protected String buildUserAgent(String applicationId, String sdkName, String sdkVersion) { - StringBuilder userAgentBuilder = new StringBuilder(); - - // Only add the application ID if it is present as it is optional. - if (applicationId != null) { - userAgentBuilder.append(applicationId).append(" "); - } - - // Add the required default User-Agent string. - userAgentBuilder.append(String.format(DEFAULT_USER_AGENT_FORMAT, sdkName, sdkVersion)); - - // Only add the platform telemetry if it is allowed as it is optional. - if (!telemetryDisabled()) { - String platformInfo = String.format(PLATFORM_INFO_FORMAT, JAVA_VERSION, OS_NAME, OS_VERSION); - userAgentBuilder.append(" ") - .append("(") - .append(platformInfo) - .append(")"); - } - - return userAgentBuilder.toString(); - } - - private boolean telemetryDisabled() { - return configuration.get(Configuration.PROPERTY_AZURE_TELEMETRY_DISABLED, false); - } - @Override public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { String userAgent = context.getHttpRequest().getHeaders().getValue(USER_AGENT_KEY); @@ -124,7 +74,8 @@ public Mono process(HttpPipelineCallContext context, HttpPipelineN applicationId = httpLogOptions.getApplicationId(); } - context.getHttpRequest().setHeader(USER_AGENT_KEY, buildUserAgent(applicationId, sdkName, sdkVersion)); + context.getHttpRequest().setHeader(USER_AGENT_KEY, + UserAgentUtil.toUserAgentString(applicationId, sdkName, sdkVersion, configuration)); return next.process(); } } From 05db46a2ac981638aa320ae92bc2be458512b7f2 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 14 Jul 2020 13:24:25 +0800 Subject: [PATCH 2/7] fix for toReactorContext issue --- .../resources/fluentcore/AzureServiceClient.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java index 36e6a93b9b92..df5b02b7e3de 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java @@ -70,13 +70,11 @@ public SerializerAdapter getSerializerAdapter() { * @return the default client context. */ public Context getContext() { - Context context = Context.NONE; if (sdkName == null) { sdkName = this.getClass().getPackage().getName(); } - context = context.addData("Sdk-Name", sdkName); - context = context.addData("Sdk-Version", SDK_VERSION); - return context; + return new Context("Sdk-Name", sdkName) + .addData("Sdk-Version", SDK_VERSION); } /** From 2e33ff8f2ff4630fd54caafc9cb01a8e451b42ec Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 14 Jul 2020 14:13:15 +0800 Subject: [PATCH 3/7] copy UserAgentUtil until keyvault data --- .../fluentcore/policy/UserAgentPolicy.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java index 440da636b175..eddd2a2fae6b 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java @@ -10,7 +10,6 @@ import com.azure.core.http.policy.HttpPipelinePolicy; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; -import com.azure.core.util.UserAgentUtil; import reactor.core.publisher.Mono; /** @@ -46,6 +45,32 @@ public UserAgentPolicy(HttpLogOptions httpLogOptions, Configuration configuratio } } + private static String toUserAgentString(String applicationId, String sdkName, String sdkVersion, Configuration configuration) { + StringBuilder userAgentBuilder = new StringBuilder(); + if (applicationId != null) { + applicationId = applicationId.length() > 24 ? applicationId.substring(0, 24) : applicationId; + userAgentBuilder.append(applicationId).append(" "); + } + + userAgentBuilder.append("azsdk-java").append("-").append(sdkName).append("/").append(sdkVersion); + if (!isTelemetryDisabled(configuration)) { + userAgentBuilder.append(" ").append("(").append(getPlatformInfo()).append(")"); + } + + return userAgentBuilder.toString(); + } + + private static String getPlatformInfo() { + String javaVersion = Configuration.getGlobalConfiguration().get("java.version"); + String osName = Configuration.getGlobalConfiguration().get("os.name"); + String osVersion = Configuration.getGlobalConfiguration().get("os.version"); + return String.format("%s; %s; %s", javaVersion, osName, osVersion); + } + + private static boolean isTelemetryDisabled(Configuration configuration) { + return configuration == null ? (Boolean)Configuration.getGlobalConfiguration().get("AZURE_TELEMETRY_DISABLED", false) : (Boolean)configuration.get("AZURE_TELEMETRY_DISABLED", false); + } + @Override public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { String userAgent = context.getHttpRequest().getHeaders().getValue(USER_AGENT_KEY); @@ -75,7 +100,8 @@ public Mono process(HttpPipelineCallContext context, HttpPipelineN } context.getHttpRequest().setHeader(USER_AGENT_KEY, - UserAgentUtil.toUserAgentString(applicationId, sdkName, sdkVersion, configuration)); + //UserAgentUtil.toUserAgentString(applicationId, sdkName, sdkVersion, configuration)); + toUserAgentString(applicationId, sdkName, sdkVersion, configuration)); return next.process(); } } From 0fd94a5426981ea5a61dbb3e8fb4b106c3ffa210 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 14 Jul 2020 14:42:33 +0800 Subject: [PATCH 4/7] read version from properties prepared by maven --- sdk/resources/mgmt/pom.xml | 8 ++++++++ .../resources/fluentcore/AzureServiceClient.java | 12 +++++++++--- .../mgmt/src/main/resources/azure.properties | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 sdk/resources/mgmt/src/main/resources/azure.properties diff --git a/sdk/resources/mgmt/pom.xml b/sdk/resources/mgmt/pom.xml index 2e7e2628ede5..74405f70e0d7 100644 --- a/sdk/resources/mgmt/pom.xml +++ b/sdk/resources/mgmt/pom.xml @@ -37,6 +37,7 @@ UTF-8 + ${project.version} @@ -88,6 +89,13 @@ + + + src/main/resources + true + + + org.apache.maven.plugins diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java index df5b02b7e3de..320ae2ce8e67 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java @@ -12,6 +12,7 @@ import com.azure.core.management.polling.PollResult; import com.azure.core.management.serializer.AzureJacksonAdapter; import com.azure.core.util.Context; +import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; import com.azure.core.util.polling.AsyncPollResponse; import com.azure.core.util.polling.LongRunningOperationStatus; @@ -45,15 +46,17 @@ public abstract class AzureServiceClient { private final ClientLogger logger = new ClientLogger(getClass()); + private static final Map PROPERTIES = + CoreUtils.getProperties("azure.properties"); + protected AzureServiceClient(HttpPipeline httpPipeline, AzureEnvironment environment) { ((AzureJacksonAdapter) serializerAdapter).serializer().registerModule(DateTimeDeserializer.getModule()); } - private static final String SDK_VERSION = "2.0.0-SNAPSHOT"; - private final SerializerAdapter serializerAdapter = new AzureJacksonAdapter(); private String sdkName; + private static String sdkVersion; /** * Gets serializer adapter for JSON serialization/de-serialization. @@ -73,8 +76,11 @@ public Context getContext() { if (sdkName == null) { sdkName = this.getClass().getPackage().getName(); } + if (sdkVersion == null) { + sdkVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); + } return new Context("Sdk-Name", sdkName) - .addData("Sdk-Version", SDK_VERSION); + .addData("Sdk-Version", sdkVersion); } /** diff --git a/sdk/resources/mgmt/src/main/resources/azure.properties b/sdk/resources/mgmt/src/main/resources/azure.properties new file mode 100644 index 000000000000..defbd48204e4 --- /dev/null +++ b/sdk/resources/mgmt/src/main/resources/azure.properties @@ -0,0 +1 @@ +version=${project.version} From 912f1f7841774b034e95e3cb4d4e6be4d883e028 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 14 Jul 2020 15:02:48 +0800 Subject: [PATCH 5/7] nit --- sdk/resources/mgmt/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/resources/mgmt/pom.xml b/sdk/resources/mgmt/pom.xml index 74405f70e0d7..030fcf07163a 100644 --- a/sdk/resources/mgmt/pom.xml +++ b/sdk/resources/mgmt/pom.xml @@ -37,7 +37,6 @@ UTF-8 - ${project.version} From 48effddd758fb6447f36758648ad8101d4a505ad Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 14 Jul 2020 15:07:24 +0800 Subject: [PATCH 6/7] fix checkstyle --- .../fluentcore/policy/UserAgentPolicy.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java index eddd2a2fae6b..8bd010728836 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/UserAgentPolicy.java @@ -45,16 +45,35 @@ public UserAgentPolicy(HttpLogOptions httpLogOptions, Configuration configuratio } } - private static String toUserAgentString(String applicationId, String sdkName, String sdkVersion, Configuration configuration) { + // begin of UserAgentUtil.toUserAgentString + public static final String DEFAULT_USER_AGENT_HEADER = "azsdk-java"; + private static final String PLATFORM_INFO_FORMAT = "%s; %s; %s"; + private static final int MAX_APP_ID_LENGTH = 24; + + private static String toUserAgentString(String applicationId, String sdkName, String sdkVersion, + Configuration configuration) { StringBuilder userAgentBuilder = new StringBuilder(); + + // Only add the application ID if it is present as it is optional. if (applicationId != null) { - applicationId = applicationId.length() > 24 ? applicationId.substring(0, 24) : applicationId; + applicationId = applicationId.length() > MAX_APP_ID_LENGTH ? applicationId.substring(0, MAX_APP_ID_LENGTH) + : applicationId; userAgentBuilder.append(applicationId).append(" "); } - userAgentBuilder.append("azsdk-java").append("-").append(sdkName).append("/").append(sdkVersion); + // Add the required default User-Agent string. + userAgentBuilder.append(DEFAULT_USER_AGENT_HEADER) + .append("-") + .append(sdkName) + .append("/") + .append(sdkVersion); + + // Only add the platform telemetry if it is allowed as it is optional. if (!isTelemetryDisabled(configuration)) { - userAgentBuilder.append(" ").append("(").append(getPlatformInfo()).append(")"); + userAgentBuilder.append(" ") + .append("(") + .append(getPlatformInfo()) + .append(")"); } return userAgentBuilder.toString(); @@ -64,12 +83,16 @@ private static String getPlatformInfo() { String javaVersion = Configuration.getGlobalConfiguration().get("java.version"); String osName = Configuration.getGlobalConfiguration().get("os.name"); String osVersion = Configuration.getGlobalConfiguration().get("os.version"); - return String.format("%s; %s; %s", javaVersion, osName, osVersion); + + return String.format(PLATFORM_INFO_FORMAT, javaVersion, osName, osVersion); } private static boolean isTelemetryDisabled(Configuration configuration) { - return configuration == null ? (Boolean)Configuration.getGlobalConfiguration().get("AZURE_TELEMETRY_DISABLED", false) : (Boolean)configuration.get("AZURE_TELEMETRY_DISABLED", false); + return (configuration == null) + ? Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_TELEMETRY_DISABLED, false) + : configuration.get(Configuration.PROPERTY_AZURE_TELEMETRY_DISABLED, false); } + // end of UserAgentUtil.toUserAgentString @Override public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { From 74dc2c201359f6be7802bf98d854bbd1ee03681e Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 14 Jul 2020 17:24:46 +0800 Subject: [PATCH 7/7] nit --- .../fluentcore/AzureServiceClient.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java index 320ae2ce8e67..27cc88d828cc 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/AzureServiceClient.java @@ -49,14 +49,19 @@ public abstract class AzureServiceClient { private static final Map PROPERTIES = CoreUtils.getProperties("azure.properties"); - protected AzureServiceClient(HttpPipeline httpPipeline, AzureEnvironment environment) { - ((AzureJacksonAdapter) serializerAdapter).serializer().registerModule(DateTimeDeserializer.getModule()); + private static final String SDK_VERSION; + static { + SDK_VERSION = PROPERTIES.getOrDefault("version", "UnknownVersion"); } private final SerializerAdapter serializerAdapter = new AzureJacksonAdapter(); - private String sdkName; - private static String sdkVersion; + private final String sdkName; + + protected AzureServiceClient(HttpPipeline httpPipeline, AzureEnvironment environment) { + sdkName = this.getClass().getPackage().getName(); + ((AzureJacksonAdapter) serializerAdapter).serializer().registerModule(DateTimeDeserializer.getModule()); + } /** * Gets serializer adapter for JSON serialization/de-serialization. @@ -73,14 +78,8 @@ public SerializerAdapter getSerializerAdapter() { * @return the default client context. */ public Context getContext() { - if (sdkName == null) { - sdkName = this.getClass().getPackage().getName(); - } - if (sdkVersion == null) { - sdkVersion = PROPERTIES.getOrDefault("version", "UnknownVersion"); - } return new Context("Sdk-Name", sdkName) - .addData("Sdk-Version", sdkVersion); + .addData("Sdk-Version", SDK_VERSION); } /**