Skip to content

Commit b016a2d

Browse files
committed
api: Default transport and Json methods. Overloads of getDefault and fromStream on GoogleCredential using defaults.
https://codereview.appspot.com/79890047/
1 parent 7fbab07 commit b016a2d

21 files changed

+131
-20
lines changed

google-api-client/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@
6464
</dependency>
6565
<dependency>
6666
<groupId>com.google.http-client</groupId>
67-
<artifactId>google-http-client-gson</artifactId>
68-
<scope>test</scope>
67+
<artifactId>google-http-client-jackson2</artifactId>
6968
</dependency>
7069
<dependency>
7170
<groupId>com.google.http-client</groupId>
72-
<artifactId>google-http-client-jackson</artifactId>
71+
<artifactId>google-http-client-gson</artifactId>
7372
<scope>test</scope>
7473
</dependency>
7574
<dependency>

google-api-client/src/main/java/com/google/api/client/googleapis/GoogleUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
package com.google.api.client.googleapis;
1616

17+
import com.google.api.client.http.HttpTransport;
18+
import com.google.api.client.http.javanet.NetHttpTransport;
19+
import com.google.api.client.json.JsonFactory;
20+
import com.google.api.client.json.jackson2.JacksonFactory;
21+
import com.google.api.client.util.Beta;
1722
import com.google.api.client.util.SecurityUtils;
1823

1924
import java.io.IOException;
@@ -78,6 +83,38 @@ public static synchronized KeyStore getCertificateTrustStore()
7883
return certTrustStore;
7984
}
8085

86+
/**
87+
* {@link Beta} <br/>
88+
* Returns a cached default implementation of the JsonFactory interface.
89+
*/
90+
@Beta
91+
public static JsonFactory getDefaultJsonFactory() {
92+
return JsonFactoryInstanceHolder.INSTANCE;
93+
}
94+
95+
@Beta
96+
private static class JsonFactoryInstanceHolder {
97+
// The jackson2.JacksonFactory was introduced as a product dependency in 1.19 to enable
98+
// other APIs to not require one of these for input. This was the most commonly used
99+
// implementation in public samples. This is a compile-time dependency to help detect the
100+
// dependency as early as possible.
101+
static final JsonFactory INSTANCE = new JacksonFactory();
102+
}
103+
104+
/**
105+
* {@link Beta} <br/>
106+
* Returns a cached default implementation of the HttpTransport interface.
107+
*/
108+
@Beta
109+
public static HttpTransport getDefaultTransport() {
110+
return TransportInstanceHolder.INSTANCE;
111+
}
112+
113+
@Beta
114+
private static class TransportInstanceHolder {
115+
static final HttpTransport INSTANCE = new NetHttpTransport();
116+
}
117+
81118
private GoogleUtils() {
82119
}
83120
}

