/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.apache.axis2.transport.http.impl.httpclient4; import org.apache.axis2.AxisFault; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.MessageContext; import org.apache.axis2.transport.http.AxisRequestEntity; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.axis2.transport.http.HTTPSender; import org.apache.axis2.transport.http.Request; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.client.HttpClient; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.HttpClientConnectionManager; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.ssl.SSLContexts; import java.net.URL; import javax.net.ssl.SSLContext; public class HTTPSenderImpl extends HTTPSender { private static final Log log = LogFactory.getLog(HTTPSenderImpl.class); @Override protected Request createRequest(MessageContext msgContext, String methodName, URL url, AxisRequestEntity requestEntity) throws AxisFault { return new RequestImpl(getHttpClient(msgContext), msgContext, methodName, url, requestEntity); } private HttpClient getHttpClient(MessageContext msgContext) { ConfigurationContext configContext = msgContext.getConfigurationContext(); HttpClient httpClient = (HttpClient) msgContext .getProperty(HTTPConstants.CACHED_HTTP_CLIENT); if (httpClient == null) { httpClient = (HttpClient) configContext. getProperty(HTTPConstants.CACHED_HTTP_CLIENT); } if (httpClient != null) { return httpClient; } synchronized (this) { httpClient = (HttpClient) msgContext. getProperty(HTTPConstants.CACHED_HTTP_CLIENT); if (httpClient == null) { httpClient = (HttpClient) configContext .getProperty(HTTPConstants.CACHED_HTTP_CLIENT); } if (httpClient != null) { return httpClient; } HttpClientConnectionManager connManager = (HttpClientConnectionManager) msgContext .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER); if (connManager == null) { connManager = (HttpClientConnectionManager) msgContext .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER); } if (connManager == null) { // reuse HttpConnectionManager synchronized (configContext) { connManager = (HttpClientConnectionManager) configContext .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER); if (connManager == null) { log.trace("Making new ConnectionManager"); SSLContext sslContext = (SSLContext)configContext.getProperty(SSLContext.class.getName()); if (sslContext == null) { sslContext = SSLContexts.createDefault(); } Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", new SSLConnectionSocketFactory(sslContext)) .build(); connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); ((PoolingHttpClientConnectionManager)connManager).setMaxTotal(200); ((PoolingHttpClientConnectionManager)connManager).setDefaultMaxPerRoute(200); configContext.setProperty( HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER, connManager); } } } /* * Create a new instance of HttpClient since the way it is used here * it's not fully thread-safe. */ return HttpClientBuilder.create() .setConnectionManager(connManager) .setConnectionManagerShared(true) .build(); } } }