21
21
import java .io .InputStream ;
22
22
import java .io .UnsupportedEncodingException ;
23
23
import java .net .URLEncoder ;
24
+ import java .util .ArrayList ;
24
25
import java .util .Arrays ;
25
26
import java .util .HashMap ;
27
+ import java .util .List ;
26
28
import java .util .Map ;
27
29
import java .util .Properties ;
28
30
import java .util .logging .Level ;
35
37
public final class RequestUtils {
36
38
37
39
private static final Logger LOG = Logger .getLogger (RequestUtils .class .getName ());
40
+ private static final String [] properties =
41
+ new String [] { "java.vendor" , "java.version" , "os.arch" , "os.name" , "os.version" };
42
+ private static String userAgent ;
38
43
39
44
private RequestUtils () {
40
45
// This is a utility class - no instantiation allowed.
@@ -135,7 +140,7 @@ public static String join(Iterable<?> iterable, String separator) {
135
140
return sb .toString ();
136
141
}
137
142
138
- public static String loadCoreVersion () {
143
+ private static String loadCoreVersion () {
139
144
ClassLoader classLoader = RequestUtils .class .getClassLoader ();
140
145
InputStream inputStream = classLoader .getResourceAsStream ("sdk-core-version.properties" );
141
146
Properties properties = new Properties ();
@@ -149,6 +154,32 @@ public static String loadCoreVersion() {
149
154
return properties .getProperty ("version" , "unknown-version" );
150
155
}
151
156
157
+ /**
158
+ * Builds the user agent using System properties.
159
+ *
160
+ * @return the string that represents the user agent
161
+ */
162
+ private static String buildUserAgent () {
163
+ final List <String > details = new ArrayList <>();
164
+ for (String propertyName : properties ) {
165
+ details .add (propertyName + "=" + System .getProperty (propertyName ));
166
+ }
167
+
168
+ return "ibm-java-sdk-core/" + loadCoreVersion () + " (" + RequestUtils .join (details , "; " ) + ")" ;
169
+ }
170
+
171
+ /**
172
+ * Gets the user agent.
173
+ *
174
+ * @return the user agent
175
+ */
176
+ public static synchronized String getUserAgent () {
177
+ if (userAgent == null ) {
178
+ userAgent = buildUserAgent ();
179
+ }
180
+ return userAgent ;
181
+ }
182
+
152
183
/**
153
184
* Returns a request body that encapsulates the specified file qualified with the specified content type.
154
185
*
0 commit comments