Java Code Examples for org.apache.commons.httpclient.params.HttpClientParams#setConnectionManagerTimeout()

The following examples show how to use org.apache.commons.httpclient.params.HttpClientParams#setConnectionManagerTimeout() . 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: MultiGetRequest.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * GET urls in parallel using the executor service.
 * @param urls absolute URLs to GET
 * @param timeoutMs timeout in milliseconds for each GET request
 * @return instance of CompletionService. Completion service will provide
 *   results as they arrive. The order is NOT same as the order of URLs
 */
public CompletionService<GetMethod> execute(List<String> urls, int timeoutMs) {
  HttpClientParams clientParams = new HttpClientParams();
  clientParams.setConnectionManagerTimeout(timeoutMs);
  HttpClient client = new HttpClient(clientParams, _connectionManager);

  CompletionService<GetMethod> completionService = new ExecutorCompletionService<>(_executor);
  for (String url : urls) {
    completionService.submit(() -> {
      try {
        GetMethod getMethod = new GetMethod(url);
        getMethod.getParams().setSoTimeout(timeoutMs);
        client.executeMethod(getMethod);
        return getMethod;
      } catch (Exception e) {
        // Log only exception type and message instead of the whole stack trace
        LOGGER.warn("Caught '{}' while executing GET on URL: {}", e.toString(), url);
        throw e;
      }
    });
  }
  return completionService;
}
 
Example 2
Source File: CFActivator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private HttpClient createHttpClient() {
	//see http://hc.apache.org/httpclient-3.x/threading.html
	MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
	HttpConnectionManagerParams params = connectionManager.getParams();
	params.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, PreferenceHelper.getInt(ServerConstants.HTTP_MAX_CONN_HOST_CONF_KEY, 50));
	params.setMaxTotalConnections(PreferenceHelper.getInt(ServerConstants.HTTP_MAX_CONN_TOTAL_CONF_KEY, 150));
	params.setConnectionTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_CONN_TIMEOUT_CONF_KEY, 15000)); //15s
	params.setSoTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_SO_TIMEOUT_CONF_KEY, 30000)); //30s
	connectionManager.setParams(params);

	HttpClientParams clientParams = new HttpClientParams();
	clientParams.setConnectionManagerTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_CONN_MGR_TIMEOUT_CONF_KEY, 300000)); // 5 minutes

	return new HttpClient(clientParams, connectionManager);
}
 
Example 3
Source File: CcuClient.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void start() throws HomematicClientException {
    logger.info("Starting {}", CcuClient.class.getSimpleName());
    super.start();

    tclregaScripts = loadTclRegaScripts();

    httpClient = new HttpClient(new SimpleHttpConnectionManager(true));
    HttpClientParams params = httpClient.getParams();
    Long timeout = context.getConfig().getTimeout() * 1000L;
    params.setConnectionManagerTimeout(timeout);
    params.setSoTimeout(timeout.intValue());
    params.setContentCharset("ISO-8859-1");
}
 
Example 4
Source File: ICQPropertyHandler.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
*/
  @SuppressWarnings({ "unchecked", "unused" })
  @Override
  public boolean isValid(final FormItem formItem, final Map formContext) {
      boolean result;
      final TextElement textElement = (TextElement) formItem;

      if (StringHelper.containsNonWhitespace(textElement.getValue())) {

          // Use an HttpClient to fetch a profile information page from ICQ.
          final HttpClient httpClient = HttpClientFactory.getHttpClientInstance();
          final HttpClientParams httpClientParams = httpClient.getParams();
          httpClientParams.setConnectionManagerTimeout(2500);
          httpClient.setParams(httpClientParams);
          final HttpMethod httpMethod = new GetMethod(ICQ_NAME_VALIDATION_URL);
          final NameValuePair uinParam = new NameValuePair(ICQ_NAME_URL_PARAMETER, textElement.getValue());
          httpMethod.setQueryString(new NameValuePair[] { uinParam });
          // Don't allow redirects since otherwise, we won't be able to get the HTTP 302 further down.
          httpMethod.setFollowRedirects(false);
          try {
              // Get the user profile page
              httpClient.executeMethod(httpMethod);
              final int httpStatusCode = httpMethod.getStatusCode();
              // Looking at the HTTP status code tells us whether a user with the given ICQ name exists.
              if (httpStatusCode == HttpStatus.SC_OK) {
                  // ICQ tells us that a user name is valid if it sends an HTTP 200...
                  result = true;
              } else if (httpStatusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                  // ...and if it's invalid, it sends an HTTP 302.
                  textElement.setErrorKey("form.name.icq.error", null);
                  result = false;
              } else {
                  // For HTTP status codes other than 200 and 302 we will silently assume that the given ICQ name is valid, but inform the user about this.
                  textElement.setExampleKey("form.example.icqname.notvalidated", null);
                  log.warn("ICQ name validation: Expected HTTP status 200 or 301, but got " + httpStatusCode);
                  result = true;
              }
          } catch (final Exception e) {
              // In case of any exception, assume that the given ICQ name is valid (The opposite would block easily upon network problems), and inform the user about
              // this.
              textElement.setExampleKey("form.example.icqname.notvalidated", null);
              log.warn("ICQ name validation: Exception: " + e.getMessage());
              result = true;
          }
      } else {
          result = true;
      }
      return result;
  }
 
