Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/box/sdk/BoxAPIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class BoxAPIConnection {
private String baseUploadURL;
private boolean autoRefresh;
private int maxRequestAttempts;
private int connectTimeout;
private int readTimeout;
private List<BoxAPIConnectionListener> listeners;
private RequestInterceptor interceptor;
private Map<String, String> customHeaders;
Expand Down Expand Up @@ -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<BoxAPIConnectionListener>();
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/com/box/sdk/BoxAPIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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.
Expand All @@ -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
*/
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/box/sdk/BoxAPIConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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());
}
}