static public void expectStatusCodes()

in ch-httpclient-util/src/main/java/com/cloudhopper/httpclient/util/HttpClientUtil.java [131:194]


    static public void expectStatusCodes(int [] expectedStatusCodes, HttpResponse response) throws UnexpectedHttpStatusCodeException {
        // verify the response wasn't null
        if (response == null) {
            throw new UnexpectedHttpStatusCodeException(expectedStatusCodes, "HttpResponse was null [expected statusCodes " + Arrays.toString(expectedStatusCodes) + "]");
        }

        // get the status line
        StatusLine status = response.getStatusLine();

        // verify the status line object wasn't null
        if (status == null) {
            throw new UnexpectedHttpStatusCodeException(expectedStatusCodes, "HttpResponse contained a null StatusLine [expected statusCode=" + Arrays.toString(expectedStatusCodes) + "]");
        }

        // try to match any of the expected status codes
        boolean wasExpected = false;
        for (int expectedStatusCode : expectedStatusCodes) {
            if (status.getStatusCode() == expectedStatusCode) {
                wasExpected = true;
                break;
            }
        }

        // verify the expected status code matches
        if (!wasExpected) {
            // prepare the error message we'll set in the exception
            StringBuilder message = new StringBuilder(200);
            String body = null;

            message.append("Unexpected HTTP status code: expected [");
            message.append(Arrays.toString(expectedStatusCodes));
            message.append("] actual [");
            message.append(status.getStatusCode());
            message.append("] reason [");
            message.append(status.getReasonPhrase());
            message.append("]");

            // attempt to read the body of the response
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                try {
                    // consume content and process it
                    body = EntityUtils.toString(entity);
                    // append the first 100 chars of response
                    if (body != null) {
                        message.append(" responseBody [");
                        if (body.length() > 100) {
                            message.append(body.substring(0, 100));
                        } else {
                            message.append(body);
                        }
                        message.append("]]");
                    }
                } catch (IOException e) {
                    //logger.warn("IOException while trying to read content for unexpected status code", e);
                } finally {
                    try { entity.consumeContent(); } catch (Exception ignore) { }
                }
            }

            throw new UnexpectedHttpStatusCodeException(expectedStatusCodes, status.getStatusCode(), message.toString());
        }
    }