From 5076ce165483b9d592a3986b46d79a21c28ff81c Mon Sep 17 00:00:00 2001 From: arm Date: Wed, 29 Apr 2015 20:39:57 -0500 Subject: [PATCH] fixes #2 when Vera responds back with bad content type, BAD VERA! --- pom.xml | 5 ++++ .../java/com/armzilla/ha/hue/HueMulator.java | 30 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index d614f24..2344833 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,11 @@ spring-boot-starter-test test + + org.apache.httpcomponents + httpclient + 4.3.6 + diff --git a/src/main/java/com/armzilla/ha/hue/HueMulator.java b/src/main/java/com/armzilla/ha/hue/HueMulator.java index 562ec1a..e6d1dfd 100644 --- a/src/main/java/com/armzilla/ha/hue/HueMulator.java +++ b/src/main/java/com/armzilla/ha/hue/HueMulator.java @@ -3,6 +3,11 @@ import com.armzilla.ha.api.hue.DeviceResponse; import com.armzilla.ha.api.hue.HueApiResponse; import com.armzilla.ha.dao.*; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; @@ -16,6 +21,7 @@ import org.springframework.web.client.RestTemplate; import javax.servlet.http.HttpServletRequest; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -30,6 +36,10 @@ public class HueMulator { protected static RestTemplate restTemplate = new RestTemplate(); @Autowired private DeviceRepository repository; + private HttpClient httpClient; + public HueMulator(){ + httpClient = HttpClients.createDefault(); //patched for now, moving away from HueMulator doing work + } @RequestMapping(value = "/{userId}/lights", method = RequestMethod.GET, produces = "application/json") @@ -120,8 +130,10 @@ public ResponseEntity stateChange(@PathVariable(value = "lightId") Strin } else { url = device.getOffUrl(); } - - String response = restTemplate.getForObject(url, String.class); + //make call + if(!doHttpGETRequest(url)){ + return new ResponseEntity<>(null, null, HttpStatus.SERVICE_UNAVAILABLE); + } HttpHeaders headerMap = new HttpHeaders(); headerMap.set("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); @@ -140,4 +152,18 @@ public ResponseEntity stateChange(@PathVariable(value = "lightId") Strin ResponseEntity entity = new ResponseEntity<>(setting, headerMap, HttpStatus.OK); return entity; } + + protected boolean doHttpGETRequest(String url){ + HttpGet httpGet = new HttpGet(url); + try { + HttpResponse response = httpClient.execute(httpGet); + if(response.getStatusLine().getStatusCode() == 200){ + EntityUtils.consume(response.getEntity()); //close out inputstream + return true; + } + } catch (IOException e) { + log.error("Error calling out to HA gateway", e); + } + return false; + } }