Search in sources :

Example 6 with DigestSchemeFactory

use of org.apache.http.impl.auth.DigestSchemeFactory in project jmeter by apache.

the class HTTPHC4Impl method setupClient.

private MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager> setupClient(HttpClientKey key, JMeterVariables jMeterVariables, HttpClientContext clientContext) throws GeneralSecurityException {
    Map<HttpClientKey, MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager>> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get();
    clientContext.setAttribute(CONTEXT_ATTRIBUTE_CLIENT_KEY, key);
    CloseableHttpClient httpClient = null;
    MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager> triple = null;
    boolean concurrentDwn = this.testElement.isConcurrentDwn();
    Map<String, Object> samplerContext = JMeterContextService.getContext().getSamplerContext();
    if (concurrentDwn) {
        triple = (MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager>) samplerContext.get(CONTEXT_ATTRIBUTE_PARENT_SAMPLE_CLIENT_STATE);
    }
    if (triple == null) {
        triple = mapHttpClientPerHttpClientKey.get(key);
    }
    if (triple != null) {
        httpClient = triple.getLeft();
    }
    setupProxyAuthState(triple, clientContext);
    resetStateIfNeeded(triple, jMeterVariables, clientContext, mapHttpClientPerHttpClientKey);
    if (httpClient == null) {
        // One-time init for this client
        DnsResolver resolver = this.testElement.getDNSResolver();
        if (resolver == null) {
            resolver = SystemDefaultDnsResolver.INSTANCE;
        }
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", new LazyLayeredConnectionSocketFactory()).register("http", CONNECTION_SOCKET_FACTORY).build();
        // Modern browsers use more connections per host than the current httpclient default (2)
        // when using parallel download the httpclient and connection manager are shared by the downloads threads
        // to be realistic JMeter must set an higher value to DefaultMaxPerRoute
        PoolingHttpClientConnectionManager pHCCM = new PoolingHttpClientConnectionManager(new JMeterDefaultHttpClientConnectionOperator(registry, null, resolver), null, TIME_TO_LIVE, TimeUnit.MILLISECONDS);
        pHCCM.setValidateAfterInactivity(VALIDITY_AFTER_INACTIVITY_TIMEOUT);
        if (concurrentDwn) {
            try {
                int maxConcurrentDownloads = Integer.parseInt(this.testElement.getConcurrentPool());
                pHCCM.setDefaultMaxPerRoute(Math.max(maxConcurrentDownloads, pHCCM.getDefaultMaxPerRoute()));
            } catch (NumberFormatException nfe) {
            // no need to log -> will be done by the sampler
            }
        }
        CookieSpecProvider cookieSpecProvider = new IgnoreSpecProvider();
        Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.IGNORE_COOKIES, cookieSpecProvider).build();
        HttpClientBuilder builder = HttpClients.custom().setConnectionManager(pHCCM).setSchemePortResolver(new DefaultSchemePortResolver()).setDnsResolver(resolver).setRequestExecutor(REQUEST_EXECUTOR).setSSLSocketFactory(new LazyLayeredConnectionSocketFactory()).setDefaultCookieSpecRegistry(cookieSpecRegistry).setDefaultSocketConfig(SocketConfig.DEFAULT).setRedirectStrategy(new LaxRedirectStrategy()).setConnectionTimeToLive(TIME_TO_LIVE, TimeUnit.MILLISECONDS).setRetryHandler(new StandardHttpRequestRetryHandler(RETRY_COUNT, REQUEST_SENT_RETRY_ENABLED)).setConnectionReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE).setProxyAuthenticationStrategy(getProxyAuthStrategy());
        if (DISABLE_DEFAULT_UA) {
            builder.disableDefaultUserAgent();
        }
        Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.BASIC, new BasicSchemeFactory()).register(AuthSchemes.DIGEST, new DigestSchemeFactory()).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).register(AuthSchemes.SPNEGO, new DynamicSPNegoSchemeFactory(AuthManager.STRIP_PORT, AuthManager.USE_CANONICAL_HOST_NAME)).register(AuthSchemes.KERBEROS, new DynamicKerberosSchemeFactory(AuthManager.STRIP_PORT, AuthManager.USE_CANONICAL_HOST_NAME)).build();
        builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
        if (IDLE_TIMEOUT > 0) {
            builder.setKeepAliveStrategy(IDLE_STRATEGY);
        }
        // Set up proxy details
        AuthScope proxyAuthScope = null;
        NTCredentials proxyCredentials = null;
        if (key.hasProxy) {
            HttpHost proxy = new HttpHost(key.proxyHost, key.proxyPort, key.proxyScheme);
            builder.setProxy(proxy);
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            if (!key.proxyUser.isEmpty()) {
                proxyAuthScope = new AuthScope(key.proxyHost, key.proxyPort);
                proxyCredentials = new NTCredentials(key.proxyUser, key.proxyPass, LOCALHOST, PROXY_DOMAIN);
                credsProvider.setCredentials(proxyAuthScope, proxyCredentials);
            }
            builder.setDefaultCredentialsProvider(credsProvider);
        }
        builder.disableContentCompression().addInterceptorLast(RESPONSE_CONTENT_ENCODING);
        if (BASIC_AUTH_PREEMPTIVE) {
            builder.addInterceptorFirst(PREEMPTIVE_AUTH_INTERCEPTOR);
        } else {
            builder.setDefaultCredentialsProvider(new ManagedCredentialsProvider(getAuthManager(), proxyAuthScope, proxyCredentials));
        }
        httpClient = builder.build();
        if (log.isDebugEnabled()) {
            log.debug("Created new HttpClient: @{} {}", System.identityHashCode(httpClient), key);
        }
        triple = MutableTriple.of(httpClient, null, pHCCM);
        // save the agent for next time round
        mapHttpClientPerHttpClientKey.put(key, triple);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Reusing the HttpClient: @{} {}", System.identityHashCode(httpClient), key);
        }
    }
    if (concurrentDwn) {
        samplerContext.put(CONTEXT_ATTRIBUTE_PARENT_SAMPLE_CLIENT_STATE, triple);
    }
    return triple;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicSchemeFactory(org.apache.http.impl.auth.BasicSchemeFactory) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) NTCredentials(org.apache.http.auth.NTCredentials) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) LazyLayeredConnectionSocketFactory(org.apache.jmeter.protocol.http.sampler.hc.LazyLayeredConnectionSocketFactory) SlowHCPlainConnectionSocketFactory(org.apache.jmeter.protocol.http.util.SlowHCPlainConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) IgnoreSpecProvider(org.apache.http.impl.cookie.IgnoreSpecProvider) HttpHost(org.apache.http.HttpHost) DigestSchemeFactory(org.apache.http.impl.auth.DigestSchemeFactory) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) MutableTriple(org.apache.commons.lang3.tuple.MutableTriple) DynamicSPNegoSchemeFactory(org.apache.jmeter.protocol.http.control.DynamicSPNegoSchemeFactory) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) DnsResolver(org.apache.http.conn.DnsResolver) LazyLayeredConnectionSocketFactory(org.apache.jmeter.protocol.http.sampler.hc.LazyLayeredConnectionSocketFactory) DynamicKerberosSchemeFactory(org.apache.jmeter.protocol.http.control.DynamicKerberosSchemeFactory) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler) AuthState(org.apache.http.auth.AuthState) DefaultSchemePortResolver(org.apache.http.impl.conn.DefaultSchemePortResolver) AuthScope(org.apache.http.auth.AuthScope) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) NTLMSchemeFactory(org.apache.http.impl.auth.NTLMSchemeFactory)

