Example usage for org.apache.http.client.methods HttpPut getEntity

List of usage examples for org.apache.http.client.methods HttpPut getEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPut getEntity.

Prototype

public HttpEntity getEntity() 

Source Link

Usage

From source file:com.imaginary.home.device.hue.HueMethod.java

public JSONObject put(@Nonnull String resource, JSONObject body) throws HueException {
    Logger std = Hue.getLogger(HueMethod.class);
    Logger wire = Hue.getWireLogger(HueMethod.class);

    if (std.isTraceEnabled()) {
        std.trace("enter - " + HueMethod.class.getName() + ".put(" + resource + ")");
    }/*w  ww.  j a  va 2 s.  c  o  m*/
    if (wire.isDebugEnabled()) {
        wire.debug("");
        wire.debug(">>> [PUT (" + (new Date()) + ")] -> " + hue.getAPIEndpoint() + resource);
    }
    try {
        HttpClient client = getClient();
        HttpPut method = new HttpPut(hue.getAPIEndpoint() + resource);

        method.addHeader("Content-Type", "application/json");

        try {
            if (body != null) {
                //noinspection deprecation
                method.setEntity(new StringEntity(body.toString(), "application/json", "UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {
            throw new HueException(e);
        }
        if (wire.isDebugEnabled()) {
            wire.debug(method.getRequestLine().toString());
            for (Header header : method.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                try {
                    wire.debug(EntityUtils.toString(method.getEntity()));
                } catch (IOException ignore) {
                }

                wire.debug("");
            }
        }
        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(method);
            status = response.getStatusLine();
        } catch (IOException e) {
            std.error("PUT: Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (std.isTraceEnabled()) {
                e.printStackTrace();
            }
            throw new HueException(e);
        }
        if (std.isDebugEnabled()) {
            std.debug("PUT: HTTP Status " + status);
        }
        Header[] headers = response.getAllHeaders();

        if (wire.isDebugEnabled()) {
            wire.debug(status.toString());
            for (Header h : headers) {
                if (h.getValue() != null) {
                    wire.debug(h.getName() + ": " + h.getValue().trim());
                } else {
                    wire.debug(h.getName() + ":");
                }
            }
            wire.debug("");
        }
        if (status.getStatusCode() == HttpServletResponse.SC_NO_CONTENT) {
            return null;
        } else if (status.getStatusCode() != HttpServletResponse.SC_OK
                && status.getStatusCode() != HttpServletResponse.SC_CREATED
                && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED) {
            std.error("PUT: Expected NO_CONTENT or CREATED or OK or ACCEPTED for PUT request, got "
                    + status.getStatusCode());

            HttpEntity entity = response.getEntity();

            if (entity == null) {
                throw new HueException(status.getStatusCode(), "An error was returned without explanation");
            }
            String json;

            try {
                json = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new HueException(status.getStatusCode(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(json);
                wire.debug("");
            }
            throw new HueException(status.getStatusCode(), json);
        } else {
            try {
                String json = EntityUtils.toString(response.getEntity());

                if (wire.isDebugEnabled()) {
                    wire.debug(json);
                    wire.debug("");
                }
                if (json.startsWith("[")) {
                    JSONArray arr = new JSONArray(json);

                    if (arr.length() > 0) {
                        JSONObject ob = arr.getJSONObject(0);

                        if (ob.has("error")) {
                            ob = ob.getJSONObject("error");
                            if (ob.has("description")) {
                                throw new HueException(ob.getString("description"));
                            }
                        }
                        return ob;
                    }
                    return null;
                }
                return new JSONObject(json);
            } catch (IOException e) {
                throw new HueException(status.getStatusCode(), e.getMessage());
            } catch (JSONException e) {
                throw new HueException(status.getStatusCode(), e.getMessage());
            }
        }
    } finally {
        if (std.isTraceEnabled()) {
            std.trace("exit - " + HueMethod.class.getName() + ".put()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("<<< [PUT (" + (new Date()) + ")] -> " + hue.getAPIEndpoint() + resource
                    + " <--------------------------------------------------------------------------------------");
            wire.debug("");
        }
    }
}

From source file:org.dasein.cloud.nimbula.NimbulaMethod.java

@SuppressWarnings("unused")
public @Nonnegative int put(@Nonnull String targetId, @Nonnull Map<String, Object> state)
        throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("ENTER - " + NimbulaMethod.class.getName() + ".put(" + targetId + "," + state + ")");
    }/*from  ww w.j  a  v a2s  . c o m*/
    try {
        authenticate();
        String target = getUrl(url, targetId);

        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug(">>> [PUT (" + (new Date()) + ")] -> " + target
                    + " >--------------------------------------------------------------------------------------");
        }
        try {
            ProviderContext ctx = cloud.getContext();

            if (ctx == null) {
                throw new CloudException("No context was set for this request");
            }
            HttpClient client = getClient(ctx, target.startsWith("https"));
            HttpPut put = new HttpPut(target);

            put.addHeader("Content-Type", "application/json");
            put.setHeader("Cookie", authCookie);
            try {
                //noinspection deprecation
                put.setEntity(
                        new StringEntity((new JSONObject(state)).toString(), "application/json", "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new InternalException(e);
            }

            if (wire.isDebugEnabled()) {
                wire.debug(put.getRequestLine().toString());
                for (Header header : put.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");

                try {
                    wire.debug(EntityUtils.toString(put.getEntity()));
                } catch (IOException ignore) {
                }

                wire.debug("");
            }
            HttpResponse response;

            try {
                response = client.execute(put);
                if (wire.isDebugEnabled()) {
                    wire.debug(response.getStatusLine().toString());
                    for (Header header : response.getAllHeaders()) {
                        wire.debug(header.getName() + ": " + header.getValue());
                    }
                    wire.debug("");
                }
            } catch (IOException e) {
                logger.error("I/O error from server communications: " + e.getMessage());
                e.printStackTrace();
                throw new InternalException(e);
            }
            int code = response.getStatusLine().getStatusCode();

            logger.debug("HTTP STATUS: " + code);

            if (code != HttpServletResponse.SC_NO_CONTENT) {
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    try {
                        this.response = EntityUtils.toString(entity);
                    } catch (IOException e) {
                        throw new CloudException(e);
                    }
                    if (wire.isDebugEnabled()) {
                        wire.debug(this.response);
                        wire.debug("");
                    }
                }
                checkResponse(response, code, this.response);
            } else {
                checkResponse(response, code);
            }
            return code;
        } finally {
            if (wire.isDebugEnabled()) {
                wire.debug("<<< [PUT (" + (new Date()) + ")] -> " + url
                        + "/ <--------------------------------------------------------------------------------------");
                wire.debug("");
            }
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + NimbulaMethod.class.getName() + ".put()");
        }
    }
}

From source file:org.dasein.cloud.joyent.JoyentMethod.java

protected String putString(@Nonnull String authToken, @Nonnull String endpoint, @Nonnull String resource,
        @Nullable String payload) throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("ENTER - " + JoyentMethod.class.getName() + ".doPutString(" + endpoint + "," + resource
                + ",PAYLOAD)");
    }/*from ww w.j a  va2 s. com*/
    if (wire.isDebugEnabled()) {
        wire.debug("");
        wire.debug(">>> [PUT (" + (new Date()) + ")] -> " + endpoint + resource
                + " >--------------------------------------------------------------------------------------");
    }
    try {
        HttpClient client = clientFactory.getClient(endpoint);
        HttpPut put = new HttpPut(endpoint + resource);

        put.addHeader("Content-Type", "application/json");
        put.addHeader("Accept", "application/json");
        put.addHeader("X-Auth-Token", authToken);

        put.setEntity(new StringEntity(payload == null ? "" : payload, APPLICATION_JSON_UTF8));

        if (wire.isDebugEnabled()) {
            wire.debug(put.getRequestLine().toString());
            for (Header header : put.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");

            try {
                wire.debug(EntityUtils.toString(put.getEntity()));
            } catch (IOException ignore) {
            }

            wire.debug("");
        }
        HttpResponse response;

        try {
            response = client.execute(put);
            if (wire.isDebugEnabled()) {
                wire.debug(response.getStatusLine().toString());
                for (Header header : response.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
            }
        } catch (IOException e) {
            logger.error("I/O error from server communications: " + e.getMessage());
            throw new InternalException(e);
        }
        int code = response.getStatusLine().getStatusCode();

        logger.debug("HTTP STATUS: " + code);

        if (code != HttpStatus.SC_CREATED && code != HttpStatus.SC_ACCEPTED
                && code != HttpStatus.SC_NO_CONTENT) {
            logger.error("Expected CREATED, ACCEPTED, or NO CONTENT for put request, got " + code);
            String json = null;

            try {
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    json = EntityUtils.toString(entity);
                    if (wire.isDebugEnabled()) {
                        wire.debug(json);
                        wire.debug("");
                    }
                }
            } catch (IOException e) {
                logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                throw new CloudException(e);
            }
            JoyentException.ExceptionItems items = JoyentException.parseException(code, json);

            if (items == null) {
                items = new JoyentException.ExceptionItems();
                items.code = 404;
                items.type = CloudErrorType.COMMUNICATION;
                items.message = "itemNotFound";
                items.details = "No such object: " + resource;
            }
            logger.error("[" + code + " : " + items.message + "] " + items.details);
            throw new JoyentException(items);
        } else {
            if (code == HttpStatus.SC_ACCEPTED || code == HttpStatus.SC_CREATED) {
                String json = null;

                try {
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        json = EntityUtils.toString(entity);
                        if (wire.isDebugEnabled()) {
                            wire.debug(json);
                            wire.debug("");
                        }
                    }
                } catch (IOException e) {
                    logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                    throw new CloudException(e);
                }
                if (json != null && !json.trim().equals("")) {
                    return json;
                }
            }
            return null;
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("EXIT - " + JoyentMethod.class.getName() + ".doPutString()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("<<< [PUT (" + (new Date()) + ")] -> " + endpoint + resource
                    + " <--------------------------------------------------------------------------------------");
            wire.debug("");
        }
    }
}

