Example usage for org.apache.http.client NonRepeatableRequestException NonRepeatableRequestException

List of usage examples for org.apache.http.client NonRepeatableRequestException NonRepeatableRequestException

Introduction

In this page you can find the example usage for org.apache.http.client NonRepeatableRequestException NonRepeatableRequestException.

Prototype

public NonRepeatableRequestException(final String message, final Throwable cause) 

Source Link

Document

Creates a new NonRepeatableEntityException with the specified detail message.

Usage

From source file:org.vietspider.net.apache.DefaultRequestDirector.java

/**
 * Execute request and retry in case of a recoverable I/O failure
 *//*from  w  ww  .  j av  a 2 s.  c om*/
private HttpResponse tryExecute(final RoutedRequest req, final HttpContext context)
        throws HttpException, IOException {
    RequestWrapper wrapper = req.getRequest();
    HttpRoute route = req.getRoute();
    HttpResponse response = null;

    boolean retrying = true;
    Exception retryReason = null;
    while (retrying) {
        // Increment total exec count (with redirects)
        execCount++;
        // Increment exec count for this particular request
        wrapper.incrementExecCount();
        if (!wrapper.isRepeatable()) {
            this.log.debug("Cannot retry non-repeatable request");
            if (retryReason != null) {
                throw new NonRepeatableRequestException(
                        "Cannot retry request " + "with a non-repeatable request entity.  The cause lists the "
                                + "reason the original request failed.",
                        retryReason);
            } else {
                throw new NonRepeatableRequestException(
                        "Cannot retry request " + "with a non-repeatable request entity.");
            }
        }

        try {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Attempt " + execCount + " to execute request");
            }
            response = requestExec.execute(wrapper, managedConn, context);
            retrying = false;

        } catch (IOException ex) {
            this.log.debug("Closing the connection.");
            try {
                managedConn.close();
            } catch (IOException ignore) {
            }
            if (retryHandler.retryRequest(ex, wrapper.getExecCount(), context)) {
                if (this.log.isInfoEnabled()) {
                    this.log.info("I/O exception (" + ex.getClass().getName()
                            + ") caught when processing request: " + ex.getMessage());
                }
                if (this.log.isDebugEnabled()) {
                    this.log.debug(ex.getMessage(), ex);
                }
                this.log.info("Retrying request");
                retryReason = ex;
            } else {
                throw ex;
            }

            // If we have a direct route to the target host
            // just re-open connection and re-try the request
            if (!route.isTunnelled()) {
                this.log.debug("Reopening the direct connection.");
                managedConn.open(route, context, params);
            } else {
                // otherwise give up
                this.log.debug("Proxied connection. Need to start over.");
                retrying = false;
            }

        }
    }
    return response;
}

From source file:org.apache.http.impl.client.DefaultRequestDirector.java

/**
 * Execute request and retry in case of a recoverable I/O failure
 *//*from  w w  w .j  a  v  a 2  s. c  om*/
private HttpResponse tryExecute(final RoutedRequest req, final HttpContext context)
        throws HttpException, IOException {
    final RequestWrapper wrapper = req.getRequest();
    final HttpRoute route = req.getRoute();
    HttpResponse response = null;

    Exception retryReason = null;
    for (;;) {
        // Increment total exec count (with redirects)
        execCount++;
        // Increment exec count for this particular request
        wrapper.incrementExecCount();
        if (!wrapper.isRepeatable()) {
            this.log.debug("Cannot retry non-repeatable request");
            if (retryReason != null) {
                throw new NonRepeatableRequestException(
                        "Cannot retry request " + "with a non-repeatable request entity.  The cause lists the "
                                + "reason the original request failed.",
                        retryReason);
            } else {
                throw new NonRepeatableRequestException(
                        "Cannot retry request " + "with a non-repeatable request entity.");
            }
        }

        try {
            if (!managedConn.isOpen()) {
                // If we have a direct route to the target host
                // just re-open connection and re-try the request
                if (!route.isTunnelled()) {
                    this.log.debug("Reopening the direct connection.");
                    managedConn.open(route, context, params);
                } else {
                    // otherwise give up
                    this.log.debug("Proxied connection. Need to start over.");
                    break;
                }
            }

            if (this.log.isDebugEnabled()) {
                this.log.debug("Attempt " + execCount + " to execute request");
            }
            response = requestExec.execute(wrapper, managedConn, context);
            break;

        } catch (final IOException ex) {
            this.log.debug("Closing the connection.");
            try {
                managedConn.close();
            } catch (final IOException ignore) {
            }
            if (retryHandler.retryRequest(ex, wrapper.getExecCount(), context)) {
                if (this.log.isInfoEnabled()) {
                    this.log.info("I/O exception (" + ex.getClass().getName()
                            + ") caught when processing request to " + route + ": " + ex.getMessage());
                }
                if (this.log.isDebugEnabled()) {
                    this.log.debug(ex.getMessage(), ex);
                }
                if (this.log.isInfoEnabled()) {
                    this.log.info("Retrying request to " + route);
                }
                retryReason = ex;
            } else {
                if (ex instanceof NoHttpResponseException) {
                    final NoHttpResponseException updatedex = new NoHttpResponseException(
                            route.getTargetHost().toHostString() + " failed to respond");
                    updatedex.setStackTrace(ex.getStackTrace());
                    throw updatedex;
                } else {
                    throw ex;
                }
            }
        }
    }
    return response;
}

