Skip to content

Commit

Permalink
Merge pull request #14 from nomiero/master
Browse files Browse the repository at this point in the history
Add ability to create a custom user-agent suffix
  • Loading branch information
shipunyc committed Mar 7, 2015
2 parents 89b5dd9 + 9d6da33 commit e80298f
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>0.9.4</version>
<version>0.9.5</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java SDK for Microsoft Azure DocumentDB</description>
<url>http://azure.microsoft.com/en-us/services/documentdb/</url>
Expand Down
19 changes: 19 additions & 0 deletions src/com/microsoft/azure/documentdb/ConnectionPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public ConnectionPolicy() {
this.mediaReadMode = MediaReadMode.Buffered;
this.maxPoolSize = DEFAULT_MAX_POOL_SIZE;
this.idleConnectionTimeout = DEFAULT_IDLE_CONNECTION_TIMEOUT;
this.userAgentSuffix = "";
}

private int maxConnections;
Expand Down Expand Up @@ -169,6 +170,24 @@ public void setIdleConnectionTimeout(int idleConnectionTimeout) {
this.idleConnectionTimeout = idleConnectionTimeout;
}

private String userAgentSuffix;

/**
* sets the value of the user-agent suffix.
* @param userAgentSuffix The value to be appended to the user-agent header, this is used for monitoring purposes.
*/
public void setUserAgentSuffix(String userAgentSuffix) {
this.userAgentSuffix = userAgentSuffix;
}

/**
* Gets the value of user-agent suffix.
* @return the value of user-agent suffix.
*/
public String getUserAgentSuffix() {
return this.userAgentSuffix;
}

/**
* Gets the default connection policy.
*
Expand Down
9 changes: 8 additions & 1 deletion src/com/microsoft/azure/documentdb/DocumentClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,19 @@ private void initialize(URI serviceEndpoint,
this.sessionContainer = new SessionContainer(this.serviceEndpoint.getHost());
this.desiredConsistencyLevel = desiredConsistencyLevel;

UserAgentContainer userAgentContainer = new UserAgentContainer();
String userAgentSuffix = connectionPolicy.getUserAgentSuffix();
if(userAgentSuffix != null && userAgentSuffix.length() > 0) {
userAgentContainer.setSuffix(userAgentSuffix);
}

this.gatewayProxy = new GatewayProxy(this.serviceEndpoint,
this.connectionPolicy,
desiredConsistencyLevel,
this.queryCompatibilityMode,
this.masterKey,
this.resourceTokens);
this.resourceTokens,
userAgentContainer);
}

RetryPolicy getRetryPolicy() {
Expand Down
11 changes: 8 additions & 3 deletions src/com/microsoft/azure/documentdb/GatewayProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ public GatewayProxy(URI serviceEndpoint,
ConsistencyLevel consistencyLevel,
DocumentClient.QueryCompatibilityMode queryCompatibilityMode,
String masterKey,
Map<String, String> resourceTokens) {
Map<String, String> resourceTokens,
UserAgentContainer userAgentContainer) {
this.serviceEndpoint = serviceEndpoint;
this.defaultHeaders = new HashMap<String, String>();
this.defaultHeaders.put(HttpConstants.HttpHeaders.CACHE_CONTROL,
"no-cache");
this.defaultHeaders.put(HttpConstants.HttpHeaders.VERSION,
HttpConstants.Versions.CURRENT_VERSION);
this.defaultHeaders.put(HttpConstants.HttpHeaders.USER_AGENT,
HttpConstants.Versions.USER_AGENT);

if (userAgentContainer == null) {
userAgentContainer = new UserAgentContainer();
}

this.defaultHeaders.put(HttpConstants.HttpHeaders.USER_AGENT, userAgentContainer.getUserAgent());

if (consistencyLevel != null) {
this.defaultHeaders.put(HttpConstants.HttpHeaders.CONSISTENCY_LEVEL,
Expand Down
2 changes: 1 addition & 1 deletion src/com/microsoft/azure/documentdb/HttpConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static class Versions {

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

public static String USER_AGENT = "documentdb-java-sdk-0.9.4";
public static String USER_AGENT = "documentdb-java-sdk-0.9.5";
}

public static class StatusCodes {
Expand Down
2 changes: 1 addition & 1 deletion src/com/microsoft/azure/documentdb/QueryIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public List<T> toList() {
public void reset() {
this.hasStarted = false;
this.continuation = this.initialContinuation;
this.items = new ArrayList<T>();
this.items.clear();
this.currentIndex = 0;
this.hasNext = true;
}
Expand Down
34 changes: 34 additions & 0 deletions src/com/microsoft/azure/documentdb/UserAgentContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.microsoft.azure.documentdb;

/**
* The user agent object, which is used to track the version of the SDK.
*/
class UserAgentContainer {

private static final String BASE_USER_AGENT = HttpConstants.Versions.USER_AGENT;
private static final int MAX_SUFFIX_LENGTH = 64;
private String suffix;
private String userAgent;

UserAgentContainer() {
this.suffix = "";
this.userAgent = BASE_USER_AGENT;
}

public void setSuffix(String suffix) {
if(suffix.length() > MAX_SUFFIX_LENGTH) {
suffix = suffix.substring(0, MAX_SUFFIX_LENGTH);
}

this.suffix = suffix;
this.userAgent = BASE_USER_AGENT.concat(this.suffix);
}

public String getSuffix() {
return this.suffix;
}

String getUserAgent() {
return this.userAgent;
}
}
7 changes: 7 additions & 0 deletions src/com/microsoft/azure/documentdb/test/GatewayTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,13 @@ public void testConflictCrud() throws DocumentClientException {
null).getQueryIterable().toList();
}

@Test
public void testCustomizedUserAgentCrud() throws DocumentClientException {
ConnectionPolicy policy = ConnectionPolicy.GetDefault();
policy.setUserAgentSuffix("My-Custom-User-Agent");
Assert.assertEquals("User-agent suffix should've been added", "My-Custom-User-Agent", policy.getUserAgentSuffix());
}

private static String getUID() {
UUID u = UUID.randomUUID();
return ("" + u.getMostSignificantBits()) + Math.abs(u.getLeastSignificantBits());
Expand Down

0 comments on commit e80298f

Please sign in to comment.