diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index ec130a34c..85648cc79 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -533,7 +533,12 @@ public String getAccessToken() { } } - return this.accessToken; + this.refreshLock.readLock().lock(); + try { + return this.accessToken; + } finally { + this.refreshLock.readLock().unlock(); + } } /** @@ -760,10 +765,13 @@ public boolean needsRefresh() { boolean needsRefresh; this.refreshLock.readLock().lock(); - long now = System.currentTimeMillis(); - long tokenDuration = (now - this.lastRefresh); - needsRefresh = (tokenDuration >= this.expires - REFRESH_EPSILON); - this.refreshLock.readLock().unlock(); + try { + long now = System.currentTimeMillis(); + long tokenDuration = (now - this.lastRefresh); + needsRefresh = (tokenDuration >= this.expires - REFRESH_EPSILON); + } finally { + this.refreshLock.readLock().unlock(); + } return needsRefresh; } @@ -775,37 +783,32 @@ public boolean needsRefresh() { */ public void refresh() { this.refreshLock.writeLock().lock(); - - if (!this.canRefresh()) { - this.refreshLock.writeLock().unlock(); - throw new IllegalStateException("The BoxAPIConnection cannot be refreshed because it doesn't have a " - + "refresh token."); - } - - URL url; try { - url = new URL(getTokenURL()); - } catch (MalformedURLException e) { - this.refreshLock.writeLock().unlock(); - assert false : "An invalid refresh URL indicates a bug in the SDK."; - throw new RuntimeException("An invalid refresh URL indicates a bug in the SDK.", e); - } + if (!this.canRefresh()) { + throw new IllegalStateException("The BoxAPIConnection cannot be refreshed because it doesn't have a " + + "refresh token."); + } - BoxAPIRequest request = createTokenRequest(url); + URL url; + try { + url = new URL(getTokenURL()); + } catch (MalformedURLException e) { + assert false : "An invalid refresh URL indicates a bug in the SDK."; + throw new RuntimeException("An invalid refresh URL indicates a bug in the SDK.", e); + } - String json; - try (BoxAPIResponse boxAPIResponse = request.send()) { - BoxJSONResponse response = (BoxJSONResponse) boxAPIResponse; - json = response.getJSON(); - } catch (BoxAPIException e) { - this.refreshLock.writeLock().unlock(); - this.notifyError(e); - throw e; - } + BoxAPIRequest request = createTokenRequest(url); - try { - extractTokens(Json.parse(json).asObject()); + String json; + try (BoxAPIResponse boxAPIResponse = request.send()) { + BoxJSONResponse response = (BoxJSONResponse) boxAPIResponse; + json = response.getJSON(); + } catch (BoxAPIException e) { + this.notifyError(e); + throw e; + } + extractTokens(Json.parse(json).asObject()); this.notifyRefresh(); } finally { this.refreshLock.writeLock().unlock(); diff --git a/src/main/java/com/box/sdk/BoxAPIRequest.java b/src/main/java/com/box/sdk/BoxAPIRequest.java index a23e794ba..4ea97a00e 100644 --- a/src/main/java/com/box/sdk/BoxAPIRequest.java +++ b/src/main/java/com/box/sdk/BoxAPIRequest.java @@ -585,54 +585,55 @@ private BoxAPIResponse trySend(ProgressListener listener) { } } } + long start = System.currentTimeMillis(); + Request request = composeRequest(listener); + Response response; + if (this.followRedirects) { + response = api.execute(request); + } else { + response = api.executeWithoutRedirect(request); + } + logDebug(format("[trySend] connection.connect() took %dms%n", (System.currentTimeMillis() - start))); - Request.Builder requestBuilder = new Request.Builder().url(getUrl()); + BoxAPIResponse result = BoxAPIResponse.toBoxResponse(response); + this.logRequest(); + long getResponseStart = System.currentTimeMillis(); + logDebug(format( + "[trySend] Get Response (read network) took %dms%n", System.currentTimeMillis() - getResponseStart + )); + return result; + } + private Request composeRequest(ProgressListener listener) { + Request.Builder requestBuilder = new Request.Builder().url(getUrl()); if (this.shouldAuthenticate) { requestBuilder.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + this.api.lockAccessToken()); } - requestBuilder.addHeader("User-Agent", this.api.getUserAgent()); - requestBuilder.addHeader("X-Box-UA", this.api.getBoxUAHeader()); - headers.forEach(h -> { - requestBuilder.removeHeader(h.getKey()); - requestBuilder.addHeader(h.getKey(), h.getValue()); - }); - - if (this.api instanceof SharedLinkAPIConnection) { - SharedLinkAPIConnection sharedItemAPI = (SharedLinkAPIConnection) this.api; - String boxAPIValue = BoxSharedLink.getSharedLinkHeaderValue( - sharedItemAPI.getSharedLink(), - sharedItemAPI.getSharedLinkPassword() - ); - requestBuilder.addHeader("BoxApi", boxAPIValue); - } - try { - long start = System.currentTimeMillis(); - writeMethodWithBody(requestBuilder, listener); - Request request = requestBuilder.build(); - Response response; - if (this.followRedirects) { - response = api.execute(request); - } else { - response = api.executeWithoutRedirect(request); + requestBuilder.addHeader("User-Agent", this.api.getUserAgent()); + requestBuilder.addHeader("X-Box-UA", this.api.getBoxUAHeader()); + headers.forEach(h -> { + requestBuilder.removeHeader(h.getKey()); + requestBuilder.addHeader(h.getKey(), h.getValue()); + }); + + if (this.api instanceof SharedLinkAPIConnection) { + SharedLinkAPIConnection sharedItemAPI = (SharedLinkAPIConnection) this.api; + String boxAPIValue = BoxSharedLink.getSharedLinkHeaderValue( + sharedItemAPI.getSharedLink(), + sharedItemAPI.getSharedLinkPassword() + ); + requestBuilder.addHeader("BoxApi", boxAPIValue); } - logDebug(format("[trySend] connection.connect() took %dms%n", (System.currentTimeMillis() - start))); - BoxAPIResponse result = BoxAPIResponse.toBoxResponse(response); - this.logRequest(); - long getResponseStart = System.currentTimeMillis(); - logDebug(format( - "[trySend] Get Response (read network) took %dms%n", System.currentTimeMillis() - getResponseStart - )); - return result; + writeMethodWithBody(requestBuilder, listener); + return requestBuilder.build(); } finally { if (this.shouldAuthenticate) { this.api.unlockAccessToken(); } } - } protected void writeMethodWithBody(Request.Builder requestBuilder, ProgressListener listener) {