This repository was archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGenomicsFactory.java
More file actions
498 lines (447 loc) · 17.1 KB
/
GenomicsFactory.java
File metadata and controls
498 lines (447 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* Copyright (C) 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.cloud.genomics.utils;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.services.CommonGoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient;
import com.google.api.client.googleapis.util.Utils;
import com.google.api.client.http.HttpBackOffIOExceptionHandler;
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler;
import com.google.api.client.http.HttpIOExceptionHandler;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.HttpUnsuccessfulResponseHandler;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.ExponentialBackOff;
import com.google.api.services.genomics.Genomics;
import com.google.api.services.genomics.GenomicsScopes;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
/**
* Code required to manufacture instances of a {@link Genomics} stub.
*
* Several authentication mechanisms are supported. For more detail, see
* https://developers.google.com/api-client-library/java/google-api-java-client/oauth2
*/
public class GenomicsFactory {
private static final int DEFAULT_CONNECT_TIMEOUT = 20000;
private static final int DEFAULT_READ_TIMEOUT = 20000;
private static final int DEFAULT_NUMBER_OF_RETRIES = 5;
private static final String DEFAULT_APPLICATION_NAME = "genomics";
/**
* A builder class for {@link GenomicsFactory} objects.
*/
public static class Builder {
// TODO is application name used for anything other than the credential store path?
// If not, it should be removed.
@VisibleForTesting final String applicationName;
private HttpTransport httpTransport = Utils.getDefaultTransport();
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
private int readTimeout = DEFAULT_READ_TIMEOUT;
private int numRetries = DEFAULT_NUMBER_OF_RETRIES;
private JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
private Optional<String> rootUrl = Optional.absent();
private Optional<String> servicePath = Optional.absent();
private Collection<String> scopes = GenomicsScopes.all();
Builder(String applicationName) {
this.applicationName = applicationName;
}
/**
* Build the {@link GenomicsFactory}.
*
* @return The built {@link GenomicsFactory}
*/
public GenomicsFactory build() {
return new GenomicsFactory(
applicationName,
httpTransport,
connectTimeout,
readTimeout,
numRetries,
jsonFactory,
scopes,
rootUrl,
servicePath);
}
/**
* Sets the {@link HttpTransport} to use. Most code will never need to call this method.
*
* @param httpTransport the {@code HttpTransport} to use
* @return this builder
*/
public Builder setHttpTransport(HttpTransport httpTransport) {
this.httpTransport = httpTransport;
return this;
}
/**
* @deprecated
*/
@Deprecated
public Builder setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
/**
* @deprecated
*/
@Deprecated
public Builder setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
/**
* The number of times to retry a failed request to the Genomics API.
*
* @param numRetries
* @return this
*/
public Builder setNumberOfRetries(int numRetries) {
this.numRetries = numRetries;
return this;
}
/**
* Sets the {@link JsonFactory} to use. Most code will never need to call this method.
*
* @param jsonFactory the {@code JsonFactory} to use
* @return this builder
*/
public Builder setJsonFactory(JsonFactory jsonFactory) {
this.jsonFactory = jsonFactory;
return this;
}
/**
* The URL of the endpoint to send requests to. The default is
* {@code https://www.googleapis.com}.
*
* @param rootUrl The URL of the endpoint to send requests to
* @return this builder
*/
public Builder setRootUrl(String rootUrl) {
this.rootUrl = Optional.of(rootUrl);
return this;
}
/**
* Sets the URL-encoded service path of the service.
* Setting this field is uncommon and should only be used when trying to use a
* non-Google API provider.
*
* @param servicePath The URL-encoded service path of the service.
* @return this builder
*/
public Builder setServicePath(String servicePath) {
this.servicePath = Optional.of(servicePath);
return this;
}
/**
* The OAuth scopes to attach to outgoing requests. Most code will not have to call this method.
*
* @param scopes The OAuth scopes to attach to outgoing requests
* @return this builder
*/
public Builder setScopes(Collection<String> scopes) {
this.scopes = scopes;
return this;
}
}
/**
* Create a new {@link Builder} for {@code GenomicsFactory} objects.
*
* @return the new {@code Builder} object.
*/
public static Builder builder() {
return new Builder(DEFAULT_APPLICATION_NAME);
}
/**
* Create a new {@link Builder} for {@code GenomicsFactory} objects.
*
* @param applicationName The name of this application.
* @return the new {@code Builder} object.
*/
public static Builder builder(String applicationName) {
return new Builder(applicationName);
}
private final String applicationName;
private final HttpTransport httpTransport;
private final int connectTimeout;
private final int readTimeout;
private final int numRetries;
private final JsonFactory jsonFactory;
private final Optional<String> rootUrl;
private final Optional<String> servicePath;
private final Collection<String> scopes;
private final AtomicInteger initializedRequestsCount = new AtomicInteger();
private final AtomicInteger unsuccessfulResponsesCount = new AtomicInteger();
private final AtomicInteger ioExceptionsCount = new AtomicInteger();
private GenomicsFactory(
String applicationName,
HttpTransport httpTransport,
int connectTimeout,
int readTimeout,
int numRetries,
JsonFactory jsonFactory,
Collection<String> scopes,
Optional<String> rootUrl,
Optional<String> servicePath) {
this.applicationName = applicationName;
this.httpTransport = httpTransport;
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
this.numRetries = numRetries;
this.jsonFactory = jsonFactory;
this.scopes = scopes;
this.rootUrl = rootUrl;
this.servicePath = servicePath;
}
private <T extends AbstractGoogleJsonClient.Builder> T prepareBuilder(T builder,
final HttpRequestInitializer delegate,
GoogleClientRequestInitializer googleClientRequestInitializer) {
builder
.setHttpRequestInitializer(getHttpRequestInitializer(delegate))
.setApplicationName(applicationName)
.setGoogleClientRequestInitializer(googleClientRequestInitializer);
if (rootUrl.isPresent()) {
builder.setRootUrl(rootUrl.get());
}
if (servicePath.isPresent()) {
builder.setServicePath(servicePath.get());
}
return builder;
}
private Genomics.Builder getGenomicsBuilder() {
return new Genomics.Builder(httpTransport, jsonFactory, null);
}
private HttpRequestInitializer getHttpRequestInitializer(final HttpRequestInitializer delegate) {
return new HttpRequestInitializer() {
@Override public void initialize(final HttpRequest request) throws IOException {
initializedRequestsCount.incrementAndGet();
if (null != delegate) {
delegate.initialize(request);
}
final HttpBackOffUnsuccessfulResponseHandler unsuccessfulResponseHandler
= new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff());
final HttpIOExceptionHandler ioExceptionHandler
= new HttpBackOffIOExceptionHandler(new ExponentialBackOff());
request
.setConnectTimeout(connectTimeout)
.setReadTimeout(readTimeout)
.setNumberOfRetries(numRetries)
.setUnsuccessfulResponseHandler(
new HttpUnsuccessfulResponseHandler() {
@Nullable private final HttpUnsuccessfulResponseHandler
delegate = request.getUnsuccessfulResponseHandler();
@Override public boolean handleResponse(HttpRequest req,
HttpResponse response, boolean supportsRetry) throws IOException {
unsuccessfulResponsesCount.incrementAndGet();
return (null != delegate
&& delegate.handleResponse(req, response, supportsRetry))
|| unsuccessfulResponseHandler.handleResponse(
req, response, supportsRetry);
}
})
.setIOExceptionHandler(
new HttpIOExceptionHandler() {
@Nullable private final HttpIOExceptionHandler
delegate = request.getIOExceptionHandler();
@Override public boolean handleIOException(HttpRequest req,
boolean supportsRetry) throws IOException {
ioExceptionsCount.incrementAndGet();
return (null != delegate
&& delegate.handleIOException(req, supportsRetry))
|| ioExceptionHandler.handleIOException(req, supportsRetry);
}
});
}
};
}
public HttpTransport getHttpTransport() {
return httpTransport;
}
public JsonFactory getJsonFactory() {
return jsonFactory;
}
public final int initializedRequestsCount() {
return initializedRequestsCount.get();
}
public final int unsuccessfulResponsesCount() {
return unsuccessfulResponsesCount.get();
}
public final int ioExceptionsCount() {
return ioExceptionsCount.get();
}
/**
* Create a {@link Genomics} stub using an API key.
*
* @param apiKey The API key of the Google Cloud Platform project.
* @return The new {@code Genomics} stub
*/
public Genomics fromApiKey(String apiKey) {
Preconditions.checkNotNull(apiKey);
return fromApiKey(getGenomicsBuilder(), apiKey).build();
}
/**
* Prepare an AbstractGoogleJsonClient.Builder using an API key.
*
* @param builder The builder to be prepared.
* @param apiKey The API key of the Google Cloud Platform project.
* @return The passed in builder, for easy chaining.
*/
public <T extends AbstractGoogleJsonClient.Builder> T fromApiKey(T builder, String apiKey) {
Preconditions.checkNotNull(builder);
Preconditions.checkNotNull(apiKey);
return prepareBuilder(builder, null, new CommonGoogleClientRequestInitializer(apiKey));
}
/**
* Create a {@link Genomics} stub using a {@code client_secrets.json} {@link File}.
*
* @param clientSecretsJson {@code client_secrets.json} file.
* @return The new {@code Genomics} stub
*/
public Genomics fromClientSecretsFile(File clientSecretsJson) {
Preconditions.checkNotNull(clientSecretsJson);
return fromClientSecretsFile(getGenomicsBuilder(), clientSecretsJson).build();
}
/**
* Prepare an AbstractGoogleJsonClient.Builder using a {@code client_secrets.json} {@link File}.
*
* @param builder The builder to be prepared.
* @param clientSecretsJson {@code client_secrets.json} file.
* @return The passed in builder, for easy chaining.
*/
public <T extends AbstractGoogleJsonClient.Builder> T fromClientSecretsFile(T builder,
File clientSecretsJson) {
Preconditions.checkNotNull(builder);
Preconditions.checkNotNull(clientSecretsJson);
return prepareBuilder(builder,
CredentialFactory.getCredentialFromClientSecrets(clientSecretsJson.getAbsolutePath(),
applicationName),
null);
}
/**
* Create a {@link Genomics} stub using a credential.
*
* @param credential The credential to be used for requests.
* @return The new {@code Genomics} stub
*/
public Genomics fromCredential(Credential credential) {
Preconditions.checkNotNull(credential);
return fromCredential(getGenomicsBuilder(), credential).build();
}
/**
* Prepare an AbstractGoogleJsonClient.Builder using a credential.
*
* @param builder The builder to be prepared.
* @param credential The credential to be used for requests.
* @return The passed in builder, for easy chaining.
*/
public <T extends AbstractGoogleJsonClient.Builder> T fromCredential(T builder, Credential credential) {
Preconditions.checkNotNull(builder);
Preconditions.checkNotNull(credential);
return prepareBuilder(builder, credential, null);
}
/**
* Create a {@link Genomics} stub using the Application Default Credential.
*
* @return The new {@code Genomics} stub
*/
public Genomics fromApplicationDefaultCredential() {
return fromCredential(CredentialFactory.getApplicationDefaultCredential());
}
/**
* Prepare an AbstractGoogleJsonClient.Builder using the Application Default Credential.
*
* @param builder The builder to be prepared.
* @return The passed in builder, for easy chaining.
*/
public <T extends AbstractGoogleJsonClient.Builder> T fromApplicationDefaultCredential(T builder) {
Preconditions.checkNotNull(builder);
return fromCredential(builder, CredentialFactory.getApplicationDefaultCredential());
}
/**
* Create a new genomics stub from the given service account ID and private key {@link File}.
*
* @param serviceAccountId The service account ID (typically an email address)
* @param p12File The file on disk containing the private key
* @return The new {@code Genomics} stub
* @throws GeneralSecurityException
* @throws IOException
*/
public Genomics fromServiceAccount(String serviceAccountId, File p12File)
throws GeneralSecurityException, IOException {
Preconditions.checkNotNull(serviceAccountId);
Preconditions.checkNotNull(p12File);
return fromServiceAccount(getGenomicsBuilder(), serviceAccountId, p12File).build();
}
/**
* Prepare an AbstractGoogleJsonClient.Builder with the given service account ID
* and private key {@link File}.
*
* @param builder The builder to be prepared.
* @param serviceAccountId The service account ID (typically an email address)
* @param p12File The file on disk containing the private key
* @return The passed in builder, for easy chaining.
* @throws GeneralSecurityException
* @throws IOException
*/
public <T extends AbstractGoogleJsonClient.Builder> T fromServiceAccount(T builder,
String serviceAccountId, File p12File) throws GeneralSecurityException, IOException {
Preconditions.checkNotNull(builder);
GoogleCredential creds = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountId)
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKeyFromP12File(p12File)
.build();
creds.refreshToken();
return prepareBuilder(builder, creds, null);
}
/**
* Create a new genomics stub from the given OfflineAuth object.
*
* @param auth The OfflineAuth
* @return The new {@code Genomics} stub
*/
public Genomics fromOfflineAuth(OfflineAuth auth) {
Preconditions.checkNotNull(auth);
return fromOfflineAuth(getGenomicsBuilder(), auth).build();
}
/**
* Prepare an AbstractGoogleJsonClient.Builder with the given OfflineAuth object.
*
* @param builder The builder to be prepared.
* @param auth The OfflineAuth
* @return The passed in builder, for easy chaining.
*/
public <T extends AbstractGoogleJsonClient.Builder> T fromOfflineAuth(T builder, OfflineAuth auth) {
Preconditions.checkNotNull(builder);
Preconditions.checkNotNull(auth);
if(auth.hasApiKey()) {
return fromApiKey(builder, auth.getApiKey());
}
return fromCredential(builder, auth.getCredential());
}
}