Skip to content

Commit 54bbcf3

Browse files
committed
SPR-6180 - Upgrade Apache HttpClient to version 4.0
1 parent 313546a commit 54bbcf3

11 files changed

+381
-57
lines changed

org.springframework.web/ivy.xml

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
conf="optional, httpclient->compile"/>
6161
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1"
6262
conf="compile, commons-logging->compile"/>
63+
<dependency org="org.apache.httpcomponents" name="com.springsource.org.apache.httpcomponents.httpclient" rev="4.1.1"
64+
conf="optional, httpclient->compile"/>
6365
<dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15"
6466
conf="optional, log4j->compile"/>
6567
<dependency org="org.codehaus.jackson" name="com.springsource.org.codehaus.jackson.mapper" rev="1.4.2"

org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java

+2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
* @author Arjen Poutsma
4141
* @since 3.0
4242
* @see CommonsClientHttpRequestFactory#createRequest(java.net.URI, HttpMethod)
43+
* @deprecated In favor of {@link HttpComponentsClientHttpRequest}
4344
*/
45+
@Deprecated
4446
final class CommonsClientHttpRequest extends AbstractBufferingClientHttpRequest {
4547

4648
private final HttpClient httpClient;

org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -45,7 +45,9 @@
4545
* @author Arjen Poutsma
4646
* @since 3.0
4747
* @see org.springframework.http.client.SimpleClientHttpRequestFactory
48+
* @deprecated In favor of {@link HttpComponentsClientHttpRequestFactory}
4849
*/
50+
@Deprecated
4951
public class CommonsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean {
5052

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

org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -34,7 +34,9 @@
3434
* @author Arjen Poutsma
3535
* @since 3.0
3636
* @see CommonsClientHttpRequest#execute()
37+
* @deprecated In favor of {@link HttpComponentsClientHttpResponse}
3738
*/
39+
@Deprecated
3840
final class CommonsClientHttpResponse implements ClientHttpResponse {
3941

4042
private final HttpMethod httpMethod;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.apache.http.HttpEntity;
25+
import org.apache.http.HttpEntityEnclosingRequest;
26+
import org.apache.http.HttpResponse;
27+
import org.apache.http.client.HttpClient;
28+
import org.apache.http.client.methods.HttpUriRequest;
29+
import org.apache.http.entity.ByteArrayEntity;
30+
import org.apache.http.protocol.HTTP;
31+
32+
import org.springframework.http.HttpHeaders;
33+
import org.springframework.http.HttpMethod;
34+
35+
/**
36+
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
37+
* Apache HTTPComponents HttpClient to execute requests.
38+
*
39+
* <p>Created via the {@link HttpComponentsClientHttpRequestFactory}.
40+
*
41+
* @author Oleg Kalnichevski
42+
* @author Arjen Poutsma
43+
* @since 3.0
44+
* @see HttpComponentsClientHttpRequestFactory#createRequest(URI, HttpMethod)
45+
*/
46+
final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpRequest {
47+
48+
private final HttpClient httpClient;
49+
50+
private final HttpUriRequest httpRequest;
51+
52+
public HttpComponentsClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest) {
53+
this.httpClient = httpClient;
54+
this.httpRequest = httpRequest;
55+
}
56+
57+
public HttpMethod getMethod() {
58+
return HttpMethod.valueOf(httpRequest.getMethod());
59+
}
60+
61+
public URI getURI() {
62+
return httpRequest.getURI();
63+
}
64+
65+
@Override
66+
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
67+
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
68+
String headerName = entry.getKey();
69+
if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN) &&
70+
!headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) {
71+
for (String headerValue : entry.getValue()) {
72+
httpRequest.addHeader(headerName, headerValue);
73+
}
74+
}
75+
}
76+
if (httpRequest instanceof HttpEntityEnclosingRequest) {
77+
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest;
78+
HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
79+
entityEnclosingRequest.setEntity(requestEntity);
80+
}
81+
HttpResponse httpResponse = httpClient.execute(httpRequest);
82+
return new HttpComponentsClientHttpResponse(httpResponse);
83+
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
22+
import org.apache.http.client.HttpClient;
23+
import org.apache.http.client.methods.HttpDelete;
24+
import org.apache.http.client.methods.HttpGet;
25+
import org.apache.http.client.methods.HttpHead;
26+
import org.apache.http.client.methods.HttpOptions;
27+
import org.apache.http.client.methods.HttpPost;
28+
import org.apache.http.client.methods.HttpPut;
29+
import org.apache.http.client.methods.HttpTrace;
30+
import org.apache.http.client.methods.HttpUriRequest;
31+
import org.apache.http.conn.scheme.PlainSocketFactory;
32+
import org.apache.http.conn.scheme.Scheme;
33+
import org.apache.http.conn.scheme.SchemeRegistry;
34+
import org.apache.http.conn.ssl.SSLSocketFactory;
35+
import org.apache.http.impl.client.DefaultHttpClient;
36+
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
37+
import org.apache.http.params.CoreConnectionPNames;
38+
39+
import org.springframework.beans.factory.DisposableBean;
40+
import org.springframework.http.HttpMethod;
41+
import org.springframework.util.Assert;
42+
43+
/**
44+
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that uses
45+
* <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/">Http Components HttpClient</a> to create requests.
46+
*
47+
* <p>Allows to use a pre-configured {@link HttpClient} instance -
48+
* potentially with authentication, HTTP connection pooling, etc.
49+
*
50+
* @author Oleg Kalnichevski
51+
* @author Arjen Poutsma
52+
* @since 3.1
53+
*/
54+
public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean {
55+
56+
private static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 100;
57+
58+
private static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 5;
59+
60+
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000);
61+
62+
private HttpClient httpClient;
63+
64+
/**
65+
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory} with a default {@link HttpClient} that
66+
* uses a default {@link org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager}
67+
*/
68+
public HttpComponentsClientHttpRequestFactory() {
69+
SchemeRegistry schemeRegistry = new SchemeRegistry();
70+
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
71+
schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
72+
73+
ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
74+
connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
75+
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
76+
77+
httpClient = new DefaultHttpClient(connectionManager);
78+
this.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
79+
}
80+
81+
/**
82+
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory} with the given {@link HttpClient}
83+
* instance.
84+
*
85+
* @param httpClient the HttpClient instance to use for this factory
86+
*/
87+
public HttpComponentsClientHttpRequestFactory(HttpClient httpClient) {
88+
Assert.notNull(httpClient, "httpClient must not be null");
89+
this.httpClient = httpClient;
90+
}
91+
92+
/**
93+
* Set the {@code HttpClient} used by this factory.
94+
*/
95+
public void setHttpClient(HttpClient httpClient) {
96+
this.httpClient = httpClient;
97+
}
98+
99+
/**
100+
* Set the socket read timeout for the underlying HttpClient. A value of 0 means <em>never</em> timeout.
101+
*
102+
* @param timeout the timeout value in milliseconds
103+
* @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)
104+
*/
105+
public void setReadTimeout(int timeout) {
106+
if (timeout < 0) {
107+
throw new IllegalArgumentException("timeout must be a non-negative value");
108+
}
109+
getHttpClient().getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
110+
}
111+
112+
/**
113+
* Return the {@code HttpClient} used by this factory.
114+
*/
115+
public HttpClient getHttpClient() {
116+
return this.httpClient;
117+
}
118+
119+
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
120+
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
121+
postProcessHttpRequest(httpRequest);
122+
return new HttpComponentsClientHttpRequest(getHttpClient(), httpRequest);
123+
}
124+
125+
/**
126+
* Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
127+
*
128+
* @param httpMethod the HTTP method
129+
* @param uri the URI
130+
* @return the Commons HttpMethodBase object
131+
*/
132+
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
133+
switch (httpMethod) {
134+
case GET:
135+
return new HttpGet(uri);
136+
case DELETE:
137+
return new HttpDelete(uri);
138+
case HEAD:
139+
return new HttpHead(uri);
140+
case OPTIONS:
141+
return new HttpOptions(uri);
142+
case POST:
143+
return new HttpPost(uri);
144+
case PUT:
145+
return new HttpPut(uri);
146+
case TRACE:
147+
return new HttpTrace(uri);
148+
default:
149+
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
150+
}
151+
}
152+
153+
/**
154+
* Template method that allows for manipulating the {@link HttpUriRequest} before it is returned as part of a {@link
155+
* HttpComponentsClientHttpRequest}.
156+
* <p>The default implementation is empty.
157+
*
158+
* @param request the request to process
159+
*/
160+
protected void postProcessHttpRequest(HttpUriRequest request) {
161+
}
162+
163+
/**
164+
* Shutdown hook that closes the underlying {@link org.apache.http.conn.ClientConnectionManager
165+
* ClientConnectionManager}'s connection pool, if any.
166+
*/
167+
public void destroy() {
168+
getHttpClient().getConnectionManager().shutdown();
169+
}
170+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
import org.apache.http.Header;
23+
import org.apache.http.HttpEntity;
24+
import org.apache.http.HttpResponse;
25+
import org.apache.http.util.EntityUtils;
26+
27+
import org.springframework.http.HttpHeaders;
28+
import org.springframework.http.HttpStatus;
29+
30+
/**
31+
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses
32+
* Apache Http Components HttpClient to execute requests.
33+
*
34+
* <p>Created via the {@link HttpComponentsClientHttpRequest}.
35+
*
36+
* @author Oleg Kalnichevski
37+
* @author Arjen Poutsma
38+
* @since 3.0
39+
* @see HttpComponentsClientHttpRequest#execute()
40+
*/
41+
final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
42+
43+
private final HttpResponse httpResponse;
44+
45+
private HttpHeaders headers;
46+
47+
public HttpComponentsClientHttpResponse(HttpResponse httpResponse) {
48+
this.httpResponse = httpResponse;
49+
}
50+
51+
public HttpStatus getStatusCode() throws IOException {
52+
return HttpStatus.valueOf(httpResponse.getStatusLine().getStatusCode());
53+
}
54+
55+
public String getStatusText() throws IOException {
56+
return httpResponse.getStatusLine().getReasonPhrase();
57+
}
58+
59+
public HttpHeaders getHeaders() {
60+
if (headers == null) {
61+
headers = new HttpHeaders();
62+
for (Header header : httpResponse.getAllHeaders()) {
63+
headers.add(header.getName(), header.getValue());
64+
}
65+
}
66+
return headers;
67+
}
68+
69+
public InputStream getBody() throws IOException {
70+
HttpEntity entity = httpResponse.getEntity();
71+
return entity != null ? entity.getContent() : null;
72+
}
73+
74+
public void close() {
75+
HttpEntity entity = httpResponse.getEntity();
76+
if (entity != null) {
77+
try {
78+
// Release underlying connection back to the connection manager
79+
EntityUtils.consume(entity);
80+
}
81+
catch (IOException e) {
82+
// ignore
83+
}
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)