Skip to content

Commit e88d20d

Browse files
authored
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
1 parent 2590e6e commit e88d20d

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
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;
@@ -54,9 +55,11 @@
5455
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
5556
import org.apache.hc.client5.http.protocol.HttpClientContext;
5657
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
58+
import org.apache.hc.client5.http.routing.RoutingSupport;
5759
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
5860
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
5961
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
62+
import org.apache.hc.core5.http.ClassicHttpRequest;
6063
import org.apache.hc.core5.http.ClassicHttpResponse;
6164
import org.apache.hc.core5.http.Header;
6265
import org.apache.hc.core5.http.HttpEntity;
@@ -275,14 +278,25 @@ private HttpExecuteResponse execute(HttpUriRequestBase apacheRequest, MetricColl
275278
HttpClientContext localRequestContext = Apache5Utils.newClientContext(requestConfig.proxyConfiguration());
276279
THREAD_LOCAL_REQUEST_METRIC_COLLECTOR.set(metricCollector);
277280
try {
278-
HttpResponse httpResponse = httpClient.execute(apacheRequest, localRequestContext);
279-
// Create a connection-aware input stream that closes the response when closed
281+
HttpHost target = determineTarget(apacheRequest);
282+
ClassicHttpResponse httpResponse = httpClient.executeOpen(target, apacheRequest, localRequestContext);
280283
return createResponse(httpResponse, apacheRequest);
281284
} finally {
282285
THREAD_LOCAL_REQUEST_METRIC_COLLECTOR.remove();
283286
}
284287
}
285288

289+
/**
290+
* Determines the target host from the request using Apache HttpClient's official routing support utility.
291+
*/
292+
private static HttpHost determineTarget(ClassicHttpRequest request) throws IOException {
293+
try {
294+
return RoutingSupport.determineHost(request);
295+
} catch (HttpException ex) {
296+
throw new ClientProtocolException(ex);
297+
}
298+
}
299+
286300
private HttpUriRequestBase toApacheRequest(HttpExecuteRequest request) {
287301
return apacheHttpRequestFactory.create(request, requestConfig);
288302
}

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);

http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/Apache5HttpClientWireMockTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void closeReleasesResources() throws Exception {
287287
}
288288

289289
@Test
290-
public void connectionTimeout_exceedsLimit_throwsException() throws Exception {
290+
public void connectionTimeout_exceedsLimit_throwsException() {
291291
// Test connection timeout with a very short timeout and non-responsive address
292292
try (SdkHttpClient client = Apache5HttpClient.builder()
293293
.connectionTimeout(Duration.ofMillis(100))
@@ -318,7 +318,7 @@ public void connectionTimeout_exceedsLimit_throwsException() throws Exception {
318318
}
319319

320320
@Test
321-
public void socketTimeout_exceedsLimit_throwsException() throws Exception {
321+
public void socketTimeout_exceedsLimit_throwsException() {
322322
// Configure WireMock to delay response longer than socket timeout
323323
mockServer.stubFor(any(urlPathEqualTo("/delayed"))
324324
.willReturn(aResponse()

http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/MetricReportingTest.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
3131
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
3232
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
33-
import org.apache.hc.core5.http.ClassicHttpResponse;
34-
import org.apache.hc.core5.http.HttpVersion;
35-
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
3633
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
37-
import org.apache.hc.core5.http.message.BasicHttpResponse;
3834
import org.apache.hc.core5.http.protocol.HttpContext;
3935
import org.apache.hc.core5.pool.PoolStats;
4036
import org.junit.Before;
@@ -43,7 +39,6 @@
4339
import org.mockito.Mock;
4440
import org.mockito.junit.MockitoJUnitRunner;
4541
import software.amazon.awssdk.http.HttpExecuteRequest;
46-
import software.amazon.awssdk.http.HttpExecuteResponse;
4742
import software.amazon.awssdk.http.SdkHttpFullRequest;
4843
import software.amazon.awssdk.http.SdkHttpMethod;
4944
import software.amazon.awssdk.http.apache5.internal.Apache5HttpRequestConfig;
@@ -63,12 +58,12 @@ public class MetricReportingTest {
6358

6459
@Before
6560
public void methodSetup() throws IOException {
66-
67-
when(mockHttpClient.execute(any(HttpUriRequest.class), any(HttpContext.class)))
68-
.thenReturn(new BasicClassicHttpResponse(200, "OK"));
69-
61+
// Create a response that can be reused
62+
BasicClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
63+
// Mock executeOpen which is now being used
64+
when(mockHttpClient.executeOpen(any(), any(HttpUriRequest.class), any(HttpContext.class)))
65+
.thenReturn(response);
7066
when(mockHttpClient.getHttpClientConnectionManager()).thenReturn(cm);
71-
7267
PoolStats stats = new PoolStats(1, 2, 3, 4);
7368
when(cm.getTotalStats()).thenReturn(stats);
7469
}
@@ -117,11 +112,11 @@ private Apache5HttpClient newClient() {
117112
}
118113

119114
private HttpExecuteRequest newRequest(MetricCollector collector) {
120-
final SdkHttpFullRequest sdkRequest = SdkHttpFullRequest.builder()
121-
.method(SdkHttpMethod.HEAD)
122-
.host("amazonaws.com")
123-
.protocol("https")
124-
.build();
115+
SdkHttpFullRequest sdkRequest = SdkHttpFullRequest.builder()
116+
.method(SdkHttpMethod.HEAD)
117+
.host("amazonaws.com")
118+
.protocol("https")
119+
.build();
125120
return HttpExecuteRequest.builder()
126121
.request(sdkRequest)
127122
.metricCollector(collector)

0 commit comments

Comments
 (0)