From bb65650e534039fa39d22f08844d0174e8a37b9b Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 29 Sep 2016 16:21:15 -0500 Subject: [PATCH] Added ip check if upnp config address is not available on the current host. Added checks for content type and body for put and post requests. --- pom.xml | 2 +- .../bwssystems/HABridge/BridgeSettings.java | 76 ++++++++++++------- .../bwssystems/HABridge/hue/HueMulator.java | 30 +++++--- 3 files changed, 66 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index 3674447..8aee4b9 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 3.1.0d + 3.1.0e jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java index 3e6715c..67d5b8d 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java @@ -39,7 +39,6 @@ public class BridgeSettings extends BackupHandler { return theBridgeSettings; } public void buildSettings() { - InetAddress address = null; String addressString = null; String theVeraAddress = null; String theHarmonyAddress = null; @@ -109,34 +108,18 @@ public class BridgeSettings extends BackupHandler { } if(theBridgeSettings.getUpnpConfigAddress() == null || theBridgeSettings.getUpnpConfigAddress().equals("")) { - try { - log.info("Getting an IP address for this host...."); - Enumeration ifs = NetworkInterface.getNetworkInterfaces(); - - while (ifs.hasMoreElements() && addressString == null) { - NetworkInterface xface = ifs.nextElement(); - Enumeration addrs = xface.getInetAddresses(); - String name = xface.getName(); - int IPsPerNic = 0; - - while (addrs.hasMoreElements() && IPsPerNic == 0) { - address = addrs.nextElement(); - if (InetAddressUtils.isIPv4Address(address.getHostAddress())) { - log.debug(name + " ... has IPV4 addr " + address); - if(!name.equalsIgnoreCase(Configuration.LOOP_BACK_INTERFACE)|| !address.getHostAddress().equalsIgnoreCase(Configuration.LOOP_BACK_ADDRESS)) { - IPsPerNic++; - addressString = address.getHostAddress(); - log.info("Adding " + addressString + " from interface " + name + " as our default upnp config address."); - } - } - } - } - } catch (SocketException e) { - log.error("Cannot get ip address of this host, Exiting with message: " + e.getMessage(), e); - return; - } - - theBridgeSettings.setUpnpConfigAddress(addressString); + addressString = checkIpAddress(null, true); + if(addressString != null) { + theBridgeSettings.setUpnpConfigAddress(addressString); + log.info("Adding " + addressString + " as our default upnp config address."); + } + else + log.error("Cannot get ip address of this host."); + } + else { + addressString = checkIpAddress(theBridgeSettings.getUpnpConfigAddress(), false); + if(addressString == null) + log.warn("The upnp config address, " + theBridgeSettings.getUpnpConfigAddress() + ", does not match any known IP's on this host."); } if(theBridgeSettings.getUpnpResponsePort() == null) @@ -258,4 +241,39 @@ public class BridgeSettings extends BackupHandler { return content; } + + private String checkIpAddress(String ipAddress, boolean checkForLocalhost) { + Enumeration ifs = null; + try { + ifs = NetworkInterface.getNetworkInterfaces(); + } catch(SocketException e) { + log.error("checkIpAddress cannot get ip address of this host, Exiting with message: " + e.getMessage(), e); + return null; + } + String addressString = null; + InetAddress address = null; + while (ifs.hasMoreElements() && addressString == null) { + NetworkInterface xface = ifs.nextElement(); + Enumeration addrs = xface.getInetAddresses(); + String name = xface.getName(); + int IPsPerNic = 0; + + while (addrs.hasMoreElements() && IPsPerNic == 0) { + address = addrs.nextElement(); + if (InetAddressUtils.isIPv4Address(address.getHostAddress())) { + log.debug(name + " ... has IPV4 addr " + address); + if(checkForLocalhost && (!name.equalsIgnoreCase(Configuration.LOOP_BACK_INTERFACE) || !address.getHostAddress().equalsIgnoreCase(Configuration.LOOP_BACK_ADDRESS))) { + IPsPerNic++; + addressString = address.getHostAddress(); + log.debug("checkIpAddress found " + addressString + " from interface " + name); + } + else if(ipAddress != null && ipAddress.equalsIgnoreCase(address.getHostAddress())){ + addressString = ipAddress; + IPsPerNic++; + } + } + } + } + return addressString; + } } diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index 34d5ad1..f665bdb 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -1043,33 +1043,39 @@ public class HueMulator implements HueErrorStringSet { protected String doHttpRequest(String url, String httpVerb, String contentType, String body, NameValue[] headers) { HttpUriRequest request = null; String theContent = null; + ContentType parsedContentType = null; + StringEntity requestBody = null; + if(contentType != null && contentType.length() > 0) { + parsedContentType = ContentType.parse(contentType); + if(body != null && body.length() > 0) + requestBody = new StringEntity(body, parsedContentType); + } + try { if(HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb) || httpVerb == null) { request = new HttpGet(url); }else if(HttpPost.METHOD_NAME.equalsIgnoreCase(httpVerb)){ HttpPost postRequest = new HttpPost(url); - ContentType parsedContentType = ContentType.parse(contentType); - StringEntity requestBody = new StringEntity(body, parsedContentType); - postRequest.setEntity(requestBody); + if(requestBody != null) + postRequest.setEntity(requestBody); request = postRequest; }else if(HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)){ HttpPut putRequest = new HttpPut(url); - ContentType parsedContentType = ContentType.parse(contentType); - StringEntity requestBody = new StringEntity(body, parsedContentType); - putRequest.setEntity(requestBody); + if(requestBody != null) + putRequest.setEntity(requestBody); request = putRequest; } } catch(IllegalArgumentException e) { - log.warn("Error calling out to HA gateway: IllegalArgumentException in log", e); + log.warn("Error creating outbound http request: IllegalArgumentException in log", e); return null; } log.debug("Making outbound call in doHttpRequest: " + request); + if(headers != null && headers.length > 0) { + for(int i = 0; i < headers.length; i++) { + request.setHeader(headers[i].getName(), headers[i].getValue()); + } + } try { - if(headers != null && headers.length > 0) { - for(int i = 0; i < headers.length; i++) { - request.setHeader(headers[i].getName(), headers[i].getValue()); - } - } HttpResponse response; if(url.startsWith("https")) response = httpclientSSL.execute(request);