0
# Utilities
1
2
Core utility classes providing version information, certificate management, and common functionality used throughout the library.
3
4
## Core Imports
5
6
```java
7
import com.google.api.client.googleapis.GoogleUtils;
8
import com.google.api.client.googleapis.util.Utils;
9
import com.google.api.client.googleapis.notifications.NotificationUtils;
10
```
11
12
## Version and Library Information
13
14
### GoogleUtils
15
16
Central utility class providing version information and certificate management.
17
18
```java { .api }
19
public final class GoogleUtils {
20
public static final String VERSION;
21
public static final Integer MAJOR_VERSION;
22
public static final Integer MINOR_VERSION;
23
public static final Integer BUGFIX_VERSION;
24
25
public static synchronized KeyStore getCertificateTrustStore()
26
throws IOException, GeneralSecurityException;
27
}
28
```
29
30
**Usage Example:**
31
32
```java
33
import com.google.api.client.googleapis.GoogleUtils;
34
import java.security.KeyStore;
35
36
// Get library version information
37
System.out.println("Google API Client Version: " + GoogleUtils.VERSION);
38
System.out.println("Major Version: " + GoogleUtils.MAJOR_VERSION);
39
System.out.println("Minor Version: " + GoogleUtils.MINOR_VERSION);
40
System.out.println("Bugfix Version: " + GoogleUtils.BUGFIX_VERSION);
41
42
// Get Google's certificate trust store
43
try {
44
KeyStore trustStore = GoogleUtils.getCertificateTrustStore();
45
System.out.println("Trust store loaded with " + trustStore.size() + " certificates");
46
} catch (Exception e) {
47
System.err.println("Failed to load trust store: " + e.getMessage());
48
}
49
```
50
51
### Version Pattern Matching
52
53
```java
54
// The library uses a specific version pattern for parsing
55
// Pattern: (\\d+)\\.(\\d+)\\.(\\d+)(-SNAPSHOT)?
56
// Examples: "2.8.0", "2.8.0-SNAPSHOT"
57
58
public class VersionInfo {
59
public static void printVersionDetails() {
60
String version = GoogleUtils.VERSION;
61
System.out.println("Full Version String: " + version);
62
63
// Check if it's a snapshot build
64
boolean isSnapshot = version.contains("-SNAPSHOT");
65
System.out.println("Is Snapshot Build: " + isSnapshot);
66
67
// Version components are available as separate constants
68
System.out.println("Version Components: " +
69
GoogleUtils.MAJOR_VERSION + "." +
70
GoogleUtils.MINOR_VERSION + "." +
71
GoogleUtils.BUGFIX_VERSION);
72
}
73
}
74
```
75
76
## General Utilities
77
78
### Utils
79
80
General utility methods for common operations.
81
82
```java { .api }
83
public final class Utils {
84
public static final String USER_AGENT_SUFFIX = "google-api-java-client/" + GoogleUtils.VERSION;
85
86
public static String getDefaultUserAgent();
87
public static String getUserAgent(String applicationName);
88
}
89
```
90
91
**Usage Example:**
92
93
```java
94
import com.google.api.client.googleapis.util.Utils;
95
96
// Get default user agent string
97
String defaultUserAgent = Utils.getDefaultUserAgent();
98
System.out.println("Default User Agent: " + defaultUserAgent);
99
100
// Get user agent with custom application name
101
String customUserAgent = Utils.getUserAgent("MyApplication/1.0");
102
System.out.println("Custom User Agent: " + customUserAgent);
103
104
// Example output:
105
// "MyApplication/1.0 google-api-java-client/2.8.0"
106
```
107
108
## Notification Utilities
109
110
### NotificationUtils
111
112
Utility methods for generating notification channel identifiers.
113
114
```java { .api }
115
public final class NotificationUtils {
116
public static String randomUuidString();
117
}
118
```
119
120
**Usage Example:**
121
122
```java
123
import com.google.api.client.googleapis.notifications.NotificationUtils;
124
125
// Generate a random UUID string for notification channel ID
126
String channelId = NotificationUtils.randomUuidString();
127
System.out.println("Generated channel ID: " + channelId);
128
129
// Example usage in creating a notification channel
130
public void createNotificationChannel(String resourceUri, long expiration) {
131
String channelId = NotificationUtils.randomUuidString();
132
133
// Use the generated ID to create a notification channel
134
// (implementation depends on specific Google API being used)
135
System.out.println("Creating notification channel with ID: " + channelId);
136
System.out.println("Resource URI: " + resourceUri);
137
System.out.println("Expiration: " + new Date(expiration));
138
}
139
```
140
141
## Certificate Management
142
143
### Trust Store Operations
144
145
```java
146
import java.security.KeyStore;
147
import java.security.cert.Certificate;
148
import java.util.Enumeration;
149
150
public class CertificateInspector {
151
public static void inspectGoogleTrustStore() throws Exception {
152
KeyStore trustStore = GoogleUtils.getCertificateTrustStore();
153
154
System.out.println("Google Trust Store Information:");
155
System.out.println("Type: " + trustStore.getType());
156
System.out.println("Provider: " + trustStore.getProvider().getName());
157
System.out.println("Number of certificates: " + trustStore.size());
158
159
// List all certificate aliases
160
Enumeration<String> aliases = trustStore.aliases();
161
while (aliases.hasMoreElements()) {
162
String alias = aliases.nextElement();
163
Certificate cert = trustStore.getCertificate(alias);
164
165
System.out.println("Certificate alias: " + alias);
166
System.out.println("Certificate type: " + cert.getType());
167
// Additional certificate details can be extracted here
168
}
169
}
170
}
171
```
172
173
## User Agent Management
174
175
### Application Identification
176
177
```java
178
public class UserAgentManager {
179
private final String applicationName;
180
private final String applicationVersion;
181
182
public UserAgentManager(String applicationName, String applicationVersion) {
183
this.applicationName = applicationName;
184
this.applicationVersion = applicationVersion;
185
}
186
187
public String buildUserAgent() {
188
String appIdentifier = applicationName;
189
if (applicationVersion != null && !applicationVersion.isEmpty()) {
190
appIdentifier += "/" + applicationVersion;
191
}
192
193
return Utils.getUserAgent(appIdentifier);
194
}
195
196
public HttpRequestInitializer createRequestInitializer(HttpRequestInitializer baseInitializer) {
197
return new HttpRequestInitializer() {
198
@Override
199
public void initialize(HttpRequest request) throws IOException {
200
if (baseInitializer != null) {
201
baseInitializer.initialize(request);
202
}
203
204
// Set custom user agent
205
HttpHeaders headers = request.getHeaders();
206
if (headers.getUserAgent() == null) {
207
headers.setUserAgent(buildUserAgent());
208
}
209
}
210
};
211
}
212
}
213
214
// Usage
215
UserAgentManager userAgentManager = new UserAgentManager("MyApp", "2.1.0");
216
String userAgent = userAgentManager.buildUserAgent();
217
// Result: "MyApp/2.1.0 google-api-java-client/2.8.0"
218
```
219
220
## Library Compatibility
221
222
### Version Compatibility Checks
223
224
```java
225
public class CompatibilityChecker {
226
private static final int MIN_SUPPORTED_MAJOR = 2;
227
private static final int MIN_SUPPORTED_MINOR = 0;
228
229
public static boolean isCompatibleVersion() {
230
return GoogleUtils.MAJOR_VERSION >= MIN_SUPPORTED_MAJOR &&
231
(GoogleUtils.MAJOR_VERSION > MIN_SUPPORTED_MAJOR ||
232
GoogleUtils.MINOR_VERSION >= MIN_SUPPORTED_MINOR);
233
}
234
235
public static void checkCompatibility() {
236
if (!isCompatibleVersion()) {
237
System.err.println("Warning: Google API Client version " + GoogleUtils.VERSION +
238
" may not be compatible with this application. " +
239
"Minimum required version: " + MIN_SUPPORTED_MAJOR + "." +
240
MIN_SUPPORTED_MINOR + ".0");
241
}
242
}
243
244
public static String getVersionInfo() {
245
return String.format(
246
"Google API Client for Java v%s (Build: %d.%d.%d)",
247
GoogleUtils.VERSION,
248
GoogleUtils.MAJOR_VERSION,
249
GoogleUtils.MINOR_VERSION,
250
GoogleUtils.BUGFIX_VERSION
251
);
252
}
253
}
254
```
255
256
## Configuration Helpers
257
258
### Library Configuration
259
260
```java
261
public class LibraryConfiguration {
262
public static void printSystemInformation() {
263
System.out.println("=== Google API Client Configuration ===");
264
System.out.println("Library Version: " + GoogleUtils.VERSION);
265
System.out.println("Java Version: " + System.getProperty("java.version"));
266
System.out.println("Java Vendor: " + System.getProperty("java.vendor"));
267
System.out.println("OS Name: " + System.getProperty("os.name"));
268
System.out.println("OS Version: " + System.getProperty("os.version"));
269
System.out.println("OS Architecture: " + System.getProperty("os.arch"));
270
271
// Check for important system properties that affect the library
272
String httpAgent = System.getProperty("http.agent");
273
if (httpAgent != null) {
274
System.out.println("System HTTP Agent: " + httpAgent);
275
}
276
277
String proxyHost = System.getProperty("http.proxyHost");
278
String proxyPort = System.getProperty("http.proxyPort");
279
if (proxyHost != null) {
280
System.out.println("HTTP Proxy: " + proxyHost +
281
(proxyPort != null ? ":" + proxyPort : ""));
282
}
283
284
String httpsProxyHost = System.getProperty("https.proxyHost");
285
String httpsProxyPort = System.getProperty("https.proxyPort");
286
if (httpsProxyHost != null) {
287
System.out.println("HTTPS Proxy: " + httpsProxyHost +
288
(httpsProxyPort != null ? ":" + httpsProxyPort : ""));
289
}
290
}
291
}
292
```
293
294
## Types
295
296
### KeyStore
297
298
Java security KeyStore for certificate and key management.
299
300
### IOException
301
302
Exception for I/O operations.
303
304
### GeneralSecurityException
305
306
Exception for general security-related operations.
307
308
309
### HttpRequestInitializer
310
311
Interface for initializing HTTP requests.
312
313
### HttpRequest
314
315
HTTP request representation.
316
317
### HttpHeaders
318
319
HTTP headers container.
320
321
### String
322
323
Java string class.
324
325
### Integer
326
327
Java integer wrapper class.
328
329
### Certificate
330
331
Java security certificate representation.
332
333
### Date
334
335
Java date class for timestamp representation.