google-api-client/src/main/java/com/google/api/client/googleapis/auth/oauth2/GoogleCredential.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.api.client.auth.oauth2.DataStoreCredentialRefreshListener;
2222
import com.google.api.client.auth.oauth2.TokenRequest;
2323
import com.google.api.client.auth.oauth2.TokenResponse;
24+
import com.google.api.client.googleapis.GoogleUtils;
2425
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.Details;
2526
import com.google.api.client.http.GenericUrl;
2627
import com.google.api.client.http.HttpExecuteInterceptor;
@@ -173,6 +174,23 @@ public class GoogleCredential extends Credential {
173174
private static DefaultCredentialProvider defaultCredentialProvider =
174175
new DefaultCredentialProvider();
175176

177+
/**
178+
* {@link Beta} <br/>
179+
* Returns a default credential for the application.
180+
*
181+
* <p>Returns the built-in service account's credential for the application if running on
182+
* Google App Engine or Google Compute Engine, or returns the credential pointed to by the
183+
* environment variable GOOGLE_CREDENTIALS_DEFAULT.
184+
* </p>
185+
*
186+
* @return the credential instance.
187+
* @throws IOException if the credential cannot be created in the current environment.
188+
*/
189+
@Beta
190+
public static GoogleCredential getDefault() throws IOException {
191+
return getDefault(GoogleUtils.getDefaultTransport(), GoogleUtils.getDefaultJsonFactory());
192+
}
193+
176194
/**
177195
* {@link Beta} <br/>
178196
* Returns a default credential for the application.
@@ -195,6 +213,22 @@ public static GoogleCredential getDefault(HttpTransport transport, JsonFactory j
195213
return defaultCredentialProvider.getDefaultCredential(transport, jsonFactory);
196214
}
197215

216+
/**
217+
* {@link Beta} <br/>
218+
* Return a credential defined by a Json file.
219+
*
220+
* @param credentialStream the stream with the credential definition.
221+
* @return the credential defined by the credentialStream.
222+
* @throws IOException if the credential cannot be created from the stream.
223+
*/
224+
@Beta
225+
public static GoogleCredential fromStream(InputStream credentialStream) throws IOException {
226+
return fromStream(
227+
credentialStream,
228+
GoogleUtils.getDefaultTransport(),
229+
GoogleUtils.getDefaultJsonFactory());
230+
}
231+
198232
/**
199233
* {@link Beta} <br/>
200234
* Return a credential defined by a Json file.

google-api-client/src/test/java/com/google/api/client/googleapis/GoogleUtilsTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@
1414

1515
package com.google.api.client.googleapis;
1616

17+
import com.google.api.client.http.HttpTransport;
18+
import com.google.api.client.http.javanet.NetHttpTransport;
19+
import com.google.api.client.json.JsonFactory;
20+
import com.google.api.client.json.jackson2.JacksonFactory;
21+
1722
import junit.framework.TestCase;
1823

24+
import java.io.IOException;
25+
import java.net.URLDecoder;
1926
import java.security.KeyStore;
2027
import java.util.Enumeration;
28+
import java.util.HashMap;
29+
import java.util.Map;
2130

2231
/**
2332
* Tests {@link GoogleUtils}.
@@ -37,4 +46,36 @@ public void testGetCertificateTrustStore() throws Exception {
3746
// has been added or removed
3847
assertEquals(70, trustStore.size());
3948
}
49+
50+
public void testGetDefaultJsonFactory() {
51+
JsonFactory jsonFactory = GoogleUtils.getDefaultJsonFactory();
52+
assertNotNull(jsonFactory);
53+
assertTrue(jsonFactory instanceof JacksonFactory);
54+
JsonFactory secondCall = GoogleUtils.getDefaultJsonFactory();
55+
assertSame(jsonFactory, secondCall);
56+
}
57+
58+
public void testGetDefaultTransport() {
59+
HttpTransport transport = GoogleUtils.getDefaultTransport();
60+
assertNotNull(transport);
61+
assertTrue(transport instanceof NetHttpTransport);
62+
HttpTransport secondCall = GoogleUtils.getDefaultTransport();
63+
assertSame(transport, secondCall);
64+
}
65+
66+
public static Map<String, String> parseQuery(String query) throws IOException {
67+
Map<String, String> map = new HashMap<String, String>();
68+
String[] entries = query.split("&");
69+
for (String entry : entries) {
70+
String[] sides = entry.split("=");
71+
if (sides.length != 2) {
72+
throw new IOException("Invalid Query String");
73+
}
74+
String key = URLDecoder.decode(sides[0], "UTF-8");
75+
String value = URLDecoder.decode(sides[1], "UTF-8");
76+
map.put(key, value);
77+
}
78+
return map;
79+
}
80+
4081
}

google-api-client/src/test/java/com/google/api/client/googleapis/auth/oauth2/DefaultCredentialProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.google.api.client.http.LowLevelHttpResponse;
2222
import com.google.api.client.json.GenericJson;
2323
import com.google.api.client.json.JsonFactory;
24-
import com.google.api.client.json.jackson.JacksonFactory;
24+
import com.google.api.client.json.jackson2.JacksonFactory;
2525
import com.google.api.client.testing.http.MockHttpTransport;
2626
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
2727
import com.google.api.client.util.Joiner;

google-api-client/src/test/java/com/google/api/client/googleapis/auth/oauth2/GoogleAuthorizationCodeFlowTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package com.google.api.client.googleapis.auth.oauth2;
1616

17-
import com.google.api.client.json.jackson.JacksonFactory;
17+
import com.google.api.client.json.jackson2.JacksonFactory;
1818
import com.google.api.client.testing.http.MockHttpTransport;
1919
import com.google.common.collect.ImmutableList;
2020

google-api-client/src/test/java/com/google/api/client/googleapis/auth/oauth2/GoogleAuthorizationCodeTokenRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.google.api.client.googleapis.auth.oauth2;
1616

1717
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
18-
import com.google.api.client.json.jackson.JacksonFactory;
18+
import com.google.api.client.json.jackson2.JacksonFactory;
1919
import com.google.api.client.testing.http.MockHttpTransport;
2020

2121
import junit.framework.TestCase;

google-api-client/src/test/java/com/google/api/client/googleapis/auth/oauth2/GoogleCredentialTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.google.api.client.http.HttpTransport;
1818
import com.google.api.client.json.GenericJson;
1919
import com.google.api.client.json.JsonFactory;
20-
import com.google.api.client.json.jackson.JacksonFactory;
20+
import com.google.api.client.json.jackson2.JacksonFactory;
2121
import com.google.api.client.testing.http.MockHttpTransport;
2222
import com.google.api.client.testing.util.SecurityTestUtils;
2323
import com.google.api.client.util.Joiner;

google-api-client/src/test/java/com/google/api/client/googleapis/auth/oauth2/GoogleIdTokenVerifierTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import com.google.api.client.auth.openidconnect.IdTokenVerifier;
1818
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
19-
import com.google.api.client.json.jackson.JacksonFactory;
19+
import com.google.api.client.json.jackson2.JacksonFactory;
2020
import com.google.api.client.json.webtoken.JsonWebSignature.Header;
2121
import com.google.api.client.testing.http.FixedClock;
2222
import com.google.api.client.util.Clock;

google-api-client/src/test/java/com/google/api/client/googleapis/auth/oauth2/GooglePublicKeysManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.google.api.client.http.LowLevelHttpResponse;
2121
import com.google.api.client.json.Json;
2222
import com.google.api.client.json.JsonFactory;
23-
import com.google.api.client.json.jackson.JacksonFactory;
23+
import com.google.api.client.json.jackson2.JacksonFactory;
2424
import com.google.api.client.testing.http.FixedClock;
2525
import com.google.api.client.testing.http.MockHttpTransport;
2626
import com.google.api.client.testing.http.MockLowLevelHttpRequest;

0 commit comments

Comments
 (0)