|
9 | 9 | import org.apache.http.*;
|
10 | 10 | import org.apache.http.client.*;
|
11 | 11 | import org.apache.http.client.methods.*;
|
| 12 | +import org.apache.http.conn.*; |
| 13 | +import org.apache.http.conn.scheme.*; |
| 14 | +import org.apache.http.conn.ssl.*; |
12 | 15 | import org.apache.http.entity.StringEntity;
|
13 | 16 | import org.apache.http.impl.client.*;
|
| 17 | +import org.apache.http.impl.conn.*; |
| 18 | +import org.apache.http.params.*; |
14 | 19 | import org.apache.http.util.EntityUtils;
|
15 | 20 |
|
16 | 21 | import java.io.File;
|
| 22 | +import java.net.Socket; |
| 23 | +import java.net.UnknownHostException; |
17 | 24 | import java.net.URLEncoder;
|
18 | 25 |
|
19 | 26 | import java.util.Map;
|
|
22 | 29 | import java.io.IOException;
|
23 | 30 | import java.io.UnsupportedEncodingException;
|
24 | 31 |
|
| 32 | +import java.security.GeneralSecurityException; |
| 33 | +import java.security.KeyManagementException; |
| 34 | +import java.security.KeyStore; |
| 35 | +import java.security.NoSuchAlgorithmException; |
| 36 | +import java.security.SecureRandom; |
| 37 | +import java.security.cert.*; |
| 38 | + |
| 39 | +import java.text.DateFormat; |
| 40 | +import java.text.SimpleDateFormat; |
| 41 | + |
| 42 | +import java.util.Date; |
| 43 | +import java.util.Random; |
| 44 | +import java.util.logging.Level; |
| 45 | +import java.util.logging.Logger; |
| 46 | + |
| 47 | +import javax.crypto.Mac; |
| 48 | +import javax.crypto.spec.SecretKeySpec; |
| 49 | + |
| 50 | +import javax.net.ssl.SSLContext; |
| 51 | +import javax.net.ssl.TrustManager; |
| 52 | +import javax.net.ssl.X509TrustManager; |
| 53 | + |
25 | 54 | public class ApiInvoker {
|
26 | 55 | private static ApiInvoker INSTANCE = new ApiInvoker();
|
27 | 56 | private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
28 | 57 |
|
29 | 58 | private HttpClient client = null;
|
| 59 | + |
| 60 | + private boolean ignoreSSLCertificates = false; |
| 61 | + |
| 62 | + private ClientConnectionManager ignoreSSLConnectionManager; |
| 63 | + |
| 64 | + public ApiInvoker() { |
| 65 | + initConnectionManager(); |
| 66 | + } |
| 67 | + |
30 | 68 | public static ApiInvoker getInstance() {
|
31 | 69 | return INSTANCE;
|
32 | 70 | }
|
33 | 71 |
|
| 72 | + public void ignoreSSLCertificates(boolean ignoreSSLCertificates) { |
| 73 | + this.ignoreSSLCertificates = ignoreSSLCertificates; |
| 74 | + } |
| 75 | + |
34 | 76 | public void addDefaultHeader(String key, String value) {
|
35 | 77 | defaultHeaderMap.put(key, value);
|
36 | 78 | }
|
@@ -114,8 +156,11 @@ public String invokeAPI(String host, String path, String method, Map<String, Str
|
114 | 156 | }
|
115 | 157 | else if ("POST".equals(method)) {
|
116 | 158 | HttpPost post = new HttpPost(url);
|
117 |
| - post.setHeader("Content-Type", contentType); |
118 |
| - post.setEntity(new StringEntity(serialize(body), "UTF-8")); |
| 159 | + |
| 160 | + if (body != null) { |
| 161 | + post.setHeader("Content-Type", contentType); |
| 162 | + post.setEntity(new StringEntity(serialize(body), "UTF-8")); |
| 163 | + } |
119 | 164 | for(String key : headers.keySet()) {
|
120 | 165 | post.setHeader(key, headers.get(key));
|
121 | 166 | }
|
@@ -167,8 +212,61 @@ else if(code >= 200 && code < 300) {
|
167 | 212 | }
|
168 | 213 |
|
169 | 214 | private HttpClient getClient(String host) {
|
170 |
| - if(client == null) |
171 |
| - client = new DefaultHttpClient(); |
| 215 | + if (client == null) { |
| 216 | + if (ignoreSSLCertificates && ignoreSSLConnectionManager != null) { |
| 217 | + // Trust self signed certificates |
| 218 | + client = new DefaultHttpClient(ignoreSSLConnectionManager, new BasicHttpParams()); |
| 219 | + } else { |
| 220 | + client = new DefaultHttpClient(); |
| 221 | + } |
| 222 | + } |
172 | 223 | return client;
|
173 | 224 | }
|
| 225 | + |
| 226 | + private void initConnectionManager() { |
| 227 | + try { |
| 228 | + final SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 229 | + |
| 230 | + // set up a TrustManager that trusts everything |
| 231 | + TrustManager[] trustManagers = new TrustManager[] { |
| 232 | + new X509TrustManager() { |
| 233 | + public X509Certificate[] getAcceptedIssuers() { |
| 234 | + return null; |
| 235 | + } |
| 236 | + public void checkClientTrusted(X509Certificate[] certs, String authType) {} |
| 237 | + public void checkServerTrusted(X509Certificate[] certs, String authType) {} |
| 238 | + }}; |
| 239 | + |
| 240 | + sslContext.init(null, trustManagers, new SecureRandom()); |
| 241 | + |
| 242 | + SSLSocketFactory sf = new SSLSocketFactory((KeyStore)null) { |
| 243 | + private javax.net.ssl.SSLSocketFactory sslFactory = sslContext.getSocketFactory(); |
| 244 | + |
| 245 | + public Socket createSocket(Socket socket, String host, int port, boolean autoClose) |
| 246 | + throws IOException, UnknownHostException { |
| 247 | + return sslFactory.createSocket(socket, host, port, autoClose); |
| 248 | + } |
| 249 | + |
| 250 | + public Socket createSocket() throws IOException { |
| 251 | + return sslFactory.createSocket(); |
| 252 | + } |
| 253 | + }; |
| 254 | + |
| 255 | + sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
| 256 | + Scheme httpsScheme = new Scheme("https", sf, 443); |
| 257 | + SchemeRegistry schemeRegistry = new SchemeRegistry(); |
| 258 | + schemeRegistry.register(httpsScheme); |
| 259 | + schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); |
| 260 | + |
| 261 | + ignoreSSLConnectionManager = new SingleClientConnManager(new BasicHttpParams(), schemeRegistry); |
| 262 | + } catch (NoSuchAlgorithmException e) { |
| 263 | + // This will only be thrown if SSL isn't available for some reason. |
| 264 | + } catch (KeyManagementException e) { |
| 265 | + // This might be thrown when passing a key into init(), but no key is being passed. |
| 266 | + } catch (GeneralSecurityException e) { |
| 267 | + // This catches anything else that might go wrong. |
| 268 | + // If anything goes wrong we default to the standard connection manager. |
| 269 | + } |
| 270 | + } |
174 | 271 | }
|
| 272 | + |
0 commit comments