working on ssl calls

This commit is contained in:
BWS Systems
2019-05-31 15:19:24 -05:00
parent 2d3fac691b
commit f266945b7e
3 changed files with 105 additions and 22 deletions

View File

@@ -5,6 +5,8 @@ import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import javax.net.ssl.SSLContext;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@@ -29,31 +31,40 @@ public class HTTPHandler {
callType = null; callType = null;
} }
public HTTPHandler(String type) { public HTTPHandler(String type) {
super(); super();
callType = type; callType = type;
} }
// This function executes the url from the device repository against the // This function executes the url from the device repository against the
// target as http or https as defined // target as http or https as defined
public String doHttpRequest(String url, String httpVerb, String contentType, String body, NameValue[] headers) { public String doHttpRequest(String url, String httpVerb, String contentType, String body, NameValue[] headers) {
log.debug("doHttpRequest with url <<<" + url + ">>>, verb: " + httpVerb + ", contentType: " + contentType + ", body <<<" + body + ">>>" );
if(headers != null && headers.length > 0)
for(int i = 0; i < headers.length; i++)
log.debug("header index " + i + " name: <<<" + headers[i].getName() + ">>>, value: <<<" + headers[i].getValue() + ">>>");
HttpUriRequest request = null; HttpUriRequest request = null;
String theContent = null; String theContent = null;
URI theURI = null; URI theURI = null;
boolean usingSSL = false;
ContentType parsedContentType = null; ContentType parsedContentType = null;
StringEntity requestBody = null; StringEntity requestBody = null;
log.debug("doHttpRequest with url <<<" + url + ">>>, verb: " + httpVerb + ", contentType: " + contentType
+ ", body <<<" + body + ">>>");
if (headers != null && headers.length > 0) {
for (int i = 0; i < headers.length; i++) {
log.debug("header index " + i + " name: <<<" + headers[i].getName() + ">>>, value: <<<"
+ headers[i].getValue() + ">>>");
}
}
if (contentType != null && !contentType.trim().isEmpty()) { if (contentType != null && !contentType.trim().isEmpty()) {
parsedContentType = ContentType.parse(contentType); parsedContentType = ContentType.parse(contentType);
if (body != null && body.length() > 0) if (body != null && body.length() > 0)
requestBody = new StringEntity(body, parsedContentType); requestBody = new StringEntity(body, parsedContentType);
} }
if (url.startsWith("https:")) {
usingSSL = true;
}
try { try {
theURI = new URI(url); theURI = new URI(url);
} catch (URISyntaxException e1) { } catch (URISyntaxException e1) {
@@ -90,7 +101,11 @@ public class HTTPHandler {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
for (int retryCount = 0; retryCount < 2; retryCount++) { for (int retryCount = 0; retryCount < 2; retryCount++) {
try { try {
response = HttpClientPool.getClient().execute(request); if (usingSSL) {
response = HttpClientPool.getSSLClient().execute(request);
} else {
response = HttpClientPool.getClient().execute(request);
}
log.debug((httpVerb == null ? "GET" : httpVerb) + " execute (" + retryCount + ") on URL responded: " log.debug((httpVerb == null ? "GET" : httpVerb) + " execute (" + retryCount + ") on URL responded: "
+ response.getStatusLine().getStatusCode()); + response.getStatusLine().getStatusCode());
if (response != null && response.getEntity() != null) { if (response != null && response.getEntity() != null) {
@@ -106,22 +121,27 @@ public class HTTPHandler {
// ignore // ignore
// content // content
} catch (Exception e) { } catch (Exception e) {
log.debug("Error ocurred in handling response entity after successful call, still responding success. " log.debug(
+ e.getMessage(), e); "Error ocurred in handling response entity after successful call, still responding success. "
+ e.getMessage(),
e);
} }
} }
if (response != null && response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) { if (response != null && response.getStatusLine().getStatusCode() >= 200
if(theContent == null) && response.getStatusLine().getStatusCode() < 300) {
if (theContent == null)
theContent = ""; theContent = "";
log.debug("Successfull response - The http response is <<<" + theContent + ">>>"); log.debug("Successfull response - The http response is <<<" + theContent + ">>>");
retryCount = 2; retryCount = 2;
} else if (DeviceMapTypes.FHEM_DEVICE[DeviceMapTypes.typeIndex].equals(callType) && response.getStatusLine().getStatusCode() == 302) { } else if (DeviceMapTypes.FHEM_DEVICE[DeviceMapTypes.typeIndex].equals(callType)
if(theContent == null) && response.getStatusLine().getStatusCode() == 302) {
if (theContent == null)
theContent = ""; theContent = "";
log.debug("Successfull response - The http response is <<<" + theContent + ">>>"); log.debug("Successfull response - The http response is <<<" + theContent + ">>>");
retryCount = 2; retryCount = 2;
} else if (response != null) { } else if (response != null) {
log.warn("HTTP response code was not an expected successful response of between 200 - 299, the code was: " log.warn(
"HTTP response code was not an expected successful response of between 200 - 299, the code was: "
+ response.getStatusLine() + " with the content of <<<" + theContent + ">>>"); + response.getStatusLine() + " with the content of <<<" + theContent + ">>>");
if (response.getStatusLine().getStatusCode() == 504) { if (response.getStatusLine().getStatusCode() == 504) {
log.warn("HTTP response code was 504, retrying..."); log.warn("HTTP response code was 504, retrying...");
@@ -133,12 +153,12 @@ public class HTTPHandler {
} catch (ClientProtocolException e) { } catch (ClientProtocolException e) {
log.warn("Client Protocol Exception received, retyring...."); log.warn("Client Protocol Exception received, retyring....");
}catch (IOException e) { } catch (IOException e) {
log.warn("Error calling out to HA gateway: IOException in log: " + e.getMessage()); log.warn("Error calling out to HA gateway: IOException in log: " + e.getMessage());
retryCount = 2; retryCount = 2;
} }
if(retryCount < 2) { if (retryCount < 2) {
theContent = null; theContent = null;
try { try {
Thread.sleep(1000); Thread.sleep(1000);
@@ -149,11 +169,11 @@ public class HTTPHandler {
} }
return theContent; return theContent;
} }
public void setCallType(String callType) { public void setCallType(String callType) {
this.callType = callType; this.callType = callType;
} }
public void closeHandler() { public void closeHandler() {
} }
} }

View File

@@ -4,10 +4,22 @@ import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
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.NoopHostnameVerifier;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -31,9 +43,52 @@ public final class HttpClientPool {
// Increase default max connection per route to 20 // Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20); cm.setDefaultMaxPerRoute(20);
// Build the client. // Build the client.
threadSafeClient = HttpClients.custom() threadSafeClient = HttpClients.custom().setConnectionManager(cm).build();
.setConnectionManager(cm) // Start up an eviction thread.
.build(); monitor = new IdleConnectionMonitorThread(cm);
// Don't stop quitting.
monitor.setDaemon(true);
monitor.start();
}
public CloseableHttpClient get() {
return threadSafeClient;
}
}
// Single-element enum to implement Singleton.
private static enum SingletonSSL {
// Just one of me so constructor will be called once.
SSLClient;
// The thread-safe client.
private final CloseableHttpClient threadSafeClient;
// The pool monitor.
private final IdleConnectionMonitorThread monitor;
private TrustStrategy acceptingTrustStrategy = null;
private SSLContext sslContext = null;
private SSLConnectionSocketFactory sslsf = null;
private Registry<ConnectionSocketFactory> socketFactoryRegistry = null;
private NoopHostnameVerifier hostnameVerifier = null;
// The constructor creates it - thus late
private SingletonSSL() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
try {
acceptingTrustStrategy = (cert, authType) -> true;
hostnameVerifier = new NoopHostnameVerifier();
sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
} catch (Exception e) {
HttpClientPool.log.warn("SingletonSSL failed on SSL init");
}
// Build the client.
threadSafeClient = HttpClients.custom().setConnectionManager(cm).setSSLSocketFactory(sslsf)
.setSSLHostnameVerifier(hostnameVerifier).build();
// Start up an eviction thread. // Start up an eviction thread.
monitor = new IdleConnectionMonitorThread(cm); monitor = new IdleConnectionMonitorThread(cm);
// Don't stop quitting. // Don't stop quitting.
@@ -52,6 +107,11 @@ public final class HttpClientPool {
return Singleton.Client.get(); return Singleton.Client.get();
} }
public static CloseableHttpClient getSSLClient() {
// The thread safe client is held by the singleton.
return SingletonSSL.SSLClient.get();
}
// Watches for stale connections and evicts them. // Watches for stale connections and evicts them.
private static class IdleConnectionMonitorThread extends Thread { private static class IdleConnectionMonitorThread extends Thread {
// The manager to watch. // The manager to watch.
@@ -123,6 +183,7 @@ public final class HttpClientPool {
public static void shutdown() throws InterruptedException, IOException { public static void shutdown() throws InterruptedException, IOException {
// Shutdown the monitor. // Shutdown the monitor.
Singleton.Client.monitor.shutdown(); Singleton.Client.monitor.shutdown();
SingletonSSL.SSLClient.monitor.shutdown();
} }
} }

View File

@@ -102,8 +102,10 @@ public class MozIotInstance {
headers[1].setName("Accept"); headers[1].setName("Accept");
headers[1].setValue("application/json"); headers[1].setValue("application/json");
aUrl = aUrl + mozIotIP.getIp() + ":" + mozIotIP.getPort() + "/login"; aUrl = aUrl + mozIotIP.getIp() + ":" + mozIotIP.getPort() + "/login";
log.info("gateway login URL: " + aUrl);
String commandData = "{\"email\": \"" + mozIotIP.getUsername() + "\", \"password\":\"" + mozIotIP.getPassword() String commandData = "{\"email\": \"" + mozIotIP.getUsername() + "\", \"password\":\"" + mozIotIP.getPassword()
+ "\"}"; + "\"}";
log.info("The login body: " + commandData);
String theData = httpClient.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", commandData, headers); String theData = httpClient.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", commandData, headers);
if (theData != null) { if (theData != null) {
log.info("GET Mozilla login - data: " + theData); log.info("GET Mozilla login - data: " + theData);