11package com .google .gcloud ;
22
3- import java .util .Arrays ;
4- import java .util .List ;
53
6- import com .google .api .client .auth .oauth2 .Credential ;
4+ import com .google .api .client .googleapis .compute .ComputeCredential ;
5+ import com .google .api .client .googleapis .javanet .GoogleNetHttpTransport ;
76import com .google .api .client .http .HttpRequestInitializer ;
87import com .google .api .client .http .HttpTransport ;
8+ import com .google .api .client .http .javanet .NetHttpTransport ;
9+ import com .google .api .client .json .jackson .JacksonFactory ;
10+ import com .google .common .base .MoreObjects ;
11+ import com .google .common .base .Preconditions ;
912
10- public class ServiceOptions {
13+ import java .io .IOException ;
14+ import java .security .GeneralSecurityException ;
15+ import java .security .PrivateKey ;
16+ import java .util .Set ;
17+
18+ public abstract class ServiceOptions {
1119
12- private final String host ;
1320 private static final String DEFAULT_HOST = "https://www.googleapis.com" ;
14- private final HttpRequestInitializer initializer ;
15- private final Credential credential ;
16- private final HttpTransport transport ;
17-
18- public static final List <String > SCOPES = Arrays .asList (
19- "https://www.googleapis.com/auth/datastore" ,
20- "https://www.googleapis.com/auth/userinfo.email" );
21-
22- ServiceOptions (Builder b ) {
23- this .dataset = b .dataset ;
24- this .host = b .host != null ? b .host : DEFAULT_HOST ;
25- this .initializer = b .initializer ;
26- this .credential = b .credential ;
27- this .transport = b .transport ;
21+
22+ private final String host ;
23+ private final HttpTransport httpTransport ;
24+ private final HttpRequestInitializer httpRequestInitializer ;
25+
26+ protected ServiceOptions (Builder builder ) {
27+ host = MoreObjects .firstNonNull (builder .host , DEFAULT_HOST );
28+ httpTransport = MoreObjects .firstNonNull (builder .httpTransport , getDefaultHttpTransport ());
29+ httpRequestInitializer = builder .httpRequestInitializer ;
2830 }
2931
30- /**
31- * Builder for {@link ServiceOptions}.
32- */
33- protected static class Builder {
34- private String dataset ;
35- private String host ;
36- private HttpRequestInitializer initializer ;
37- private Credential credential ;
38- private HttpTransport transport ;
39-
40- public Builder () { }
41-
42- public Builder (ServiceOptions options ) {
43- this .dataset = options .dataset ;
44- this .host = options .host ;
45- this .initializer = options .initializer ;
46- this .credential = options .credential ;
47- this .transport = options .transport ;
32+ private static HttpTransport getDefaultHttpTransport () {
33+ try {
34+ NetHttpTransport transport = GoogleNetHttpTransport .newTrustedTransport ();
35+ // Try to connect using Google Compute Engine service account credentials.
36+ ComputeCredential credential = new ComputeCredential (transport , new JacksonFactory ());
37+ // Force token refresh to detect if we are running on Google Compute Engine.
38+ credential .refreshToken ();
39+ return credential .getTransport ();
40+ } catch (IOException | GeneralSecurityException e ) {
41+ return new NetHttpTransport ();
4842 }
43+ }
4944
50- public ServiceOptions build () {
51- return new ServiceOptions (this );
52- }
45+ protected abstract static class Builder {
5346
54- /**
55- * Sets the dataset used to access the datastore.
56- */
57- public Builder dataset (String newDataset ) {
58- dataset = newDataset ;
59- return this ;
47+ private String host ;
48+ private HttpTransport httpTransport ;
49+ private HttpRequestInitializer httpRequestInitializer ;
50+ private PrivateKey privateKey ;
51+
52+ public Builder () {}
53+
54+ protected Builder (ServiceOptions options ) {
55+ host = options .host ;
56+ httpTransport = options .httpTransport ;
57+ httpRequestInitializer = options .httpRequestInitializer ;
6058 }
6159
62- /**
63- * Sets the host used to access the datastore.
64- */
65- public Builder host (String newHost ) {
66- host = newHost ;
60+ protected abstract ServiceOptions build ();
61+
62+ public Builder setHost (String host ) {
63+ this .host = host ;
6764 return this ;
6865 }
6966
70- /**
71- * Sets the (optional) initializer to run on HTTP requests to the API.
72- */
73- public Builder initializer (HttpRequestInitializer newInitializer ) {
74- initializer = newInitializer ;
67+ public Builder setHttpTransport (HttpTransport httpTransport ) {
68+ this .httpTransport = httpTransport ;
7569 return this ;
7670 }
7771
78- /**
79- * Sets the Google APIs credentials used to access the API.
80- */
81- public Builder credential (Credential newCredential ) {
82- credential = newCredential ;
72+ public Builder setHttpRequestInitializer (HttpRequestInitializer httpRequestInitializer ) {
73+ // TODO: replace HttpRequestInitializer with CrendentialProvider - 2 subclasses
74+ // one that is set with HttpRequestInitializer (and another that is set
75+ // with both private key and service account)
76+ // Also, consider instead of HttpRequestIntializer option to have "AppEngine" option
77+ // which will use reflection to create HttpRequestInitializer
78+ Preconditions .checkArgument (
79+ privateKey == null , "Can't set both PrivateKey and HttpRequestInitializer" );
80+ this .httpRequestInitializer = httpRequestInitializer ;
8381 return this ;
8482 }
8583
86- /**
87- * Sets the transport used to access the API.
88- */
89- public Builder transport (HttpTransport transport ) {
90- this .transport = transport ;
84+ public Builder setPrivateKey (PrivateKey privateKey ) {
85+ Preconditions .checkArgument (
86+ httpRequestInitializer == null , "Can't set both PrivateKey and HttpRequestInitializer" );
87+ this .privateKey = privateKey ;
9188 return this ;
9289 }
9390 }
9491
95- // === getters ===
96-
97- public String getDataset () {
98- return dataset ;
99- }
92+ protected abstract Set <String > getScopes ();
10093
10194 public String getHost () {
10295 return host ;
10396 }
10497
105- public HttpRequestInitializer getInitializer () {
106- return initializer ;
107- }
108-
109- public Credential getCredential () {
110- return credential ;
98+ public HttpTransport getHttpTransport () {
99+ return httpTransport ;
111100 }
112101
113- public HttpTransport getTransport () {
114- return transport ;
102+ public HttpRequestInitializer getHttpRequestInitializer () {
103+ return httpRequestInitializer ;
115104 }
116- }
105+ }
0 commit comments