Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2013 George Reese Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.imaginary.home.device.hue;
 
 import  org.json.JSONArray;
 import  org.json.JSONException;
 import  org.json.JSONObject;
 
 import  javax.annotation.Nonnull;
 import java.util.Date;
 
 public class HueMethod {
     private Hue    hue;
 
     @SuppressWarnings("UnusedDeclaration")
     private HueMethod() { }
 
     public HueMethod(Hue hue) {
         this. = hue;
     }
 
     public void delete(@Nonnull String resourcethrows HueException {
         Logger std = Hue.getLogger(HueMethod.class);
         Logger wire = Hue.getWireLogger(HueMethod.class);
 
         ifstd.isTraceEnabled() ) {
             std.trace("enter - " + HueMethod.class.getName() + ".delete(" +resource + ")");
         }
         ifwire.isDebugEnabled() ) {
             wire.debug("");
             wire.debug(">>> [DELETE (" + (new Date()) + ")] -> " + .getAPIEndpoint() + resource);
         }
         try {
             HttpClient client = getClient();
             HttpDelete method = new HttpDelete(.getAPIEndpoint() + resource);
 
             method.addHeader("Content-Type""application/json");
             ifwire.isDebugEnabled() ) {
                 wire.debug(method.getRequestLine().toString());
                 forHeader header : method.getAllHeaders() ) {
                     wire.debug(header.getName() + ": " + header.getValue());
                 }
                 wire.debug("");
             }
             HttpResponse response;
             StatusLine status;
 
             try {
                 response = client.execute(method);
                 status = response.getStatusLine();
             }
             catchIOException e ) {
                 std.error("DELETE: Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
                 ifstd.isTraceEnabled() ) {
                     e.printStackTrace();
                 }
                 throw new HueException(e);
             }
             ifstd.isDebugEnabled() ) {
                 std.debug("DELETE: HTTP Status " + status);
             }
             Header[] headers = response.getAllHeaders();
            ifwire.isDebugEnabled() ) {
                wire.debug(status.toString());
                forHeader h : headers ) {
                    ifh.getValue() != null ) {
                        wire.debug(h.getName() + ": " + h.getValue().trim());
                    }
                    else {
                        wire.debug(h.getName() + ":");
                    }
                }
                wire.debug("");
            }
            ifstatus.getStatusCode() != . && status.getStatusCode() != . && status.getStatusCode() != . ) {
                std.error("DELETE: Expected OK or NO_CONTENT or ACCEPTED for DELETE request, got " + status.getStatusCode());
                HttpEntity entity = response.getEntity();
                ifentity == null ) {
                    throw new HueException(status.getStatusCode(), "An error was returned without explanation");
                }
                String body;
                try {
                    body = EntityUtils.toString(entity);
                }
                catchIOException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
                ifwire.isDebugEnabled() ) {
                    wire.debug(body);
                }
                wire.debug("");
                throw new HueException(status.getStatusCode(), body);
            }
        }
        finally {
            ifstd.isTraceEnabled() ) {
                std.trace("exit - " + HueMethod.class.getName() + ".delete()");
            }
            ifwire.isDebugEnabled() ) {
                wire.debug("<<< [DELETE (" + (new Date()) + ")] -> " + .getAPIEndpoint() + resource + " <--------------------------------------------------------------------------------------");
                wire.debug("");
            }
        }
    }
    public JSONObject get(@Nonnull String resourcethrows HueException {
        Logger std = Hue.getLogger(HueMethod.class);
        Logger wire = Hue.getWireLogger(HueMethod.class);
        ifstd.isTraceEnabled() ) {
            std.trace("enter - " + HueMethod.class.getName() + ".get(" +resource + ")");
        }
        ifwire.isDebugEnabled() ) {
            wire.debug("");
            wire.debug(">>> [GET (" + (new Date()) + ")] -> " + .getAPIEndpoint() + resource);
        }
        try {
            HttpClient client = getClient();
            HttpGet method = new HttpGet(.getAPIEndpoint() + resource);
            method.addHeader("Content-Type""application/json");
            ifwire.isDebugEnabled() ) {
                wire.debug(method.getRequestLine().toString());
                forHeader header : method.getAllHeaders() ) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
            }
            HttpResponse response;
            StatusLine status;
            try {
                response = client.execute(method);
                status = response.getStatusLine();
            }
            catchIOException e ) {
                std.error("GET: Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
                ifstd.isTraceEnabled() ) {
                    e.printStackTrace();
                }
                throw new HueException(e);
            }
            ifstd.isDebugEnabled() ) {
                std.debug("GET: HTTP Status " + status);
            }
            Header[] headers = response.getAllHeaders();
            ifwire.isDebugEnabled() ) {
                wire.debug(status.toString());
                forHeader h : headers ) {
                    ifh.getValue() != null ) {
                        wire.debug(h.getName() + ": " + h.getValue().trim());
                    }
                    else {
                        wire.debug(h.getName() + ":");
                    }
                }
                wire.debug("");
            }
            ifstatus.getStatusCode() == . ) {
                return null;
            }
            else ifstatus.getStatusCode() != . ) {
                std.error("GET: Expected OK or NOT_FOUND for GET request, got " + status.getStatusCode());
                HttpEntity entity = response.getEntity();
                ifentity == null ) {
                    throw new HueException(status.getStatusCode(), "An error was returned without explanation");
                }
                String json;
                try {
                    json = EntityUtils.toString(entity);
                }
                catchIOException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
                ifwire.isDebugEnabled() ) {
                    wire.debug(json);
                    wire.debug("");
                }
                throw new HueException(status.getStatusCode(), json);
            }
            else {
                try {
                    String json = EntityUtils.toString(response.getEntity());
                    ifwire.isDebugEnabled() ) {
                        wire.debug(json);
                        wire.debug("");
                    }
                    return new JSONObject(json);
                }
                catchIOException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
                catch( JSONException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
            }
        }
        finally {
            ifstd.isTraceEnabled() ) {
                std.trace("exit - " + HueMethod.class.getName() + ".get()");
            }
            ifwire.isDebugEnabled() ) {
                wire.debug("<<< [GET (" + (new Date()) + ")] -> " + .getAPIEndpoint() + resource + " <--------------------------------------------------------------------------------------");
                wire.debug("");
            }
        }
    }
    protected @Nonnull HttpClient getClient() {
        boolean ssl = .getAPIEndpoint().startsWith("https");
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params.);
        //noinspection deprecation
        HttpProtocolParams.setContentCharset(params.);
        HttpProtocolParams.setUserAgent(params"Imaginary Home");
        Properties p = .getCustomProperties();
        String proxyHost = p.getProperty("proxyHost");
        String proxyPort = p.getProperty("proxyPort");
        ifproxyHost != null ) {
            int port = 0;
            ifproxyPort != null && proxyPort.length() > 0 ) {
                port = Integer.parseInt(proxyPort);
            }
            params.setParameter(.new HttpHost(proxyHostportssl ? "https" : "http"));
        }
        return new DefaultHttpClient(params);
    }
    public JSONObject post(@Nonnull String resource, JSONObject bodythrows HueException {
        Logger std = Hue.getLogger(HueMethod.class);
        Logger wire = Hue.getWireLogger(HueMethod.class);
        ifstd.isTraceEnabled() ) {
            std.trace("enter - " + HueMethod.class.getName() + ".post(" +resource + ")");
        }
        ifwire.isDebugEnabled() ) {
            wire.debug("");
            wire.debug(">>> [POST (" + (new Date()) + ")] -> " + .getAPIEndpoint() + resource);
        }
        try {
            HttpClient client = getClient();
            HttpPost method = new HttpPost(.getAPIEndpoint() + resource);
            method.addHeader("Content-Type""application/json");
            try {
                ifbody != null ) {
                    //noinspection deprecation
                    method.setEntity(new StringEntity(body.toString(), "application/json""UTF-8"));
                }
            }
            catchUnsupportedEncodingException e ) {
                throw new HueException(e);
            }
            ifwire.isDebugEnabled() ) {
                wire.debug(method.getRequestLine().toString());
                forHeader header : method.getAllHeaders() ) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
                ifbody != null ) {
                    try { wire.debug(EntityUtils.toString(method.getEntity())); }
                    catchIOException ignore ) { }
                    wire.debug("");
                }
            }
            HttpResponse response;
            StatusLine status;
            try {
                response = client.execute(method);
                status = response.getStatusLine();
            }
            catchIOException e ) {
                std.error("POST: Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
                ifstd.isTraceEnabled() ) {
                    e.printStackTrace();
                }
                throw new HueException(e);
            }
            ifstd.isDebugEnabled() ) {
                std.debug("POST: HTTP Status " + status);
            }
            Header[] headers = response.getAllHeaders();
            ifwire.isDebugEnabled() ) {
                wire.debug(status.toString());
                forHeader h : headers ) {
                    ifh.getValue() != null ) {
                        wire.debug(h.getName() + ": " + h.getValue().trim());
                    }
                    else {
                        wire.debug(h.getName() + ":");
                    }
                }
                wire.debug("");
            }
            ifstatus.getStatusCode() != . && status.getStatusCode() != . && status.getStatusCode() != . ) {
                std.error("POST: Expected CREATED or OK or ACCEPTED for POST request, got " + status.getStatusCode());
                HttpEntity entity = response.getEntity();
                ifentity == null ) {
                    throw new HueException(status.getStatusCode(), "An error was returned without explanation");
                }
                String json;
                try {
                    json = EntityUtils.toString(entity);
                }
                catchIOException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
                ifwire.isDebugEnabled() ) {
                    wire.debug(json);
                    wire.debug("");
                }
                throw new HueException(status.getStatusCode(), json);
            }
            else {
                try {
                    String json = EntityUtils.toString(response.getEntity());
                    ifwire.isDebugEnabled() ) {
                        wire.debug(json);
                        wire.debug("");
                    }
                    ifjson.startsWith("[") ) {
                        JSONArray arr = new JSONArray(json);
                        ifarr.length() > 0 ) {
                            JSONObject ob = arr.getJSONObject(0);
                            ifob.has("error") ) {
                                ob = ob.getJSONObject("error");
                                ifob.has("description") ) {
                                    throw new HueException(ob.getString("description"));
                                }
                            }
                            return ob;
                        }
                        return null;
                    }
                    return new JSONObject(json);
                }
                catchIOException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
                catch( JSONException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
            }
        }
        finally {
            ifstd.isTraceEnabled() ) {
                std.trace("exit - " + HueMethod.class.getName() + ".post()");
            }
            ifwire.isDebugEnabled() ) {
                wire.debug("<<< [POST (" + (new Date()) + ")] -> " + .getAPIEndpoint() + resource + " <--------------------------------------------------------------------------------------");
                wire.debug("");
            }
        }
    }
    public JSONObject put(@Nonnull String resource, JSONObject bodythrows HueException {
        Logger std = Hue.getLogger(HueMethod.class);
        Logger wire = Hue.getWireLogger(HueMethod.class);
        ifstd.isTraceEnabled() ) {
            std.trace("enter - " + HueMethod.class.getName() + ".put(" +resource + ")");
        }
        ifwire.isDebugEnabled() ) {
            wire.debug("");
            wire.debug(">>> [PUT (" + (new Date()) + ")] -> " + .getAPIEndpoint() + resource);
        }
        try {
            HttpClient client = getClient();
            HttpPut method = new HttpPut(.getAPIEndpoint() + resource);
            method.addHeader("Content-Type""application/json");
            try {
                ifbody != null ) {
                    //noinspection deprecation
                    method.setEntity(new StringEntity(body.toString(), "application/json""UTF-8"));
                }
            }
            catchUnsupportedEncodingException e ) {
                throw new HueException(e);
            }
            ifwire.isDebugEnabled() ) {
                wire.debug(method.getRequestLine().toString());
                forHeader header : method.getAllHeaders() ) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
                ifbody != null ) {
                    try { wire.debug(EntityUtils.toString(method.getEntity())); }
                    catchIOException ignore ) { }
                    wire.debug("");
                }
            }
            HttpResponse response;
            StatusLine status;
            try {
                response = client.execute(method);
                status = response.getStatusLine();
            }
            catchIOException e ) {
                std.error("PUT: Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
                ifstd.isTraceEnabled() ) {
                    e.printStackTrace();
                }
                throw new HueException(e);
            }
            ifstd.isDebugEnabled() ) {
                std.debug("PUT: HTTP Status " + status);
            }
            Header[] headers = response.getAllHeaders();
            ifwire.isDebugEnabled() ) {
                wire.debug(status.toString());
                forHeader h : headers ) {
                    ifh.getValue() != null ) {
                        wire.debug(h.getName() + ": " + h.getValue().trim());
                    }
                    else {
                        wire.debug(h.getName() + ":");
                    }
                }
                wire.debug("");
            }
            ifstatus.getStatusCode() == . ) {
                return null;
            }
            else ifstatus.getStatusCode() != . && status.getStatusCode() != . && status.getStatusCode() != . ) {
                std.error("PUT: Expected NO_CONTENT or CREATED or OK or ACCEPTED for PUT request, got " + status.getStatusCode());
                HttpEntity entity = response.getEntity();
                ifentity == null ) {
                    throw new HueException(status.getStatusCode(), "An error was returned without explanation");
                }
                String json;
                try {
                    json = EntityUtils.toString(entity);
                }
                catchIOException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
                ifwire.isDebugEnabled() ) {
                    wire.debug(json);
                    wire.debug("");
                }
                throw new HueException(status.getStatusCode(), json);
            }
            else {
                try {
                    String json = EntityUtils.toString(response.getEntity());
                    ifwire.isDebugEnabled() ) {
                        wire.debug(json);
                        wire.debug("");
                    }
                    ifjson.startsWith("[") ) {
                        JSONArray arr = new JSONArray(json);
                        ifarr.length() > 0 ) {
                            JSONObject ob = arr.getJSONObject(0);
                            ifob.has("error") ) {
                                ob = ob.getJSONObject("error");
                                ifob.has("description") ) {
                                    throw new HueException(ob.getString("description"));
                                }
                            }
                            return ob;
                        }
                        return null;
                    }
                    return new JSONObject(json);
                }
                catchIOException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
                catch( JSONException e ) {
                    throw new HueException(status.getStatusCode(), e.getMessage());
                }
            }
        }
        finally {
            ifstd.isTraceEnabled() ) {
                std.trace("exit - " + HueMethod.class.getName() + ".put()");
            }
            ifwire.isDebugEnabled() ) {
                wire.debug("<<< [PUT (" + (new Date()) + ")] -> " + .getAPIEndpoint() + resource + " <--------------------------------------------------------------------------------------");
                wire.debug("");
            }
        }
    }
New to GrepCode? Check out our FAQ X