List of usage examples for org.apache.http.impl.client DefaultHttpRequestRetryHandler DefaultHttpRequestRetryHandler
@SuppressWarnings("unchecked") public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled)
From source file:org.pixmob.appengine.client.AppEngineClient.java
/** * Set how many times a connection is retried in case of a network error. * @param retryCount positive integer//from ww w .j a v a 2s. c o m */ public void setRetryCount(int retryCount) { if (retryCount < 0) { throw new IllegalArgumentException("Invalid retry count: " + retryCount); } final HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(retryCount, true); loginClient.setHttpRequestRetryHandler(retryHandler); if (delegateWasSet) { final DefaultHttpClient client = (DefaultHttpClient) delegate; client.setHttpRequestRetryHandler(retryHandler); } }
From source file:anhttpclient.impl.DefaultWebBrowser.java
/** * Makes default initialization of HttpMethodBase before any request * such as cookie policy, retrycount, timeout * * @param httpMethodBase {@link HttpRequestBase} for making default initialization *///from w w w .j av a 2 s . co m private void setDefaultMethodParams(final HttpRequestBase httpMethodBase) { httpMethodBase.getParams().setBooleanParameter(CookieSpecPNames.SINGLE_COOKIE_HEADER, true); httpMethodBase.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // We use here DefaultHttpMethodRetryHandler with <b>true</b> parameter // because we suppose that if method was successfully sent its headers // it could also be retried if (AbstractHttpClient.class.isAssignableFrom(httpClient.getClass())) { ((AbstractHttpClient) httpClient) .setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, true)); } httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout); }
From source file:se.skltp.adapterservices.druglogistics.dosdispensing.RetryComponent.java
private HttpClient setupHttpClient() { HttpClientBuilder httpClientBuilder = HttpClients.custom() .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); if (endpointAddress.toLowerCase().startsWith("https")) { httpClientBuilder = httpClientBuilder.setConnectionManager(setupSSLConnectionManager()); }/*ww w . j a va 2 s . c o m*/ HttpClient httpclient = httpClientBuilder.build(); return httpclient; }
From source file:org.frontcache.FrontCacheEngine.java
private CloseableHttpClient newClient() { final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(5500) // should be slightly more then hystrix timeout for http client .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override/*from w w w . j a va 2 s.com*/ public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } return 10 * 1000; } }; return HttpClients.custom().setConnectionManager(newConnectionManager()) .setDefaultRequestConfig(requestConfig) // .setSSLHostnameVerifier(new NoopHostnameVerifier()) // for SSL do not verify certificate's host .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)) .setKeepAliveStrategy(keepAliveStrategy).setRedirectStrategy(new RedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return false; } @Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return null; } }).build(); }
From source file:com.bt.download.android.core.HttpFetcher.java
private static HttpClient setupHttpClient(boolean gzip) { SSLSocketFactory.getSocketFactory().setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); BasicHttpParams params = new BasicHttpParams(); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(20)); params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 200); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); DefaultHttpClient httpClient = new DefaultHttpClient(cm, new BasicHttpParams()); httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_RETRY_COUNT, true)); if (gzip) {//from www. j a va 2 s. c om httpClient.addRequestInterceptor(new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } }); } return httpClient; }
From source file:com.yahoo.validatar.execution.rest.JSON.java
/** * Creates a HttpClient to use for making requests. * * @param metadata The map containing the configuration for this client. * @return The created HttpClient object. *///from w ww .java 2 s. com private HttpClient createClient(Map<String, String> metadata) { int timeout = Integer.valueOf(metadata.getOrDefault(METADATA_TIMEOUT_KEY, String.valueOf(defaultTimeout))); int retries = Integer.valueOf(metadata.getOrDefault(METADATA_RETRY_KEY, String.valueOf(defaultRetries))); RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build(); return HttpClientBuilder.create().setDefaultRequestConfig(config) .setRetryHandler(new DefaultHttpRequestRetryHandler(retries, false)).build(); }
From source file:com.nesscomputing.httpclient.factory.httpclient4.ApacheHttpClient4Factory.java
private <T> T executeRequest(final HttpRequestBase httpRequest, final HttpClientRequest<T> httpClientRequest) throws IOException { final DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, setFollowRedirects(params, httpClientRequest)); httpClient.getCookieSpecs().register(NessCookieSpecFactory.NESS_COOKIE_POLICY, new NessCookieSpecFactory()); httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(retries, false)); contributeCookies(httpClient, httpClientRequest); contributeParameters(httpClient, httpRequest, httpClientRequest); contributeHeaders(httpRequest, httpClientRequest); contributeVirtualHost(httpRequest, httpClientRequest); contributeAuthentication(httpClient, httpClientRequest); try {// ww w .j av a 2 s . co m final HttpContext httpContext = new BasicHttpContext(); final HttpResponse httpResponse = httpClient.execute(httpRequest, httpContext); final HttpClientResponseHandler<T> responseHandler = httpClientRequest.getHttpHandler(); try { final HttpClientResponse internalResponse = new InternalResponse(httpRequest, httpResponse); HttpClientResponse response = internalResponse; if (CollectionUtils.isNotEmpty(httpClientObservers)) { LOG.trace("Executing Observers"); for (HttpClientObserver observer : httpClientObservers) { response = observer.onResponseReceived(response); } if (response != internalResponse) { LOG.trace("Response was modified by Observers!"); } } if (responseHandler != null) { LOG.trace("Executing Response Handler"); return responseHandler.handle(response); } else { LOG.debug("No response handler found, discarding response."); return null; } } finally { // Make sure that the content has definitely been consumed. Otherwise, // keep-alive does not work. EntityUtils.consume(httpResponse.getEntity()); } } catch (IOException ioe) { LOG.debug(ioe, "Aborting Request!"); httpRequest.abort(); throw ioe; } catch (RuntimeException re) { LOG.debug(re, "Aborting Request!"); httpRequest.abort(); throw re; } }