org.apache.commons.httpclient.params.HttpClientParams Java Examples

The following examples show how to use org.apache.commons.httpclient.params.HttpClientParams. 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: DavGatewayHttpClientFacade.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Enable NTLM authentication on http client
 *
 * @param httpClient HttpClient instance
 */
public static void addNTLM(HttpClient httpClient) {
    // disable preemptive authentication
    httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, false);

    ArrayList<String> authPrefs = new ArrayList<>();
    authPrefs.add(AuthPolicy.NTLM);
    authPrefs.add(AuthPolicy.DIGEST);
    authPrefs.add(AuthPolicy.BASIC);
    httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    // make sure NTLM is always active
    needNTLM = true;

    // separate domain from username in credentials
    AuthScope authScope = new AuthScope(null, -1);
    NTCredentials credentials = (NTCredentials) httpClient.getState().getCredentials(authScope);
    if (credentials != null && (credentials.getDomain() == null || credentials.getDomain().isEmpty())) {
        setCredentials(httpClient, credentials.getUserName(), credentials.getPassword());
    }
}
 
Example #2
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 #3
Source File: BigSwitchApiTest.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    HttpClientParams hmp = mock(HttpClientParams.class);
    when(_client.getParams()).thenReturn(hmp);
    _api = new BigSwitchBcfApi(){
        @Override
        protected HttpClient createHttpClient() {
            return _client;
        }

        @Override
        protected HttpMethod createMethod(String type, String uri, int port) {
            return _method;
        }
    };
    _api.setControllerAddress("10.10.0.10");
    _api.setControllerUsername("myname");
    _api.setControllerPassword("mypassword");
}
 
Example #4
Source File: HTTPMetadataProvider.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor.
 * 
 * @param metadataURL the URL to fetch the metadata
 * @param requestTimeout the time, in milliseconds, to wait for the metadata server to respond
 * 
 * @throws MetadataProviderException thrown if the URL is not a valid URL or the metadata can not be retrieved from
 *             the URL
 */
@Deprecated
public HTTPMetadataProvider(String metadataURL, int requestTimeout) throws MetadataProviderException {
    super();
    try {
        metadataURI = new URI(metadataURL);
    } catch (URISyntaxException e) {
        throw new MetadataProviderException("Illegal URL syntax", e);
    }

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setSoTimeout(requestTimeout);
    httpClient = new HttpClient(clientParams);
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(requestTimeout);
    authScope = new AuthScope(metadataURI.getHost(), metadataURI.getPort());

}
 
Example #5
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 #6
Source File: Simulator.java    From realtime-analytics with GNU General Public License v2.0 6 votes vote down vote up
private void init() {
	initList(m_ipList, ipFilePath);
	initList(m_uaList, uaFilePath);
	initList(m_itemList, itmFilePath);
	initList(m_tsList,tsFilePath);
	initList(m_siteList,siteFilePath);
	initList(m_dsList,dsFilePath);
	initGUIDList();

	String finalURL = "";
	if (batchMode) {
		finalURL = BATCH_URL;
	} else {
		finalURL = URL;
	}
	m_payload = readFromResource();
	HttpClientParams clientParams = new HttpClientParams();
	clientParams.setSoTimeout(60000);
	m_client = new HttpClient(clientParams);
	m_method = new PostMethod(NODE + finalURL + EVENTTYPE);
	m_method.setRequestHeader("Connection", "Keep-Alive");
	m_method.setRequestHeader("Accept-Charset", "UTF-8");
}
 
Example #7
Source File: HttpClient.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates an instance of HttpClient using the given 
 * {@link HttpClientParams parameter set}.
 * 
 * @param params The {@link HttpClientParams parameters} to use.
 * 
 * @see HttpClientParams
 * 
 * @since 3.0
 */
public HttpClient(HttpClientParams params) {
    super();
    if (params == null) {
        throw new IllegalArgumentException("Params may not be null");  
    }
    this.params = params;
    this.httpConnectionManager = null;
    Class clazz = params.getConnectionManagerClass();
    if (clazz != null) {
        try {
            this.httpConnectionManager = (HttpConnectionManager) clazz.newInstance();
        } catch (Exception e) {
            LOG.warn("Error instantiating connection manager class, defaulting to"
                + " SimpleHttpConnectionManager", 
                e);
        }
    }
    if (this.httpConnectionManager == null) {
        this.httpConnectionManager = new SimpleHttpConnectionManager();
    }
    if (this.httpConnectionManager != null) {
        this.httpConnectionManager.getParams().setDefaults(this.params);
    }
}
 
