Search in sources :

Example 11 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project orientdb by orientechnologies.

the class BaseHttpTest method exec.

protected BaseHttpTest exec() throws IOException {
    final HttpHost targetHost = new HttpHost(getHost(), getPort(), getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost), new UsernamePasswordCredentials(getUserName(), getUserPassword()));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    if (keepAlive != null)
        request.addHeader("Connection", keepAlive ? "Keep-Alive" : "Close");
    if (payload != null && request instanceof HttpEntityEnclosingRequestBase)
        ((HttpEntityEnclosingRequestBase) request).setEntity(payload);
    final CloseableHttpClient httpClient = HttpClients.createDefault();
    // DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(retry, false);
    // context.setAttribute(HttpMethodParams.RETRY_HANDLER, retryhandler);
    response = httpClient.execute(targetHost, request, context);
    return this;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 12 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project geode by apache.

the class GeodeRestClient method doRequest.

public HttpResponse doRequest(HttpRequestBase request, String username, String password) throws Exception {
    HttpHost targetHost = new HttpHost(bindAddress, restPort, protocol);
    HttpClientBuilder clientBuilder = HttpClients.custom();
    HttpClientContext clientContext = HttpClientContext.create();
    // configures the clientBuilder and clientContext
    if (username != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(username, password));
        clientBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    if (useHttps) {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        clientBuilder.setSSLContext(ctx);
        clientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
    }
    return clientBuilder.build().execute(targetHost, request, clientContext);
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) SecureRandom(java.security.SecureRandom) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SSLContext(javax.net.ssl.SSLContext) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 13 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project jackrabbit by apache.

the class RepositoryServiceImpl method getContext.

protected HttpContext getContext(SessionInfo sessionInfo) throws RepositoryException {
    HttpClientContext result = HttpClientContext.create();
    if (sessionInfo != null) {
        checkSessionInfo(sessionInfo);
        org.apache.http.auth.Credentials creds = ((SessionInfoImpl) sessionInfo).getCredentials().getHttpCredentials();
        if (creds != null) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new org.apache.http.auth.AuthScope(httpHost.getHostName(), httpHost.getPort()), creds);
            BasicScheme basicAuth = new BasicScheme();
            AuthCache authCache = new BasicAuthCache();
            authCache.put(httpHost, basicAuth);
            result.setCredentialsProvider(credsProvider);
            result.setAuthCache(authCache);
        }
    }
    return result;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Example 14 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project jackrabbit by apache.

the class WebDAVTest method setUp.

protected void setUp() throws Exception {
    this.uri = URI.create(System.getProperty("webdav.test.url", "http://localhost:8080/repository/default/"));
    this.root = this.uri.toASCIIString();
    if (!this.root.endsWith("/")) {
        this.root += "/";
    }
    this.username = System.getProperty("webdav.test.username", "admin");
    this.password = System.getProperty("webdav.test.password", "admin");
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(this.username, this.password));
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    // Add AuthCache to the execution context
    this.context = HttpClientContext.create();
    this.context.setCredentialsProvider(credsProvider);
    this.context.setAuthCache(authCache);
    this.client = HttpClients.custom().setConnectionManager(cm).build();
    super.setUp();
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 15 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project jena by apache.

the class TestAuth method update_with_auth_11.

@Test
public void update_with_auth_11() {
    UpdateRequest updates = UpdateFactory.create("CREATE SILENT GRAPH <http://graph>");
    UpdateProcessRemoteBase ue = (UpdateProcessRemoteBase) UpdateExecutionFactory.createRemote(updates, authServiceUpdate);
    // Auth credentials for valid user with correct password scoped to correct URI
    // Also using pre-emptive auth
    BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
    URI scope = URI.create(authServiceUpdate);
    credsProv.setCredentials(new AuthScope(scope.getHost(), scope.getPort()), new UsernamePasswordCredentials("allowed", "password"));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(new HttpHost(scope.getHost()), basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProv);
    context.setAuthCache(authCache);
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build();
    ue.setClient(client);
    ue.setHttpContext(context);
    ue.execute();
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) UpdateRequest(org.apache.jena.update.UpdateRequest) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) UpdateProcessRemoteBase(org.apache.jena.sparql.modify.UpdateProcessRemoteBase) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) URI(java.net.URI) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Aggregations

BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)75 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)63 CredentialsProvider (org.apache.http.client.CredentialsProvider)48 AuthScope (org.apache.http.auth.AuthScope)41 HttpHost (org.apache.http.HttpHost)22 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)20 HttpResponse (org.apache.http.HttpResponse)16 HttpGet (org.apache.http.client.methods.HttpGet)16 HttpClient (org.apache.http.client.HttpClient)14 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)14 IOException (java.io.IOException)13 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 AuthCache (org.apache.http.client.AuthCache)9 RequestConfig (org.apache.http.client.config.RequestConfig)9 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)9 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 BasicScheme (org.apache.http.impl.auth.BasicScheme)8 Test (org.junit.Test)8 HttpEntity (org.apache.http.HttpEntity)6 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)6