From 15cbff6f1f169f4ad83c4d5567c653063a87a666 Mon Sep 17 00:00:00 2001 From: Matt Willer Date: Wed, 14 Nov 2018 17:03:08 -0800 Subject: [PATCH] Allow per-connection timeouts --- .../java/com/box/sdk/BoxAPIConnection.java | 36 +++++++++++++++++++ src/main/java/com/box/sdk/BoxAPIRequest.java | 27 ++++++++++++-- .../com/box/sdk/BoxAPIConnectionTest.java | 18 ++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index 6cff50e67..16380e538 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -66,6 +66,8 @@ public class BoxAPIConnection { private String baseUploadURL; private boolean autoRefresh; private int maxRequestAttempts; + private int connectTimeout; + private int readTimeout; private List listeners; private RequestInterceptor interceptor; private Map customHeaders; @@ -96,6 +98,8 @@ public BoxAPIConnection(String clientID, String clientSecret, String accessToken this.baseUploadURL = DEFAULT_BASE_UPLOAD_URL; this.autoRefresh = true; this.maxRequestAttempts = BoxGlobalSettings.getMaxRequestAttempts(); + this.connectTimeout = BoxGlobalSettings.getConnectTimeout(); + this.readTimeout = BoxGlobalSettings.getReadTimeout(); this.refreshLock = new ReentrantReadWriteLock(); this.userAgent = "Box Java SDK v" + SDK_VERSION + " (Java " + JAVA_VERSION + ")"; this.listeners = new ArrayList(); @@ -430,6 +434,38 @@ public void setMaxRequestAttempts(int attempts) { this.maxRequestAttempts = attempts; } + /** + * Gets the connect timeout for this connection in milliseconds. + * @return the number of milliseconds to connect before timing out. + */ + public int getConnectTimeout() { + return this.connectTimeout; + } + + /** + * Sets the connect timeout for this connection. + * @param connectTimeout The number of milliseconds to wait for the connection to be established. + */ + public void setConnectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + } + + /** + * Gets the read timeout for this connection in milliseconds. + * @return the number of milliseconds to wait for bytes to be read before timing out. + */ + public int getReadTimeout() { + return this.readTimeout; + } + + /** + * Sets the read timeout for this connection. + * @param readTimeout The number of milliseconds to wait for bytes to be read. + */ + public void setReadTimeout(int readTimeout) { + this.readTimeout = readTimeout; + } + /** * Gets the proxy value to use for API calls to Box. * @return the current proxy. diff --git a/src/main/java/com/box/sdk/BoxAPIRequest.java b/src/main/java/com/box/sdk/BoxAPIRequest.java index 5ace6f38c..3f95cbebe 100644 --- a/src/main/java/com/box/sdk/BoxAPIRequest.java +++ b/src/main/java/com/box/sdk/BoxAPIRequest.java @@ -138,8 +138,13 @@ public BoxAPIRequest(BoxAPIConnection api, URL url, String method) { } this.backoffCounter = new BackoffCounter(new Time()); this.shouldAuthenticate = true; - this.connectTimeout = BoxGlobalSettings.getConnectTimeout(); - this.readTimeout = BoxGlobalSettings.getReadTimeout(); + if (api != null) { + this.connectTimeout = api.getConnectTimeout(); + this.readTimeout = api.getReadTimeout(); + } else { + this.connectTimeout = BoxGlobalSettings.getConnectTimeout(); + this.readTimeout = BoxGlobalSettings.getReadTimeout(); + } this.addHeader("Accept-Encoding", "gzip"); this.addHeader("Accept-Charset", "utf-8"); @@ -192,6 +197,14 @@ public void setConnectTimeout(int timeout) { this.connectTimeout = timeout; } + /** + * Gets the connect timeout for the request. + * @return the request connection timeout. + */ + public int getConnectTimeout() { + return this.connectTimeout; + } + /** * Sets a read timeout for this request in milliseconds. * @param timeout the timeout in milliseconds. @@ -200,7 +213,15 @@ public void setReadTimeout(int timeout) { this.readTimeout = timeout; } - /** + /** + * Gets the read timeout for the request. + * @return the request's read timeout. + */ + public int getReadTimeout() { + return this.readTimeout; + } + + /** * Sets whether or not to follow redirects (i.e. Location header) * @param followRedirects true to follow, false to not follow */ diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index b5f54c9a3..38ed7f885 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -677,4 +677,22 @@ public void shouldUseGlobalMaxRequests() { // Set back the original number to not interfere with other test cases BoxGlobalSettings.setMaxRequestAttempts(defaultMaxRequests); } + + @Test + @Category(UnitTest.class) + public void shouldUseInstanceTimeoutSettings() throws MalformedURLException { + + int instanceConnectTimeout = BoxGlobalSettings.getConnectTimeout() + 1000; + int instanceReadTimeout = BoxGlobalSettings.getReadTimeout() + 1000; + + BoxAPIConnection api = new BoxAPIConnection(""); + + api.setConnectTimeout(instanceConnectTimeout); + api.setReadTimeout(instanceReadTimeout); + + BoxAPIRequest req = new BoxAPIRequest(api, new URL("https://api.box.com/2.0/users/me"), "GET"); + + assertEquals(instanceConnectTimeout, req.getConnectTimeout()); + assertEquals(instanceReadTimeout, req.getReadTimeout()); + } }