Example usage for org.apache.http.impl.client DefaultHttpClient setHttpRequestRetryHandler

List of usage examples for org.apache.http.impl.client DefaultHttpClient setHttpRequestRetryHandler

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient setHttpRequestRetryHandler.

Prototype

public synchronized void setHttpRequestRetryHandler(final HttpRequestRetryHandler handler) 

Source Link

Usage

From source file:org.fcrepo.indexer.IndexerGroup.java

@VisibleForTesting
protected DefaultHttpClient httpClient(final String repositoryURL) {
    // try to find existing client
    if (clients.size() > 0) {
        for (final Iterator<String> it = clients.keySet().iterator(); it.hasNext();) {
            final String base = it.next();
            if (repositoryURL.startsWith(base)) {
                return clients.get(base);
            }//www  .  jav a 2 s . c  o m
        }
    }

    if (defaultClient != null) {
        return defaultClient;
    }

    // if no existing client matched, create a new one
    final String baseURL;
    if (repositoryURL.indexOf(REST_PREFIX) > 0) {
        baseURL = repositoryURL.substring(0, repositoryURL.indexOf(REST_PREFIX) + REST_PREFIX.length());
    } else if (repositoryURL.indexOf("/", FCREPO_PREFIX.length()) > 0) {
        baseURL = repositoryURL.substring(0, repositoryURL.indexOf("/", FCREPO_PREFIX.length()) + 1);
    } else {
        baseURL = repositoryURL;
    }

    final PoolingClientConnectionManager connMann = new PoolingClientConnectionManager();
    connMann.setMaxTotal(MAX_VALUE);
    connMann.setDefaultMaxPerRoute(MAX_VALUE);

    final DefaultHttpClient httpClient = new DefaultHttpClient(connMann);
    httpClient.setRedirectStrategy(new DefaultRedirectStrategy());
    httpClient.setHttpRequestRetryHandler(new StandardHttpRequestRetryHandler(0, false));

    // If the Fedora instance requires authentication, set it up here
    if (!isBlank(fedoraUsername) && !isBlank(fedoraPassword)) {
        LOGGER.debug("Adding BASIC credentials to client for repo requests.");

        final URI fedoraUri = URI.create(baseURL);
        final CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(fedoraUri.getHost(), fedoraUri.getPort()),
                new UsernamePasswordCredentials(fedoraUsername, fedoraPassword));

        httpClient.setCredentialsProvider(credsProvider);
    }
    clients.put(baseURL, httpClient);
    return httpClient;
}

From source file:org.hyperic.hq.hqapi1.HQConnection.java

private <T> T runMethod(HttpRequestBase method, String uri, ResponseHandler<T> responseHandler)
        throws IOException {
    String protocol = _isSecure ? "https" : "http";
    ServiceError error;/* ww  w  . j a va2 s  . co  m*/
    URL url = new URL(protocol, _host, _port, uri);

    try {
        method.setURI(url.toURI());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("The syntax of request url [" + uri + "] is invalid", e);
    }

    _log.debug("Setting URI: " + url.toString());

    DefaultHttpClient client = new DefaultHttpClient();

    if (_isSecure) {
        // To allow for self signed certificates
        configureSSL(client);
    }

    // Validate user & password inputs
    if (_user == null || _user.length() == 0) {
        error = new ServiceError();
        error.setErrorCode("LoginFailure");
        error.setReasonText("User name cannot be null or empty");

        return responseHandler.getErrorResponse(error);
    }

    if (_password == null || _password.length() == 0) {
        error = new ServiceError();
        error.setErrorCode("LoginFailure");
        error.setReasonText("Password cannot be null or empty");

        return responseHandler.getErrorResponse(error);
    }

    // Set Basic auth creds
    UsernamePasswordCredentials defaultcreds = new UsernamePasswordCredentials(_user, _password);

    client.getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds);

    // Preemptive authentication
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    HttpHost host = new HttpHost(_host, _port, protocol);

    authCache.put(host, basicAuth);

    BasicHttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);

    method.getParams().setParameter(ClientPNames.HANDLE_AUTHENTICATION, true);

    // Disable re-tries
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, true));

    HttpResponse response = client.execute(method, localContext);

    return responseHandler.handleResponse(response);
}

From source file:org.mule.modules.quickbooks.api.AbstractQuickBooksClientOAuth.java

public static DefaultHttpClient getThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = client.getParams();

    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);
    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        @Override//w w w  .j  av a 2 s  .  c o  m
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount > 3) {
                LOGGER.warn("Maximum tries reached for client http pool ");
                return false;
            }
            if (exception instanceof org.apache.http.NoHttpResponseException) {
                LOGGER.warn("No response from server on " + executionCount + " call");
                return true;
            }
            return false;
        }
    });

    return client;
}