Example usage for org.apache.http.client.methods HttpRequestBase getAllHeaders

List of usage examples for org.apache.http.client.methods HttpRequestBase getAllHeaders

Introduction

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

Prototype

public Header[] getAllHeaders() 

Source Link

Usage

From source file:org.dasein.cloud.azure.AzureStorageMethod.java

@SuppressWarnings("deprecation")
public Document getAsDoc(@Nonnull String httpVerb, @Nonnull String resource,
        @Nullable Map<String, String> queries, @Nullable String body, @Nullable Map<String, String> headerMap,
        boolean authorization) throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("enter - " + AzureStorageMethod.class.getName() + "." + httpVerb + "("
                + getStorageAccount() + "," + resource + ")");
    }// ww  w  .  j av  a2s  . co  m
    String endpoint = getStorageEnpoint();

    if (wire.isDebugEnabled()) {
        wire.debug(httpVerb + "--------------------------------------------------------> " + endpoint
                + getStorageAccount() + resource);
        wire.debug("");
    }
    try {
        HttpClient client = getClient();

        if (headerMap == null) {
            headerMap = new HashMap<String, String>();
        }

        HttpRequestBase method = getMethod(httpVerb, buildUrl(resource, queries), queries, headerMap,
                authorization);

        if (wire.isDebugEnabled()) {
            wire.debug(method.getRequestLine().toString());
            for (Header header : method.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                wire.debug(body);
                wire.debug("");
            }
        }

        // If it is post or put
        if (method instanceof HttpEntityEnclosingRequestBase) {

            HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) method;

            if (body != null) {
                entityEnclosingMethod.setEntity(new StringEntity(body, "application/xml", "utf-8"));
            }
        }

        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(method);
            status = response.getStatusLine();
        } catch (IOException e) {
            logger.error("GET(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (logger.isTraceEnabled()) {
                e.printStackTrace();
            }
            throw new CloudException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("GET(): 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_NOT_FOUND) {
            return null;
        }
        if (status.getStatusCode() != HttpServletResponse.SC_OK
                && status.getStatusCode() != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION) {
            logger.error(
                    httpVerb + "(): Expected OK for " + httpVerb + "request, got " + status.getStatusCode());

            HttpEntity entity = response.getEntity();
            String result;

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                result = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(result);
            }
            wire.debug("");
            AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), result);

            if (items == null) {
                return null;
            }

            logger.error(
                    httpVerb + "(): [" + status.getStatusCode() + " : " + items.message + "] " + items.details);
            throw new AzureException(items);
        } else {

            HttpEntity entity = response.getEntity();

            if (entity == null) {
                return null;
            }
            InputStream input;

            try {
                input = entity.getContent();
            } catch (IOException e) {
                logger.error(httpVerb + "(): Failed to read response error due to a cloud I/O error: "
                        + e.getMessage());
                if (logger.isTraceEnabled()) {
                    e.printStackTrace();
                }
                throw new CloudException(e);
            }
            return parseResponse(input, true);
        }
    } catch (UnsupportedEncodingException e) {
        throw new CloudException(e);
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + AzureMethod.class.getName() + ".getStream()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("--------------------------------------------------------> ");
        }
    }
}

From source file:org.dasein.cloud.azure.AzureStorageMethod.java

