Java tutorial
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.greencheek.spring.rest; import java.io.IOException; import java.net.URI; import java.security.Principal; import java.util.concurrent.atomic.AtomicReference; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpOptions; import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpTrace; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.protocol.RequestDefaultHeaders; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.springframework.beans.factory.DisposableBean; import org.springframework.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.util.Assert; /** * {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that uses * <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/">Apache HttpComponents HttpClient</a> * to create requests. * * <p>Allows to use a pre-configured {@link HttpClient} instance - * potentially with authentication, HTTP connection pooling, etc. * * @author Oleg Kalnichevski * @author Arjen Poutsma * @since 3.1 */ public class SSLCachingHttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean { private static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 100; private static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 5; private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (5 * 1000); private static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = (5 * 1000); private static final boolean DEFAULT_TCP_NO_DELAY = true; private HttpClient httpClient; private final AtomicReference<Principal> sslToken = new AtomicReference<Principal>(); private final boolean useSSLCaching; public SSLCachingHttpComponentsClientHttpRequestFactory() { this(true); } /** * Create a new instance of the HttpComponentsClientHttpRequestFactory with a default * {@link HttpClient} that uses a default {@link org.apache.http.impl.conn.PoolingClientConnectionManager}. */ public SSLCachingHttpComponentsClientHttpRequestFactory(boolean useSSLCaching) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSystemSocketFactory())); PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry); connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE); this.httpClient = new DefaultHttpClient(connectionManager); ((DefaultHttpClient) this.httpClient).removeRequestInterceptorByClass(RequestDefaultHeaders.class); setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS); setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLISECONDS); this.useSSLCaching = useSSLCaching; setTcpNoDelay(DEFAULT_TCP_NO_DELAY); } public void setTcpNoDelay(boolean tcpNoDelay) { getHttpClient().getParams().setBooleanParameter("http.tcp.nodelay", tcpNoDelay); } /** * Create a new instance of the HttpComponentsClientHttpRequestFactory * with the given {@link HttpClient} instance. * @param httpClient the HttpClient instance to use for this request factory */ public SSLCachingHttpComponentsClientHttpRequestFactory(HttpClient httpClient, boolean useSSLCaching) { Assert.notNull(httpClient, "HttpClient must not be null"); this.httpClient = httpClient; this.useSSLCaching = useSSLCaching; } public SSLCachingHttpComponentsClientHttpRequestFactory(boolean followRedirects, boolean useSSLCaching) { this(useSSLCaching); setFollowRedirects(followRedirects); } /** * Set the {@code HttpClient} used by this factory. */ public void setHttpClient(HttpClient httpClient) { this.httpClient = httpClient; } /** * Return the {@code HttpClient} used by this factory. */ public HttpClient getHttpClient() { return this.httpClient; } /** * Set the connection timeout for the underlying HttpClient. * A timeout value of 0 specifies an infinite timeout. * @param timeout the timeout value in milliseconds */ public void setConnectTimeout(int timeout) { Assert.isTrue(timeout >= 0, "Timeout must be a non-negative value"); getHttpClient().getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); } /** * Set if the client should follow redirects * @param followRedirects */ public void setFollowRedirects(boolean followRedirects) { getHttpClient().getParams().setBooleanParameter("http.protocol.handle-redirects", followRedirects); } /** * Set the socket read timeout for the underlying HttpClient. * A timeout value of 0 specifies an infinite timeout. * @param timeout the timeout value in milliseconds */ public void setReadTimeout(int timeout) { Assert.isTrue(timeout >= 0, "Timeout must be a non-negative value"); getHttpClient().getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout); } public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri); postProcessHttpRequest(httpRequest); return new SSLCachingHttpComponentsClientHttpRequest(getHttpClient(), httpRequest, createHttpContext(httpMethod, uri), sslToken, useSSLCaching); } /** * Create a Commons HttpMethodBase object for the given HTTP method and URI specification. * @param httpMethod the HTTP method * @param uri the URI * @return the Commons HttpMethodBase object */ protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) { switch (httpMethod) { case GET: return new HttpGet(uri); case DELETE: return new HttpDelete(uri); case HEAD: return new HttpHead(uri); case OPTIONS: return new HttpOptions(uri); case POST: return new HttpPost(uri); case PUT: return new HttpPut(uri); case TRACE: return new HttpTrace(uri); // Not supported in 3.0.6 // case PATCH: // return new HttpPatch(uri); default: throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod); } } /** * Template method that allows for manipulating the {@link HttpUriRequest} before it is * returned as part of a {@link SSLCachingHttpComponentsClientHttpRequest}. * <p>The default implementation is empty. * @param request the request to process */ protected void postProcessHttpRequest(HttpUriRequest request) { } /** * Template methods that creates a {@link HttpContext} for the given HTTP method and URI. * <p>The default implementation returns {@code null}. * @param httpMethod the HTTP method * @param uri the URI * @return the http context */ protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { return new BasicHttpContext(); } /** * Shutdown hook that closes the underlying * {@link org.apache.http.conn.ClientConnectionManager ClientConnectionManager}'s * connection pool, if any. */ public void destroy() { getHttpClient().getConnectionManager().shutdown(); } }