Skip to content

Commit 2f4673f

Browse files
authored
Merge pull request #12 from bunq/11-proxy
Add proxy support and missing .gitignore entry [#11]
2 parents bc331c2 + 3923a52 commit 2f4673f

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

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

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

85+
@Expose
86+
@SerializedName("proxy")
87+
private String proxy;
88+
8589
/**
8690
* Create an empty API context.
8791
*/
@@ -91,19 +95,36 @@ private ApiContext(ApiEnvironmentType environmentType, String apiKey) {
9195
}
9296

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

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

109130
return apiContext;
@@ -289,4 +310,8 @@ public SessionContext getSessionContext() {
289310
return sessionContext;
290311
}
291312

313+
public String getProxy() {
314+
return proxy;
315+
}
316+
292317
}

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)