Example usage for org.apache.http.client.methods HttpUriRequest getFirstHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest getFirstHeader

Introduction

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

Prototype

Header getFirstHeader(String str);

Source Link

Usage

From source file:org.jets3t.service.impl.rest.httpclient.RestStorageService.java

/**
 * Performs an HTTP/S request by invoking the provided HttpMethod object. If the HTTP
 * response code doesn't match the expected value, an exception is thrown.
 *
 * @param httpMethod/*  w w w. j  a v a 2  s .  co m*/
 *        the object containing a request target and all other information necessary to perform the
 *        request
 * @param expectedResponseCodes
 *        the HTTP response code(s) that indicates a successful request. If the response code received
 *        does not match this value an error must have occurred, so an exception is thrown.
 * @param context
 *        An HttpContext to facilitate information sharing in the HTTP chain
 * @throws ServiceException
 *        all exceptions are wrapped in an ServiceException. Depending on the kind of error that
 *        occurred, this exception may contain additional error information available from an XML
 *        error response document.
 */
protected HttpResponse performRequest(HttpUriRequest httpMethod, int[] expectedResponseCodes,
        HttpContext context) throws ServiceException {
    HttpResponse response = null;
    InterfaceLogBean reqBean = new InterfaceLogBean(httpMethod.getURI().toString(), "", "");
    try {
        if (log.isDebugEnabled()) {
            log.debug("Performing " + httpMethod.getMethod() + " request for '" + httpMethod.getURI().toString()
                    + "', expecting response codes: " + "[" + ServiceUtils.join(expectedResponseCodes, ",")
                    + "]");
            log.debug("Headers: " + Arrays.asList(httpMethod.getAllHeaders()));
        }
        log.debug("Endpoint: " + getEndpoint());

        // Variables to manage S3 Internal Server 500 or 503 Service Unavailable errors.
        boolean completedWithoutRecoverableError = true;
        int internalErrorCount = 0;
        int requestTimeoutErrorCount = 0;
        int redirectCount = 0;
        int authFailureCount = 0;
        boolean wasRecentlyRedirected = false;

        // Perform the request, sleeping and retrying when errors are encountered.
        int responseCode = -1;
        do {
            // Build the authorization string for the method (Unless we have just been redirected).
            if (!wasRecentlyRedirected) {
                authorizeHttpRequest(httpMethod, context);
            } else {
                // Reset redirection flag
                wasRecentlyRedirected = false;
            }

            response = httpClient.execute(httpMethod, context);
            responseCode = response.getStatusLine().getStatusCode();
            reqBean.setRespParams("[responseCode: " + responseCode + "][x-amz-request-id: "
                    + response.getFirstHeader("x-amz-request-id").getValue() + "]");
            if (responseCode == 307) {
                // Retry on Temporary Redirects, using new URI from location header
                authorizeHttpRequest(httpMethod, context); // Re-authorize *before* we change the URI
                Header locationHeader = response.getFirstHeader("location");

                // deal with implementations of HttpUriRequest
                if (httpMethod instanceof HttpRequestBase) {
                    ((HttpRequestBase) httpMethod).setURI(new URI(locationHeader.getValue()));
                } else if (httpMethod instanceof RequestWrapper) {
                    ((RequestWrapper) httpMethod).setURI(new URI(locationHeader.getValue()));
                }

                completedWithoutRecoverableError = false;
                redirectCount++;
                wasRecentlyRedirected = true;

                if (redirectCount > 5) {
                    reqBean.setResponseInfo("Exceeded 307 redirect limit (5).", "-1");
                    throw new ServiceException("Exceeded 307 redirect limit (5).");
                }
            } else if (responseCode == 500 || responseCode == 503) {
                // Retry on S3 Internal Server 500 or 503 Service Unavailable errors.
                completedWithoutRecoverableError = false;
                reqBean.setResponseInfo("Internal Server error(s).", "-1");
                ilog.error(reqBean);
                sleepOnInternalError(++internalErrorCount);
            } else {
                completedWithoutRecoverableError = true;
            }

            String contentType = "";
            if (response.getFirstHeader("Content-Type") != null) {
                contentType = response.getFirstHeader("Content-Type").getValue();
            }
            if (log.isDebugEnabled()) {
                log.debug("Response for '" + httpMethod.getMethod() + "'. Content-Type: " + contentType
                        + ", Headers: " + Arrays.asList(response.getAllHeaders()));
                log.debug("Response entity: " + response.getEntity());
                if (response.getEntity() != null) {
                    log.debug("Entity length: " + response.getEntity().getContentLength());
                }
            }

            // Check we received the expected result code.
            boolean didReceiveExpectedResponseCode = false;
            for (int i = 0; i < expectedResponseCodes.length && !didReceiveExpectedResponseCode; i++) {
                if (responseCode == expectedResponseCodes[i]) {
                    didReceiveExpectedResponseCode = true;
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("Received expected response code: " + didReceiveExpectedResponseCode);
                log.debug("  expected code(s): " + Arrays.toString(expectedResponseCodes) + ".");
            }

            if (!didReceiveExpectedResponseCode) {
                if (log.isDebugEnabled()) {
                    log.debug("Response xml: " + isXmlContentType(contentType));
                    log.debug("Response entity: " + response.getEntity());
                    log.debug("Response entity length: " + (response.getEntity() == null ? "??"
                            : "" + response.getEntity().getContentLength()));
                }

                if (response.getEntity() != null && response.getEntity().getContentLength() != 0) {
                    if (log.isDebugEnabled()) {
                        log.debug("Response '" + httpMethod.getURI().getRawPath()
                                + "' - Received error response with XML message");
                    }

                    StringBuilder sb = new StringBuilder();
                    BufferedReader reader = null;
                    try {
                        reader = new BufferedReader(
                                new InputStreamReader(new HttpMethodReleaseInputStream(response)));
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line).append("\n");
                        }
                    } finally {
                        if (reader != null) {
                            reader.close();
                        }
                    }

                    EntityUtils.consume(response.getEntity());

                    // Throw exception containing the XML message document.
                    ServiceException exception = new ServiceException("S3 Error Message.", sb.toString());

                    exception.setResponseCode(responseCode);
                    exception.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders()));
                    reqBean.setResponseInfo("http status: " + responseCode, exception.getErrorCode());
                    ilog.error(reqBean);
                    if ("RequestTimeout".equals(exception.getErrorCode())) {
                        int retryMaxCount = jets3tProperties.getIntProperty("httpclient.retry-max", 5);

                        if (requestTimeoutErrorCount < retryMaxCount) {
                            requestTimeoutErrorCount++;
                            if (log.isWarnEnabled()) {
                                log.warn("Retrying connection that failed with RequestTimeout error"
                                        + ", attempt number " + requestTimeoutErrorCount + " of "
                                        + retryMaxCount);
                            }
                            completedWithoutRecoverableError = false;
                        } else {
                            if (log.isErrorEnabled()) {
                                log.error("Exceeded maximum number of retries for RequestTimeout errors: "
                                        + retryMaxCount);
                            }
                            throw exception;
                        }
                    } else if ("RequestTimeTooSkewed".equals(exception.getErrorCode())) {
                        //                            this.timeOffset = RestUtils.getAWSTimeAdjustment();
                        if (log.isWarnEnabled()) {
                            log.warn("Adjusted time offset in response to RequestTimeTooSkewed error. "
                                    + "Local machine and S3 server disagree on the time by approximately "
                                    + (this.timeOffset / 1000) + " seconds. Retrying connection.");
                        }
                        completedWithoutRecoverableError = false;
                        throw new ServiceException("S3 Error Message.", sb.toString());
                    } else if (responseCode == 500 || responseCode == 503) {
                        // Retrying after 500 or 503 error, don't throw exception.
                    } else if (responseCode == 307) {
                        // Retrying after Temporary Redirect 307, don't throw exception.
                        if (log.isDebugEnabled()) {
                            log.debug("Following Temporary Redirect to: " + httpMethod.getURI().toString());
                        }
                    }

                    // Special handling for S3 object PUT failures causing NoSuchKey errors - Issue #85
                    else if (responseCode == 404 && "PUT".equalsIgnoreCase(httpMethod.getMethod())
                            && "NoSuchKey".equals(exception.getErrorCode())
                            // If PUT operation is trying to copy an existing source object, don't ignore 404
                            && httpMethod.getFirstHeader(getRestHeaderPrefix() + "copy-source") == null) {
                        // Retrying after mysterious PUT NoSuchKey error caused by S3, don't throw exception.
                        if (log.isDebugEnabled()) {
                            log.debug("Ignoring NoSuchKey/404 error on PUT to: "
                                    + httpMethod.getURI().toString());
                        }
                        completedWithoutRecoverableError = false;
                    }

                    else if ((responseCode == 403 || responseCode == 401)
                            && this.isRecoverable403(httpMethod, exception)) {
                        completedWithoutRecoverableError = false;
                        authFailureCount++;

                        if (authFailureCount > 1) {
                            throw new ServiceException("Exceeded 403 retry limit (1).");
                        }

                        if (log.isDebugEnabled()) {
                            log.debug("Retrying after 403 Forbidden");
                        }
                    }

                    else {
                        throw exception;
                    }
                } else {
                    reqBean.setResponseInfo("http status:" + responseCode, "-1");
                    ilog.error(reqBean);
                    // Consume response content and release connection.
                    String responseText = null;
                    byte[] responseBody = null;
                    if (response.getEntity() != null) {
                        responseBody = EntityUtils.toByteArray(response.getEntity());
                    }
                    if (responseBody != null && responseBody.length > 0) {
                        responseText = new String(responseBody);
                    }

                    if (log.isDebugEnabled()) {
                        log.debug("Releasing error response without XML content");
                    }
                    EntityUtils.consume(response.getEntity());

                    if (responseCode == 500 || responseCode == 503) {
                        // Retrying after InternalError 500, don't throw exception.
                    } else {
                        // Throw exception containing the HTTP error fields.
                        HttpException httpException = new HttpException(responseCode,
                                response.getStatusLine().getReasonPhrase());
                        ServiceException exception = new ServiceException(
                                "Request Error" + (responseText != null ? " [" + responseText + "]." : "."),
                                httpException);
                        reqBean.setResponseInfo(
                                "Request Error" + (responseText != null ? " [" + responseText + "]." : "."),
                                "-1");
                        ilog.error(reqBean);
                        exception.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders()));
                        throw exception;
                    }
                }

                // Print warning message if a non-fatal error occurred (we only reach this
                // point in the code if an exception isn't thrown above)
                if (log.isWarnEnabled()) {
                    String requestDescription = httpMethod.getMethod() + " '" + httpMethod.getURI().getPath()
                            + (httpMethod.getURI().getQuery() != null
                                    && httpMethod.getURI().getQuery().length() > 0
                                            ? "?" + httpMethod.getURI().getQuery()
                                            : "")
                            + "'" + " -- ResponseCode: " + responseCode + ", ResponseStatus: "
                            + response.getStatusLine().getReasonPhrase() + ", Request Headers: ["
                            + ServiceUtils.join(httpMethod.getAllHeaders(), ", ") + "]"
                            + ", Response Headers: [" + ServiceUtils.join(response.getAllHeaders(), ", ") + "]";
                    requestDescription = requestDescription.replaceAll("[\\n\\r\\f]", ""); // Remove any newlines.
                    log.warn("Error Response: " + requestDescription);
                }
            }
        } while (!completedWithoutRecoverableError);
    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            String msg = "Rethrowing as a ServiceException error in performRequest: " + t;
            if (t.getCause() != null) {
                msg += ", with cause: " + t.getCause();
            }
            if (log.isTraceEnabled()) {
                log.trace(msg, t);
            } else {
                log.debug(msg);
            }
        }
        if (log.isDebugEnabled() && !shuttingDown) {
            log.debug("Releasing HttpClient connection after error: " + t.getMessage());
        }
        httpMethod.abort();

        ServiceException serviceException;
        if (t instanceof ServiceException) {
            serviceException = (ServiceException) t;
        } else {
            MxDelegate.getInstance().registerS3ServiceExceptionEvent();
            serviceException = new ServiceException("Request Error: " + t, t);
        }

        // Add S3 request and host IDs from HTTP headers to exception, if they are available
        // and have not already been populated by parsing an XML error response.
        if (!serviceException.isParsedFromXmlMessage() && response != null
                && response.getFirstHeader(Constants.AMZ_REQUEST_ID_1) != null
                && response.getFirstHeader(Constants.AMZ_REQUEST_ID_2) != null) {
            serviceException.setRequestAndHostIds(
                    response.getFirstHeader(Constants.AMZ_REQUEST_ID_1).getValue(),
                    response.getFirstHeader(Constants.AMZ_REQUEST_ID_2).getValue());
            serviceException.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders()));
        }
        if (response != null) {
            try {
                serviceException.setResponseCode(response.getStatusLine().getStatusCode());
                serviceException.setResponseStatus(response.getStatusLine().getReasonPhrase());
            } catch (NullPointerException e) {
                // If no network connection is available, status info is not available
            }
        }
        if (httpMethod.getFirstHeader("Host") != null) {
            serviceException.setRequestHost(httpMethod.getFirstHeader("Host").getValue());
        }
        if (response != null && response.getFirstHeader("Date") != null) {
            serviceException.setResponseDate(response.getFirstHeader("Date").getValue());
        }
        reqBean.setResponseInfo(serviceException.getErrorMessage(), serviceException.getErrorCode());
        throw serviceException;
    }
    reqBean.setRespTime(new Date());
    reqBean.setTargetAddr(getEndpoint());
    reqBean.setResultCode("0");
    ilog.info(reqBean);
    return response;
}