public @Nullable InputStream getAsStream(@Nonnull String strMethod, @Nonnull String resource,
        @Nonnull Map<String, String> queries, @Nullable String body, @Nullable Map<String, String> headerMap,
        boolean authorization) throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("enter - " + AzureStorageMethod.class.getName() + "." + strMethod + "("
                + getStorageAccount() + "," + resource + ")");
    }//from  w  ww. j a  v a 2  s  . c om
    String endpoint = getStorageEnpoint();
    if (wire.isDebugEnabled()) {
        wire.debug(strMethod + "--------------------------------------------------------> " + endpoint
                + getStorageAccount() + resource);
        wire.debug("");
    }
    try {

        HttpClient client = getClient();

        String contentLength = null;
        if (body != null) {
            contentLength = String.valueOf(body.length());
        } else {
            contentLength = "0";
        }

        HttpRequestBase method = getMethod(strMethod, buildUrl(resource, queries), queries, headerMap,
                authorization);

        if (wire.isDebugEnabled()) {
            wire.debug(method.getRequestLine().toString());
            for (Header header : method.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                wire.debug(body);
                wire.debug("");
            }
        }

        // If it is post or put
        if (method instanceof HttpEntityEnclosingRequestBase) {

            HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) method;

            if (body != null) {
                entityEnclosingMethod.setEntity(new StringEntity(body, "application/xml", "utf-8"));
            }
        }

        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(method);
            status = response.getStatusLine();
        } catch (IOException e) {
            logger.error("post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (logger.isTraceEnabled()) {
                e.printStackTrace();
            }
            throw new CloudException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("post(): 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_NOT_FOUND) {
            return null;
        }
        if (status.getStatusCode() != HttpServletResponse.SC_OK
                && status.getStatusCode() != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION) {
            logger.error(
                    strMethod + "(): Expected OK for " + strMethod + "request, got " + status.getStatusCode());

            HttpEntity entity = response.getEntity();
            String result;

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                result = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(result);
            }
            wire.debug("");
            AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), result);

            if (items == null) {
                return null;
            }

            logger.error(strMethod + "(): [" + status.getStatusCode() + " : " + items.message + "] "
                    + items.details);
            throw new AzureException(items);
        } else {

            HttpEntity entity = response.getEntity();

            if (entity == null) {
                return null;
            }
            InputStream input;

            try {
                input = entity.getContent();
            } catch (IOException e) {
                logger.error(strMethod + "(): Failed to read response error due to a cloud I/O error: "
                        + e.getMessage());
                if (logger.isTraceEnabled()) {
                    e.printStackTrace();
                }
                throw new CloudException(e);
            }
            return input;
        }
    } catch (UnsupportedEncodingException e) {
        throw new CloudException(e);
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + AzureMethod.class.getName() + ".getStream()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("--------------------------------------------------------> ");
        }
    }
}

From source file:org.dasein.cloud.azure.AzureStorageMethod.java

public String getBlobProperty(@Nonnull String strMethod, @Nonnull String resource,
        @Nonnull Map<String, String> queries, String body, @Nullable Map<String, String> headerMap,
        boolean authorization, String propertyName) throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("enter - " + AzureStorageMethod.class.getName() + "." + strMethod + "("
                + getStorageAccount() + "," + resource + ")");
    }/*from   www  . ja v  a2  s .c o m*/
    String endpoint = getStorageEnpoint();

    if (wire.isDebugEnabled()) {
        wire.debug(strMethod + "--------------------------------------------------------> " + endpoint
                + getStorageAccount() + resource);
        wire.debug("");
    }
    try {

        HttpClient client = getClient();

        if (headerMap == null) {
            headerMap = new HashMap<String, String>();
        }

        HttpRequestBase method = getMethod(strMethod, buildUrl(resource, queries), queries, headerMap,
                authorization);

        if (wire.isDebugEnabled()) {
            wire.debug(method.getRequestLine().toString());
            for (Header header : method.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                wire.debug(body);
                wire.debug("");
            }
        }

        // If it is post or put
        if (method instanceof HttpEntityEnclosingRequestBase) {

            HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) method;

            if (body != null) {
                entityEnclosingMethod.setEntity(new StringEntity(body, "application/xml", "utf-8"));
            }
        }

        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(method);
            status = response.getStatusLine();
        } catch (IOException e) {
            logger.error("post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (logger.isTraceEnabled()) {
                e.printStackTrace();
            }
            throw new CloudException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("get(): HTTP Status " + status);
        }

        if (wire.isDebugEnabled()) {
            Header[] headers = response.getAllHeaders();
            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_NOT_FOUND) {
            return null;
        }
        if ((status.getStatusCode() != HttpServletResponse.SC_CREATED
                && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED
                && status.getStatusCode() != HttpServletResponse.SC_OK)
                && status.getStatusCode() != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION) {
            logger.error(
                    strMethod + "(): Expected OK for " + strMethod + "request, got " + status.getStatusCode());

            HttpEntity entity = response.getEntity();
            String result;

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                result = EntityUtils.toString(entity);

                int index = result.indexOf("<");
                // The result may not be a stardard xml format
                if (index > 0) {
                    result = result.substring(index);
                }
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(result);
            }
            wire.debug("");
            AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), result);
            logger.error(strMethod + "(): [" + status.getStatusCode() + " : " + items.message + "] "
                    + items.details);
            throw new AzureException(items);
        } else {
            Header header = response.getFirstHeader(propertyName);
            if (header != null) {
                return header.getValue();
            } else {
                return null;
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new CloudException(e);
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + AzureMethod.class.getName() + ".getStream()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("--------------------------------------------------------> ");
        }
    }
}

