Java Code Examples for org.apache.commons.httpclient.HttpClient#getParams()

The following examples show how to use org.apache.commons.httpclient.HttpClient#getParams() . 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 alfresco-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected HttpClient constructHttpClient()
{
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient httpClient = new HttpClient(connectionManager);
    HttpClientParams params = httpClient.getParams();
    params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);
    params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true);
    if (socketTimeout != null) 
    {
        params.setSoTimeout(socketTimeout);
    }
    HttpConnectionManagerParams connectionManagerParams = httpClient.getHttpConnectionManager().getParams();
    connectionManagerParams.setMaxTotalConnections(maxTotalConnections);
    connectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections);
    connectionManagerParams.setConnectionTimeout(connectionTimeout);

    return httpClient;
}
 
Example 2
Source File: AuthUtils.java    From httpclientAuthHelper with Apache License 2.0 5 votes vote down vote up
public static void addDefaultHeader(HttpClient httpClient, boolean removeHeader, String headerName,
                                    String headervalue) {
    HttpClientParams clientParams = httpClient.getParams();
    HashSet<Header> headerSet = (HashSet<Header>) clientParams.getParameter(HTTP_DEFAULT_HEADERS);
    if (headerSet == null) {
        headerSet = new HashSet<Header>();
        clientParams.setParameter(HTTP_DEFAULT_HEADERS, headerSet);
    }
    Header header1 = new Header(headerName, headervalue);
    if (!headerSet.contains(header1) && !removeHeader) {
        headerSet.add(header1);
    } else if (headerSet.contains(header1) && removeHeader) {
        headerSet.remove(header1);
    }
}
 
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: SolrUtils.java    From anthelion with Apache License 2.0 4 votes vote down vote up
public static CommonsHttpSolrServer getCommonsHttpSolrServer(JobConf job) throws MalformedURLException {
  HttpClient client=new HttpClient();

  // Check for username/password
  if (job.getBoolean(SolrConstants.USE_AUTH, false)) {
    String username = job.get(SolrConstants.USERNAME);

    LOG.info("Authenticating as: " + username);

    AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);

    client.getState().setCredentials(scope, new UsernamePasswordCredentials(username, job.get(SolrConstants.PASSWORD)));

    HttpClientParams params = client.getParams();
    params.setAuthenticationPreemptive(true);

    client.setParams(params);
  }

  return new CommonsHttpSolrServer(job.get(SolrConstants.SERVER_URL), client);
}
 
Example 5
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 6
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 7
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 8
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 9
Source File: SolrUtils.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
public static CommonsHttpSolrServer getCommonsHttpSolrServer(JobConf job) throws MalformedURLException {
  HttpClient client=new HttpClient();

  // Check for username/password
  if (job.getBoolean(SolrConstants.USE_AUTH, false)) {
    String username = job.get(SolrConstants.USERNAME);

    LOG.info("Authenticating as: " + username);

    AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);

    client.getState().setCredentials(scope, new UsernamePasswordCredentials(username, job.get(SolrConstants.PASSWORD)));

    HttpClientParams params = client.getParams();
    params.setAuthenticationPreemptive(true);

    client.setParams(params);
  }

  String serverURL = job.get(SolrConstants.SERVER_URL);
  
  return new CommonsHttpSolrServer(serverURL, client);
}