Skip to content

Commit a5fa13a

Browse files
---
yaml --- r: 8601 b: refs/heads/master c: f2f910f h: refs/heads/master i: 8599: 9466c84
1 parent c3b6e29 commit a5fa13a

5 files changed

Lines changed: 52 additions & 13 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: fcbf686f6d5076eab0dfa8da023eb9f5958e376c
2+
refs/heads/master: f2f910fd55d05902f1dd7fabec0024256f75034a
33
refs/heads/travis: 47e4fee4fd5af9b2a8ce46f23c72ec95f9b195b2
44
refs/heads/gh-pages: 6daca92127d91b7c2c99490080ecf8a13fa94cde
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444

trunk/google-cloud-core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
<artifactId>joda-time</artifactId>
3333
<scope>compile</scope>
3434
</dependency>
35-
<dependency>
36-
<groupId>org.json</groupId>
37-
<artifactId>json</artifactId>
38-
<scope>compile</scope>
39-
</dependency>
4035
<dependency>
4136
<groupId>com.google.http-client</groupId>
4237
<artifactId>google-http-client</artifactId>

trunk/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
import com.google.api.client.http.HttpResponse;
2828
import com.google.api.client.http.HttpTransport;
2929
import com.google.api.client.http.javanet.NetHttpTransport;
30+
import com.google.api.client.json.GenericJson;
31+
import com.google.api.client.json.JsonFactory;
32+
import com.google.api.client.json.JsonObjectParser;
33+
import com.google.api.client.json.jackson2.JacksonFactory;
34+
import com.google.api.client.util.Charsets;
3035
import com.google.api.core.ApiClock;
3136
import com.google.api.core.BetaApi;
3237
import com.google.api.core.CurrentMillisClock;
@@ -59,9 +64,6 @@
5964
import java.util.Set;
6065
import java.util.regex.Matcher;
6166
import java.util.regex.Pattern;
62-
import org.json.JSONException;
63-
import org.json.JSONObject;
64-
import org.json.JSONTokener;
6567
import org.threeten.bp.Duration;
6668

6769
/**
@@ -454,13 +456,20 @@ private static String getAppEngineProjectIdFromMetadataServer() throws IOExcepti
454456
}
455457

456458
protected static String getServiceAccountProjectId() {
459+
return getServiceAccountProjectId(System.getenv(CREDENTIAL_ENV_NAME));
460+
}
461+
462+
@InternalApi("Visible for testing")
463+
static String getServiceAccountProjectId(String credentialsPath) {
457464
String project = null;
458-
String credentialsPath = System.getenv(CREDENTIAL_ENV_NAME);
459465
if (credentialsPath != null) {
460466
try (InputStream credentialsStream = new FileInputStream(credentialsPath)) {
461-
JSONObject json = new JSONObject(new JSONTokener(credentialsStream));
462-
project = json.getString("project_id");
463-
} catch (IOException | JSONException ex) {
467+
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
468+
JsonObjectParser parser = new JsonObjectParser(jsonFactory);
469+
GenericJson fileContents = parser.parseAndClose(
470+
credentialsStream, Charsets.UTF_8, GenericJson.class);
471+
project = (String) fileContents.get("project_id");
472+
} catch (IOException e) {
464473
// ignore
465474
}
466475
}

trunk/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static junit.framework.TestCase.assertFalse;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertNotEquals;
22+
import static org.junit.Assert.assertNull;
2223
import static org.junit.Assert.assertSame;
2324
import static org.junit.Assert.assertTrue;
2425
import static org.junit.Assert.fail;
@@ -28,10 +29,14 @@
2829
import com.google.auth.oauth2.GoogleCredentials;
2930
import com.google.cloud.spi.ServiceRpcFactory;
3031
import java.io.ByteArrayInputStream;
32+
import java.io.File;
3133
import java.io.IOException;
3234
import java.io.InputStream;
35+
import java.nio.charset.Charset;
3336
import java.util.Set;
3437
import java.util.regex.Pattern;
38+
39+
import com.google.common.io.Files;
3540
import org.junit.Rule;
3641
import org.junit.Test;
3742
import org.junit.rules.ExpectedException;
@@ -283,4 +288,29 @@ public void testBaseHashCode() {
283288
assertEquals(OPTIONS.hashCode(), OPTIONS_COPY.hashCode());
284289
assertNotEquals(DEFAULT_OPTIONS.hashCode(), OPTIONS.hashCode());
285290
}
291+
292+
@Test
293+
public void testGetServiceAccountProjectId() throws Exception {
294+
File credentialsFile = File.createTempFile("credentials", ".json");
295+
credentialsFile.deleteOnExit();
296+
Files.write("{\"project_id\":\"my-project-id\"}".getBytes(), credentialsFile);
297+
298+
assertEquals("my-project-id", ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath()));
299+
}
300+
301+
@Test
302+
public void testGetServiceAccountProjectId_badJson() throws Exception {
303+
File credentialsFile = File.createTempFile("credentials", ".json");
304+
credentialsFile.deleteOnExit();
305+
Files.write("asdfghj".getBytes(), credentialsFile);
306+
307+
assertNull(ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath()));
308+
}
309+
310+
@Test
311+
public void testGetServiceAccountProjectId_nonExistentFile() throws Exception {
312+
File credentialsFile = new File("/doesnotexist");
313+
314+
assertNull(ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath()));
315+
}
286316
}

trunk/google-cloud-spanner/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,10 @@
167167
<groupId>io.opencensus</groupId>
168168
<artifactId>opencensus-contrib-grpc-util</artifactId>
169169
</dependency>
170+
<dependency>
171+
<groupId>org.json</groupId>
172+
<artifactId>json</artifactId>
173+
<scope>test</scope>
174+
</dependency>
170175
</dependencies>
171176
</project>

0 commit comments

Comments
 (0)