From source file:org.dasein.cloud.azure.AzureStorageMethod.java

public void invoke(@Nonnull String strMethod, @Nonnull String resource, @Nonnull Map<String, String> queries,
        @Nullable String body, @Nullable Map<String, String> headerMap, boolean authorization)
        throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("enter - " + AzureStorageMethod.class.getName() + "." + strMethod + "("
                + getStorageAccount() + "," + resource + ")");
    }//from  ww w  .j a v  a2  s.  c om
    String endpoint = getStorageEnpoint();

    if (wire.isDebugEnabled()) {
        wire.debug(strMethod + "--------------------------------------------------------> " + endpoint
                + getStorageAccount() + resource);
        wire.debug("");
    }
    try {
        HttpClient client = getClient();

        if (headerMap == null) {
            headerMap = new HashMap<String, String>();
        }

        HttpRequestBase method = getMethod(strMethod, buildUrl(resource, queries), queries, headerMap,
                authorization);

        if (wire.isDebugEnabled()) {
            wire.debug(method.getRequestLine().toString());
            for (Header header : method.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                wire.debug(body);
                wire.debug("");
            }
        }

        // If it is post or put
        if (method instanceof HttpEntityEnclosingRequestBase) {

            HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) method;

            if (body != null) {
                entityEnclosingMethod.setEntity(new StringEntity(body, "application/xml", "utf-8"));
            }
        }

        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(method);
            status = response.getStatusLine();
        } catch (IOException e) {
            logger.error("post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (logger.isTraceEnabled()) {
                e.printStackTrace();
            }
            throw new CloudException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("post(): 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_CREATED
                && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED
                && status.getStatusCode() != HttpServletResponse.SC_OK)
                && status.getStatusCode() != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION) {
            logger.error(
                    strMethod + "(): Expected OK for " + strMethod + "request, got " + status.getStatusCode());

            HttpEntity entity = response.getEntity();
            String result;

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                result = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(result);
            }
            wire.debug("");
            AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), result);

            if (items != null) {
                logger.error(strMethod + "(): [" + status.getStatusCode() + " : " + items.message + "] "
                        + items.details);
                throw new AzureException(items);
            } else {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(), "UnknownError",
                        result);
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new CloudException(e);
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + AzureMethod.class.getName() + ".getStream()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("--------------------------------------------------------> ");
        }
    }
}

From source file:org.dasein.cloud.azure.AzureStorageMethod.java

