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

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

Introduction

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

Prototype

void abort() throws UnsupportedOperationException;

Source Link

Document

Aborts execution of the request.

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//from  w  w  w  .ja v  a  2  s  .  c  o  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.codelibs.robot.client.http.HcHttpClient.java

protected ResponseData processHttpMethod(final String url, final HttpUriRequest httpRequest) {
    try {/*from www. ja v a2  s  .  com*/
        processRobotsTxt(url);
    } catch (final RobotCrawlAccessException e) {
        if (logger.isInfoEnabled()) {
            final StringBuilder buf = new StringBuilder();
            buf.append(e.getMessage());
            if (e.getCause() != null) {
                buf.append(e.getCause().getMessage());
            }
            logger.info(buf.toString());
        } else if (logger.isDebugEnabled()) {
            logger.debug("Crawling Access Exception at " + url, e);
        }
    }

    // request header
    for (final Header header : requestHeaderList) {
        httpRequest.addHeader(header);
    }

    ResponseData responseData = null;
    InputStream inputStream = null;
    HttpEntity httpEntity = null;
    try {
        // get a content
        final HttpResponse response = executeHttpClient(httpRequest);
        httpEntity = response.getEntity();

        final int httpStatusCode = response.getStatusLine().getStatusCode();
        // redirect
        if (isRedirectHttpStatus(httpStatusCode)) {
            final Header locationHeader = response.getFirstHeader("location");
            if (locationHeader == null) {
                logger.warn("Invalid redirect location at " + url);
            } else {
                responseData = new ResponseData();
                responseData.setRedirectLocation(locationHeader.getValue());
                return responseData;
            }
        }

        long contentLength = 0;
        String contentEncoding = Constants.UTF_8;
        if (httpEntity == null) {
            inputStream = new ByteArrayInputStream(new byte[0]);
        } else {
            final InputStream responseBodyStream = httpEntity.getContent();
            final File outputFile = File.createTempFile("s2robot-HcHttpClient-", ".out");
            DeferredFileOutputStream dfos = null;
            try {
                try {
                    dfos = new DeferredFileOutputStream(responseBodyInMemoryThresholdSize, outputFile);
                    CopyUtil.copy(responseBodyStream, dfos);
                    dfos.flush();
                } finally {
                    IOUtils.closeQuietly(dfos);
                }
            } catch (final Exception e) {
                if (!outputFile.delete()) {
                    logger.warn("Could not delete " + outputFile.getAbsolutePath());
                }
                throw e;
            }

            if (dfos.isInMemory()) {
                inputStream = new ByteArrayInputStream(dfos.getData());
                contentLength = dfos.getData().length;
                if (!outputFile.delete()) {
                    logger.warn("Could not delete " + outputFile.getAbsolutePath());
                }
            } else {
                inputStream = new TemporaryFileInputStream(outputFile);
                contentLength = outputFile.length();
            }

            final Header contentEncodingHeader = httpEntity.getContentEncoding();
            if (contentEncodingHeader != null) {
                contentEncoding = contentEncodingHeader.getValue();
            }
        }

        String contentType = null;
        final Header contentTypeHeader = response.getFirstHeader("Content-Type");
        if (contentTypeHeader != null) {
            contentType = contentTypeHeader.getValue();
            final int idx = contentType.indexOf(';');
            if (idx > 0) {
                contentType = contentType.substring(0, idx);
            }
        }

        // check file size
        if (contentLengthHelper != null) {
            final long maxLength = contentLengthHelper.getMaxLength(contentType);
            if (contentLength > maxLength) {
                throw new MaxLengthExceededException("The content length (" + contentLength + " byte) is over "
                        + maxLength + " byte. The url is " + url);
            }
        }

        responseData = new ResponseData();
        responseData.setUrl(url);
        responseData.setCharSet(contentEncoding);
        if (httpRequest instanceof HttpHead) {
            responseData.setMethod(Constants.HEAD_METHOD);
        } else {
            responseData.setMethod(Constants.GET_METHOD);
        }
        responseData.setResponseBody(inputStream);
        responseData.setHttpStatusCode(httpStatusCode);
        for (final Header header : response.getAllHeaders()) {
            responseData.addMetaData(header.getName(), header.getValue());
        }
        if (contentType == null) {
            responseData.setMimeType(defaultMimeType);
        } else {
            responseData.setMimeType(contentType);
        }
        final Header contentLengthHeader = response.getFirstHeader("Content-Length");
        if (contentLengthHeader == null) {
            responseData.setContentLength(contentLength);
        } else {
            final String value = contentLengthHeader.getValue();
            try {
                responseData.setContentLength(Long.parseLong(value));
            } catch (final Exception e) {
                responseData.setContentLength(contentLength);
            }
        }
        final Header lastModifiedHeader = response.getFirstHeader("Last-Modified");
        if (lastModifiedHeader != null) {
            final String value = lastModifiedHeader.getValue();
            if (StringUtil.isNotBlank(value)) {
                final Date d = parseLastModified(value);
                if (d != null) {
                    responseData.setLastModified(d);
                }
            }
        }

        return responseData;
    } catch (final UnknownHostException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("Unknown host(" + e.getMessage() + "): " + url, e);
    } catch (final NoRouteToHostException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("No route to host(" + e.getMessage() + "): " + url, e);
    } catch (final ConnectException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("Connection time out(" + e.getMessage() + "): " + url, e);
    } catch (final SocketException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("Socket exception(" + e.getMessage() + "): " + url, e);
    } catch (final IOException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("I/O exception(" + e.getMessage() + "): " + url, e);
    } catch (final RobotSystemException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw e;
    } catch (final Exception e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotSystemException("Failed to access " + url, e);
    } finally {
        EntityUtils.consumeQuietly(httpEntity);
    }
}

From source file:org.pixmob.droidlink.net.NetworkClient.java

private static void closeResources(HttpUriRequest request, HttpResponse response) {
    try {//from   w w  w.j  a v  a2 s.c o  m
        request.abort();
    } catch (UnsupportedOperationException ignore) {
    }
    if (response != null) {
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            try {
                entity.consumeContent();
            } catch (IOException ignore) {
            }
        }
    }
}