Example 7 with DigestSchemeFactory

use of org.apache.http.impl.auth.DigestSchemeFactory in project coprhd-controller by CoprHD.

the class WinRMTarget method createHttpClient.

/**
 * HttpClient builder
 * @return HttpClient
 * @throws HttpException
 */
protected CloseableHttpClient createHttpClient() throws HttpException {
    HttpClientBuilder httpClient = HttpClientBuilder.create();
    // Build the request config identifying the target preferred authentication schemes and other socket connection parameters.
    RequestConfig.Builder requestConfig = RequestConfig.custom().setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.SPNEGO, AuthSchemes.NTLM, AuthSchemes.DIGEST, AuthSchemes.BASIC));
    requestConfig.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
    requestConfig.setSocketTimeout(DEFAULT_CONNECTION_TIMEOUT);
    httpClient.setDefaultRequestConfig(requestConfig.build());
    // Set the request executor. The EncryptedHttpRequestExecutor is a custom request executor that is capable of encryption and works
    // using the Windows NTLM authentication scheme.
    httpClient.setRequestExecutor(new EncryptedHttpRequestExecutor());
    // Build a list of the authentication schemes
    Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.NTLM, new NTLMSchemeFactory()).register(AuthSchemes.BASIC, new BasicSchemeFactory()).register(AuthSchemes.DIGEST, new DigestSchemeFactory()).register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).register(AuthSchemes.SPNEGO, new CustomSPNegoSchemeFactory()).build();
    try {
        httpClient.setConnectionManager(createClientConnectionManager());
    } catch (Exception e) {
        throw new HttpException(e.getMessage());
    }
    httpClient.setDefaultAuthSchemeRegistry(authSchemeRegistry);
    return httpClient.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicSchemeFactory(org.apache.http.impl.auth.BasicSchemeFactory) KerberosSchemeFactory(org.apache.http.impl.auth.KerberosSchemeFactory) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpException(org.apache.http.HttpException) MalformedURLException(java.net.MalformedURLException) EncryptedHttpRequestExecutor(com.iwave.ext.windows.winrm.encryption.EncryptedHttpRequestExecutor) HttpException(org.apache.http.HttpException) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) NTLMSchemeFactory(org.apache.http.impl.auth.NTLMSchemeFactory) DigestSchemeFactory(org.apache.http.impl.auth.DigestSchemeFactory)

