Skip to content

Commit 1c73664

Browse files
committed
Defensively access deprecated AbstractHttpClient class from Apache HttpComponents
Issue: SPR-14422
1 parent 8389e3f commit 1c73664

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
import org.springframework.beans.factory.DisposableBean;
4040
import org.springframework.http.HttpMethod;
4141
import org.springframework.util.Assert;
42+
import org.springframework.util.ClassUtils;
4243

4344
/**
4445
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that
@@ -58,6 +59,20 @@
5859
*/
5960
public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean {
6061

62+
private static Class<?> abstractHttpClientClass;
63+
64+
static {
65+
try {
66+
// Looking for AbstractHttpClient class (deprecated as of HttpComponents 4.3)
67+
abstractHttpClientClass = ClassUtils.forName("org.apache.http.impl.client.AbstractHttpClient",
68+
HttpComponentsClientHttpRequestFactory.class.getClassLoader());
69+
}
70+
catch (ClassNotFoundException ex) {
71+
// Probably removed from HttpComponents in the meantime...
72+
}
73+
}
74+
75+
6176
private HttpClient httpClient;
6277

6378
private RequestConfig requestConfig;
@@ -130,7 +145,7 @@ public void setConnectTimeout(int timeout) {
130145
*/
131146
@SuppressWarnings("deprecation")
132147
private void setLegacyConnectionTimeout(HttpClient client, int timeout) {
133-
if (org.apache.http.impl.client.AbstractHttpClient.class.isInstance(client)) {
148+
if (abstractHttpClientClass != null && abstractHttpClientClass.isInstance(client)) {
134149
client.getParams().setIntParameter(org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
135150
}
136151
}
@@ -171,7 +186,7 @@ public void setReadTimeout(int timeout) {
171186
*/
172187
@SuppressWarnings("deprecation")
173188
private void setLegacySocketTimeout(HttpClient client, int timeout) {
174-
if (org.apache.http.impl.client.AbstractHttpClient.class.isInstance(client)) {
189+
if (abstractHttpClientClass != null && abstractHttpClientClass.isInstance(client)) {
175190
client.getParams().setIntParameter(org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, timeout);
176191
}
177192
}

spring-web/src/main/java/org/springframework/remoting/httpinvoker/HttpComponentsHttpInvokerRequestExecutor.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@
4343
import org.springframework.context.i18n.LocaleContextHolder;
4444
import org.springframework.remoting.support.RemoteInvocationResult;
4545
import org.springframework.util.Assert;
46+
import org.springframework.util.ClassUtils;
4647
import org.springframework.util.StringUtils;
4748

4849
/**
@@ -69,6 +70,21 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
6970

7071
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000);
7172

73+
74+
private static Class<?> abstractHttpClientClass;
75+
76+
static {
77+
try {
78+
// Looking for AbstractHttpClient class (deprecated as of HttpComponents 4.3)
79+
abstractHttpClientClass = ClassUtils.forName("org.apache.http.impl.client.AbstractHttpClient",
80+
HttpComponentsHttpInvokerRequestExecutor.class.getClassLoader());
81+
}
82+
catch (ClassNotFoundException ex) {
83+
// Probably removed from HttpComponents in the meantime...
84+
}
85+
}
86+
87+
7288
private HttpClient httpClient;
7389

7490
private RequestConfig requestConfig;
@@ -156,7 +172,7 @@ public void setConnectTimeout(int timeout) {
156172
*/
157173
@SuppressWarnings("deprecation")
158174
private void setLegacyConnectionTimeout(HttpClient client, int timeout) {
159-
if (org.apache.http.impl.client.AbstractHttpClient.class.isInstance(client)) {
175+
if (abstractHttpClientClass != null && abstractHttpClientClass.isInstance(client)) {
160176
client.getParams().setIntParameter(org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
161177
}
162178
}
@@ -198,7 +214,7 @@ public void setReadTimeout(int timeout) {
198214
*/
199215
@SuppressWarnings("deprecation")
200216
private void setLegacySocketTimeout(HttpClient client, int timeout) {
201-
if (org.apache.http.impl.client.AbstractHttpClient.class.isInstance(client)) {
217+
if (abstractHttpClientClass != null && abstractHttpClientClass.isInstance(client)) {
202218
client.getParams().setIntParameter(org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, timeout);
203219
}
204220
}

0 commit comments

Comments
 (0)