Skip to content

Commit

Permalink
Merge pull request #139 from gcurtis/non_global_proxy
Browse files Browse the repository at this point in the history
Add ability to set non-global proxy
  • Loading branch information
gcurtis committed Jun 30, 2015
2 parents b51f37f + 142c574 commit 9bb1fba
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
104 changes: 104 additions & 0 deletions src/main/java/com/box/sdk/Base64.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.box.sdk;

import java.io.ByteArrayOutputStream;

/**
* Contains a method for performing base64 encoding.
*
* <p>This class is included so that we don't need to add a dependency on Apache Commons for base64 encoding.</p>
*
* <p>The code in this class was mostly taken from https://gist.github.com/EmilHernvall/953733#file-base64-java</p>
*/
final class Base64 {

private Base64() { }

static String encode(byte[] data) {
char[] tbl = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

StringBuilder buffer = new StringBuilder();
int pad = 0;
for (int i = 0; i < data.length; i += 3) {

int b = ((data[i] & 0xFF) << 16) & 0xFFFFFF;
if (i + 1 < data.length) {
b |= (data[i + 1] & 0xFF) << 8;
} else {
pad++;
}
if (i + 2 < data.length) {
b |= (data[i + 2] & 0xFF);
} else {
pad++;
}

for (int j = 0; j < 4 - pad; j++) {
int c = (b & 0xFC0000) >> 18;
buffer.append(tbl[c]);
b <<= 6;
}
}
for (int j = 0; j < pad; j++) {
buffer.append("=");
}

return buffer.toString();
}

public static byte[] decode(String data) {
int[] tbl = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
byte[] bytes = data.getBytes();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length;) {
int b = 0;
if (tbl[bytes[i]] != -1) {
b = (tbl[bytes[i]] & 0xFF) << 18;
} else {
i++;
continue;
}

int num = 0;
if (i + 1 < bytes.length && tbl[bytes[i + 1]] != -1) {
b = b | ((tbl[bytes[i + 1]] & 0xFF) << 12);
num++;
}
if (i + 2 < bytes.length && tbl[bytes[i + 2]] != -1) {
b = b | ((tbl[bytes[i + 2]] & 0xFF) << 6);
num++;
}
if (i + 3 < bytes.length && tbl[bytes[i + 3]] != -1) {
b = b | (tbl[bytes[i + 3]] & 0xFF);
num++;
}

while (num > 0) {
int c = (b & 0xFF0000) >> 16;
buffer.write((char) c);
b <<= 8;
num--;
}
i += 4;
}
return buffer.toByteArray();
}
}
53 changes: 53 additions & 0 deletions src/main/java/com/box/sdk/BoxAPIConnection.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.box.sdk;

import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -41,6 +42,10 @@ public class BoxAPIConnection {
private volatile long lastRefresh;
private volatile long expires;

private Proxy proxy;
private String proxyUsername;
private String proxyPassword;

private String userAgent;
private String accessToken;
private String refreshToken;
Expand Down Expand Up @@ -330,6 +335,54 @@ public void setMaxRequestAttempts(int attempts) {
this.maxRequestAttempts = attempts;
}

/**
* Gets the proxy value to use for API calls to Box.
* @return the current proxy.
*/
public Proxy getProxy() {
return this.proxy;
}

/**
* Sets the proxy to use for API calls to Box.
* @param proxy the proxy to use for API calls to Box.
*/
public void setProxy(Proxy proxy) {
this.proxy = proxy;
}

/**
* Gets the username to use for a proxy that requires basic auth.
* @return the username to use for a proxy that requires basic auth.
*/
public String getProxyUsername() {
return this.proxyUsername;
}

/**
* Sets the username to use for a proxy that requires basic auth.
* @param proxyUsername the username to use for a proxy that requires basic auth.
*/
public void setProxyUsername(String proxyUsername) {
this.proxyUsername = proxyUsername;
}

/**
* Gets the password to use for a proxy that requires basic auth.
* @return the password to use for a proxy that requires basic auth.
*/
public String getProxyPassword() {
return this.proxyPassword;
}

/**
* Sets the password to use for a proxy that requires basic auth.
* @param proxyPassword the password to use for a proxy that requires basic auth.
*/
public void setProxyPassword(String proxyPassword) {
this.proxyPassword = proxyPassword;
}

/**
* Determines if this connection's access token can be refreshed. An access token cannot be refreshed if a refresh
* token was never set.
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/box/sdk/BoxAPIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ private BoxAPIResponse trySend(ProgressListener listener) {
if (this.api != null) {
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) {
String usernameAndPassword = this.api.getProxyUsername() + ":" + this.api.getProxyPassword();
String encoded = new String(Base64.encode(usernameAndPassword.getBytes()));
connection.addRequestProperty("Proxy-Authorization", "Basic " + encoded);
}
}

if (this.api instanceof SharedLinkAPIConnection) {
SharedLinkAPIConnection sharedItemAPI = (SharedLinkAPIConnection) this.api;
Expand Down Expand Up @@ -447,7 +454,11 @@ private HttpURLConnection createConnection() {
HttpURLConnection connection = null;

try {
connection = (HttpURLConnection) this.url.openConnection();
if (this.api == null || this.api.getProxy() == null) {
connection = (HttpURLConnection) this.url.openConnection();
} else {
connection = (HttpURLConnection) this.url.openConnection(this.api.getProxy());
}
} catch (IOException e) {
throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
}
Expand Down

0 comments on commit 9bb1fba

Please sign in to comment.