From source file:org.dasein.cloud.joyent.JoyentMethod.java

protected @Nullable String putHeaders(@Nonnull String authToken, @Nonnull String endpoint,
        @Nonnull String resource, @Nullable Map<String, String> customHeaders)
        throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("ENTER - " + JoyentMethod.class.getName() + ".doPutHeaders(" + endpoint + "," + resource
                + "," + customHeaders + ")");
    }/*from   w ww. j av a 2s  . co m*/
    if (wire.isDebugEnabled()) {
        wire.debug("");
        wire.debug(">>> [PUT (" + (new Date()) + ")] -> " + endpoint + "/" + resource
                + " >--------------------------------------------------------------------------------------");
    }
    try {
        HttpClient client = clientFactory.getClient(endpoint);
        HttpPut put = new HttpPut(endpoint + resource);

        put.addHeader("Content-Type", "application/json");
        put.addHeader("Accept", "application/json");
        put.addHeader("X-Auth-Token", authToken);
        if (customHeaders != null) {
            for (Map.Entry<String, String> entry : customHeaders.entrySet()) {
                String val = (entry.getValue() == null ? "" : entry.getValue());

                put.addHeader(entry.getKey(), val);
            }
        }
        if (wire.isDebugEnabled()) {
            wire.debug(put.getRequestLine().toString());
            for (Header header : put.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");

            try {
                wire.debug(EntityUtils.toString(put.getEntity()));
            } catch (IOException ignore) {
            }

            wire.debug("");
        }
        HttpResponse response;

        try {
            response = client.execute(put);
            if (wire.isDebugEnabled()) {
                wire.debug(response.getStatusLine().toString());
                for (Header header : response.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
            }
        } catch (IOException e) {
            logger.error("I/O error from server communications: " + e.getMessage());
            throw new InternalException(e);
        }
        int code = response.getStatusLine().getStatusCode();

        logger.debug("HTTP STATUS: " + code);

        if (code != HttpStatus.SC_CREATED && code != HttpStatus.SC_ACCEPTED
                && code != HttpStatus.SC_NO_CONTENT) {
            logger.error("Expected CREATED, ACCEPTED, or NO CONTENT for put request, got " + code);
            String json = null;

            try {
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    json = EntityUtils.toString(entity);
                    if (wire.isDebugEnabled()) {
                        wire.debug(json);
                        wire.debug("");
                    }
                }
            } catch (IOException e) {
                logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                throw new CloudException(e);
            }
            JoyentException.ExceptionItems items = JoyentException.parseException(code, json);

            if (items == null) {
                items = new JoyentException.ExceptionItems();
                items.code = 404;
                items.type = CloudErrorType.COMMUNICATION;
                items.message = "itemNotFound";
                items.details = "No such object: " + resource;
            }
            logger.error("[" + code + " : " + items.message + "] " + items.details);
            throw new JoyentException(items);
        } else {
            if (code == HttpStatus.SC_ACCEPTED || code == HttpStatus.SC_CREATED) {
                String json = null;

                try {
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        json = EntityUtils.toString(entity);
                        if (wire.isDebugEnabled()) {
                            wire.debug(json);
                            wire.debug("");
                        }
                    }
                } catch (IOException e) {
                    logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                    throw new CloudException(e);
                }
                if (json != null && !json.trim().equals("")) {
                    return json;
                }
            }
            return null;
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("EXIT - " + JoyentMethod.class.getName() + ".doPutHeaders()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("<<< [PUT (" + (new Date()) + ")] -> " + endpoint + "/" + resource
                    + " <--------------------------------------------------------------------------------------");
            wire.debug("");
        }
    }
}