Example 5
Source File: MSNPropertyHandler.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
*/
  @SuppressWarnings({ "unchecked" })
  @Override
  public boolean isValid(final FormItem formItem, final Map formContext) {
      boolean result;
      final TextElement textElement = (TextElement) formItem;

      if (StringHelper.containsNonWhitespace(textElement.getValue())) {

          // Use an HttpClient to fetch a profile information page from MSN.
          final HttpClient httpClient = HttpClientFactory.getHttpClientInstance();
          final HttpClientParams httpClientParams = httpClient.getParams();
          httpClientParams.setConnectionManagerTimeout(2500);
          httpClient.setParams(httpClientParams);
          final HttpMethod httpMethod = new GetMethod(MSN_NAME_VALIDATION_URL);
          final NameValuePair idParam = new NameValuePair(MSN_NAME_URL_PARAMETER, textElement.getValue());
          httpMethod.setQueryString(new NameValuePair[] { idParam });
          // Don't allow redirects since otherwise, we won't be able to get the correct status
          httpMethod.setFollowRedirects(false);
          try {
              // Get the user profile page
              httpClient.executeMethod(httpMethod);
              final int httpStatusCode = httpMethod.getStatusCode();
              // Looking at the HTTP status code tells us whether a user with the given MSN name exists.
              if (httpStatusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
                  // If the user exists, we get a 301...
                  result = true;
              } else if (httpStatusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                  // ...and if the user doesn't exist, MSN sends a 500.
                  textElement.setErrorKey("form.name.msn.error", null);
                  result = false;
              } else {
                  // For HTTP status codes other than 301 and 500 we will assume that the given MSN name is valid, but inform the user about this.
                  textElement.setExampleKey("form.example.msnname.notvalidated", null);
                  log.warn("MSN name validation: Expected HTTP status 301 or 500, but got " + httpStatusCode);
                  result = true;
              }
          } catch (final Exception e) {
              // In case of any exception, assume that the given MSN name is valid (The opposite would block easily upon network problems), and inform the user about
              // this.
              textElement.setExampleKey("form.example.msnname.notvalidated", null);
              log.warn("MSN name validation: Exception: " + e.getMessage());
              result = true;
          }
      } else {
          result = true;
      }
      return result;
  }
 
Example 6
Source File: ICQPropertyHandler.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
*/
  @SuppressWarnings({ "unchecked", "unused" })
  @Override
  public boolean isValid(final FormItem formItem, final Map formContext) {
      boolean result;
      final TextElement textElement = (TextElement) formItem;

      if (StringHelper.containsNonWhitespace(textElement.getValue())) {

          // Use an HttpClient to fetch a profile information page from ICQ.
          final HttpClient httpClient = HttpClientFactory.getHttpClientInstance();
          final HttpClientParams httpClientParams = httpClient.getParams();
          httpClientParams.setConnectionManagerTimeout(2500);
          httpClient.setParams(httpClientParams);
          final HttpMethod httpMethod = new GetMethod(ICQ_NAME_VALIDATION_URL);
          final NameValuePair uinParam = new NameValuePair(ICQ_NAME_URL_PARAMETER, textElement.getValue());
          httpMethod.setQueryString(new NameValuePair[] { uinParam });
          // Don't allow redirects since otherwise, we won't be able to get the HTTP 302 further down.
          httpMethod.setFollowRedirects(false);
          try {
              // Get the user profile page
              httpClient.executeMethod(httpMethod);
              final int httpStatusCode = httpMethod.getStatusCode();
              // Looking at the HTTP status code tells us whether a user with the given ICQ name exists.
              if (httpStatusCode == HttpStatus.SC_OK) {
                  // ICQ tells us that a user name is valid if it sends an HTTP 200...
                  result = true;
              } else if (httpStatusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                  // ...and if it's invalid, it sends an HTTP 302.
                  textElement.setErrorKey("form.name.icq.error", null);
                  result = false;
              } else {
                  // For HTTP status codes other than 200 and 302 we will silently assume that the given ICQ name is valid, but inform the user about this.
                  textElement.setExampleKey("form.example.icqname.notvalidated", null);
                  log.warn("ICQ name validation: Expected HTTP status 200 or 301, but got " + httpStatusCode);
                  result = true;
              }
          } catch (final Exception e) {
              // In case of any exception, assume that the given ICQ name is valid (The opposite would block easily upon network problems), and inform the user about
              // this.
              textElement.setExampleKey("form.example.icqname.notvalidated", null);
              log.warn("ICQ name validation: Exception: " + e.getMessage());
              result = true;
          }
      } else {
          result = true;
      }
      return result;
  }
 
