HttpClientBuilder.java
2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* 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);
}
}