public void putWithFile(@Nonnull String strMethod, @Nonnull String resource, Map<String, String> queries,
        File file, Map<String, String> headerMap, boolean authorization)
        throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("enter - " + AzureStorageMethod.class.getName() + "." + strMethod + "("
                + getStorageAccount() + "," + resource + ")");
    }//w  ww.java 2s .  c o m
    String endpoint = getStorageEnpoint();

    if (wire.isDebugEnabled()) {
        wire.debug(strMethod + "--------------------------------------------------------> " + endpoint
                + getStorageAccount() + resource);
        wire.debug("");
    }

    long begin = System.currentTimeMillis();
    try {

        HttpClient client = getClient();

        String contentLength = null;

        if (file != null) {
            contentLength = String.valueOf(file.length());
        } else {
            contentLength = "0";
        }

        HttpRequestBase method = getMethod(strMethod, buildUrl(resource, queries), queries, headerMap,
                authorization);

        if (wire.isDebugEnabled()) {
            wire.debug(method.getRequestLine().toString());
            for (Header header : method.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (file != null) {
                wire.debug(file);
                wire.debug("");
            }
        }

        // If it is post or put
        if (method instanceof HttpEntityEnclosingRequestBase) {

            HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) method;

            if (file != null) {
                entityEnclosingMethod.setEntity(new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM));
            }
        }

        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(method);
            status = response.getStatusLine();
        } catch (IOException e) {
            logger.error("post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (logger.isTraceEnabled()) {
                e.printStackTrace();
            }

            long end = System.currentTimeMillis();
            logger.debug("Totoal time -> " + (end - begin));
            throw new CloudException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("post(): 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_CREATED
                && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED
                && status.getStatusCode() != HttpServletResponse.SC_OK)
                && status.getStatusCode() != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION) {
            logger.error(
                    strMethod + "(): Expected OK for " + strMethod + "request, got " + status.getStatusCode());

            HttpEntity entity = response.getEntity();
            String result;

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                result = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(result);
            }
            wire.debug("");
            AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), result);
            logger.error(strMethod + "(): [" + status.getStatusCode() + " : " + items.message + "] "
                    + items.details);
            throw new AzureException(items);
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + AzureMethod.class.getName() + ".getStream()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("--------------------------------------------------------> ");
        }
    }
}

From source file:org.dasein.cloud.azure.AzureStorageMethod.java

public void putWithBytes(@Nonnull String strMethod, @Nonnull String resource, Map<String, String> queries,
        byte[] body, Map<String, String> headerMap, boolean authorization)
        throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
        logger.trace("enter - " + AzureStorageMethod.class.getName() + "." + strMethod + "("
                + getStorageAccount() + "," + resource + ")");
    }/*from   w  w w.  jav a 2  s. co m*/
    String endpoint = getStorageEnpoint();

    if (wire.isDebugEnabled()) {
        wire.debug(strMethod + "--------------------------------------------------------> " + endpoint
                + getStorageAccount() + resource);
        wire.debug("");
    }
    try {

        HttpClient client = getClient();

        String contentLength = null;
        if (body != null) {
            contentLength = String.valueOf(body.length);
        } else {
            contentLength = "0";
        }

        HttpRequestBase method = getMethod(strMethod, buildUrl(resource, queries), queries, headerMap,
                authorization);

        if (wire.isDebugEnabled()) {
            wire.debug(method.getRequestLine().toString());
            for (Header header : method.getAllHeaders()) {
                wire.debug(header.getName() + ": " + header.getValue());
            }
            wire.debug("");
            if (body != null) {
                wire.debug(body);
                wire.debug("");
            }
        }

        // If it is post or put
        if (method instanceof HttpEntityEnclosingRequestBase) {

            HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) method;

            if (body != null) {
                entityEnclosingMethod.setEntity(new ByteArrayEntity(body));
            }
        }

        HttpResponse response;
        StatusLine status;

        try {
            response = client.execute(method);
            status = response.getStatusLine();
        } catch (IOException e) {
            logger.error("post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
            if (logger.isTraceEnabled()) {
                e.printStackTrace();
            }
            throw new CloudException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("post(): 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_CREATED
                && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED
                && status.getStatusCode() != HttpServletResponse.SC_OK)
                && status.getStatusCode() != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION) {

            logger.error(
                    strMethod + "(): Expected OK for " + strMethod + "request, got " + status.getStatusCode());

            HttpEntity entity = response.getEntity();
            String result;

            if (entity == null) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), "An error was returned without explanation");
            }
            try {
                result = EntityUtils.toString(entity);
            } catch (IOException e) {
                throw new AzureException(CloudErrorType.GENERAL, status.getStatusCode(),
                        status.getReasonPhrase(), e.getMessage());
            }
            if (wire.isDebugEnabled()) {
                wire.debug(result);
            }
            wire.debug("");
            AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), result);
            logger.error(strMethod + "(): [" + status.getStatusCode() + " : " + items.message + "] "
                    + items.details);
            throw new AzureException(items);
        }
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("exit - " + AzureMethod.class.getName() + ".getStream()");
        }
        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug("--------------------------------------------------------> ");
        }
    }
}

