Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
**/target/**
.DS_Store
.vscode
*.class
57 changes: 39 additions & 18 deletions src/main/java/com/adyen/httpclient/AdyenHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.hc.core5.util.Timeout;

/** HTTP client implementation to invoke the Adyen APIs. Built on top of org.apache.hc.client5 */
public class AdyenHttpClient implements ClientInterface {
Expand Down Expand Up @@ -155,18 +158,13 @@ HttpUriRequestBase createRequest(
RequestConfig.Builder builder = RequestConfig.custom();

builder.setResponseTimeout(config.getReadTimeoutMillis(), TimeUnit.MILLISECONDS);
builder.setConnectTimeout(config.getConnectionTimeoutMillis(), TimeUnit.MILLISECONDS);
builder.setDefaultKeepAlive(config.getDefaultKeepAliveMillis(), TimeUnit.MILLISECONDS);
builder.setConnectionRequestTimeout(
config.getConnectionRequestTimeoutMillis(), TimeUnit.MILLISECONDS);

if (config.getProtocolUpgradeEnabled() != null) {
builder.setProtocolUpgradeEnabled(config.getProtocolUpgradeEnabled());
}
if (proxy != null && proxy.address() instanceof InetSocketAddress) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) proxy.address();
builder.setProxy(new HttpHost(inetSocketAddress.getHostName(), inetSocketAddress.getPort()));
}
httpRequest.setConfig(builder.build());

setAuthentication(httpRequest, isApiKeyRequired, config);
Expand Down Expand Up @@ -255,19 +253,42 @@ private CloseableHttpClient createCloseableHttpClient(Config config) {
sslContext = SSLContexts.createDefault();
}
HostnameVerifier hostnameVerifier = config.getHostnameVerifier();
return createHttpClientWithSocketFactory(
new SSLConnectionSocketFactory(sslContext, hostnameVerifier));
}

private CloseableHttpClient createHttpClientWithSocketFactory(
SSLConnectionSocketFactory socketFactory) {
return HttpClients.custom()
.setConnectionManager(
PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(socketFactory)
.build())
.setRedirectStrategy(new AdyenCustomRedirectStrategy())
.build();
// Create ConnectionConfig with connect timeout
ConnectionConfig connectionConfig =
ConnectionConfig.custom()
.setConnectTimeout(Timeout.ofMilliseconds(config.getConnectionTimeoutMillis()))
.build();

// Create TlsSocketStrategy with SSL context and hostname verifier
DefaultClientTlsStrategy tlsStrategy =
new DefaultClientTlsStrategy(sslContext, hostnameVerifier);

// Create connection manager with TLS strategy and connection config
var connectionManager =
PoolingHttpClientConnectionManagerBuilder.create()
.setTlsSocketStrategy(tlsStrategy)
.setDefaultConnectionConfig(connectionConfig)
.build();

// Build HTTP client with connection manager and redirect strategy
var httpClientBuilder =
HttpClients.custom()
.setConnectionManager(connectionManager)
.setRedirectStrategy(new AdyenCustomRedirectStrategy());

// Set proxy if configured
if (proxy != null && proxy.address() instanceof InetSocketAddress) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) proxy.address();
String scheme =
proxy.type() == Proxy.Type.SOCKS ? "socks" : "http"; // Map proxy type to scheme
HttpHost proxyHost =
new HttpHost(scheme, inetSocketAddress.getHostName(), inetSocketAddress.getPort());
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);
httpClientBuilder.setRoutePlanner(routePlanner);
}

return httpClientBuilder.build();
}

/** Sets content type */
Expand Down