From source file:org.apache.http.impl.client.StatiscicsLoggingRequestDirector.java

/**
 * Execute request and retry in case of a recoverable I/O failure
 *///from  w  w  w  . j a v  a 2 s. c  om
private HttpResponse tryExecute(final RoutedRequest req, final HttpContext context)
        throws HttpException, IOException {
    RequestWrapper wrapper = req.getRequest();
    HttpRoute route = req.getRoute();
    HttpResponse response = null;

    Exception retryReason = null;
    for (;;) {
        // Increment total exec count (with redirects)
        execCount++;
        // Increment exec count for this particular request
        wrapper.incrementExecCount();
        if (!wrapper.isRepeatable()) {
            this.log.debug("Cannot retry non-repeatable request");
            if (retryReason != null) {
                throw new NonRepeatableRequestException(
                        "Cannot retry request " + "with a non-repeatable request entity.  The cause lists the "
                                + "reason the original request failed.",
                        retryReason);
            } else {
                throw new NonRepeatableRequestException(
                        "Cannot retry request " + "with a non-repeatable request entity.");
            }
        }

        try {
            if (!managedConn.isOpen()) {
                // If we have a direct route to the target host
                // just re-open connection and re-try the request
                if (!route.isTunnelled()) {
                    this.log.debug("Reopening the direct connection.");
                    managedConn.open(route, context, params);
                } else {
                    // otherwise give up
                    this.log.debug("Proxied connection. Need to start over.");
                    break;
                }
            }

            if (this.log.isDebugEnabled()) {
                this.log.debug("Attempt " + execCount + " to execute request");
            }
            response = requestExec.execute(wrapper, managedConn, context);
            break;

        } catch (IOException ex) {
            this.log.debug("Closing the connection.");
            try {
                managedConn.close();
            } catch (IOException ignore) {
            }
            if (retryHandler.retryRequest(ex, wrapper.getExecCount(), context)) {
                if (this.log.isInfoEnabled()) {
                    this.log.info("I/O exception (" + ex.getClass().getName()
                            + ") caught when processing request: " + ex.getMessage());
                }
                if (this.log.isDebugEnabled()) {
                    this.log.debug(ex.getMessage(), ex);
                }
                this.log.info("Retrying request");
                retryReason = ex;
            } else {
                throw ex;
            }
        }
    }
    return response;
}

From source file:org.apache.http.impl.execchain.RetryExec.java

public CloseableHttpResponse execute(final HttpRoute route, final HttpRequestWrapper request,
        final HttpClientContext context, final HttpExecutionAware execAware) throws IOException, HttpException {
    Args.notNull(route, "HTTP route");
    Args.notNull(request, "HTTP request");
    Args.notNull(context, "HTTP context");
    final Header[] origheaders = request.getAllHeaders();
    for (int execCount = 1;; execCount++) {
        try {/* ww w  .  j ava  2  s  .  c  o  m*/
            return this.requestExecutor.execute(route, request, context, execAware);
        } catch (final IOException ex) {
            if (execAware != null && execAware.isAborted()) {
                this.log.debug("Request has been aborted");
                throw ex;
            }
            if (retryHandler.retryRequest(ex, execCount, context)) {
                if (this.log.isInfoEnabled()) {
                    this.log.info("I/O exception (" + ex.getClass().getName()
                            + ") caught when processing request to " + route + ": " + ex.getMessage());
                }
                if (this.log.isDebugEnabled()) {
                    this.log.debug(ex.getMessage(), ex);
                }
                if (!Proxies.isRepeatable(request)) {
                    this.log.debug("Cannot retry non-repeatable request");
                    throw new NonRepeatableRequestException(
                            "Cannot retry request " + "with a non-repeatable request entity", ex);
                }
                request.setHeaders(origheaders);
                if (this.log.isInfoEnabled()) {
                    this.log.info("Retrying request to " + route);
                }
            } else {
                if (ex instanceof NoHttpResponseException) {
                    final NoHttpResponseException updatedex = new NoHttpResponseException(
                            route.getTargetHost().toHostString() + " failed to respond");
                    updatedex.setStackTrace(ex.getStackTrace());
                    throw updatedex;
                } else {
                    throw ex;
                }
            }
        }
    }
}