Example 8 with DigestSchemeFactory

use of org.apache.http.impl.auth.DigestSchemeFactory in project calcite-avatica by apache.

the class AvaticaCommonsHttpClientImpl method setUsernamePassword.

@Override
public void setUsernamePassword(AuthenticationType authType, String username, String password) {
    this.credentials = new UsernamePasswordCredentials(Objects.requireNonNull(username), Objects.requireNonNull(password));
    this.credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);
    RegistryBuilder<AuthSchemeProvider> authRegistryBuilder = RegistryBuilder.create();
    switch(authType) {
        case BASIC:
            authRegistryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory());
            break;
        case DIGEST:
            authRegistryBuilder.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
            break;
        default:
            throw new IllegalArgumentException("Unsupported authentiation type: " + authType);
    }
    this.authRegistry = authRegistryBuilder.build();
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicSchemeFactory(org.apache.http.impl.auth.BasicSchemeFactory) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) DigestSchemeFactory(org.apache.http.impl.auth.DigestSchemeFactory) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

BasicSchemeFactory (org.apache.http.impl.auth.BasicSchemeFactory)8 DigestSchemeFactory (org.apache.http.impl.auth.DigestSchemeFactory)8 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)4 AuthSchemeRegistry (org.apache.http.auth.AuthSchemeRegistry)3 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)3 AuthScope (org.apache.http.auth.AuthScope)2 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 KerberosSchemeFactory (org.apache.http.impl.auth.KerberosSchemeFactory)2 NTLMSchemeFactory (org.apache.http.impl.auth.NTLMSchemeFactory)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)2 EncryptedHttpRequestExecutor (com.iwave.ext.windows.winrm.encryption.EncryptedHttpRequestExecutor)1 MalformedURLException (java.net.MalformedURLException)1 MutableTriple (org.apache.commons.lang3.tuple.MutableTriple)1 HttpEntity (org.apache.http.HttpEntity)1 HttpException (org.apache.http.HttpException)1 HttpHost (org.apache.http.HttpHost)1 HttpResponse (org.apache.http.HttpResponse)1 AuthState (org.apache.http.auth.AuthState)1