org.apache.commons.httpclient.HttpMethodRetryHandler Java Examples

The following examples show how to use org.apache.commons.httpclient.HttpMethodRetryHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpClientFactory.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
public HttpClient create() {
  HttpClient client = new HttpClient();
  if (soTimeout >= 0) {
    client.getParams().setSoTimeout(soTimeout);
  }

  ClassAliasResolver<HttpMethodRetryHandler> aliasResolver = new ClassAliasResolver<>(HttpMethodRetryHandler.class);
  HttpMethodRetryHandler httpMethodRetryHandler;
  try {
    httpMethodRetryHandler = GobblinConstructorUtils.invokeLongestConstructor(aliasResolver.resolveClass(httpMethodRetryHandlerClass), httpMethodRetryCount, httpRequestSentRetryEnabled);
  } catch (ReflectiveOperationException e) {
    throw new RuntimeException(e);
  }
  client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, httpMethodRetryHandler);

  if (connTimeout >= 0) {
    client.getHttpConnectionManager().getParams().setConnectionTimeout(connTimeout);
  }

  return client;
}
 
Example #2
Source File: HttpTemplateDownloader.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private HttpMethodRetryHandler createRetryTwiceHandler() {
    return new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= 2) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    };
}
 
Example #3
Source File: MetalinkTemplateDownloader.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
protected HttpMethodRetryHandler createRetryTwiceHandler() {
    return new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= 2) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    };
}
 
Example #4
Source File: HTTPUtils.java    From cosmic with Apache License 2.0 5 votes vote down vote up
/**
 * @return A HttpMethodRetryHandler with given number of retries.
 */
public static HttpMethodRetryHandler getHttpMethodRetryHandler(final int retryCount) {

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Initializing new HttpMethodRetryHandler with retry count " + retryCount);
    }

    return new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(final HttpMethod method, final IOException exception, final int executionCount) {
            if (executionCount >= retryCount) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    };
}
 
Example #5
Source File: HTTPUtils.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * @return A HttpMethodRetryHandler with given number of retries.
 */
public static HttpMethodRetryHandler getHttpMethodRetryHandler(final int retryCount) {

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Initializing new HttpMethodRetryHandler with retry count " + retryCount);
    }

    return new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= retryCount) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    };
}
 
Example #6
Source File: Proxy.java    From odo with Apache License 2.0 4 votes vote down vote up
/**
 * Execute a request
 *
 * @param httpMethodProxyRequest
 * @param httpServletRequest
 * @param httpServletResponse
 * @param history
 * @throws Exception
 */
private void executeRequest(HttpMethod httpMethodProxyRequest,
                            HttpServletRequest httpServletRequest,
                            PluginResponse httpServletResponse,
                            History history) throws Exception {
    int intProxyResponseCode = 999;
    // Create a default HttpClient
    HttpClient httpClient = new HttpClient();
    HttpState state = new HttpState();

    try {
        httpMethodProxyRequest.setFollowRedirects(false);
        ArrayList<String> headersToRemove = getRemoveHeaders();

        httpClient.getParams().setSoTimeout(60000);

        httpServletRequest.setAttribute("com.groupon.odo.removeHeaders", headersToRemove);

        // exception handling for httpclient
        HttpMethodRetryHandler noretryhandler = new HttpMethodRetryHandler() {
            public boolean retryMethod(
                final HttpMethod method,
                final IOException exception,
                int executionCount) {
                return false;
            }
        };

        httpMethodProxyRequest.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, noretryhandler);

        intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest.getHostConfiguration(), httpMethodProxyRequest, state);
    } catch (Exception e) {
        // Return a gateway timeout
        httpServletResponse.setStatus(504);
        httpServletResponse.setHeader(Constants.HEADER_STATUS, "504");
        httpServletResponse.flushBuffer();
        return;
    }
    logger.info("Response code: {}, {}", intProxyResponseCode,
                HttpUtilities.getURL(httpMethodProxyRequest.getURI().toString()));

    // Pass the response code back to the client
    httpServletResponse.setStatus(intProxyResponseCode);

    // Pass response headers back to the client
    Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders();
    for (Header header : headerArrayResponse) {
        // remove transfer-encoding header.  The http libraries will handle this encoding
        if (header.getName().toLowerCase().equals("transfer-encoding")) {
            continue;
        }

        httpServletResponse.setHeader(header.getName(), header.getValue());
    }

    // there is no data for a HTTP 304 or 204
    if (intProxyResponseCode != HttpServletResponse.SC_NOT_MODIFIED &&
        intProxyResponseCode != HttpServletResponse.SC_NO_CONTENT) {
        // Send the content to the client
        httpServletResponse.resetBuffer();
        httpServletResponse.getOutputStream().write(httpMethodProxyRequest.getResponseBody());
    }

    // copy cookies to servlet response
    for (Cookie cookie : state.getCookies()) {
        javax.servlet.http.Cookie servletCookie = new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue());

        if (cookie.getPath() != null) {
            servletCookie.setPath(cookie.getPath());
        }

        if (cookie.getDomain() != null) {
            servletCookie.setDomain(cookie.getDomain());
        }

        // convert expiry date to max age
        if (cookie.getExpiryDate() != null) {
            servletCookie.setMaxAge((int) ((cookie.getExpiryDate().getTime() - System.currentTimeMillis()) / 1000));
        }

        servletCookie.setSecure(cookie.getSecure());

        servletCookie.setVersion(cookie.getVersion());

        if (cookie.getComment() != null) {
            servletCookie.setComment(cookie.getComment());
        }

        httpServletResponse.addCookie(servletCookie);
    }
}
 
Example #7
Source File: HttpClientFeedFetcher.java    From rome with Apache License 2.0 4 votes vote down vote up
public synchronized void setRetryHandler(final HttpMethodRetryHandler handler) {
    httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, handler);
}