Skip to content

Commit 27f41ab

Browse files
Send JVM and OS statistics in User-Agent fixes #291
1 parent b8b6df3 commit 27f41ab

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private Call createCall(Request request) {
140140
builder.url(RequestUtils.replaceEndPoint(request.url().toString(), getEndPoint()));
141141
}
142142

143-
String userAgent = getUserAgent();
143+
String userAgent = RequestUtils.getUserAgent();
144144

145145
if (defaultHeaders != null) {
146146
for (String key : defaultHeaders.names()) {
@@ -304,16 +304,6 @@ public String getName() {
304304
}
305305

306306

307-
/**
308-
* Gets the user agent.
309-
*
310-
*
311-
* @return the user agent
312-
*/
313-
private String getUserAgent() {
314-
return "watson-apis-java-sdk/3.0.0-RC1";
315-
}
316-
317307
/**
318308
* Sets the API key.
319309
*

src/main/java/com/ibm/watson/developer_cloud/util/RequestUtils.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public class RequestUtils {
3737
*/
3838
public static final String DEFAULT_ENDPOINT = "http://do.not.use";
3939

40+
private static final String SDK_VERSION = "3.0.0-RC1";
41+
private static final String[] properties =
42+
new String[] {"java.vendor", "java.version", "os.arch", "os.name", "os.version"};
43+
private static String userAgent;
44+
4045
/**
4146
* Encode a string into a valid URL string.
4247
*
@@ -122,7 +127,7 @@ public static String replaceEndPoint(String url, String endPoint) {
122127
/**
123128
* Creates a String of all elements of an iterable, separated by a separator.
124129
*
125-
* @param iterable the iterable
130+
* @param iterable the iterable
126131
* @param separator the separator
127132
* @return the joined String
128133
*/
@@ -141,4 +146,38 @@ public static String join(Iterable<?> iterable, String separator) {
141146

142147
return sb.toString();
143148
}
149+
150+
/**
151+
* Gets the user agent.
152+
*
153+
* @return the user agent
154+
*/
155+
public static String getUserAgent() {
156+
if (userAgent == null) {
157+
userAgent = buildUserAgent();
158+
}
159+
return userAgent;
160+
}
161+
162+
/**
163+
* Builds the user agent using System properties
164+
*
165+
* @return the string that represents the user agent
166+
*/
167+
private static String buildUserAgent() {
168+
StringBuilder stringBuilder = new StringBuilder();
169+
stringBuilder.append("watson-apis-java-sdk/");
170+
stringBuilder.append(SDK_VERSION);
171+
stringBuilder.append(" (");
172+
for (String propertyName : properties) {
173+
stringBuilder.append(propertyName);
174+
stringBuilder.append("=");
175+
stringBuilder.append(System.getProperty(propertyName));
176+
stringBuilder.append("; ");
177+
}
178+
stringBuilder.append(")");
179+
180+
return stringBuilder.toString();
181+
}
182+
144183
}

src/test/java/com/ibm/watson/developer_cloud/service/GenericServiceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.ibm.watson.developer_cloud.service;
1515

1616
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertTrue;
1718

1819
import java.util.Collections;
1920
import java.util.HashMap;
@@ -234,7 +235,7 @@ public void testCustomUserAgent() throws InterruptedException {
234235
service.setDefaultHeaders(headers);
235236
service.getProfile(sampleText).execute();
236237
final RecordedRequest request = checkRequest();
237-
assertEquals("watson-apis-java-sdk/3.0.0-RC1 foo-bar", request.getHeader(HttpHeaders.USER_AGENT));
238+
assertTrue(request.getHeader(HttpHeaders.USER_AGENT).endsWith("foo-bar"));
238239
service.setDefaultHeaders(null);
239240
}
240241

0 commit comments

Comments
 (0)