Search in sources :

Example 21 with ContentType

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();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) SuccessUserResponse(com.bwssystems.HABridge.api.SuccessUserResponse) ContentType(org.apache.http.entity.ContentType) HttpClient(org.apache.http.client.HttpClient) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UserCreateRequest(com.bwssystems.HABridge.api.UserCreateRequest)

Example 22 with ContentType

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) SuccessUserResponse(com.bwssystems.HABridge.api.SuccessUserResponse) ContentType(org.apache.http.entity.ContentType) HttpClient(org.apache.http.client.HttpClient) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UserCreateRequest(com.bwssystems.HABridge.api.UserCreateRequest)

Example 23 with ContentType

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();
    }
}
Also used : ContentType(org.apache.http.entity.ContentType) InputStreamReader(java.io.InputStreamReader) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Aggregations

ContentType (org.apache.http.entity.ContentType)23 IOException (java.io.IOException)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpEntity (org.apache.http.HttpEntity)8 Charset (java.nio.charset.Charset)6 HttpResponse (org.apache.http.HttpResponse)5 HttpPost (org.apache.http.client.methods.HttpPost)5 InputStreamReader (java.io.InputStreamReader)4 HttpGet (org.apache.http.client.methods.HttpGet)4 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)4 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Header (org.apache.http.Header)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 XContentType (org.elasticsearch.common.xcontent.XContentType)3 SuccessUserResponse (com.bwssystems.HABridge.api.SuccessUserResponse)2 UserCreateRequest (com.bwssystems.HABridge.api.UserCreateRequest)2