use of org.apache.http.entity.ContentType in project ha-bridge by bwssytems.
the class HueInfo method registerWithHue.
public String registerWithHue() {
UserCreateRequest theLogin = new UserCreateRequest();
theLogin.setDevicetype("HABridge#MyMachine");
HttpPost postRequest = new HttpPost("http://" + hueAddress.getIp() + HUE_REQUEST);
ContentType parsedContentType = ContentType.parse("application/json");
StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
HttpResponse response = null;
postRequest.setEntity(requestBody);
HttpClient anHttpClient = httpClient.getHttpClient();
try {
response = anHttpClient.execute(postRequest);
log.debug("registerWithHue - POST execute on " + hueAddress.getName() + "URL responded: " + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
String theBody = EntityUtils.toString(response.getEntity());
log.debug("registerWithHue response data: " + theBody);
if (theBody.contains("[{\"error\":")) {
if (theBody.contains("link button not")) {
log.warn("registerWithHue needs link button pressed on HUE bridge: " + hueAddress.getName());
} else
log.warn("registerWithHue returned an unexpected error: " + theBody);
} else {
//read content for data, SuccessUserResponse[].class);
SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class);
hueAddress.setUsername(theResponses[0].getSuccess().getUsername());
}
}
//close out inputstream ignore content
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
log.warn("Error logging into HUE: IOException in log", e);
}
return hueAddress.getUsername();
}
use of org.apache.http.entity.ContentType in project ha-bridge by bwssytems.
the class HueUtil method registerWithHue.
public static final String registerWithHue(HTTPHandler anHttpHandler, String ipAddress, String aName, String theUser) {
UserCreateRequest theLogin = new UserCreateRequest();
theLogin.setDevicetype("HABridge#MyMachine");
HttpPost postRequest = new HttpPost("http://" + ipAddress + HUE_REQUEST);
ContentType parsedContentType = ContentType.parse("application/json");
StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
HttpResponse response = null;
postRequest.setEntity(requestBody);
HttpClient anHttpClient = anHttpHandler.getHttpClient();
try {
response = anHttpClient.execute(postRequest);
log.debug("POST execute on URL responded: " + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
String theBody = EntityUtils.toString(response.getEntity());
log.debug("registerWithHue response data: " + theBody);
if (theBody.contains("[{\"error\":")) {
if (theBody.contains("link button not")) {
log.warn("registerWithHue needs link button pressed on HUE bridge: " + aName);
} else
log.warn("registerWithHue returned an unexpected error: " + theBody);
} else {
//read content for data, SuccessUserResponse[].class);
SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class);
theUser = theResponses[0].getSuccess().getUsername();
}
}
//close out inputstream ignore content
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
log.warn("Error logging into HUE: IOException in log", e);
}
return theUser;
}
use of org.apache.http.entity.ContentType in project cdap-ingest by caskdata.
the class RestClient method toString.
/**
* Utility method for converting {@link org.apache.http.HttpEntity} HTTP entity content to String.
*
* @param httpEntity {@link org.apache.http.HttpEntity}
* @return {@link String} generated from input content stream
* @throws IOException if HTTP entity is not available
*/
public static String toString(HttpEntity httpEntity) throws IOException {
if (httpEntity == null || httpEntity.getContent() == null) {
throw new IOException("Empty HttpEntity is received.");
}
Charset charset = Charsets.UTF_8;
ContentType contentType = ContentType.get(httpEntity);
if (contentType != null && contentType.getCharset() != null) {
charset = contentType.getCharset();
}
Reader reader = new InputStreamReader(httpEntity.getContent(), charset);
try {
return CharStreams.toString(reader);
} finally {
reader.close();
}
}
Aggregations