diff --git a/README.md b/README.md index 045d80589..45f3a8497 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,22 @@ Add this dependency to your project's POM: ## Usage 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 - + +## Proxy configuration + +You can configure a proxy connection by injecting your own HttpURLConnectionClient on your client instance. + +Example: +``` +... +HttpURLConnectionClient httpURLConnectionClientWithProxy = new HttpURLConnectionClient(); + +Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("PROXY_HOST", PROXY_PORT)); +httpURLConnectionClientWithProxy.setProxy(proxy); + +client.setHttpClient(httpURLConnectionClientWithProxy); +``` + ## Support If you have any problems, questions or suggestions, create an issue here or send your inquiry to support@adyen.com. diff --git a/src/main/java/com/adyen/httpclient/HttpURLConnectionClient.java b/src/main/java/com/adyen/httpclient/HttpURLConnectionClient.java index 265bde3f6..de29c3910 100644 --- a/src/main/java/com/adyen/httpclient/HttpURLConnectionClient.java +++ b/src/main/java/com/adyen/httpclient/HttpURLConnectionClient.java @@ -25,6 +25,7 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; +import java.net.Proxy; import java.net.URL; import java.net.URLEncoder; import java.util.Map; @@ -35,6 +36,7 @@ public class HttpURLConnectionClient implements ClientInterface { private static final String CHARSET = "UTF-8"; + private Proxy proxy; /** * Does a POST request. @@ -99,9 +101,14 @@ private String getQuery(Map params) throws UnsupportedEncodingEx */ private HttpURLConnection createRequest(String requestUrl, String applicationName) throws IOException { URL targetUrl = new URL(requestUrl); + HttpURLConnection httpConnection; - // set configurations - HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection(); + // Set proxy if configured + if (proxy != null) { + httpConnection = (HttpURLConnection) targetUrl.openConnection(proxy); + } else { + httpConnection = (HttpURLConnection) targetUrl.openConnection(); + } httpConnection.setUseCaches(false); httpConnection.setDoOutput(true); httpConnection.setRequestMethod("POST"); @@ -163,4 +170,12 @@ private String doPostRequest(HttpURLConnection httpConnection, String requestBod return response; } + + public Proxy getProxy() { + return proxy; + } + + public void setProxy(Proxy proxy) { + this.proxy = proxy; + } }