From source file:org.dasein.cloud.vcloud.vCloudMethod.java

public @Nonnull String put(@Nonnull String action, @Nonnull String endpoint, @Nullable String contentType,
        @Nullable String payload) throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("ENTER: " + vCloudMethod.class.getName() + ".put(" + endpoint + ")");
    }//from   w w  w.  jav  a 2  s  . c om
    try {
        HttpClient client = null;
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug(">>> [PUT (" + (new Date()) + ")] -> " + endpoint
                    + " >--------------------------------------------------------------------------------------");
        }
        try {
            Org org = authenticate(false);
            client = getClient(false);
            HttpPut put = new HttpPut(endpoint);

            put.addHeader("Accept", "application/*+xml;version=" + org.version.version
                    + ",application/*+xml;version=" + org.version.version);

            addAuth(put, org.token);

            if (contentType != null) {
                put.addHeader("Content-Type", contentType);
            }

            if (wire.isDebugEnabled()) {
                wire.debug(put.getRequestLine().toString());
                for (Header header : put.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
            }
            if (payload != null) {
                try {
                    //noinspection deprecation
                    put.setEntity(
                            new StringEntity(payload == null ? "" : payload, "application/json", "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    throw new InternalException(e);
                }
                try {
                    wire.debug(EntityUtils.toString(put.getEntity()));
                } catch (IOException ignore) {
                }

                wire.debug("");
            }
            HttpResponse response;

            try {
                APITrace.trace(provider, "PUT " + action);
                response = client.execute(put);
                if (wire.isDebugEnabled()) {
                    wire.debug(response.getStatusLine().toString());
                    for (Header header : response.getAllHeaders()) {
                        wire.debug(header.getName() + ": " + header.getValue());
                    }
                    wire.debug("");
                }
            } catch (IOException e) {
                logger.error("I/O error from server communications: " + e.getMessage());
                throw new InternalException(e);
            }
            int code = response.getStatusLine().getStatusCode();

            logger.debug("HTTP STATUS: " + code);

            if (code == HttpServletResponse.SC_NOT_FOUND) {
                throw new CloudException("No action match for " + endpoint);
            } else if (code == HttpServletResponse.SC_UNAUTHORIZED) {
                authenticate(true);
                return post(action, endpoint, contentType, payload);
            } else if (code == HttpServletResponse.SC_NO_CONTENT) {
                return "";
            } else if (code == HttpServletResponse.SC_OK || code == HttpServletResponse.SC_CREATED
                    || code == HttpServletResponse.SC_ACCEPTED) {
                String xml = null;

                try {
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        xml = EntityUtils.toString(entity);
                        if (wire.isDebugEnabled()) {
                            wire.debug(xml);
                            wire.debug("");
                        }
                    }
                } catch (IOException e) {
                    logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                    throw new CloudException(e);
                }
                return xml;
            } else {
                logger.error("Expected OK or CREATED or NO_CONTENT or ACCEPTED for POST request, got " + code);
                String xml = null;

                try {
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        xml = EntityUtils.toString(entity);
                        if (wire.isDebugEnabled()) {
                            wire.debug(xml);
                            wire.debug("");
                        }
                    }
                } catch (IOException e) {
                    logger.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                    throw new CloudException(e);
                }

                vCloudException.Data data = null;

                if (xml != null && !xml.equals("")) {
                    Document doc = parseXML(xml);
                    String docElementTagName = doc.getDocumentElement().getTagName();
                    String nsString = "";
                    if (docElementTagName.contains(":"))
                        nsString = docElementTagName.substring(0, docElementTagName.indexOf(":") + 1);
                    NodeList errors = doc.getElementsByTagName(nsString + "Error");

                    if (errors.getLength() > 0) {
                        data = vCloudException.parseException(code, errors.item(0));
                    }
                }
                if (data == null) {
                    throw new vCloudException(CloudErrorType.GENERAL, code,
                            response.getStatusLine().getReasonPhrase(), "No further information");
                }
                logger.error("[" + code + " : " + data.title + "] " + data.description);
                throw new vCloudException(data);
            }
        } finally {
            if (client != null) {
                client.getConnectionManager().shutdown();
            }
            if (wire.isDebugEnabled()) {
                wire.debug("<<< [PUT (" + (new Date()) + ")] -> " + endpoint
                        + " <--------------------------------------------------------------------------------------");
                wire.debug("");
            }
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("EXIT: " + vCloudMethod.class.getName() + ".put()");
        }
    }
}

