diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index ac3f784e2c98..fbceafcd7af1 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -16,7 +16,9 @@ package com.google.cloud.bigquery; -import com.google.cloud.HttpServiceOptions; +import com.google.cloud.HttpTransportOptions; +import com.google.cloud.ServiceOptions; +import com.google.cloud.TransportOptions; import com.google.cloud.bigquery.spi.BigQueryRpc; import com.google.cloud.bigquery.spi.BigQueryRpcFactory; import com.google.cloud.bigquery.spi.DefaultBigQueryRpc; @@ -24,8 +26,9 @@ import java.util.Set; -public class BigQueryOptions extends HttpServiceOptions { +public class BigQueryOptions extends ServiceOptions { + private static final String API_SHORT_NAME = "BigQuery"; private static final String BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery"; private static final Set SCOPES = ImmutableSet.of(BIGQUERY_SCOPE); private static final long serialVersionUID = -2437598817433266049L; @@ -51,7 +54,7 @@ public BigQueryRpc create(BigQueryOptions options) { } public static class Builder extends - HttpServiceOptions.Builder { + ServiceOptions.Builder { private Builder() { } @@ -60,6 +63,15 @@ private Builder(BigQueryOptions options) { super(options); } + @Override + public Builder setTransportOptions(TransportOptions transportOptions) { + if (!(transportOptions instanceof HttpTransportOptions)) { + throw new IllegalArgumentException( + "Only http transport is allowed for " + API_SHORT_NAME + "."); + } + return super.setTransportOptions(transportOptions); + } + @Override public BigQueryOptions build() { return new BigQueryOptions(this); @@ -80,6 +92,19 @@ protected BigQueryRpcFactory getDefaultRpcFactory() { return DefaultBigQueryRpcFactory.INSTANCE; } + @Override + public TransportOptions getDefaultTransportOptions() { + return getDefaultHttpTransportOptions(); + } + + public static HttpTransportOptions getDefaultHttpTransportOptions() { + return HttpTransportOptions.newBuilder().build(); + } + + public HttpTransportOptions getHttpTransportOptions() { + return (HttpTransportOptions) getTransportOptions(); + } + @Override protected Set getScopes() { return SCOPES; diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/DefaultBigQueryRpc.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/DefaultBigQueryRpc.java index 247c9e0db861..069a3df09879 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/DefaultBigQueryRpc.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/DefaultBigQueryRpc.java @@ -58,12 +58,12 @@ import com.google.api.services.bigquery.model.TableList; import com.google.api.services.bigquery.model.TableReference; import com.google.api.services.bigquery.model.TableRow; +import com.google.cloud.HttpTransportOptions; import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.BigQueryOptions; import com.google.common.base.Function; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; - import java.io.IOException; import java.math.BigInteger; import java.util.List; @@ -80,8 +80,9 @@ public class DefaultBigQueryRpc implements BigQueryRpc { private final Bigquery bigquery; public DefaultBigQueryRpc(BigQueryOptions options) { - HttpTransport transport = options.getHttpTransportFactory().create(); - HttpRequestInitializer initializer = options.getHttpRequestInitializer(); + HttpTransportOptions transportOptions = options.getHttpTransportOptions(); + HttpTransport transport = transportOptions.getHttpTransportFactory().create(); + HttpRequestInitializer initializer = transportOptions.getHttpRequestInitializer(options); this.options = options; bigquery = new Bigquery.Builder(transport, new JacksonFactory(), initializer) .setRootUrl(options.getHost()) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java index 330e21b707e1..08b661e9a827 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java @@ -17,6 +17,7 @@ package com.google.cloud.bigquery.testing; import com.google.auth.oauth2.ServiceAccountCredentials; +import com.google.cloud.HttpTransportOptions; import com.google.cloud.RetryParams; import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryException; @@ -36,7 +37,8 @@ * {@link RetryParams#getMaxRetryDelayMillis()} is {@code 30000}, * {@link RetryParams#getTotalRetryPeriodMillis()} is {@code 120000} and * {@link RetryParams#getInitialRetryDelayMillis()} is {@code 250}. - * {@link BigQueryOptions#getConnectTimeout()} and {@link BigQueryOptions#getReadTimeout()} are both + * {@link HttpTransportOptions#getConnectTimeout()} and + * {@link HttpTransportOptions#getReadTimeout()} are both * set to {@code 60000}. */ public class RemoteBigQueryHelper { @@ -88,12 +90,14 @@ public static String generateDatasetName() { public static RemoteBigQueryHelper create(String projectId, InputStream keyStream) throws BigQueryHelperException { try { + HttpTransportOptions transportOptions = BigQueryOptions.getDefaultHttpTransportOptions(); + transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000) + .build(); BigQueryOptions bigqueryOptions = BigQueryOptions.newBuilder() .setCredentials(ServiceAccountCredentials.fromStream(keyStream)) .setProjectId(projectId) .setRetryParams(retryParams()) - .setConnectTimeout(60000) - .setReadTimeout(60000) + .setTransportOptions(transportOptions) .build(); return new RemoteBigQueryHelper(bigqueryOptions); } catch (IOException ex) { @@ -109,10 +113,12 @@ public static RemoteBigQueryHelper create(String projectId, InputStream keyStrea * credentials. */ public static RemoteBigQueryHelper create() { + HttpTransportOptions transportOptions = BigQueryOptions.getDefaultHttpTransportOptions(); + transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000) + .build(); BigQueryOptions bigqueryOptions = BigQueryOptions.newBuilder() .setRetryParams(retryParams()) - .setConnectTimeout(60000) - .setReadTimeout(60000) + .setTransportOptions(transportOptions) .build(); return new RemoteBigQueryHelper(bigqueryOptions); } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index 3a56df01c559..9cc9e02dd100 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -44,7 +44,9 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; - +import java.math.BigInteger; +import java.util.List; +import java.util.Map; import org.easymock.Capture; import org.easymock.EasyMock; import org.junit.After; @@ -53,10 +55,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; -import java.math.BigInteger; -import java.util.List; -import java.util.Map; - public class BigQueryImplTest { private static final String PROJECT = "project"; diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryOptionsTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryOptionsTest.java new file mode 100644 index 000000000000..d10686841627 --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryOptionsTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * 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.bigquery; + +import com.google.cloud.GrpcTransportOptions; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class BigQueryOptionsTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testInvalidTransport() { + thrown.expect(IllegalArgumentException.class); + BigQueryOptions.newBuilder().setTransportOptions(GrpcTransportOptions.newBuilder().build()); + } + +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java index 2595406b747c..dd4a697a9c45 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java @@ -22,16 +22,14 @@ import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; import com.google.cloud.bigquery.BigQueryOptions; - +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.concurrent.ExecutionException; import org.easymock.EasyMock; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.concurrent.ExecutionException; - public class RemoteBigQueryHelperTest { private static final String DATASET_NAME = "dataset-name"; @@ -82,8 +80,8 @@ public void testCreateFromStream() { RemoteBigQueryHelper helper = RemoteBigQueryHelper.create(PROJECT_ID, JSON_KEY_STREAM); BigQueryOptions options = helper.getOptions(); assertEquals(PROJECT_ID, options.getProjectId()); - assertEquals(60000, options.getConnectTimeout()); - assertEquals(60000, options.getReadTimeout()); + assertEquals(60000, options.getHttpTransportOptions().getConnectTimeout()); + assertEquals(60000, options.getHttpTransportOptions().getReadTimeout()); assertEquals(10, options.getRetryParams().getRetryMaxAttempts()); assertEquals(6, options.getRetryParams().getRetryMinAttempts()); assertEquals(30000, options.getRetryParams().getMaxRetryDelayMillis()); diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeOptions.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeOptions.java index 176417ab2d39..4115e17917e4 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeOptions.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeOptions.java @@ -16,7 +16,9 @@ package com.google.cloud.compute; -import com.google.cloud.HttpServiceOptions; +import com.google.cloud.HttpTransportOptions; +import com.google.cloud.ServiceOptions; +import com.google.cloud.TransportOptions; import com.google.cloud.compute.spi.ComputeRpc; import com.google.cloud.compute.spi.ComputeRpcFactory; import com.google.cloud.compute.spi.DefaultComputeRpc; @@ -24,8 +26,9 @@ import java.util.Set; -public class ComputeOptions extends HttpServiceOptions { +public class ComputeOptions extends ServiceOptions { + private static final String API_SHORT_NAME = "Compute"; private static final String COMPUTE_SCOPE = "https://www.googleapis.com/auth/compute"; private static final Set SCOPES = ImmutableSet.of(COMPUTE_SCOPE); private static final long serialVersionUID = 6983703596543425691L; @@ -51,7 +54,7 @@ public ComputeRpc create(ComputeOptions options) { } public static class Builder extends - HttpServiceOptions.Builder { + ServiceOptions.Builder { private Builder() { } @@ -60,6 +63,15 @@ private Builder(ComputeOptions options) { super(options); } + @Override + public Builder setTransportOptions(TransportOptions transportOptions) { + if (!(transportOptions instanceof HttpTransportOptions)) { + throw new IllegalArgumentException( + "Only http transport is allowed for " + API_SHORT_NAME + "."); + } + return super.setTransportOptions(transportOptions); + } + @Override public ComputeOptions build() { return new ComputeOptions(this); @@ -80,6 +92,19 @@ protected ComputeRpcFactory getDefaultRpcFactory() { return DefaultComputeRpcFactory.INSTANCE; } + @Override + public TransportOptions getDefaultTransportOptions() { + return getDefaultHttpTransportOptions(); + } + + public static HttpTransportOptions getDefaultHttpTransportOptions() { + return HttpTransportOptions.newBuilder().build(); + } + + public HttpTransportOptions getHttpTransportOptions() { + return (HttpTransportOptions) getTransportOptions(); + } + @Override protected Set getScopes() { return SCOPES; diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/spi/DefaultComputeRpc.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/spi/DefaultComputeRpc.java index b0bb3b598399..710b7d5a8f63 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/spi/DefaultComputeRpc.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/spi/DefaultComputeRpc.java @@ -68,10 +68,10 @@ import com.google.api.services.compute.model.Tags; import com.google.api.services.compute.model.Zone; import com.google.api.services.compute.model.ZoneList; +import com.google.cloud.HttpTransportOptions; import com.google.cloud.compute.ComputeException; import com.google.cloud.compute.ComputeOptions; import com.google.common.collect.ImmutableList; - import java.io.IOException; import java.util.Map; @@ -81,8 +81,9 @@ public class DefaultComputeRpc implements ComputeRpc { private final Compute compute; public DefaultComputeRpc(ComputeOptions options) { - HttpTransport transport = options.getHttpTransportFactory().create(); - HttpRequestInitializer initializer = options.getHttpRequestInitializer(); + HttpTransportOptions transportOptions = options.getHttpTransportOptions(); + HttpTransport transport = transportOptions.getHttpTransportFactory().create(); + HttpRequestInitializer initializer = transportOptions.getHttpRequestInitializer(options); this.options = options; compute = new Compute.Builder(transport, new JacksonFactory(), initializer) .setRootUrl(options.getHost()) diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java index dcf4e1b462d1..e2f2ec7aa421 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java @@ -17,6 +17,7 @@ package com.google.cloud.compute.testing; import com.google.auth.oauth2.ServiceAccountCredentials; +import com.google.cloud.HttpTransportOptions; import com.google.cloud.RetryParams; import com.google.cloud.compute.ComputeOptions; @@ -34,7 +35,8 @@ * {@link RetryParams#getMaxRetryDelayMillis()} is {@code 30000}, * {@link RetryParams#getTotalRetryPeriodMillis()} is {@code 120000} and * {@link RetryParams#getInitialRetryDelayMillis()} is {@code 250}. - * {@link ComputeOptions#getConnectTimeout()} and {@link ComputeOptions#getReadTimeout()} are both + * {@link HttpTransportOptions#getConnectTimeout()} and + * {@link HttpTransportOptions#getReadTimeout()} are both * set to {@code 60000}. */ public class RemoteComputeHelper { @@ -82,12 +84,14 @@ public static String baseResourceName() { */ public static RemoteComputeHelper create(String projectId, InputStream keyStream) { try { + HttpTransportOptions transportOptions = ComputeOptions.getDefaultHttpTransportOptions(); + transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000) + .build(); ComputeOptions computeOptions = ComputeOptions.newBuilder() .setCredentials(ServiceAccountCredentials.fromStream(keyStream)) .setProjectId(projectId) .setRetryParams(retryParams()) - .setConnectTimeout(60000) - .setReadTimeout(60000) + .setTransportOptions(transportOptions) .build(); return new RemoteComputeHelper(computeOptions); } catch (IOException ex) { @@ -103,10 +107,12 @@ public static RemoteComputeHelper create(String projectId, InputStream keyStream * credentials. */ public static RemoteComputeHelper create() { + HttpTransportOptions transportOptions = ComputeOptions.getDefaultHttpTransportOptions(); + transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000) + .build(); ComputeOptions computeOptions = ComputeOptions.newBuilder() .setRetryParams(retryParams()) - .setConnectTimeout(60000) - .setReadTimeout(60000) + .setTransportOptions(transportOptions) .build(); return new RemoteComputeHelper(computeOptions); } diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/ComputeOptionsTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/ComputeOptionsTest.java new file mode 100644 index 000000000000..48f34419d87d --- /dev/null +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/ComputeOptionsTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * 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.compute; + +import com.google.cloud.GrpcTransportOptions; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class ComputeOptionsTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testInvalidTransport() { + thrown.expect(IllegalArgumentException.class); + Compu