From source file:org.dasein.cloud.digitalocean.models.rest.DigitalOceanModelFactory.java

/**
 * Sent http request to the server/*from w  w  w. j a  va2s  .co m*/
 * @return Http response
 * @throws CloudException
 */
private static HttpResponse sendRequest(org.dasein.cloud.digitalocean.DigitalOcean provider, RESTMethod method,
        String token, String strUrl, DigitalOceanAction action) throws CloudException, InternalException {
    HttpRequestBase req = null;
    if (method == RESTMethod.GET) {
        req = new HttpGet(strUrl);
    } else if (method == RESTMethod.POST) {
        req = new HttpPost(strUrl);
    } else if (method == RESTMethod.PUT) {
        req = new HttpPut(strUrl);
    } else if (method == RESTMethod.DELETE) {
        req = new HttpDelete(strUrl);
    } else if (method == RESTMethod.HEAD) {
        req = new HttpHead(strUrl);
    }

    try {
        req.setHeader("Authorization", "Bearer " + token);
        req.setHeader("Accept", "application/json");
        req.setHeader("Content-Type", "application/json;charset=UTF-8");

        StringEntity requestEntity = null;
        if (req instanceof HttpEntityEnclosingRequestBase && action != null) {
            JSONObject jsonToPost = action.getParameters();
            if (jsonToPost != null) {
                requestEntity = new StringEntity(jsonToPost.toString(), ContentType.APPLICATION_JSON);
                ((HttpEntityEnclosingRequestBase) req).setEntity(requestEntity);
            }
        }

        HttpClient httpClient = provider.getClient();

        if (wire.isDebugEnabled()) {
            wire.debug("");
            wire.debug(
                    "--------------------------------------------------------------------------------------");
        }

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

            if (requestEntity != null) {
                try {
                    wire.debug(EntityUtils.toString(requestEntity));
                    wire.debug("");
                } catch (IOException ignore) {
                }
            }
        }

        HttpResponse response = null;
        int retryCount = 0;

        while (retryCount < 6) {
            response = httpClient.execute(req);

            if (wire.isDebugEnabled()) {
                wire.debug(response.getStatusLine().toString());
            }

            if (method == RESTMethod.DELETE) {
                if ((response.getStatusLine().getStatusCode() == 204)) {
                    break;
                } else {
                    retryCount++;
                    Thread.sleep(5000);
                }
            } else {
                break;
            }
        }
        if (method == RESTMethod.DELETE && (response.getStatusLine().getStatusCode() != 204)) {
            //Error occurred
            throw new CloudException("Delete method returned unexpected code, despite retrying.");
        }
        return response;
    } catch (JSONException e) {
        throw new CloudException("Problem sending request.", e);
    } catch (InterruptedException e) {
        throw new CloudException("Problem sending request.", e);
    } catch (ClientProtocolException e) {
        throw new CloudException("Problem sending request.", e);
    } catch (IOException e) {
        throw new CloudException("Problem sending request.", e);
    } finally {
        try {
            //                req.releaseConnection();
        } catch (Exception e) {
        }

    }
}