Skip to content

Commit 8e02c99

Browse files
committed
Add proxy support and missing .gitignore entry [#11]
1 parent 7b1005f commit 8e02c99

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ gradle-app.setting
6868

6969
# bunq-specific
7070
bunq.conf
71+
context-save-restore-test.conf
7172
tmp/attachment_out.jpg
7273
/tmp
7374
config.properties

src/main/java/com/bunq/sdk/context/ApiContext.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public class ApiContext implements java.io.Serializable {
8383
@SerializedName("session_context")
8484
private SessionContext sessionContext;
8585

86+
@Expose
87+
@SerializedName("proxy")
88+
private String proxy;
89+
8690
/**
8791
* Create an empty API context.
8892
*/
@@ -92,19 +96,36 @@ private ApiContext(ApiEnvironmentType environmentType, String apiKey) {
9296
}
9397

9498
/**
95-
* Create and initialize an API Context with current IP as permitted.
99+
* Create and initialize an API Context with current IP as permitted and no proxy.
96100
*/
97101
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
98102
String deviceDescription) {
99103
return create(environmentType, apiKey, deviceDescription, new ArrayList<>());
100104
}
101105

102106
/**
103-
* Create and initialize an API Context.
107+
* Create and initialize an API Context with given permitted ips and no proxy.
104108
*/
105109
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
106110
String deviceDescription, List<String> permittedIps) {
111+
return create(environmentType, apiKey, deviceDescription, permittedIps, null);
112+
}
113+
114+
/**
115+
* Create and initialize an API Context with current IP as permitted and a proxy.
116+
*/
117+
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
118+
String deviceDescription, String proxy) {
119+
return create(environmentType, apiKey, deviceDescription, new ArrayList<>(), proxy);
120+
}
121+
122+
/**
123+
* Create and initialize an API Context.
124+
*/
125+
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
126+
String deviceDescription, List<String> permittedIps, String proxy) {
107127
ApiContext apiContext = new ApiContext(environmentType, apiKey);
128+
apiContext.proxy = proxy;
108129
apiContext.initialize(deviceDescription, permittedIps);
109130

110131
return apiContext;
@@ -278,4 +299,8 @@ public SessionContext getSessionContext() {
278299
return sessionContext;
279300
}
280301

302+
public String getProxy() {
303+
return proxy;
304+
}
305+
281306
}

src/main/java/com/bunq/sdk/http/ApiClient.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import com.google.gson.JsonElement;
1212
import com.google.gson.JsonObject;
1313
import com.google.gson.JsonSyntaxException;
14+
import com.sun.jndi.toolkit.url.Uri;
1415
import java.io.IOException;
16+
import java.net.MalformedURLException;
1517
import java.net.URI;
1618
import java.security.KeyManagementException;
1719
import java.security.NoSuchAlgorithmException;
@@ -21,6 +23,7 @@
2123
import java.util.Map;
2224
import java.util.UUID;
2325
import org.apache.http.Header;
26+
import org.apache.http.HttpHost;
2427
import org.apache.http.HttpResponse;
2528
import org.apache.http.HttpStatus;
2629
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -33,6 +36,7 @@
3336
import org.apache.http.entity.ByteArrayEntity;
3437
import org.apache.http.entity.ContentType;
3538
import org.apache.http.impl.client.CloseableHttpClient;
39+
import org.apache.http.impl.client.HttpClientBuilder;
3640
import org.apache.http.impl.client.HttpClients;
3741
import org.apache.http.ssl.SSLContextBuilder;
3842
import org.apache.http.util.EntityUtils;
@@ -96,13 +100,28 @@ private CloseableHttpClient buildHttpClient() {
96100
SSLContextBuilder builder = new SSLContextBuilder();
97101
SSLConnectionSocketFactory sslConnectionSocketFactory =
98102
new SSLConnectionSocketFactory(builder.build());
103+
HttpClientBuilder httpClientBuilder = HttpClients
104+
.custom()
105+
.setSSLSocketFactory(sslConnectionSocketFactory);
106+
setProxyIfNeeded(httpClientBuilder);
99107

100-
return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
101-
} catch (NoSuchAlgorithmException | KeyManagementException exception) {
108+
return httpClientBuilder.build();
109+
} catch (NoSuchAlgorithmException | KeyManagementException | MalformedURLException exception) {
102110
throw new UncaughtExceptionError(exception);
103111
}
104112
}
105113

114+
private void setProxyIfNeeded(HttpClientBuilder httpClientBuilder)
115+
throws MalformedURLException {
116+
String proxyString = apiContext.getProxy();
117+
118+
if (proxyString != null) {
119+
Uri proxyUri = new Uri(proxyString);
120+
HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme());
121+
httpClientBuilder.setProxy(proxy);
122+
}
123+
}
124+
106125
/**
107126
* Execute a POST request.
108127
*

0 commit comments

Comments
 (0)