Skip to content

Commit 2073981

Browse files
Derek LansingGreg Curtis
authored andcommitted
Add ability to set non-global proxy
* Added a Proxy and proxy username/password to BoxAPIConnection.java * Added support for opening the URL connection using this proxy if it exists in BoxAPIRequest.java Closes #136.
1 parent b51f37f commit 2073981

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ repositories {
1616

1717
dependencies {
1818
compile 'com.eclipsesource.minimal-json:minimal-json:0.9.1'
19+
compile 'commons-codec:commons-codec:1.10'
1920
testCompile 'junit:junit:4.11'
2021
testCompile 'org.hamcrest:hamcrest-library:1.3'
2122
testCompile 'com.github.tomakehurst:wiremock:1.52'

src/main/java/com/box/sdk/BoxAPIConnection.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.box.sdk;
22

33
import java.net.MalformedURLException;
4+
import java.net.Proxy;
45
import java.net.URL;
56
import java.util.ArrayList;
67
import java.util.List;
@@ -41,6 +42,10 @@ public class BoxAPIConnection {
4142
private volatile long lastRefresh;
4243
private volatile long expires;
4344

45+
private Proxy proxy;
46+
private String proxyUsername;
47+
private String proxyPassword;
48+
4449
private String userAgent;
4550
private String accessToken;
4651
private String refreshToken;
@@ -330,6 +335,54 @@ public void setMaxRequestAttempts(int attempts) {
330335
this.maxRequestAttempts = attempts;
331336
}
332337

338+
/**
339+
* Gets the proxy value to use for API calls to Box.
340+
* @return current proxy value
341+
*/
342+
public Proxy getProxy() {
343+
return this.proxy;
344+
}
345+
346+
/**
347+
* Sets the Proxy to use for API calls to Box.
348+
* @param proxy the current Proxy
349+
*/
350+
public void setProxy(Proxy proxy) {
351+
this.proxy = proxy;
352+
}
353+
354+
/**
355+
* Gets the username to use for a Proxy that requires basic auth.
356+
* @return username to use for a Proxy that requires basic auth
357+
*/
358+
public String getProxyUsername() {
359+
return this.proxyUsername;
360+
}
361+
362+
/**
363+
* Sets the username to use for a Proxy that requires basic auth.
364+
* @param proxyUsername username to use for a Proxy that requires basic auth
365+
*/
366+
public void setProxyUsername(String proxyUsername) {
367+
this.proxyUsername = proxyUsername;
368+
}
369+
370+
/**
371+
* Gets the password to use for a Proxy that requires basic auth.
372+
* @return password to use for a Proxy that requires basic auth
373+
*/
374+
public String getProxyPassword() {
375+
return this.proxyPassword;
376+
}
377+
378+
/**
379+
* Sets the password to use for a Proxy that requires basic auth.
380+
* @param proxyPassword password to use for a Proxy that requires basic auth
381+
*/
382+
public void setProxyPassword(String proxyPassword) {
383+
this.proxyPassword = proxyPassword;
384+
}
385+
333386
/**
334387
* Determines if this connection's access token can be refreshed. An access token cannot be refreshed if a refresh
335388
* token was never set.

src/main/java/com/box/sdk/BoxAPIRequest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import java.util.logging.Level;
1515
import java.util.logging.Logger;
1616

17+
import org.apache.commons.codec.binary.Base64;
18+
19+
1720
/**
1821
* Used to make HTTP requests to the Box API.
1922
*
@@ -349,6 +352,13 @@ private BoxAPIResponse trySend(ProgressListener listener) {
349352
if (this.api != null) {
350353
connection.addRequestProperty("Authorization", "Bearer " + this.api.lockAccessToken());
351354
connection.setRequestProperty("User-Agent", this.api.getUserAgent());
355+
if (this.api.getProxy() != null) {
356+
if (this.api.getProxyUsername() != null && this.api.getProxyPassword() != null) {
357+
String usernameAndPassword = this.api.getProxyUsername() + ":" + this.api.getProxyPassword();
358+
String encoded = new String(Base64.encodeBase64(usernameAndPassword.getBytes()));
359+
connection.addRequestProperty("Proxy-authorization", "Basic " + encoded);
360+
}
361+
}
352362

353363
if (this.api instanceof SharedLinkAPIConnection) {
354364
SharedLinkAPIConnection sharedItemAPI = (SharedLinkAPIConnection) this.api;
@@ -447,7 +457,11 @@ private HttpURLConnection createConnection() {
447457
HttpURLConnection connection = null;
448458

449459
try {
450-
connection = (HttpURLConnection) this.url.openConnection();
460+
if (this.api == null || this.api.getProxy() == null) {
461+
connection = (HttpURLConnection) this.url.openConnection();
462+
} else {
463+
connection = (HttpURLConnection) this.url.openConnection(this.api.getProxy());
464+
}
451465
} catch (IOException e) {
452466
throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
453467
}

0 commit comments

Comments
 (0)