|
| 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 | +} |
0 commit comments