Example #8
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 #9
Source File: DavGatewayHttpClientFacade.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create basic http client with default params.
 *
 * @return HttpClient instance
 */
private static HttpClient getBaseInstance() {
    HttpClient httpClient = new HttpClient();
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, getUserAgent());
    httpClient.getParams().setParameter(HttpClientParams.MAX_REDIRECTS, Settings.getIntProperty("davmail.httpMaxRedirects", MAX_REDIRECTS));
    httpClient.getParams().setCookiePolicy("DavMailCookieSpec");
    return httpClient;
}
 
Example #10
Source File: ApacheHttp31SLRFactory.java    From webarchive-commons with Apache License 2.0 5 votes vote down vote up
public ApacheHttp31SLRFactory() {
  	connectionManager = new MultiThreadedHttpConnectionManager();
  	//connectionManager = new ThreadLocalHttpConnectionManager();
  	hostConfiguration = new HostConfiguration();
HttpClientParams params = new HttpClientParams();
  	http = new HttpClient(params,connectionManager);
  	http.setHostConfiguration(hostConfiguration);
  }
 
Example #11
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 #12
Source File: HttpMethodDirector.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public HttpMethodDirector(
    final HttpConnectionManager connectionManager,
    final HostConfiguration hostConfiguration,
    final HttpClientParams params,
    final HttpState state
) {
    super();
    this.connectionManager = connectionManager;
    this.hostConfiguration = hostConfiguration;
    this.params = params;
    this.state = state;
    this.authProcessor = new AuthChallengeProcessor(this.params);
}
 
Example #13
Source File: HttpClient.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates an instance of HttpClient with a user specified 
 * {@link HttpClientParams parameter set} and 
 * {@link HttpConnectionManager HTTP connection manager}.
 * 
 * @param params The {@link HttpClientParams parameters} to use.
 * @param httpConnectionManager The {@link HttpConnectionManager connection manager}
 * to use.
 * 
 * @since 3.0
 */
public HttpClient(HttpClientParams params, HttpConnectionManager httpConnectionManager) {
    super();
    if (httpConnectionManager == null) {
        throw new IllegalArgumentException("httpConnectionManager cannot be null");  
    }
    if (params == null) {
        throw new IllegalArgumentException("Params may not be null");  
    }
    this.params = params; 
    this.httpConnectionManager = httpConnectionManager;
    this.httpConnectionManager.getParams().setDefaults(this.params);
}
 
Example #14
Source File: ProxyClient.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Assigns {@link HttpClientParams HTTP protocol parameters} for this ProxyClient.
 * 
 * @see HttpClientParams
 */
public synchronized void setParams(final HttpClientParams params) {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    this.params = params;
}
 
Example #15
Source File: SolrAdminHTTPClient.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void init()
{
    ParameterCheck.mandatory("baseUrl", baseUrl);
    
	StringBuilder sb = new StringBuilder();
	sb.append(baseUrl + "/admin/cores");
	this.adminUrl = sb.toString();

	httpClient = httpClientFactory.getHttpClient();
	HttpClientParams params = httpClient.getParams();
	params.setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
	httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("admin", "admin"));
}
 
Example #16
Source File: HttpClientBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds an HTTP client with the given settings. Settings are NOT reset to their default values after a client has
 * been created.
 * 
 * @return the created client.
 */
public HttpClient buildClient() {
    if (httpsProtocolSocketFactory != null) {
        Protocol.registerProtocol("https", new Protocol("https", httpsProtocolSocketFactory, 443));
    }

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setAuthenticationPreemptive(isPreemptiveAuthentication());
    clientParams.setContentCharset(getContentCharSet());
    clientParams.setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(
            connectionRetryAttempts, false));

    HttpConnectionManagerParams connMgrParams = new HttpConnectionManagerParams();
    connMgrParams.setConnectionTimeout(getConnectionTimeout());
    connMgrParams.setDefaultMaxConnectionsPerHost(getMaxConnectionsPerHost());
    connMgrParams.setMaxTotalConnections(getMaxTotalConnections());
    connMgrParams.setReceiveBufferSize(getReceiveBufferSize());
    connMgrParams.setSendBufferSize(getSendBufferSize());
    connMgrParams.setTcpNoDelay(isTcpNoDelay());

    MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
    connMgr.setParams(connMgrParams);

    HttpClient httpClient = new HttpClient(clientParams, connMgr);

    if (proxyHost != null) {
        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setProxy(proxyHost, proxyPort);
        httpClient.setHostConfiguration(hostConfig);

        if (proxyUsername != null) {
            AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort);
            UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUsername,
                    proxyPassword);
            httpClient.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }

    return httpClient;
}
 
Example #17
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 #18
Source File: BaseWebScriptTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception
{
    super.setUp();
    
    if (remoteServer != null)
    {
        httpClient = new HttpClient();
        httpClient.getParams().setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
        if (remoteServer.username != null)
        {
            httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(remoteServer.username, remoteServer.password));
        }
    }
}
 
Example #19
Source File: SAMLProcessorConfigurer.java    From spring-boot-security-saml with MIT License 5 votes vote down vote up
@VisibleForTesting
protected HTTPArtifactBinding createDefaultArtifactBinding(ServiceProviderBuilder builder) {
    HttpClientParams params = new HttpClientParams();
    params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 60000);
    HttpClient httpClient = new HttpClient(params, new MultiThreadedHttpConnectionManager());
    ArtifactResolutionProfileImpl artifactResolutionProfile = new ArtifactResolutionProfileImpl(httpClient);
    builder.setSharedObject(ArtifactResolutionProfile.class, artifactResolutionProfile);
    HTTPSOAP11Binding soapBinding = new HTTPSOAP11Binding(parserPool);
    artifactResolutionProfile.setProcessor(new SAMLProcessorImpl(soapBinding));
    return new HTTPArtifactBinding(parserPool, getVelocityEngine(), artifactResolutionProfile);
}
 
Example #20
Source File: HttpMethodDirector.java    From http4e with Apache License 2.0 5 votes vote down vote up
public HttpMethodDirector(
    final HttpConnectionManager connectionManager,
    final HostConfiguration hostConfiguration,
    final HttpClientParams params,
    final HttpState state
) {
    super();
    this.connectionManager = connectionManager;
    this.hostConfiguration = hostConfiguration;
    this.params = params;
    this.state = state;
    this.authProcessor = new AuthChallengeProcessor(this.params);
}
 
Example #21
Source File: ServerStatusChecker.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Basic constructor sets the listener to notify when
 * servers goes down/up. Also sets the polling time
 * which decides how long we wait between doing checks.
 * 
 * @param listener The listener
 * @param pollingTime The time we wait between checks, in milliseconds
 */
public ServerStatusChecker(ServerStatusListener listener, long pollingTime) {
    this.listener = listener;
    this.pollingTime = Math.max(30*1000, pollingTime);
    setPriority(Thread.NORM_PRIORITY-1);
    setDaemon(true);
    
    online = new LinkedList();
    offline = new LinkedList();
    httpClient = new HttpClient(); 
    httpClient.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE, false);
    httpClient.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
}
 
Example #22
Source File: HttpClientFeedFetcher.java    From rome with Apache License 2.0 4 votes vote down vote up
public HttpClientFeedFetcher(final FeedFetcherCache cache, final CredentialSupplier credentialSupplier) {
    setHttpClientParams(new HttpClientParams());
    setFeedInfoCache(cache);
    setCredentialSupplier(credentialSupplier);
}
 
Example #23
Source File: AbstractSpnegoAuthSchemeTest.java    From elasticsearch-hadoop with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuthWithReverseLookupServicePrincipal() throws Exception {
    // Configure logins
    Configuration configuration = new Configuration();
    SecurityUtil.setAuthenticationMethod(UserGroupInformation.AuthenticationMethod.KERBEROS, configuration);
    UserGroupInformation.setConfiguration(configuration);

    // Login as Client and Execute Test
    UserGroupInformation client = UserGroupInformation.loginUserFromKeytabAndReturnUGI(KerberosSuite.PRINCIPAL_CLIENT, KEYTAB_FILE.getAbsolutePath());

    client.doAs(new PrivilegedExceptionAction<Void>() {
        @Override
        public Void run() throws Exception {
            HttpParams params = new HttpClientParams();

            // Order auth schemes
            EsHadoopAuthPolicies.registerAuthSchemes();
            List<String> authPreferences = new ArrayList<String>();
            authPreferences.add(EsHadoopAuthPolicies.NEGOTIATE);
            params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPreferences);

            AuthChallengeProcessor authChallengeProcessor = new AuthChallengeProcessor(params);

            Map<String, String> dnsMappings = new HashMap<String, String>();
            dnsMappings.put("es.build.elastic.co", "127.0.0.1");

            TestMethod method = new TestMethod();
            method.setHeaders(new Header[]{new Header("WWW-Authenticate", "Negotiate")});
            method.setURI(new org.apache.commons.httpclient.URI("http", null, "127.0.0.1", 9200));

            Credentials credentials = new SpnegoCredentials(HadoopUserProvider.create(new TestSettings()), "HTTP/[email protected]");

            // Parse Challenge
            Map challenges = AuthChallengeParser.parseChallenges(method.getResponseHeaders("WWW-Authenticate"));
            assertThat(challenges.isEmpty(), not(true));
            assertThat(challenges.containsKey("negotiate"), is(true));
            assertThat(challenges.get("negotiate"), is("Negotiate"));
            AuthScheme scheme = authChallengeProcessor.processChallenge(method.getHostAuthState(), challenges);

            assertNotNull(scheme);
            assertThat(scheme, instanceOf(SpnegoAuthScheme.class));
            method.getHostAuthState().setAuthAttempted(true);

            // Execute Auth
            Header[] authHeaders = method.getRequestHeaders("Authorization");
            for (Header authHeader : authHeaders) {
                if (authHeader.isAutogenerated()) {
                    method.removeRequestHeader(authHeader);
                }
            }
            AuthState authState = method.getHostAuthState();
            AuthScheme authScheme = authState.getAuthScheme();
            assertNotNull(authScheme);
            assertThat(authScheme.isConnectionBased(), is(not(true)));

            // Replace scheme with test harness scheme
            authScheme = new TestScheme(dnsMappings);
            String authString = authScheme.authenticate(credentials, method);

            assertNotNull(authString);
            assertThat(authString, startsWith("Negotiate "));
            method.addRequestHeader(new Header("Authorization", authString, true));

            return null;
        }
    });
}
 
Example #24
Source File: AbstractSpnegoAuthSchemeTest.java    From elasticsearch-hadoop with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuthWithHostBasedServicePrincipal() throws Exception {
    // Configure logins
    Configuration configuration = new Configuration();
    SecurityUtil.setAuthenticationMethod(UserGroupInformation.AuthenticationMethod.KERBEROS, configuration);
    UserGroupInformation.setConfiguration(configuration);

    // Login as Client and Execute Test
    UserGroupInformation client = UserGroupInformation.loginUserFromKeytabAndReturnUGI(KerberosSuite.PRINCIPAL_CLIENT, KEYTAB_FILE.getAbsolutePath());

    client.doAs(new PrivilegedExceptionAction<Void>() {
        @Override
        public Void run() throws Exception {
            HttpParams params = new HttpClientParams();

            // Order auth schemes
            EsHadoopAuthPolicies.registerAuthSchemes();
            List<String> authPreferences = new ArrayList<String>();
            authPreferences.add(EsHadoopAuthPolicies.NEGOTIATE);
            params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPreferences);

            AuthChallengeProcessor authChallengeProcessor = new AuthChallengeProcessor(params);

            Map<String, String> dnsMappings = new HashMap<String, String>();
            dnsMappings.put("es.build.elastic.co", "127.0.0.1");

            TestMethod method = new TestMethod();
            method.setHeaders(new Header[]{new Header("WWW-Authenticate", "Negotiate")});
            method.setURI(new org.apache.commons.httpclient.URI("http", null, "es.build.elastic.co", 9200));

            Credentials credentials = new SpnegoCredentials(HadoopUserProvider.create(new TestSettings()), "HTTP/[email protected]");

            // Parse Challenge
            Map challenges = AuthChallengeParser.parseChallenges(method.getResponseHeaders("WWW-Authenticate"));
            assertThat(challenges.isEmpty(), not(true));
            assertThat(challenges.containsKey("negotiate"), is(true));
            assertThat(challenges.get("negotiate"), is("Negotiate"));
            AuthScheme scheme = authChallengeProcessor.processChallenge(method.getHostAuthState(), challenges);

            assertNotNull(scheme);
            assertThat(scheme, instanceOf(SpnegoAuthScheme.class));
            method.getHostAuthState().setAuthAttempted(true);

            // Execute Auth
            Header[] authHeaders = method.getRequestHeaders("Authorization");
            for (Header authHeader : authHeaders) {
                if (authHeader.isAutogenerated()) {
                    method.removeRequestHeader(authHeader);
                }
            }
            AuthState authState = method.getHostAuthState();
            AuthScheme authScheme = authState.getAuthScheme();
            assertNotNull(authScheme);
            assertThat(authScheme.isConnectionBased(), is(not(true)));

            // Replace scheme with test harness scheme
            authScheme = new TestScheme(dnsMappings);
            String authString = authScheme.authenticate(credentials, method);

            assertNotNull(authString);
            assertThat(authString, startsWith("Negotiate "));
            method.addRequestHeader(new Header("Authorization", authString, true));

            return null;
        }
    });
}
 
Example #25
Source File: AbstractSpnegoAuthSchemeTest.java    From elasticsearch-hadoop with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuth() throws Exception {
    // Configure logins
    Configuration configuration = new Configuration();
    SecurityUtil.setAuthenticationMethod(UserGroupInformation.AuthenticationMethod.KERBEROS, configuration);
    UserGroupInformation.setConfiguration(configuration);

    // Login as Client and Execute Test
    UserGroupInformation client = UserGroupInformation.loginUserFromKeytabAndReturnUGI(KerberosSuite.PRINCIPAL_CLIENT, KEYTAB_FILE.getAbsolutePath());

    client.doAs(new PrivilegedExceptionAction<Void>() {
        @Override
        public Void run() throws Exception {
            HttpParams params = new HttpClientParams();

            // Order auth schemes
            EsHadoopAuthPolicies.registerAuthSchemes();
            List<String> authPreferences = new ArrayList<String>();
            authPreferences.add(EsHadoopAuthPolicies.NEGOTIATE);
            params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPreferences);

            AuthChallengeProcessor authChallengeProcessor = new AuthChallengeProcessor(params);
            TestMethod method = new TestMethod();
            method.setHeaders(new Header[]{new Header("WWW-Authenticate", "Negotiate")});

            Credentials credentials = new SpnegoCredentials(HadoopUserProvider.create(new TestSettings()), KerberosSuite.PRINCIPAL_SERVER);

            // Parse Challenge
            Map challenges = AuthChallengeParser.parseChallenges(method.getResponseHeaders("WWW-Authenticate"));
            assertThat(challenges.isEmpty(), not(true));
            assertThat(challenges.containsKey("negotiate"), is(true));
            assertThat(challenges.get("negotiate"), is("Negotiate"));
            AuthScheme scheme = authChallengeProcessor.processChallenge(method.getHostAuthState(), challenges);

            assertNotNull(scheme);
            assertThat(scheme, instanceOf(SpnegoAuthScheme.class));
            method.getHostAuthState().setAuthAttempted(true);

            // Execute Auth
            Header[] authHeaders = method.getRequestHeaders("Authorization");
            for (Header authHeader : authHeaders) {
                if (authHeader.isAutogenerated()) {
                    method.removeRequestHeader(authHeader);
                }
            }
            AuthState authState = method.getHostAuthState();
            AuthScheme authScheme = authState.getAuthScheme();
            assertNotNull(authScheme);
            assertThat(authScheme.isConnectionBased(), is(not(true)));
            String authString = authScheme.authenticate(credentials, method);

            assertNotNull(authString);
            assertThat(authString, startsWith("Negotiate "));
            method.addRequestHeader(new Header("Authorization", authString, true));

            return null;
        }
    });
}
 
Example #26
Source File: HttpClientFeedFetcher.java    From rome with Apache License 2.0 4 votes vote down vote up
public synchronized void setHttpClientParams(final HttpClientParams httpClientParams) {
    this.httpClientParams = httpClientParams;
}
 
Example #27
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);
    }
}
 
Example #28
Source File: TraceeHttpClientDecorator.java    From tracee with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void setParams(HttpClientParams params) {
	delegate.setParams(params);
}
 
Example #29
Source File: TraceeHttpClientDecorator.java    From tracee with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public HttpClientParams getParams() {
	return delegate.getParams();
}
 
Example #30
Source File: ApacheHttp31SLRFactory.java    From webarchive-commons with Apache License 2.0 4 votes vote down vote up
@Override
  public void setNumRetries(int numRetries) {
http.getParams().setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(numRetries, true));
  }