Example 7
Source File: MSNPropertyHandler.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
*/
  @SuppressWarnings({ "unchecked" })
  @Override
  public boolean isValid(final FormItem formItem, final Map formContext) {
      boolean result;
      final TextElement textElement = (TextElement) formItem;

      if (StringHelper.containsNonWhitespace(textElement.getValue())) {

          // Use an HttpClient to fetch a profile information page from MSN.
          final HttpClient httpClient = HttpClientFactory.getHttpClientInstance();
          final HttpClientParams httpClientParams = httpClient.getParams();
          httpClientParams.setConnectionManagerTimeout(2500);
          httpClient.setParams(httpClientParams);
          final HttpMethod httpMethod = new GetMethod(MSN_NAME_VALIDATION_URL);
          final NameValuePair idParam = new NameValuePair(MSN_NAME_URL_PARAMETER, textElement.getValue());
          httpMethod.setQueryString(new NameValuePair[] { idParam });
          // Don't allow redirects since otherwise, we won't be able to get the correct status
          httpMethod.setFollowRedirects(false);
          try {
              // Get the user profile page
              httpClient.executeMethod(httpMethod);
              final int httpStatusCode = httpMethod.getStatusCode();
              // Looking at the HTTP status code tells us whether a user with the given MSN name exists.
              if (httpStatusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
                  // If the user exists, we get a 301...
                  result = true;
              } else if (httpStatusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                  // ...and if the user doesn't exist, MSN sends a 500.
                  textElement.setErrorKey("form.name.msn.error", null);
                  result = false;
              } else {
                  // For HTTP status codes other than 301 and 500 we will assume that the given MSN name is valid, but inform the user about this.
                  textElement.setExampleKey("form.example.msnname.notvalidated", null);
                  log.warn("MSN name validation: Expected HTTP status 301 or 500, but got " + httpStatusCode);
                  result = true;
              }
          } catch (final Exception e) {
              // In case of any exception, assume that the given MSN name is valid (The opposite would block easily upon network problems), and inform the user about
              // this.
              textElement.setExampleKey("form.example.msnname.notvalidated", null);
              log.warn("MSN name validation: Exception: " + e.getMessage());
              result = true;
          }
      } else {
          result = true;
      }
      return result;
  }
 
Example 8
Source File: CommonsHttpTransport.java    From elasticsearch-hadoop with Apache License 2.0 4 votes vote down vote up
public CommonsHttpTransport(Settings settings, SecureSettings secureSettings, String host) {
    if (log.isDebugEnabled()) {
        log.debug("Creating new CommonsHttpTransport");
    }
    this.settings = settings;
    this.secureSettings = secureSettings;
    this.clusterName = settings.getClusterInfoOrUnnamedLatest().getClusterName().getName(); // May be a bootstrap client.
    if (StringUtils.hasText(settings.getSecurityUserProviderClass())) {
        this.userProvider = UserProvider.create(settings);
    } else {
        this.userProvider = null;
    }
    httpInfo = host;
    sslEnabled = settings.getNetworkSSLEnabled();

    String pathPref = settings.getNodesPathPrefix();
    pathPrefix = (StringUtils.hasText(pathPref) ? addLeadingSlashIfNeeded(StringUtils.trimWhitespace(pathPref)) : StringUtils.trimWhitespace(pathPref));

    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(
            settings.getHttpRetries(), false) {

        @Override
        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (super.retryMethod(method, exception, executionCount)) {
                stats.netRetries++;
                return true;
            }
            return false;
        }
    });

    // Max time to wait for a connection from the connectionMgr pool
    params.setConnectionManagerTimeout(settings.getHttpTimeout());
    // Max time to wait for data from a connection.
    params.setSoTimeout((int) settings.getHttpTimeout());
    // explicitly set the charset
    params.setCredentialCharset(StringUtils.UTF_8.name());
    params.setContentCharset(StringUtils.UTF_8.name());

    HostConfiguration hostConfig = new HostConfiguration();

    hostConfig = setupSSLIfNeeded(settings, secureSettings, hostConfig);
    hostConfig = setupSocksProxy(settings, secureSettings, hostConfig);
    Object[] authSettings = setupHttpOrHttpsProxy(settings, secureSettings, hostConfig);
    hostConfig = (HostConfiguration) authSettings[0];

    try {
        hostConfig.setHost(new URI(escapeUri(host, sslEnabled), false));
    } catch (IOException ex) {
        throw new EsHadoopTransportException("Invalid target URI " + host, ex);
    }
    client = new HttpClient(params, new SocketTrackingConnectionManager());
    client.setHostConfiguration(hostConfig);

    addHttpAuth(settings, secureSettings, authSettings);
    completeAuth(authSettings);

    HttpConnectionManagerParams connectionParams = client.getHttpConnectionManager().getParams();
    // make sure to disable Nagle's protocol
    connectionParams.setTcpNoDelay(true);
    // Max time to wait to establish an initial HTTP connection
    connectionParams.setConnectionTimeout((int) settings.getHttpTimeout());

    this.headers = new HeaderProcessor(settings);

    if (log.isTraceEnabled()) {
        log.trace("Opening HTTP transport to " + httpInfo);
    }
}