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.
This commit is contained in:
Admin
2016-09-29 16:21:15 -05:00
parent 7f7e96465b
commit bb65650e53
3 changed files with 66 additions and 42 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId> <groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId> <artifactId>ha-bridge</artifactId>
<version>3.1.0d</version> <version>3.1.0e</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>HA Bridge</name> <name>HA Bridge</name>

View File

@@ -39,7 +39,6 @@ public class BridgeSettings extends BackupHandler {
return theBridgeSettings; return theBridgeSettings;
} }
public void buildSettings() { public void buildSettings() {
InetAddress address = null;
String addressString = null; String addressString = null;
String theVeraAddress = null; String theVeraAddress = null;
String theHarmonyAddress = null; String theHarmonyAddress = null;
@@ -109,34 +108,18 @@ public class BridgeSettings extends BackupHandler {
} }
if(theBridgeSettings.getUpnpConfigAddress() == null || theBridgeSettings.getUpnpConfigAddress().equals("")) { if(theBridgeSettings.getUpnpConfigAddress() == null || theBridgeSettings.getUpnpConfigAddress().equals("")) {
try { addressString = checkIpAddress(null, true);
log.info("Getting an IP address for this host...."); if(addressString != null) {
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
while (ifs.hasMoreElements() && addressString == null) {
NetworkInterface xface = ifs.nextElement();
Enumeration<InetAddress> 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); 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) if(theBridgeSettings.getUpnpResponsePort() == null)
@@ -258,4 +241,39 @@ public class BridgeSettings extends BackupHandler {
return content; return content;
} }
private String checkIpAddress(String ipAddress, boolean checkForLocalhost) {
Enumeration<NetworkInterface> 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<InetAddress> 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;
}
} }

View File

@@ -1043,33 +1043,39 @@ public class HueMulator implements HueErrorStringSet {
protected String doHttpRequest(String url, String httpVerb, String contentType, String body, NameValue[] headers) { protected String doHttpRequest(String url, String httpVerb, String contentType, String body, NameValue[] headers) {
HttpUriRequest request = null; HttpUriRequest request = null;
String theContent = 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 { try {
if(HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb) || httpVerb == null) { if(HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb) || httpVerb == null) {
request = new HttpGet(url); request = new HttpGet(url);
}else if(HttpPost.METHOD_NAME.equalsIgnoreCase(httpVerb)){ }else if(HttpPost.METHOD_NAME.equalsIgnoreCase(httpVerb)){
HttpPost postRequest = new HttpPost(url); HttpPost postRequest = new HttpPost(url);
ContentType parsedContentType = ContentType.parse(contentType); if(requestBody != null)
StringEntity requestBody = new StringEntity(body, parsedContentType);
postRequest.setEntity(requestBody); postRequest.setEntity(requestBody);
request = postRequest; request = postRequest;
}else if(HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)){ }else if(HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)){
HttpPut putRequest = new HttpPut(url); HttpPut putRequest = new HttpPut(url);
ContentType parsedContentType = ContentType.parse(contentType); if(requestBody != null)
StringEntity requestBody = new StringEntity(body, parsedContentType);
putRequest.setEntity(requestBody); putRequest.setEntity(requestBody);
request = putRequest; request = putRequest;
} }
} catch(IllegalArgumentException e) { } 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; return null;
} }
log.debug("Making outbound call in doHttpRequest: " + request); log.debug("Making outbound call in doHttpRequest: " + request);
try {
if(headers != null && headers.length > 0) { if(headers != null && headers.length > 0) {
for(int i = 0; i < headers.length; i++) { for(int i = 0; i < headers.length; i++) {
request.setHeader(headers[i].getName(), headers[i].getValue()); request.setHeader(headers[i].getName(), headers[i].getValue());
} }
} }
try {
HttpResponse response; HttpResponse response;
if(url.startsWith("https")) if(url.startsWith("https"))
response = httpclientSSL.execute(request); response = httpclientSSL.execute(request);