diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 206e1fecaa58..c70975502a06 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -22,6 +22,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.api.client.extensions.appengine.http.UrlFetchTransport; +import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; @@ -60,6 +61,8 @@ public abstract class ServiceOptions< private final AuthCredentials authCredentials; private final RetryParams retryParams; private final ServiceRpcFactory serviceRpcFactory; + private final int connectTimeout; + private final int readTimeout; public interface HttpTransportFactory extends Serializable { HttpTransport create(); @@ -102,6 +105,8 @@ protected abstract static class Builder< private AuthCredentials authCredentials; private RetryParams retryParams; private ServiceRpcFactory serviceRpcFactory; + private int connectTimeout = -1; + private int readTimeout = -1; protected Builder() {} @@ -121,35 +126,89 @@ protected B self() { return (B) this; } + /** + * Sets project id. + * + * @return the builder. + */ public B projectId(String projectId) { this.projectId = projectId; return self(); } + /** + * Sets service host. + * + * @return the builder. + */ public B host(String host) { this.host = host; return self(); } + /** + * Sets the transport factory. + * + * @return the builder. + */ public B httpTransportFactory(HttpTransportFactory httpTransportFactory) { this.httpTransportFactory = httpTransportFactory; return self(); } + /** + * Sets the service authentication credentials. + * + * @return the builder. + */ public B authCredentials(AuthCredentials authCredentials) { this.authCredentials = authCredentials; return self(); } + /** + * Sets configuration parameters for request retries. + * + * @return the builder. + */ public B retryParams(RetryParams retryParams) { this.retryParams = retryParams; return self(); } + /** + * Sets the factory for rpc services. + * + * @return the builder + */ public B serviceRpcFactory(ServiceRpcFactory serviceRpcFactory) { this.serviceRpcFactory = serviceRpcFactory; return self(); } + + /** + * Sets the timeout in milliseconds to establish a connection. + * + * @param connectTimeout connection timeout in milliseconds. 0 for an infinite timeout, a + * negative number for the default value (20000). + * @return the builder. + */ + public B connectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + return self(); + } + + /** + * Sets the timeout in milliseconds to read data from an established connection. + * + * @param readTimeout read timeout in milliseconds. 0 for an infinite timeout, a + * negative number for the default value (20000). + * @return the builder. + */ + public B readTimeout(int readTimeout) { + this.readTimeout = readTimeout; + return self(); + } } protected ServiceOptions(Builder builder) { @@ -160,6 +219,8 @@ protected ServiceOptions(Builder builder) { authCredentials = firstNonNull(builder.authCredentials, defaultAuthCredentials()); retryParams = builder.retryParams; serviceRpcFactory = builder.serviceRpcFactory; + connectTimeout = builder.connectTimeout; + readTimeout = builder.readTimeout; } private static AuthCredentials defaultAuthCredentials() { @@ -277,33 +338,84 @@ protected static String getAppEngineProjectId() { protected abstract Set scopes(); + /** + * Returns the project id. + */ public String projectId() { return projectId; } + /** + * Returns the service host. + */ public String host() { return host; } + /** + * Returns the transport factory. + */ public HttpTransportFactory httpTransportFactory() { return httpTransportFactory; } + /** + * Returns the authentication credentials. + */ public AuthCredentials authCredentials() { return authCredentials; } + /** + * Returns configuration parameters for request retries. + */ public RetryParams retryParams() { return retryParams != null ? retryParams : RetryParams.noRetries(); } + /** + * Returns the factory for rpc services. + */ public ServiceRpcFactory serviceRpcFactory() { return serviceRpcFactory; } + /** + * Returns a request initializer responsible for initializing requests according to service + * options. + */ public HttpRequestInitializer httpRequestInitializer() { HttpTransport httpTransport = httpTransportFactory.create(); - return authCredentials().httpRequestInitializer(httpTransport, scopes()); + final HttpRequestInitializer baseRequestInitializer = + authCredentials().httpRequestInitializer(httpTransport, scopes()); + return new HttpRequestInitializer() { + @Override + public void initialize(HttpRequest httpRequest) throws IOException { + baseRequestInitializer.initialize(httpRequest); + if (connectTimeout >= 0) { + httpRequest.setConnectTimeout(connectTimeout); + } + if (readTimeout >= 0) { + httpRequest.setReadTimeout(readTimeout); + } + } + }; + } + + /** + * Returns the timeout in milliseconds to establish a connection. 0 is an infinite timeout, a + * negative number is the default value (20000). + */ + public int connectTimeout() { + return connectTimeout; + } + + /** + * Returns the timeout in milliseconds to read from an established connection. 0 is an infinite + * timeout, a negative number is the default value (20000). + */ + public int readTimeout() { + return readTimeout; } protected int baseHashCode() {