HttpClientBuilder.java 2.32 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.httpclient.HostConfiguration
 *  org.apache.commons.httpclient.HttpClient
 *  org.apache.commons.httpclient.protocol.Protocol
 *  org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.mcm.campaign.impl;

import com.day.cq.mcm.campaign.impl.IntegrationConfig;
import com.day.cq.mcm.campaign.impl.hcext.EasySSLProtocolSocketFactory;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientBuilder {
    private static final Logger log = LoggerFactory.getLogger(HttpClientBuilder.class);

    public static HttpClient createHttpClient(String url, IntegrationConfig config) throws URISyntaxException {
        HttpClient hc = new HttpClient();
        HostConfiguration hostCfg = hc.getHostConfiguration();
        URI uri = new URI(url);
        String protocol = uri.getScheme();
        String host = uri.getHost();
        int port = uri.getPort();
        if ("https".equals(protocol)) {
            port = port < 0 ? 443 : port;
            log.debug("https connection: {}:{}", new Object[]{host, port});
            if (config.useRelaxedSSL()) {
                log.debug("using relaxed SSL");
                Protocol https = new Protocol("https", (SecureProtocolSocketFactory)new EasySSLProtocolSocketFactory(true), port);
                hostCfg.setHost(host, port, https);
            } else {
                hostCfg.setHost(host, port, protocol);
            }
        } else {
            port = port < 0 ? 80 : port;
            log.debug("http connection: {}:{}", new Object[]{host, port});
            hostCfg.setHost(host, port);
        }
        return hc;
    }

    public static String excludeHost(String url) {
        if (url.startsWith("/")) {
            return url;
        }
        int protocolPos = url.indexOf("://");
        int pathPos = url.indexOf(47, protocolPos > 0 ? protocolPos + 3 : 0);
        return url.substring(pathPos);
    }
}