Skip to content

Commit e80298f

Browse files
committed
Merge pull request #14 from nomiero/master
Add ability to create a custom user-agent suffix
2 parents 89b5dd9 + 9d6da33 commit e80298f

File tree

8 files changed

+79
-7
lines changed

8 files changed

+79
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.microsoft.azure</groupId>
55
<artifactId>azure-documentdb</artifactId>
6-
<version>0.9.4</version>
6+
<version>0.9.5</version>
77
<name>${project.groupId}:${project.artifactId}</name>
88
<description>Java SDK for Microsoft Azure DocumentDB</description>
99
<url>http://azure.microsoft.com/en-us/services/documentdb/</url>

src/com/microsoft/azure/documentdb/ConnectionPolicy.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public ConnectionPolicy() {
2929
this.mediaReadMode = MediaReadMode.Buffered;
3030
this.maxPoolSize = DEFAULT_MAX_POOL_SIZE;
3131
this.idleConnectionTimeout = DEFAULT_IDLE_CONNECTION_TIMEOUT;
32+
this.userAgentSuffix = "";
3233
}
3334

3435
private int maxConnections;
@@ -169,6 +170,24 @@ public void setIdleConnectionTimeout(int idleConnectionTimeout) {
169170
this.idleConnectionTimeout = idleConnectionTimeout;
170171
}
171172

173+
private String userAgentSuffix;
174+
175+
/**
176+
* sets the value of the user-agent suffix.
177+
* @param userAgentSuffix The value to be appended to the user-agent header, this is used for monitoring purposes.
178+
*/
179+
public void setUserAgentSuffix(String userAgentSuffix) {
180+
this.userAgentSuffix = userAgentSuffix;
181+
}
182+
183+
/**
184+
* Gets the value of user-agent suffix.
185+
* @return the value of user-agent suffix.
186+
*/
187+
public String getUserAgentSuffix() {
188+
return this.userAgentSuffix;
189+
}
190+
172191
/**
173192
* Gets the default connection policy.
174193
*

src/com/microsoft/azure/documentdb/DocumentClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,19 @@ private void initialize(URI serviceEndpoint,
128128
this.sessionContainer = new SessionContainer(this.serviceEndpoint.getHost());
129129
this.desiredConsistencyLevel = desiredConsistencyLevel;
130130

131+
UserAgentContainer userAgentContainer = new UserAgentContainer();
132+
String userAgentSuffix = connectionPolicy.getUserAgentSuffix();
133+
if(userAgentSuffix != null && userAgentSuffix.length() > 0) {
134+
userAgentContainer.setSuffix(userAgentSuffix);
135+
}
136+
131137
this.gatewayProxy = new GatewayProxy(this.serviceEndpoint,
132138
this.connectionPolicy,
133139
desiredConsistencyLevel,
134140
this.queryCompatibilityMode,
135141
this.masterKey,
136-
this.resourceTokens);
142+
this.resourceTokens,
143+
userAgentContainer);
137144
}
138145

139146
RetryPolicy getRetryPolicy() {

src/com/microsoft/azure/documentdb/GatewayProxy.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,20 @@ public GatewayProxy(URI serviceEndpoint,
5353
ConsistencyLevel consistencyLevel,
5454
DocumentClient.QueryCompatibilityMode queryCompatibilityMode,
5555
String masterKey,
56-
Map<String, String> resourceTokens) {
56+
Map<String, String> resourceTokens,
57+
UserAgentContainer userAgentContainer) {
5758
this.serviceEndpoint = serviceEndpoint;
5859
this.defaultHeaders = new HashMap<String, String>();
5960
this.defaultHeaders.put(HttpConstants.HttpHeaders.CACHE_CONTROL,
6061
"no-cache");
6162
this.defaultHeaders.put(HttpConstants.HttpHeaders.VERSION,
6263
HttpConstants.Versions.CURRENT_VERSION);
63-
this.defaultHeaders.put(HttpConstants.HttpHeaders.USER_AGENT,
64-
HttpConstants.Versions.USER_AGENT);
64+
65+
if (userAgentContainer == null) {
66+
userAgentContainer = new UserAgentContainer();
67+
}
68+
69+
this.defaultHeaders.put(HttpConstants.HttpHeaders.USER_AGENT, userAgentContainer.getUserAgent());
6570

6671
if (consistencyLevel != null) {
6772
this.defaultHeaders.put(HttpConstants.HttpHeaders.CONSISTENCY_LEVEL,

src/com/microsoft/azure/documentdb/HttpConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static class Versions {
153153

154154
public static String CURRENT_VERSION = "2014-08-21";
155155

156-
public static String USER_AGENT = "documentdb-java-sdk-0.9.4";
156+
public static String USER_AGENT = "documentdb-java-sdk-0.9.5";
157157
}
158158

159159
public static class StatusCodes {

src/com/microsoft/azure/documentdb/QueryIterable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public List<T> toList() {
150150
public void reset() {
151151
this.hasStarted = false;
152152
this.continuation = this.initialContinuation;
153-
this.items = new ArrayList<T>();
153+
this.items.clear();
154154
this.currentIndex = 0;
155155
this.hasNext = true;
156156
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.microsoft.azure.documentdb;
2+
3+
/**
4+
* The user agent object, which is used to track the version of the SDK.
5+
*/
6+
class UserAgentContainer {
7+
8+
private static final String BASE_USER_AGENT = HttpConstants.Versions.USER_AGENT;
9+
private static final int MAX_SUFFIX_LENGTH = 64;
10+
private String suffix;
11+
private String userAgent;
12+
13+
UserAgentContainer() {
14+
this.suffix = "";
15+
this.userAgent = BASE_USER_AGENT;
16+
}
17+
18+
public void setSuffix(String suffix) {
19+
if(suffix.length() > MAX_SUFFIX_LENGTH) {
20+
suffix = suffix.substring(0, MAX_SUFFIX_LENGTH);
21+
}
22+
23+
this.suffix = suffix;
24+
this.userAgent = BASE_USER_AGENT.concat(this.suffix);
25+
}
26+
27+
public String getSuffix() {
28+
return this.suffix;
29+
}
30+
31+
String getUserAgent() {
32+
return this.userAgent;
33+
}
34+
}

src/com/microsoft/azure/documentdb/test/GatewayTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,13 @@ public void testConflictCrud() throws DocumentClientException {
13561356
null).getQueryIterable().toList();
13571357
}
13581358

1359+
@Test
1360+
public void testCustomizedUserAgentCrud() throws DocumentClientException {
1361+
ConnectionPolicy policy = ConnectionPolicy.GetDefault();
1362+
policy.setUserAgentSuffix("My-Custom-User-Agent");
1363+
Assert.assertEquals("User-agent suffix should've been added", "My-Custom-User-Agent", policy.getUserAgentSuffix());
1364+
}
1365+
13591366
private static String getUID() {
13601367
UUID u = UUID.randomUUID();
13611368
return ("" + u.getMostSignificantBits()) + Math.abs(u.getLeastSignificantBits());

0 commit comments

Comments
 (0)