Skip to content

Commit c7059ae

Browse files
author
Ajay Kannan
committed
fix merge conflict
2 parents d8478d7 + 2171251 commit c7059ae

3 files changed

Lines changed: 80 additions & 26 deletions

File tree

gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.api.services.datastore.client.DatastoreException;
3333
import com.google.api.services.datastore.client.DatastoreFactory;
3434
import com.google.api.services.datastore.client.DatastoreOptions.Builder;
35+
import com.google.common.base.Preconditions;
3536
import com.google.common.collect.ImmutableMap;
3637
import com.google.gcloud.datastore.DatastoreOptions;
3738
import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason;
@@ -40,6 +41,10 @@
4041
import org.json.JSONObject;
4142
import org.json.JSONTokener;
4243

44+
import java.net.InetAddress;
45+
import java.net.MalformedURLException;
46+
import java.net.URL;
47+
import java.net.UnknownHostException;
4348
import java.util.HashMap;
4449
import java.util.Map;
4550

@@ -62,14 +67,45 @@ public class DefaultDatastoreRpc implements DatastoreRpc {
6267
}
6368

6469
public DefaultDatastoreRpc(DatastoreOptions options) {
70+
String normalizedHost = normalizeHost(options.host());
6571
client = DatastoreFactory.get().create(
6672
new Builder()
6773
.dataset(options.projectId())
68-
.host(options.host())
74+
.host(normalizedHost)
6975
.initializer(options.httpRequestInitializer())
7076
.build());
7177
}
7278

