All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cloudslang.content.httpclient.build.conn.ConnectionManagerBuilder Maven / Gradle / Ivy

There is a newer version: 0.1.77
Show newest version
/*
 * (c) Copyright 2017 EntIT Software LLC, a Micro Focus company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 *
 * The Apache License is available 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 io.cloudslang.content.httpclient.build.conn;

import com.hp.oo.sdk.content.plugin.GlobalSessionObject;
import com.hp.oo.sdk.content.plugin.SessionResource;
import io.cloudslang.content.httpclient.HttpClientInputs;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
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.conn.PoolingHttpClientConnectionManager;

import java.util.HashMap;
import java.util.Map;

public class ConnectionManagerBuilder {
    private GlobalSessionObject> connectionPoolHolder;
    private SSLConnectionSocketFactory sslsf;
    private String connectionManagerMapKey;
    private String defaultMaxPerRoute;
    private String totalMax;

    public ConnectionManagerBuilder setConnectionPoolHolder(GlobalSessionObject connectionPoolHolder) {
        this.connectionPoolHolder = connectionPoolHolder;
        return this;
    }

    public ConnectionManagerBuilder setSslsf(SSLConnectionSocketFactory sslsf) {
        this.sslsf = sslsf;
        return this;
    }

    public ConnectionManagerBuilder setTotalMax(String totalMax) {
        this.totalMax = totalMax;
        return this;
    }

    public ConnectionManagerBuilder setDefaultMaxPerRoute(String defaultMaxPerRoute) {
        this.defaultMaxPerRoute = defaultMaxPerRoute;
        return this;
    }

    public ConnectionManagerBuilder setConnectionManagerMapKey(String... connectionManagerMapKeys) {
        this.connectionManagerMapKey = buildConnectionManagerMapKey(connectionManagerMapKeys);
        return this;
    }

    public static String buildConnectionManagerMapKey(String... connectionManagerMapKeys) {
        StringBuilder keyBuilder = new StringBuilder();
        for (String token : connectionManagerMapKeys) {
            keyBuilder.append(token).append(":");
        }
        if (keyBuilder.length() > 0) {
            keyBuilder.deleteCharAt(keyBuilder.length() - 1);
        }
        return keyBuilder.toString();
    }

    public PoolingHttpClientConnectionManager buildConnectionManager() {
        if (connectionPoolHolder != null) {
            PoolingHttpClientConnectionManager connManager = null;
            synchronized (connectionPoolHolder) {
                Map connectionManagerMap
                        = connectionPoolHolder.get();

                if (connectionManagerMap == null) {
                    final HashMap connectionManagerMapFinal = new HashMap<>();
                    connectionPoolHolder.setResource(new SessionResource>() {
                        @Override
                        public Map get() {
                            return connectionManagerMapFinal;
                        }

                        @Override
                        public void release() {
                        }
                    });
                    connectionManagerMap = connectionPoolHolder.get();
                }

                connManager = connectionManagerMap.get(connectionManagerMapKey);
                if (connManager == null) {
                    Registry socketFactoryRegistry = RegistryBuilder.create()
                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
                            .register("https", sslsf)
                            .build();
                    connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

                    connectionManagerMap.put(connectionManagerMapKey, connManager);
                }
            }

            //the DefaultMaxPerRoute default is 2
            if (!StringUtils.isEmpty(defaultMaxPerRoute)) {
                try {
                    connManager.setDefaultMaxPerRoute(Integer.parseInt(defaultMaxPerRoute));
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException("the '" + HttpClientInputs.CONNECTIONS_MAX_PER_ROUTE
                            + "' input should be integer" + e.getMessage(), e);
                }
            }
            //the Default totalMax default is 20
            if (!StringUtils.isEmpty(totalMax)) {
                try {
                    connManager.setMaxTotal(Integer.parseInt(totalMax));
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException("the '" + HttpClientInputs.CONNECTIONS_MAX_TOTAL
                            + "' input should be integer" + e.getMessage(), e);
                }
            }
            return connManager;
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy