Ejemplo n.º 1
0
  /**
   * Prepares the REST client
   *
   * @throws URISyntaxException
   */
  private void restClientInit() throws URISyntaxException {
    DefaultApacheHttpClientConfig clientConfig = new DefaultApacheHttpClientConfig();
    clientConfig.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, Boolean.TRUE);
    clientConfig
        .getProperties()
        .put(ApacheHttpClientConfig.PROPERTY_PREEMPTIVE_AUTHENTICATION, Boolean.TRUE);
    clientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, getHttpTimeout());
    clientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, getHttpTimeout());
    clientConfig
        .getProperties()
        .put(ClientConfig.PROPERTY_THREADPOOL_SIZE, getHttpMaxConnections());

    crowdServer = new URI(getCrowdServerUrl()).resolve("rest/usermanagement/1/");

    ApacheHttpClientState httpState = new ApacheHttpClientState();
    httpState.setCredentials(
        null,
        crowdServer.getHost(),
        crowdServer.getPort(),
        getApplicationName(),
        getApplicationPassword());
    if (getHttpProxyHost().trim().length() > 0 && getHttpProxyPort() > 0) {
      clientConfig
          .getProperties()
          .put(
              ApacheHttpClientConfig.PROPERTY_PROXY_URI,
              getHttpProxyHost() + ':' + getHttpProxyPort());

      if (getHttpProxyUsername() != null && getHttpProxyPassword() != null) {
        httpState.setProxyCredentials(
            null,
            getHttpProxyHost(),
            getHttpProxyPort(),
            getHttpProxyUsername(),
            getHttpProxyPassword());
      }
    }
    clientConfig.getProperties().put(ApacheHttpClientConfig.PROPERTY_HTTP_STATE, httpState);

    if (LOG.isDebugEnabled()) {
      LOG.debug("HTTP Client config");
      LOG.debug(getCrowdServerUrl());
      LOG.debug(crowdServer.toString());
      LOG.debug(
          "PROPERTY_THREADPOOL_SIZE:"
              + clientConfig.getProperty(ClientConfig.PROPERTY_THREADPOOL_SIZE));
      LOG.debug(
          "PROPERTY_READ_TIMEOUT:" + clientConfig.getProperty(ClientConfig.PROPERTY_READ_TIMEOUT));
      LOG.debug(
          "PROPERTY_CONNECT_TIMEOUT:"
              + clientConfig.getProperty(ClientConfig.PROPERTY_CONNECT_TIMEOUT));
      LOG.debug(
          "PROPERTY_PROXY_URI:"
              + clientConfig.getProperty(ApacheHttpClientConfig.PROPERTY_PROXY_URI));
      LOG.debug("Crowd application name:" + getApplicationName());
    }

    client = ApacheHttpClient.create(clientConfig);
  }
 private HttpState getHttpState(Map<String, Object> props) {
   ApacheHttpClientState httpState =
       (ApacheHttpClientState) props.get(DefaultApacheHttpClientConfig.PROPERTY_HTTP_STATE);
   if (httpState != null) {
     return httpState.getHttpState();
   } else {
     return null;
   }
 }