From 4a7418eedd5224e15ff9ac8b1288f45e34e822d6 Mon Sep 17 00:00:00 2001 From: Greg Curtis Date: Mon, 13 Jul 2015 14:34:20 -0700 Subject: [PATCH] Use proxy when authenticating Update BoxAPIConnection so that the proxy configuration (along with other connection-level settings) is applied when performing OAuth requests. Closes #142. --- src/main/java/com/box/sdk/BoxAPIConnection.java | 10 ++++------ src/main/java/com/box/sdk/BoxAPIRequest.java | 12 ++++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index 020fde8e6..521b42040 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -139,9 +139,8 @@ public void authenticate(String authCode) { String urlParameters = String.format("grant_type=authorization_code&code=%s&client_id=%s&client_secret=%s", authCode, this.clientID, this.clientSecret); - BoxAPIRequest request = new BoxAPIRequest(url, "POST"); - request.addHeader("Content-Type", "application/x-www-form-urlencoded"); - request.addHeader("User-Agent", this.getUserAgent()); + BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); + request.shouldAuthenticate(false); request.setBody(urlParameters); BoxJSONResponse response = (BoxJSONResponse) request.send(); @@ -433,9 +432,8 @@ public void refresh() { String urlParameters = String.format("grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s", this.refreshToken, this.clientID, this.clientSecret); - BoxAPIRequest request = new BoxAPIRequest(url, "POST"); - request.addHeader("Content-Type", "application/x-www-form-urlencoded"); - request.addHeader("User-Agent", this.getUserAgent()); + BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); + request.shouldAuthenticate(false); request.setBody(urlParameters); String json; diff --git a/src/main/java/com/box/sdk/BoxAPIRequest.java b/src/main/java/com/box/sdk/BoxAPIRequest.java index 603f397e4..cb8311d13 100644 --- a/src/main/java/com/box/sdk/BoxAPIRequest.java +++ b/src/main/java/com/box/sdk/BoxAPIRequest.java @@ -46,6 +46,7 @@ public class BoxAPIRequest { private long bodyLength; private Map> requestProperties; private int numRedirects; + private boolean shouldAuthenticate; /** * Constructs an unauthenticated BoxAPIRequest. @@ -68,6 +69,7 @@ public BoxAPIRequest(BoxAPIConnection api, URL url, String method) { this.method = method; this.headers = new ArrayList(); this.backoffCounter = new BackoffCounter(new Time()); + this.shouldAuthenticate = true; this.addHeader("Accept-Encoding", "gzip"); this.addHeader("Accept-Charset", "utf-8"); @@ -347,7 +349,9 @@ private BoxAPIResponse trySend(ProgressListener listener) { } if (this.api != null) { - connection.addRequestProperty("Authorization", "Bearer " + this.api.lockAccessToken()); + if (this.shouldAuthenticate) { + connection.addRequestProperty("Authorization", "Bearer " + this.api.lockAccessToken()); + } connection.setRequestProperty("User-Agent", this.api.getUserAgent()); if (this.api.getProxy() != null) { if (this.api.getProxyUsername() != null && this.api.getProxyPassword() != null) { @@ -393,7 +397,7 @@ private BoxAPIResponse trySend(ProgressListener listener) { throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e); } } finally { - if (this.api != null) { + if (this.api != null && this.shouldAuthenticate) { this.api.unlockAccessToken(); } } @@ -483,6 +487,10 @@ private HttpURLConnection createConnection() { return connection; } + void shouldAuthenticate(boolean shouldAuthenticate) { + this.shouldAuthenticate = shouldAuthenticate; + } + private static boolean isResponseRetryable(int responseCode) { return (responseCode >= 500 || responseCode == 429); }