Example usage for javax.servlet.http HttpServletResponse SC_ACCEPTED

List of usage examples for javax.servlet.http HttpServletResponse SC_ACCEPTED

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_ACCEPTED.

Prototype

int SC_ACCEPTED

To view the source code for javax.servlet.http HttpServletResponse SC_ACCEPTED.

Click Source Link

Document

Status code (202) indicating that a request was accepted for processing, but was not completed.

Usage

From source file:org.dasein.cloud.rackspace.AbstractMethod.java

protected @Nullable String putStream(@Nonnull String authToken, @Nonnull String endpoint,
        @Nonnull String resource, @Nullable String md5Hash, @Nullable InputStream stream)
        throws CloudException, InternalException {
    Logger std = RackspaceCloud.getLogger(RackspaceCloud.class, "std");
    Logger wire = RackspaceCloud.getLogger(RackspaceCloud.class, "wire");

    if (std.isTraceEnabled()) {
        std.trace("enter - " + AbstractMethod.class.getName() + ".putStream(" + authToken + "," + endpoint + ","
                + resource + "," + md5Hash + ",INPUTSTREAM)");
    }/*ww w . j av a 2s.com*/
    if (wire.isDebugEnabled()) {
        wire.debug("---------------------------------------------------------------------------------"
                + endpoint + resource);
        wire.debug("");
    }
    try {
        HttpClient client = getClient();
        HttpPut put = new HttpPut(endpoint + resource);

        put.addHeader("Content-Type", "application/octet-stream");
        put.addHeader("X-Auth-Token", authToken);
        if (md5Hash != null) {
            put.addHeader("ETag", md5Hash);
        }
        put.setEntity(new InputStreamEntity(stream, -1, ContentType.APPLICATION_OCTET_STREAM));

        if (wire.isDebugEnabled()) {
            wire.debug(put.getRequestLine().toString());
            for (Header header : put.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            wire.debug("--> BINARY DATA <--");
        }
        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) {
            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);

        String responseHash = null;

        for (Header h : response.getAllHeaders()) {
            if (h.getName().equals("ETag")) {
                responseHash = h.getValue();
            }
        }
        if (responseHash != null && md5Hash != null && !responseHash.equals(md5Hash)) {
            throw new CloudException("MD5 hash values do not match, probably data corruption");
        }
        if (code != HttpServletResponse.SC_CREATED && code != HttpServletResponse.SC_ACCEPTED
                && code != HttpServletResponse.SC_NO_CONTENT) {
            std.error("putStream(): Expected CREATED, ACCEPTED, or NO CONTENT for PUT request, got " + code);
            HttpEntity entity = response.getEntity();
            String json = null;

            if (entity != null) {
                try {
                    json = EntityUtils.toString(entity);

                    if (wire.isDebugEnabled()) {
                        wire.debug(json);
                        wire.debug("");
                    }
                } catch (IOException e) {
                    throw new CloudException(e);
                }
            }
            RackspaceException.ExceptionItems items = (json == null ? null
                    : RackspaceException.parseException(code, json));

            if (items == null) {
                items = new RackspaceException.ExceptionItems();
                items.code = 404;
                items.type = CloudErrorType.COMMUNICATION;
                items.message = "itemNotFound";
                items.details = "No such object: " + resource;
            }
            std.error("putStream(): [" + code + " : " + items.message + "] " + items.details);
            throw new RackspaceException(items);
        } else {
            if (code == HttpServletResponse.SC_ACCEPTED) {
                HttpEntity entity = response.getEntity();
                String json = null;

                if (entity != null) {
                    try {
                        json = EntityUtils.toString(entity);

                        if (wire.isDebugEnabled()) {
                            wire.debug(json);
                            wire.debug("");
                        }
                    } catch (IOException e) {
                        throw new CloudException(e);
                    }
                }
                if (json != null && !json.trim().equals("")) {
                    return json;
                }
            }
            return null;
        }
    } finally {
        if (std.isTraceEnabled()) {
            std.trace("exit - " + RackspaceCloud.class.getName() + ".putStream()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("---------------------------------------------------------------------------------"
                    + endpoint + resource);
        }
    }
}

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

public @Nonnull String post(@Nonnull String action, @Nonnull String endpoint, @Nullable String contentType,
        @Nullable String payload) throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("ENTER: " + vCloudMethod.class.getName() + ".post(" + endpoint + ")");
    }/*from   w  w w.ja va  2 s .  co  m*/
    try {
        HttpClient client = null;
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug(">>> [POST (" + (new Date()) + ")] -> " + endpoint
                    + " >--------------------------------------------------------------------------------------");
        }
        try {
            Org org = authenticate(false);
            client = getClient(false);
            HttpPost post = new HttpPost(endpoint);

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

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

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

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

            try {
                APITrace.trace(provider, "POST " + action);
                response = client.execute(post);
                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("<<< [POST (" + (new Date()) + ")] -> " + endpoint
                        + " <--------------------------------------------------------------------------------------");
                wire.debug("");
            }
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("EXIT: " + vCloudMethod.class.getName() + ".post()");
        }
    }
}

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 + ")");
    }/* w ww .  j  a v  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()");
        }
    }
}