79+
private static String normalizeHost(String host) {
80+
host = host.toLowerCase();
81+
if (includesScheme(host)) {
82+
Preconditions.checkArgument(!(host.startsWith("https://") && isLocalHost(host)),
83+
"\"https\" is not supported for localhost. Use \"http\" instead.");
84+
return host;
85+
}
86+
return "http://" + host;
87+
}
88+
89+
private static boolean isLocalHost(String host) {
90+
if (host != null) {
91+
try {
92+
String normalizedHost = host;
93+
if (!includesScheme(normalizedHost)) {
94+
normalizedHost = "http://" + normalizedHost;
95+
}
96+
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
97+
return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress();
98+
} catch (UnknownHostException | MalformedURLException e) {
99+
// ignore
100+
}
101+
}
102+
return false;
103+
}
104+
105+
private static boolean includesScheme(String url) {
106+
return url.startsWith("http://") || url.startsWith("https://");
107+
}
108+
73109
private static DatastoreRpcException translate(DatastoreException exception) {
74110
String message = exception.getMessage();
75111
String reasonStr = "";

gcloud-java-storage/src/main/java/com/google/gcloud/storage/testing/RemoteGcsHelper.java

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.gcloud.AuthCredentials;
2121
import com.google.gcloud.storage.BlobInfo;
22+
import com.google.gcloud.RetryParams;
2223
import com.google.gcloud.storage.Storage;
2324
import com.google.gcloud.storage.StorageException;
2425
import com.google.gcloud.storage.StorageOptions;
@@ -96,50 +97,37 @@ public static String generateBucketName() {
9697
}
9798

9899
/**
99-
* Creates a {@code RemoteGcsHelper} object.
100+
* Creates a {@code RemoteGcsHelper} object for the given project id and JSON key path.
100101
*
102+
* @param projectId id of the project to be used for running the tests
103+
* @param keyPath path to the JSON key to be used for running the tests
101104
* @param options creation options
102105
* @return A {@code RemoteGcsHelper} object for the provided options.
103-
* @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if environment variables
104-
* {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
105-
* pointed by {@code GCLOUD_TESTS_KEY} does not exist
106+
* @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if the file pointed by
107+
* {@code keyPath} does not exist
106108
*/
107-
public static RemoteGcsHelper create(Option... options) throws GcsHelperException {
109+
public static RemoteGcsHelper create(String projectId, String keyPath, Option... options)
110+
throws GcsHelperException {
108111
boolean keyFromClassPath = false;
109112
Map<Class<? extends Option>, Option> optionsMap = Option.asImmutableMap(options);
110113
if (optionsMap.containsKey(KeyFromClasspath.class)) {
111114
keyFromClassPath =
112115
((KeyFromClasspath) optionsMap.get(KeyFromClasspath.class)).keyFromClasspath();
113116
}
114-
String projectId = System.getenv(PROJECT_ID_ENV_VAR);
115-
String stringKeyPath = System.getenv(PRIVATE_KEY_ENV_VAR);
116-
if (projectId == null) {
117-
String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set";
118-
if (log.isLoggable(Level.WARNING)) {
119-
log.log(Level.WARNING, message);
120-
}
121-
throw new GcsHelperException(message);
122-
}
123-
if (stringKeyPath == null) {
124-
String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set";
125-
if (log.isLoggable(Level.WARNING)) {
126-
log.log(Level.WARNING, message);
127-
}
128-
throw new GcsHelperException(message);
129-
}
130117
try {
131118
InputStream keyFileStream;
132119
if (keyFromClassPath) {
133-
keyFileStream = RemoteGcsHelper.class.getResourceAsStream(stringKeyPath);
120+
keyFileStream = RemoteGcsHelper.class.getResourceAsStream(keyPath);
134121
if (keyFileStream == null) {
135-
throw new FileNotFoundException(stringKeyPath + " not found in classpath");
122+
throw new FileNotFoundException(keyPath + " not found in classpath");
136123
}
137124
} else {
138-
keyFileStream = new FileInputStream(stringKeyPath);
125+
keyFileStream = new FileInputStream(keyPath);
139126
}
140127
StorageOptions storageOptions = StorageOptions.builder()
141128
.authCredentials(AuthCredentials.createForJson(keyFileStream))
142129
.projectId(projectId)
130+
.retryParams(RetryParams.getDefaultInstance())
143131
.build();
144132
return new RemoteGcsHelper(storageOptions);
145133
} catch (FileNotFoundException ex) {
@@ -155,6 +143,36 @@ public static RemoteGcsHelper create(Option... options) throws GcsHelperExceptio
155143
}
156144
}
157145

146+
/**
147+
* Creates a {@code RemoteGcsHelper} object. Project id and path to JSON key are read from two
148+
* environment variables: {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY}.
149+
*
150+
* @param options creation options
151+
* @return A {@code RemoteGcsHelper} object for the provided options.
152+
* @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if environment variables
153+
* {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
154+
* pointed by {@code GCLOUD_TESTS_KEY} does not exist
155+
*/
156+
public static RemoteGcsHelper create(Option... options) throws GcsHelperException {
157+
String projectId = System.getenv(PROJECT_ID_ENV_VAR);
158+
String keyPath = System.getenv(PRIVATE_KEY_ENV_VAR);
159+
if (projectId == null) {
160+
String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set";
161+
if (log.isLoggable(Level.WARNING)) {
162+
log.log(Level.WARNING, message);
163+
}
164+
throw new GcsHelperException(message);
165+
}
166+
if (keyPath == null) {
167+
String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set";
168+
if (log.isLoggable(Level.WARNING)) {
169+
log.log(Level.WARNING, message);
170+
}
171+
throw new GcsHelperException(message);
172+
}
173+
return create(projectId, keyPath, options);
174+
}
175+
158176
private static class DeleteBucketTask implements Callable<Boolean> {
159177

160178
private Storage storage;

gcloud-java-storage/src/test/java/com/google/gcloud/storage/ITStorageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static void beforeClass() {
6767
@AfterClass
6868
public static void afterClass()
6969
throws ExecutionException, TimeoutException, InterruptedException {
70-
if (!RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS)) {
70+
if (storage != null && !RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS)) {
7171
if (log.isLoggable(Level.WARNING)) {
7272
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", bucket);
7373
}

0 commit comments

Comments
 (0)