From source file:org.dasein.cloud.openstack.nova.os.AbstractMethod.java

protected @Nullable String putString(@Nonnull String authToken, @Nonnull String endpoint,
        @Nonnull String resource, @Nullable String payload) throws CloudException, InternalException {
    Logger std = NovaOpenStack.getLogger(NovaOpenStack.class, "std");
    Logger wire = NovaOpenStack.getLogger(NovaOpenStack.class, "wire");

    if (std.isTraceEnabled()) {
        std.trace("enter - " + AbstractMethod.class.getName() + ".putString(" + authToken + "," + endpoint + ","
                + resource + "," + payload + ")");
    }/*w  w w. ja  va2 s .c o  m*/
    if (wire.isDebugEnabled()) {
        wire.debug("---------------------------------------------------------------------------------"
                + endpoint + resource);
        wire.debug("");
    }
    HttpClient client = null;
    try {
        client = getClient();
        HttpPut put = new HttpPut(endpoint + resource);

        put.addHeader("Content-Type", "application/json");
        put.addHeader("X-Auth-Token", authToken);

        if (wire.isDebugEnabled()) {
            wire.debug(put.getRequestLine().toString());
            for (Header header : put.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
        }
        if (payload != null) {
            try {
                //noinspection deprecation
                put.setEntity(new StringEntity(payload == null ? "" : payload, "application/json", "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new InternalException(e);
            }
            try {
                wire.debug(EntityUtils.toString(put.getEntity()));
            } catch (IOException ignore) {
            }

            wire.debug("");
        }
        HttpResponse response;

        try {
            APITrace.trace(provider, "PUT " + toAPIResource(resource));
            response = client.execute(put);
            if (wire.isDebugEnabled()) {
                wire.debug(response.getStatusLine().toString());
                for (Header header : response.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
            }
        } catch (IOException e) {
            std.error("I/O error from server communications: " + e.getMessage());
            e.printStackTrace();
            throw new InternalException(e);
        }
        int code = response.getStatusLine().getStatusCode();

        std.debug("HTTP STATUS: " + code);

        if (code != HttpStatus.SC_CREATED && code != HttpStatus.SC_ACCEPTED && code != HttpStatus.SC_NO_CONTENT
                && code != HttpStatus.SC_OK) {
            std.error("putString(): Expected CREATED, ACCEPTED, or NO CONTENT for put request, got " + code);
            String data = null;

            try {
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    data = EntityUtils.toString(entity);
                    if (wire.isDebugEnabled()) {
                        wire.debug(data);
                        wire.debug("");
                    }
                }
            } catch (IOException e) {
                std.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                e.printStackTrace();
                throw new CloudException(e);
            }
            NovaException.ExceptionItems items = NovaException.parseException(code, data);

            if (items == null) {
                items = new NovaException.ExceptionItems();
                items.code = 404;
                items.type = CloudErrorType.COMMUNICATION;
                items.message = "itemNotFound";
                items.details = "No such object: " + resource;
            }
            std.error("putString(): [" + code + " : " + items.message + "] " + items.details);
            throw new NovaException(items);
        } else {
            if (code == HttpStatus.SC_ACCEPTED || code == HttpStatus.SC_CREATED) {
                String data = null;

                try {
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        data = EntityUtils.toString(entity);
                        if (wire.isDebugEnabled()) {
                            wire.debug(data);
                            wire.debug("");
                        }
                    }
                } catch (IOException e) {
                    std.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                    e.printStackTrace();
                    throw new CloudException(e);
                }
                if (data != null && !data.trim().equals("")) {
                    return data;
                }
            }
            return null;
        }
    } finally {
        if (client != null) {
            client.getConnectionManager().shutdown();
        }
        if (std.isTraceEnabled()) {
            std.trace("exit - " + AbstractMethod.class.getName() + ".putString()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("---------------------------------------------------------------------------------"
                    + endpoint + resource);
        }
    }
}

From source file:com.infinities.skyport.openstack.nova.os.SkyportNovaMethod.java

@Override
protected @Nullable String putString(@Nonnull String authToken, @Nonnull String endpoint,
        @Nonnull String resource, @Nullable String payload) throws CloudException, InternalException {
    Logger std = NovaOpenStack.getLogger(NovaOpenStack.class, "std");
    Logger wire = NovaOpenStack.getLogger(NovaOpenStack.class, "wire");

    if (std.isTraceEnabled()) {
        std.trace("enter - " + AbstractMethod.class.getName() + ".putString(" + authToken + "," + endpoint + ","
                + resource + "," + payload + ")");
    }/*from  www  .  ja  v  a  2  s . c  o  m*/
    if (wire.isDebugEnabled()) {
        wire.debug("---------------------------------------------------------------------------------"
                + endpoint + resource);
        wire.debug("");
    }
    HttpClient client = null;
    try {
        client = getClient();
        HttpPut put = new HttpPut(endpoint + resource);

        put.addHeader("Content-Type", "application/json");
        put.addHeader("X-Auth-Token", authToken);

        if (wire.isDebugEnabled()) {
            wire.debug(put.getRequestLine().toString());
            for (Header header : put.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
        }
        if (payload != null) {
            try {
                // noinspection deprecation
                put.setEntity(new StringEntity(payload == null ? "" : payload, "application/json", "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new InternalException(e);
            }
            try {
                wire.debug(EntityUtils.toString(put.getEntity()));
            } catch (IOException ignore) {
            }

            wire.debug("");
        }
        HttpResponse response;

        try {
            APITrace.trace(provider, "PUT " + toAPIResource(resource));
            response = client.execute(put);
            if (wire.isDebugEnabled()) {
                wire.debug(response.getStatusLine().toString());
                for (Header header : response.getAllHeaders()) {
                    wire.debug(header.getName() + ": " + header.getValue());
                }
                wire.debug("");
            }
        } catch (IOException e) {
            std.error("I/O error from server communications: " + e.getMessage());
            e.printStackTrace();
            throw new InternalException(e);
        }
        int code = response.getStatusLine().getStatusCode();

        std.debug("HTTP STATUS: " + code);

        if (code != HttpStatus.SC_CREATED && code != HttpStatus.SC_ACCEPTED && code != HttpStatus.SC_NO_CONTENT
                && code != HttpStatus.SC_OK) {
            std.error("putString(): Expected CREATED, ACCEPTED, or NO CONTENT for put request, got " + code);
            String data = null;

            try {
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    data = EntityUtils.toString(entity);
                    if (wire.isDebugEnabled()) {
                        wire.debug(data);
                        wire.debug("");
                    }
                }
            } catch (IOException e) {
                std.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                e.printStackTrace();
                throw new CloudException(e);
            }
            NovaException.ExceptionItems items = NovaException.parseException(code, data);

            if (items == null) {
                items = new NovaException.ExceptionItems();
                items.code = 404;
                items.type = CloudErrorType.COMMUNICATION;
                items.message = "itemNotFound";
                items.details = "No such object: " + resource;
            }
            std.error("putString(): [" + code + " : " + items.message + "] " + items.details);
            throw new NovaException(items);
        } else {
            if (code == HttpStatus.SC_ACCEPTED || code == HttpStatus.SC_CREATED) {
                String data = null;

                try {
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        data = EntityUtils.toString(entity);
                        if (wire.isDebugEnabled()) {
                            wire.debug(data);
                            wire.debug("");
                        }
                    }
                } catch (IOException e) {
                    std.error("Failed to read response error due to a cloud I/O error: " + e.getMessage());
                    e.printStackTrace();
                    throw new CloudException(e);
                }
                if (data != null && !data.trim().equals("")) {
                    return data;
                }
            }
            return null;
        }
    } finally {
        if (client != null) {
            client.getConnectionManager().shutdown();
        }
        if (std.isTraceEnabled()) {
            std.trace("exit - " + AbstractMethod.class.getName() + ".putString()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("---------------------------------------------------------------------------------"
                    + endpoint + resource);
        }
    }
}