Skip to content

Commit 53ece86

Browse files
authored
Made UserAgentUtil throw for applicationIds longer than 24 characters. (Azure#23643)
* Aligned UserAgentUtil with ClientOptions and HttpLogOptions so all of them throw when an `applicationId` longer than 24 characters is provided. * Updated tests. * Updated JavaDocs. * Applied PR feedback.
1 parent 5e41f82 commit 53ece86

File tree

5 files changed

+45
-25
lines changed

5 files changed

+45
-25
lines changed

sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLogOptions.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class HttpLogOptions {
2929
private final ClientLogger logger = new ClientLogger(HttpLogOptions.class);
3030

3131
private static final int MAX_APPLICATION_ID_LENGTH = 24;
32+
private static final String INVALID_APPLICATION_ID_LENGTH = "'applicationId' length cannot be greater than "
33+
+ MAX_APPLICATION_ID_LENGTH;
34+
private static final String INVALID_APPLICATION_ID_SPACE = "'applicationId' cannot contain spaces.";
3235
private static final List<String> DEFAULT_HEADERS_WHITELIST = Arrays.asList(
3336
"x-ms-client-request-id",
3437
"x-ms-return-client-request-id",
@@ -173,23 +176,26 @@ public String getApplicationId() {
173176
* Sets the custom application specific id supplied by the user of the client library.
174177
*
175178
* @param applicationId The user specified application id.
179+
*
176180
* @return The updated HttpLogOptions object.
181+
*
182+
* @throws IllegalArgumentException If {@code applicationId} contains spaces or is larger than 24 characters in
183+
* length.
184+
*
177185
* @deprecated Use {@link ClientOptions} to configure {@code applicationId}.
178186
*/
179187
@Deprecated
180188
public HttpLogOptions setApplicationId(final String applicationId) {
181189
if (!CoreUtils.isNullOrEmpty(applicationId)) {
182190
if (applicationId.length() > MAX_APPLICATION_ID_LENGTH) {
183-
throw logger
184-
.logExceptionAsError(new IllegalArgumentException("'applicationId' length cannot be greater than "
185-
+ MAX_APPLICATION_ID_LENGTH));
191+
throw logger.logExceptionAsError(new IllegalArgumentException(INVALID_APPLICATION_ID_LENGTH));
186192
} else if (applicationId.contains(" ")) {
187-
throw logger
188-
.logExceptionAsError(new IllegalArgumentException("'applicationId' must not contain a space."));
189-
} else {
190-
this.applicationId = applicationId;
193+
throw logger.logExceptionAsError(new IllegalArgumentException(INVALID_APPLICATION_ID_SPACE));
191194
}
192195
}
196+
197+
this.applicationId = applicationId;
198+
193199
return this;
194200
}
195201

sdk/core/azure-core/src/main/java/com/azure/core/util/ClientOptions.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,23 @@ public String getApplicationId() {
4848
* {@codesnippet com.azure.core.util.ClientOptions.setApplicationId#String}
4949
*
5050
* @param applicationId The application ID.
51+
*
5152
* @return The updated ClientOptions object.
52-
* @throws IllegalArgumentException If {@code applicationId} contains spaces or larger than 24 in length.
53+
*
54+
* @throws IllegalArgumentException If {@code applicationId} contains spaces or is larger than 24 characters in
55+
* length.
5356
*/
5457
public ClientOptions setApplicationId(String applicationId) {
55-
if (CoreUtils.isNullOrEmpty(applicationId)) {
56-
this.applicationId = applicationId;
57-
} else if (applicationId.length() > MAX_APPLICATION_ID_LENGTH) {
58-
throw logger.logExceptionAsError(new IllegalArgumentException(INVALID_APPLICATION_ID_LENGTH));
59-
} else if (applicationId.contains(" ")) {
60-
throw logger.logExceptionAsError(new IllegalArgumentException(INVALID_APPLICATION_ID_SPACE));
61-
} else {
62-
this.applicationId = applicationId;
58+
if (!CoreUtils.isNullOrEmpty(applicationId)) {
59+
if (applicationId.length() > MAX_APPLICATION_ID_LENGTH) {
60+
throw logger.logExceptionAsError(new IllegalArgumentException(INVALID_APPLICATION_ID_LENGTH));
61+
} else if (applicationId.contains(" ")) {
62+
throw logger.logExceptionAsError(new IllegalArgumentException(INVALID_APPLICATION_ID_SPACE));
63+
}
6364
}
6465

66+
this.applicationId = applicationId;
67+
6568
return this;
6669
}
6770

sdk/core/azure-core/src/main/java/com/azure/core/util/UserAgentUtil.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
* <a href="https://azure.github.io/azure-sdk/general_azurecore.html#telemetry-policy">design guidelines</a>.
99
*/
1010
public final class UserAgentUtil {
11-
11+
private static final int MAX_APPLICATION_ID_LENGTH = 24;
12+
private static final String INVALID_APPLICATION_ID_LENGTH = "'applicationId' length cannot be greater than "
13+
+ MAX_APPLICATION_ID_LENGTH;
14+
private static final String INVALID_APPLICATION_ID_SPACE = "'applicationId' cannot contain spaces.";
1215
public static final String DEFAULT_USER_AGENT_HEADER = "azsdk-java";
1316

1417
// From the design guidelines, the platform info format is:
@@ -30,17 +33,24 @@ private UserAgentUtil() {
3033
* @param sdkVersion Version of the SDK.
3134
* @param configuration The configuration to use to determine if platform info should be included in the user agent
3235
* string.
36+
*
3337
* @return User agent string as specified in design guidelines.
38+
*
39+
* @throws IllegalArgumentException If {@code applicationId} contains spaces or is larger than 24 characters in
40+
* length.
3441
*/
3542
public static String toUserAgentString(String applicationId, String sdkName, String sdkVersion,
3643
Configuration configuration) {
3744
StringBuilder userAgentBuilder = new StringBuilder();
3845

39-
// Only add the application ID if it is present as it is optional.
40-
if (applicationId != null) {
41-
applicationId = applicationId.length() > MAX_APP_ID_LENGTH ? applicationId.substring(0, MAX_APP_ID_LENGTH)
42-
: applicationId;
43-
userAgentBuilder.append(applicationId).append(" ");
46+
if (!CoreUtils.isNullOrEmpty(applicationId)) {
47+
if (applicationId.length() > MAX_APPLICATION_ID_LENGTH) {
48+
throw new IllegalArgumentException(INVALID_APPLICATION_ID_LENGTH);
49+
} else if (applicationId.contains(" ")) {
50+
throw new IllegalArgumentException(INVALID_APPLICATION_ID_SPACE);
51+
} else {
52+
userAgentBuilder.append(applicationId).append(" ");
53+
}
4454
}
4555

4656
// Add the required default User-Agent string.

sdk/core/azure-core/src/test/java/com/azure/core/http/policy/HttpLogOptionsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class HttpLogOptionsTest {
1313
@SuppressWarnings("deprecation")
1414
@Test
1515
public void testMaxApplicationId() {
16-
assertThrows(IllegalArgumentException.class, () -> new HttpLogOptions()
17-
.setApplicationId("AppId-0123456789012345678912345"));
16+
assertThrows(IllegalArgumentException.class,
17+
() -> new HttpLogOptions().setApplicationId("AppId-0123456789012345678912345"));
1818
}
1919

2020
@SuppressWarnings("deprecation")

sdk/core/azure-core/src/test/java/com/azure/core/util/UserAgentUtilTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.azure.core.util;
55

66
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
78

89
import org.junit.jupiter.api.Test;
910

@@ -38,7 +39,7 @@ void testUserAgentStringFormat() {
3839
Configuration.getGlobalConfiguration().clone().put("AZURE_TELEMETRY_DISABLED", "true")));
3940

4041
// long app id should be truncated
41-
assertEquals("ReallyLongApplicationIde azsdk-java-azure-storage-blob/12.0.0 " + plaform,
42+
assertThrows(IllegalArgumentException.class, () ->
4243
UserAgentUtil.toUserAgentString("ReallyLongApplicationIdentity", "azure-storage-blob", "12.0.0", null));
4344

4445
// null sdk name and version

0 commit comments

Comments
 (0)