Skip to content

Commit 559e770

Browse files
authored
Update Apache HttpClient 5 to use recommended APIs and remove deprecated code (#6312)
* Add initial empty module for Apache5x for seting up package (#6075) * Add initial module for Apache5x for seting up package * Add based on new module checklist * Baseline or Copy all the ApacheSDKHttpClient classes to newly added Apache5SDKHttpClient (#6088) * Add initial module for Apache5x for seting up package * Add based on new module checklist * Baseline all the classes from Apache4 SDK client to the new Apache5 module * Phase 2 , getting Apache 5 compilation and Junit ready along with clearing Checkstyles and spotbug issues (#6100) * Phase 2 , getting Apache 5 compilation and Junit ready along with clearing Checkstyles and spotbug issues * Handle comments from review * Handle comments from Zoe * Update the snap shot * Fix HTTP authentication retry failures by improving RepeatableInputStreamRequestEntity repeatability (#6132) * Fix HTTP authentication retry failures by improving RepeatableInputStreamRequestEntity repeatability * Upated test cases * Handled comments * Updated snap shot after merge from master * Revert "Updated snap shot after merge from master" This reverts commit 64f7d33. * Updated snap shot after merge from master * Fix architecture test failures for apache5.x (#6140) * Fix architecture test failures for apache5.x * Checkstyle issues * Updated snap shot after merge from master * Use reference of PoolingHttpClientConnectionManager instead of HttpClientConnectionManager for Connection Manager (#6147) * Fix architecture test failures for apache5.x * Checkstyle issues * Update to use PoolingHttpClientConnectionManager class reference that is implementation of HttpClientConnectionManager * Fix Apache5 HTTP client retry failures with non-resettable streams (#6154) * Fix architecture test failures for apache5.x * Checkstyle issues * Update to use PoolingHttpClientConnectionManager class reference that is implementation of HttpClientConnectionManager * Fix stream reset failure in RepeatableInputStreamRequestEntity by storing content reference to avoid multiple ContentStreamProvider.newStream() calls that cause IOException when retrying requests with non-resettable streams * writeTo_ConcurrentWrites_HandlesCorrectly no longer needed since even Apache 4.x doesnot suports this * Merge PR#6165 #6165 * Disable Client based retries and define httpcore5 httpclient5 in .brazil.json (#6191) * Fix architecture test failures for apache5.x * Checkstyle issues * Update to use PoolingHttpClientConnectionManager class reference that is implementation of HttpClientConnectionManager * Fix stream reset failure in RepeatableInputStreamRequestEntity by storing content reference to avoid multiple ContentStreamProvider.newStream() calls that cause IOException when retrying requests with non-resettable streams * writeTo_ConcurrentWrites_HandlesCorrectly no longer needed since even Apache 4.x doesnot suports this * Fix connectionPoolingWorks by setting skipping setConnectionTimeToLive is value is set to 0 since 0 is treated as Infinite timeToLive in Sdk and Apache 4.x but treated as immediate closeConnection in apache 5.x * disableAutomaticRetries in Apache 5.x since SDK handles retries , also define Apache5 dependencies in .brazil.json * Added Test case for Async , handled review ocmments * Update snapshots * Do not buffer the Response stream using BufferedHttpEntity (#6200) * Fix architecture test failures for apache5.x * Checkstyle issues * Update to use PoolingHttpClientConnectionManager class reference that is implementation of HttpClientConnectionManager * Fix stream reset failure in RepeatableInputStreamRequestEntity by storing content reference to avoid multiple ContentStreamProvider.newStream() calls that cause IOException when retrying requests with non-resettable streams * writeTo_ConcurrentWrites_HandlesCorrectly no longer needed since even Apache 4.x doesnot suports this * Fix connectionPoolingWorks by setting skipping setConnectionTimeToLive is value is set to 0 since 0 is treated as Infinite timeToLive in Sdk and Apache 4.x but treated as immediate closeConnection in apache 5.x * disableAutomaticRetries in Apache 5.x since SDK handles retries , also define Apache5 dependencies in .brazil.json * Added Test case for Async , handled review ocmments * Donot do buffer the response using BufferedHttpEntity since it might cause memory issue, this behaviour is same as Apache4.x * Fix compilation issues * Fix checkstyle issues * Remove test which are specific to apache http * Merge from master * Apache5x SDkBenhmark Tests (#6206) * Fix architecture test failures for apache5.x * Checkstyle issues * Update to use PoolingHttpClientConnectionManager class reference that is implementation of HttpClientConnectionManager * Fix stream reset failure in RepeatableInputStreamRequestEntity by storing content reference to avoid multiple ContentStreamProvider.newStream() calls that cause IOException when retrying requests with non-resettable streams * writeTo_ConcurrentWrites_HandlesCorrectly no longer needed since even Apache 4.x doesnot suports this * Fix connectionPoolingWorks by setting skipping setConnectionTimeToLive is value is set to 0 since 0 is treated as Infinite timeToLive in Sdk and Apache 4.x but treated as immediate closeConnection in apache 5.x * disableAutomaticRetries in Apache 5.x since SDK handles retries , also define Apache5 dependencies in .brazil.json * Added Test case for Async , handled review ocmments * Donot do buffer the response using BufferedHttpEntity since it might cause memory issue, this behaviour is same as Apache4.x * Fix compilation issues * Fix checkstyle issues * Remove test which are specific to apache http * Add benchmark for Apache5 and add Streaming Api test cases * Clean up unused APIs and add test to make sure it can be handled with alternatives (#6211) * Clean up unused APIs and add test to make sure it can be handled with alternatives * Added NTCredentials to keep backward compatibilty with Apache4.x * Upgrade Apache5 org.apache.httpcomponents.client5 to latest available version (#6214) * Fix architecture test failures for apache5.x * Checkstyle issues * Update to use PoolingHttpClientConnectionManager class reference that is implementation of HttpClientConnectionManager * Fix stream reset failure in RepeatableInputStreamRequestEntity by storing content reference to avoid multiple ContentStreamProvider.newStream() calls that cause IOException when retrying requests with non-resettable streams * writeTo_ConcurrentWrites_HandlesCorrectly no longer needed since even Apache 4.x doesnot suports this * Fix connectionPoolingWorks by setting skipping setConnectionTimeToLive is value is set to 0 since 0 is treated as Infinite timeToLive in Sdk and Apache 4.x but treated as immediate closeConnection in apache 5.x * disableAutomaticRetries in Apache 5.x since SDK handles retries , also define Apache5 dependencies in .brazil.json * Added Test case for Async , handled review ocmments * Donot do buffer the response using BufferedHttpEntity since it might cause memory issue, this behaviour is same as Apache4.x * Fix compilation issues * Fix checkstyle issues * Remove test which are specific to apache http * Add benchmark for Apache5 and add Streaming Api test cases * Update Apache5 to 5.5 * Preview API annotation added for Public APIs and TODOs addressed (#6215) * Fix architecture test failures for apache5.x * Checkstyle issues * Update to use PoolingHttpClientConnectionManager class reference that is implementation of HttpClientConnectionManager * Fix stream reset failure in RepeatableInputStreamRequestEntity by storing content reference to avoid multiple ContentStreamProvider.newStream() calls that cause IOException when retrying requests with non-resettable streams * writeTo_ConcurrentWrites_HandlesCorrectly no longer needed since even Apache 4.x doesnot suports this * Fix connectionPoolingWorks by setting skipping setConnectionTimeToLive is value is set to 0 since 0 is treated as Infinite timeToLive in Sdk and Apache 4.x but treated as immediate closeConnection in apache 5.x * disableAutomaticRetries in Apache 5.x since SDK handles retries , also define Apache5 dependencies in .brazil.json * Added Test case for Async , handled review ocmments * Donot do buffer the response using BufferedHttpEntity since it might cause memory issue, this behaviour is same as Apache4.x * Fix compilation issues * Fix checkstyle issues * Remove test which are specific to apache http * Add benchmark for Apache5 and add Streaming Api test cases * Update Apache5 to 5.5 * Preview ready , addressing open TODOs * Added PublicApi since checkstyle was failing * Updated the snapshot * Updated thr Brazil package nma e to have preview as suffix * Updated Version as -PREVIEW to release apache5 as preview release (#6219) * Updated Version as -PREVIEW * japi cmp needs to be disables since this is a new version and we dont have old version to compare * Handled Surface API review comments (#6224) * Handled Surface API review comments * Added a single test for localaddress , handled review comments * Removing internal package name as -preview after internal discussion * Fix transient text case failures * update pom.xml for apache5.x * Handled commed for merge to master PR 6220 (#6240) * Handled Surface API review comments * Added a single test for localaddress , handled review comments * Removing internal package name as -preview after internal discussion * Fix transient text case failures * Handled comment for merge to master PR for apache 5.x * Added change logs * Review comment * Handled Review comments * Move connection timeout configuration from RequestConfig to ConnectionConfig in Apache HttpClient 5 (#6293) * Replacing deprecated API like connectionTimeout on RequestConfig and passing it via defaultconnectionconfigs * Handle review comments * Replace httpclient.execute call with httpclient.executeOpen as mentioned in deprecation notes of httpclient.execute (#6298) * Replacing deprecated API like connectionTimeout on RequestConfig and passing it via defaultconnectionconfigs * Handle review comments * replace httpclient.execute call with httpclient.executeOpen as mentioned in deprecation notes of httpclient.execute * nit updates * fixed sonar quebe issues * Replace deprecated SSLConnectionSocketFactory with recommended API (#6281) * Replace deprecated SSLConnectionSocketFactory with recommended API * Fixed checkstyle issues * Changed name tlsSocketStrategy on builder * Removed warning log * added more test cases * updated after review * Added ConnectionSocketFactory to Apache5Client builder same as Apache4 * handled PR comments * Removed unused classes after moving to SSL sockets * Added change logs * removed old change logs * remove unused imports * inermittent port used in wiremock fixed * update change logs
1 parent 2800f91 commit 559e770

20 files changed

+378
-449
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "Apache HTTP Client 5",
4+
"contributor": "",
5+
"description": "Replace deprecated APIs from Apache HttpClient 5.5.x with corresponding recommended APIs"
6+
}

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/Apache5HttpClient.java

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@
3939
import javax.net.ssl.SSLContext;
4040
import javax.net.ssl.TrustManager;
4141
import javax.net.ssl.X509TrustManager;
42+
import org.apache.hc.client5.http.ClientProtocolException;
4243
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
4344
import org.apache.hc.client5.http.DnsResolver;
4445
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
4546
import org.apache.hc.client5.http.auth.CredentialsProvider;
4647
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
48+
import org.apache.hc.client5.http.config.ConnectionConfig;
4749
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
4850
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
4951
import org.apache.hc.client5.http.impl.classic.HttpClients;
@@ -53,9 +55,11 @@
5355
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
5456
import org.apache.hc.client5.http.protocol.HttpClientContext;
5557
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
58+
import org.apache.hc.client5.http.routing.RoutingSupport;
5659
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
5760
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
58-
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
61+
import org.apache.hc.client5.http.ssl.TlsSocketStrategy;
62+
import org.apache.hc.core5.http.ClassicHttpRequest;
5963
import org.apache.hc.core5.http.ClassicHttpResponse;
6064
import org.apache.hc.core5.http.Header;
6165
import org.apache.hc.core5.http.HttpEntity;
@@ -70,6 +74,7 @@
7074
import org.apache.hc.core5.pool.PoolStats;
7175
import org.apache.hc.core5.ssl.SSLInitializationException;
7276
import org.apache.hc.core5.util.TimeValue;
77+
import org.apache.hc.core5.util.Timeout;
7378
import software.amazon.awssdk.annotations.SdkPreviewApi;
7479
import software.amazon.awssdk.annotations.SdkPublicApi;
7580
import software.amazon.awssdk.annotations.SdkTestInternalApi;
@@ -285,14 +290,25 @@ private HttpExecuteResponse execute(HttpUriRequestBase apacheRequest, MetricColl
285290
HttpClientContext localRequestContext = Apache5Utils.newClientContext(requestConfig.proxyConfiguration());
286291
THREAD_LOCAL_REQUEST_METRIC_COLLECTOR.set(metricCollector);
287292
try {
288-
HttpResponse httpResponse = httpClient.execute(apacheRequest, localRequestContext);
289-
// Create a connection-aware input stream that closes the response when closed
293+
HttpHost target = determineTarget(apacheRequest);
294+
ClassicHttpResponse httpResponse = httpClient.executeOpen(target, apacheRequest, localRequestContext);
290295
return createResponse(httpResponse, apacheRequest);
291296
} finally {
292297
THREAD_LOCAL_REQUEST_METRIC_COLLECTOR.remove();
293298
}
294299
}
295300

301+
/**
302+
* Determines the target host from the request using Apache HttpClient's official routing support utility.
303+
*/
304+
private static HttpHost determineTarget(ClassicHttpRequest request) throws IOException {
305+
try {
306+
return RoutingSupport.determineHost(request);
307+
} catch (HttpException ex) {
308+
throw new ClientProtocolException(ex);
309+
}
310+
}
311+
296312
private HttpUriRequestBase toApacheRequest(HttpExecuteRequest request) {
297313
return apacheHttpRequestFactory.create(request, requestConfig);
298314
}
@@ -355,7 +371,6 @@ private Apache5HttpRequestConfig createRequestConfig(DefaultBuilder builder,
355371
AttributeMap resolvedOptions) {
356372
return Apache5HttpRequestConfig.builder()
357373
.socketTimeout(resolvedOptions.get(SdkHttpConfigurationOption.READ_TIMEOUT))
358-
.connectionTimeout(resolvedOptions.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT))
359374
.connectionAcquireTimeout(
360375
resolvedOptions.get(SdkHttpConfigurationOption.CONNECTION_ACQUIRE_TIMEOUT))
361376
.proxyConfiguration(builder.proxyConfiguration)
@@ -464,12 +479,15 @@ public interface Builder extends SdkHttpClient.Builder<Apache5HttpClient.Builder
464479
Builder dnsResolver(DnsResolver dnsResolver);
465480

466481
/**
467-
* Configuration that defines a custom Socket factory. If set to a null value, a default factory is used.
468-
* <p>
469-
* When set to a non-null value, the use of a custom factory implies the configuration options TRUST_ALL_CERTIFICATES,
470-
* TLS_TRUST_MANAGERS_PROVIDER, and TLS_KEY_MANAGERS_PROVIDER are ignored.
482+
* Configure a custom TLS strategy for SSL/TLS connections.
483+
* This is the preferred method over the ConnectionSocketFactory.
484+
*
485+
* @param tlsSocketStrategy The TLS strategy to use for upgrading connections to TLS.
486+
* If null, default TLS configuration will be used.
487+
* @return This builder for method chaining
488+
471489
*/
472-
Builder socketFactory(SSLConnectionSocketFactory socketFactory);
490+
Builder tlsSocketStrategy(TlsSocketStrategy tlsSocketStrategy);
473491

474492
/**
475493
* Configuration that defines an HTTP route planner that computes the route an HTTP request should take.
@@ -527,7 +545,7 @@ private static final class DefaultBuilder implements Builder {
527545
private HttpRoutePlanner httpRoutePlanner;
528546
private CredentialsProvider credentialsProvider;
529547
private DnsResolver dnsResolver;
530-
private SSLConnectionSocketFactory socketFactory;
548+
private TlsSocketStrategy tlsStrategy;
531549

532550
private DefaultBuilder() {
533551
}
@@ -649,15 +667,11 @@ public void setDnsResolver(DnsResolver dnsResolver) {
649667
}
650668

651669
@Override
652-
public Builder socketFactory(SSLConnectionSocketFactory socketFactory) {
653-
this.socketFactory = socketFactory;
670+
public Builder tlsSocketStrategy(TlsSocketStrategy tlsSocketStrategy) {
671+
this.tlsStrategy = tlsSocketStrategy;
654672
return this;
655673
}
656674

657-
public void setSocketFactory(SSLConnectionSocketFactory socketFactory) {
658-
socketFactory(socketFactory);
659-
}
660-
661675
@Override
662676
public Builder httpRoutePlanner(HttpRoutePlanner httpRoutePlanner) {
663677
this.httpRoutePlanner = httpRoutePlanner;
@@ -731,31 +745,44 @@ public SdkHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
731745
private static class ApacheConnectionManagerFactory {
732746

733747
public PoolingHttpClientConnectionManager create(Apache5HttpClient.DefaultBuilder configuration,
734-
AttributeMap standardOptions) {
735-
// TODO : Deprecated method needs to be removed with new replacements
736-
SSLConnectionSocketFactory sslsf = getPreferredSocketFactory(configuration, standardOptions);
748+
AttributeMap standardOptions) {
749+
750+
TlsSocketStrategy tlsStrategy = getPreferredTlsStrategy(configuration, standardOptions);
737751

738752
PoolingHttpClientConnectionManagerBuilder builder =
739753
PoolingHttpClientConnectionManagerBuilder.create()
740-
.setSSLSocketFactory(sslsf)
754+
.setTlsSocketStrategy(tlsStrategy)
741755
.setSchemePortResolver(DefaultSchemePortResolver.INSTANCE)
742756
.setDnsResolver(configuration.dnsResolver);
743-
Duration connectionTtl = standardOptions.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE);
744-
if (!connectionTtl.isZero()) {
745-
// Skip TTL=0 to maintain backward compatibility (infinite in 4.x vs immediate expiration in 5.x)
746-
builder.setConnectionTimeToLive(TimeValue.of(connectionTtl.toMillis(), TimeUnit.MILLISECONDS));
747-
}
748757
builder.setMaxConnPerRoute(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS));
749758
builder.setMaxConnTotal(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS));
750759
builder.setDefaultSocketConfig(buildSocketConfig(standardOptions));
760+
builder.setDefaultConnectionConfig(getConnectionConfig(standardOptions));
751761
return builder.build();
752762
}
753763

754-
private SSLConnectionSocketFactory getPreferredSocketFactory(Apache5HttpClient.DefaultBuilder configuration,
755-
AttributeMap standardOptions) {
756-
return Optional.ofNullable(configuration.socketFactory)
757-
.orElseGet(() -> new SdkTlsSocketFactory(getSslContext(standardOptions),
758-
getHostNameVerifier(standardOptions)));
764+
private static ConnectionConfig getConnectionConfig(AttributeMap standardOptions) {
765+
ConnectionConfig.Builder connectionConfigBuilder =
766+
ConnectionConfig.custom()
767+
.setConnectTimeout(Timeout.ofMilliseconds(
768+
standardOptions.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT).toMillis()))
769+
.setSocketTimeout(Timeout.ofMilliseconds(
770+
standardOptions.get(SdkHttpConfigurationOption.READ_TIMEOUT).toMillis()));
771+
Duration connectionTtl = standardOptions.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE);
772+
if (!connectionTtl.isZero()) {
773+
// Skip TTL=0 to maintain backward compatibility (infinite in 4.x vs immediate expiration in 5.x)
774+
connectionConfigBuilder.setTimeToLive(TimeValue.ofMilliseconds(connectionTtl.toMillis()));
775+
}
776+
return connectionConfigBuilder.build();
777+
}
778+
779+
private TlsSocketStrategy getPreferredTlsStrategy(Apache5HttpClient.DefaultBuilder configuration,
780+
AttributeMap standardOptions) {
781+
if (configuration.tlsStrategy != null) {
782+
return configuration.tlsStrategy;
783+
}
784+
return new SdkTlsSocketFactory(getSslContext(standardOptions),
785+
getHostNameVerifier(standardOptions));
759786
}
760787

761788

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/Apache5HttpRequestConfig.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727
public final class Apache5HttpRequestConfig {
2828

2929
private final Duration socketTimeout;
30-
private final Duration connectionTimeout;
3130
private final Duration connectionAcquireTimeout;
3231
private final boolean expectContinueEnabled;
3332
private final ProxyConfiguration proxyConfiguration;
3433

3534
private Apache5HttpRequestConfig(Builder builder) {
3635
this.socketTimeout = builder.socketTimeout;
37-
this.connectionTimeout = builder.connectionTimeout;
3836
this.connectionAcquireTimeout = builder.connectionAcquireTimeout;
3937
this.expectContinueEnabled = builder.expectContinueEnabled;
4038
this.proxyConfiguration = builder.proxyConfiguration;
@@ -44,10 +42,6 @@ public Duration socketTimeout() {
4442
return socketTimeout;
4543
}
4644

47-
public Duration connectionTimeout() {
48-
return connectionTimeout;
49-
}
50-
5145
public Duration connectionAcquireTimeout() {
5246
return connectionAcquireTimeout;
5347
}
@@ -73,7 +67,6 @@ public static Builder builder() {
7367
public static final class Builder {
7468

7569
private Duration socketTimeout;
76-
private Duration connectionTimeout;
7770
private Duration connectionAcquireTimeout;
7871
private boolean expectContinueEnabled;
7972
private ProxyConfiguration proxyConfiguration;
@@ -86,11 +79,6 @@ public Builder socketTimeout(Duration socketTimeout) {
8679
return this;
8780
}
8881

89-
public Builder connectionTimeout(Duration connectionTimeout) {
90-
this.connectionTimeout = connectionTimeout;
91-
return this;
92-
}
93-
9482
public Builder connectionAcquireTimeout(Duration connectionAcquireTimeout) {
9583
this.connectionAcquireTimeout = connectionAcquireTimeout;
9684
return this;

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/conn/ClientConnectionManagerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static HttpClientConnectionManager wrap(HttpClientConnectionManager orig)
4949
/**
5050
* Further wraps {@link LeaseRequest} to capture performance metrics.
5151
*/
52-
private static class InstrumentedHttpClientConnectionManager extends DelegatingHttpClientConnectionManager {
52+
private static final class InstrumentedHttpClientConnectionManager extends DelegatingHttpClientConnectionManager {
5353

5454
private InstrumentedHttpClientConnectionManager(HttpClientConnectionManager delegate) {
5555
super(delegate);

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/conn/ClientConnectionRequestFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static LeaseRequest wrap(LeaseRequest orig) {
5555
/**
5656
* Measures the latency of {@link LeaseRequest#get(Timeout)}.
5757
*/
58-
private static class InstrumentedConnectionRequest extends DelegatingConnectionRequest {
58+
private static final class InstrumentedConnectionRequest extends DelegatingConnectionRequest {
5959

6060
private InstrumentedConnectionRequest(LeaseRequest delegate) {
6161
super(delegate);

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/conn/SdkTlsSocketFactory.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,50 @@
1616
package software.amazon.awssdk.http.apache5.internal.conn;
1717

1818
import java.io.IOException;
19-
import java.net.InetSocketAddress;
2019
import java.net.Socket;
2120
import java.util.Arrays;
2221
import javax.net.ssl.HostnameVerifier;
2322
import javax.net.ssl.SSLContext;
2423
import javax.net.ssl.SSLSocket;
25-
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
26-
import org.apache.hc.core5.http.HttpHost;
24+
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
2725
import org.apache.hc.core5.http.protocol.HttpContext;
28-
import org.apache.hc.core5.util.TimeValue;
2926
import software.amazon.awssdk.annotations.SdkInternalApi;
30-
import software.amazon.awssdk.http.apache5.internal.net.SdkSocket;
27+
import software.amazon.awssdk.http.apache5.internal.net.SdkSslSocket;
3128
import software.amazon.awssdk.utils.Logger;
3229

3330
@SdkInternalApi
34-
public class SdkTlsSocketFactory extends SSLConnectionSocketFactory {
31+
public class SdkTlsSocketFactory extends DefaultClientTlsStrategy {
3532

3633
private static final Logger log = Logger.loggerFor(SdkTlsSocketFactory.class);
3734

3835
public SdkTlsSocketFactory(SSLContext sslContext, HostnameVerifier hostnameVerifier) {
3936
super(sslContext, hostnameVerifier);
4037
if (sslContext == null) {
4138
throw new IllegalArgumentException(
42-
"sslContext must not be null. " + "Use SSLContext.getDefault() if you are unsure.");
39+
"sslContext must not be null. Use SSLContext.getDefault() if you are unsure.");
4340
}
4441
}
4542

4643
@Override
47-
protected final void prepareSocket(SSLSocket socket) {
44+
protected void initializeSocket(SSLSocket socket) {
45+
super.initializeSocket(socket);
4846
log.debug(() -> String.format("socket.getSupportedProtocols(): %s, socket.getEnabledProtocols(): %s",
4947
Arrays.toString(socket.getSupportedProtocols()),
5048
Arrays.toString(socket.getEnabledProtocols())));
5149
}
5250

5351
@Override
54-
public Socket connectSocket(TimeValue connectTimeout,
55-
Socket socket,
56-
HttpHost host,
57-
InetSocketAddress remoteAddress,
58-
InetSocketAddress localAddress,
59-
HttpContext context) throws IOException {
60-
log.trace(() -> String.format("Connecting to %s:%s", remoteAddress.getAddress(), remoteAddress.getPort()));
52+
public SSLSocket upgrade(Socket socket,
53+
String target,
54+
int port,
55+
Object attachment,
56+
HttpContext context) throws IOException {
57+
log.trace(() -> String.format("Upgrading socket to TLS for %s:%s", target, port));
6158

62-
Socket connectSocket = super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
63-
return new SdkSocket(connectSocket);
59+
SSLSocket upgradedSocket = super.upgrade(socket, target, port, attachment, context);
60+
61+
// Wrap the upgraded SSLSocket in SdkSSLSocket for logging
62+
return new SdkSslSocket(upgradedSocket);
6463
}
64+
6565
}

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/impl/Apache5HttpRequestFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class Apache5HttpRequestFactory {
5252
private static final List<String> IGNORE_HEADERS = Arrays.asList(HttpHeaders.CONTENT_LENGTH, HttpHeaders.HOST,
5353
HttpHeaders.TRANSFER_ENCODING);
5454

55-
public HttpUriRequestBase create(final HttpExecuteRequest request, final Apache5HttpRequestConfig requestConfig) {
55+
public HttpUriRequestBase create(HttpExecuteRequest request, Apache5HttpRequestConfig requestConfig) {
5656
HttpUriRequestBase base = createApacheRequest(request, sanitizeUri(request.httpRequest()));
5757
addHeadersToRequest(base, request.httpRequest());
5858
addRequestConfig(base, request.httpRequest(), requestConfig);
@@ -90,12 +90,10 @@ private URI sanitizeUri(SdkHttpRequest request) {
9090
private void addRequestConfig(HttpUriRequestBase base,
9191
SdkHttpRequest request,
9292
Apache5HttpRequestConfig requestConfig) {
93-
int connectTimeout = saturatedCast(requestConfig.connectionTimeout().toMillis());
9493
int connectAcquireTimeout = saturatedCast(requestConfig.connectionAcquireTimeout().toMillis());
9594
RequestConfig.Builder requestConfigBuilder = RequestConfig
9695
.custom()
9796
.setConnectionRequestTimeout(connectAcquireTimeout, TimeUnit.MILLISECONDS)
98-
.setConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
9997
.setResponseTimeout(saturatedCast(requestConfig.socketTimeout().toMillis()), TimeUnit.MILLISECONDS);
10098

10199
/*

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/impl/Apache5SdkHttpClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public ClassicHttpResponse execute(HttpHost target, ClassicHttpRequest request)
6565
return delegate.execute(target, request);
6666
}
6767

68+
@Override
69+
public ClassicHttpResponse executeOpen(HttpHost target, ClassicHttpRequest request, HttpContext context) throws IOException {
70+
return delegate.executeOpen(target, request, context);
71+
}
72+
6873
@Override
6974
public HttpResponse execute(HttpHost target, ClassicHttpRequest request, HttpContext context) throws IOException {
7075
return delegate.execute(target, request, context);

0 commit comments

Comments
 (0)