Skip to content

Commit 650171e

Browse files
authored
Merge pull request #97 from Adyen/proxy-config
Add proxy configuration in HttpURLConnectionClient
2 parents 76bcfc1 + 918f0ef commit 650171e

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,22 @@ Add this dependency to your project's POM:
3030
## Usage
3131

3232
The code examples on using this library are located in the library section of the java-sample-code repository: https://github.com/adyen/adyen-java-sample-code
33-
33+
34+
## Proxy configuration
35+
36+
You can configure a proxy connection by injecting your own HttpURLConnectionClient on your client instance.
37+
38+
Example:
39+
```
40+
...
41+
HttpURLConnectionClient httpURLConnectionClientWithProxy = new HttpURLConnectionClient();
42+
43+
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("PROXY_HOST", PROXY_PORT));
44+
httpURLConnectionClientWithProxy.setProxy(proxy);
45+
46+
client.setHttpClient(httpURLConnectionClientWithProxy);
47+
```
48+
3449
## Support
3550

3651
If you have any problems, questions or suggestions, create an issue here or send your inquiry to [email protected].

src/main/java/com/adyen/httpclient/HttpURLConnectionClient.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.OutputStream;
2626
import java.io.UnsupportedEncodingException;
2727
import java.net.HttpURLConnection;
28+
import java.net.Proxy;
2829
import java.net.URL;
2930
import java.net.URLEncoder;
3031
import java.util.Map;
@@ -35,6 +36,7 @@
3536

3637
public class HttpURLConnectionClient implements ClientInterface {
3738
private static final String CHARSET = "UTF-8";
39+
private Proxy proxy;
3840

3941
/**
4042
* Does a POST request.
@@ -99,9 +101,14 @@ private String getQuery(Map<String, String> params) throws UnsupportedEncodingEx
99101
*/
100102
private HttpURLConnection createRequest(String requestUrl, String applicationName) throws IOException {
101103
URL targetUrl = new URL(requestUrl);
104+
HttpURLConnection httpConnection;
102105

103-
// set configurations
104-
HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
106+
// Set proxy if configured
107+
if (proxy != null) {
108+
httpConnection = (HttpURLConnection) targetUrl.openConnection(proxy);
109+
} else {
110+
httpConnection = (HttpURLConnection) targetUrl.openConnection();
111+
}
105112
httpConnection.setUseCaches(false);
106113
httpConnection.setDoOutput(true);
107114
httpConnection.setRequestMethod("POST");
@@ -163,4 +170,12 @@ private String doPostRequest(HttpURLConnection httpConnection, String requestBod
163170

164171
return response;
165172
}
173+
174+
public Proxy getProxy() {
175+
return proxy;
176+
}
177+
178+
public void setProxy(Proxy proxy) {
179+
this.proxy = proxy;
180+
}
166181
}

0 commit comments

Comments
 (0)