From source file:org.fcrepo.integration.http.api.AbstractResourceIT.java

/**
 * Executes an HTTP request and parses the RDF found in the response, returning it in a
 * {@link CloseableDataset}, then closes the response.
 *
 * @param client the client to use//w w  w  . j av a2  s . c  o  m
 * @param req the request to execute
 * @return the graph retrieved
 * @throws IOException in case of IOException
 */
private CloseableDataset getDataset(final CloseableHttpClient client, final HttpUriRequest req)
        throws IOException {
    if (!req.containsHeader(ACCEPT)) {
        req.addHeader(ACCEPT, "application/n-triples");
    }
    logger.debug("Retrieving RDF using mimeType: {}", req.getFirstHeader(ACCEPT));

    try (final CloseableHttpResponse response = client.execute(req)) {
        assertEquals(OK.getStatusCode(), response.getStatusLine().getStatusCode());
        final CloseableDataset result = parseTriples(response.getEntity());
        logger.trace("Retrieved RDF: {}", result);
        return result;
    }

}

From source file:org.flowable.app.rest.service.BaseSpringRestTestCase.java

protected CloseableHttpResponse internalExecuteRequest(HttpUriRequest request, int expectedStatusCode,
        boolean addJsonContentType) {
    CloseableHttpResponse response = null;
    try {/*from  w  w w  .  jav  a2 s .co  m*/
        if (addJsonContentType && request.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
            // Revert to default content-type
            request.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
        }
        response = client.execute(request);
        Assert.assertNotNull(response.getStatusLine());

        int responseStatusCode = response.getStatusLine().getStatusCode();
        if (expectedStatusCode != responseStatusCode) {
            LOGGER.info("Wrong status code : {}, but should be {}", responseStatusCode, expectedStatusCode);
            if (response.getEntity() != null) {
                LOGGER.info("Response body: {}", IOUtils.toString(response.getEntity().getContent(), "utf-8"));
            }
        }

        Assert.assertEquals(expectedStatusCode, responseStatusCode);
        httpResponses.add(response);
        return response;

    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.flowable.cmmn.rest.service.BaseSpringRestTestCase.java

protected CloseableHttpResponse internalExecuteRequest(HttpUriRequest request, int expectedStatusCode,
        boolean addJsonContentType) {
    try {/* w  w w . j a  v a 2  s  .  co  m*/
        if (addJsonContentType && request.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
            // Revert to default content-type
            request.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
        }
        CloseableHttpResponse response = client.execute(request);
        Assert.assertNotNull(response.getStatusLine());

        int responseStatusCode = response.getStatusLine().getStatusCode();
        if (expectedStatusCode != responseStatusCode) {
            LOGGER.info("Wrong status code : {}, but should be {}", responseStatusCode, expectedStatusCode);
            if (response.getEntity() != null) {
                LOGGER.info("Response body: {}", IOUtils.toString(response.getEntity().getContent(), "utf-8"));
            }
        }

        Assert.assertEquals(expectedStatusCode, responseStatusCode);
        httpResponses.add(response);
        return response;

    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.flowable.content.rest.service.api.BaseSpringContentRestTestCase.java

protected CloseableHttpResponse internalExecuteRequest(HttpUriRequest request, int expectedStatusCode,
        boolean addJsonContentType) {
    CloseableHttpResponse response = null;
    try {/*w w  w  . j av a 2s.c  o  m*/
        if (addJsonContentType && request.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
            // Revert to default content-type
            request.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
        }
        response = client.execute(request);
        Assert.assertNotNull(response.getStatusLine());

        int responseStatusCode = response.getStatusLine().getStatusCode();
        if (expectedStatusCode != responseStatusCode) {
            LOGGER.info("Wrong status code : {}, but should be {}", responseStatusCode, expectedStatusCode);
            LOGGER.info("Response body: {}", IOUtils.toString(response.getEntity().getContent()));
        }

        Assert.assertEquals(expectedStatusCode, responseStatusCode);
        httpResponses.add(response);
        return response;

    } catch (ClientProtocolException e) {
        Assert.fail(e.getMessage());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    return null;
}