Search in sources :

Example 11 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext 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 12 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext 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 13 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext 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)

Example 14 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project jena by apache.

the class HttpQuery method exec.

/**
     * Execute the operation
     * 
     * @return Model The resulting model
     * @throws QueryExceptionHTTP
     */
public InputStream exec() throws QueryExceptionHTTP {
    // Select the appropriate HttpClient to use
    HttpClientContext hcc = HttpClientContext.adapt(getContext());
    RequestConfig.Builder builder = RequestConfig.copy(hcc.getRequestConfig());
    contextualizeCompressionSettings(builder);
    contextualizeTimeoutSettings(builder);
    hcc.setRequestConfig(builder.build());
    try {
        if (usesPOST())
            return execPost();
        return execGet();
    } catch (QueryExceptionHTTP httpEx) {
        log.trace("Exception in exec", httpEx);
        throw httpEx;
    } catch (JenaException jEx) {
        log.trace("JenaException in exec", jEx);
        throw jEx;
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) JenaException(org.apache.jena.shared.JenaException) HttpClientContext(org.apache.http.client.protocol.HttpClientContext)

Example 15 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project jena by apache.

the class QueryEngineHTTP method makeHttpQuery.

private HttpQuery makeHttpQuery() {
    if (closed)
        throw new ARQException("HTTP execution already closed");
    HttpQuery httpQuery = new HttpQuery(service);
    httpQuery.merge(getServiceParams(service, context));
    httpQuery.addParam(HttpParams.pQuery, queryString);
    for (String dft : defaultGraphURIs) {
        httpQuery.addParam(HttpParams.pDefaultGraph, dft);
    }
    for (String name : namedGraphURIs) {
        httpQuery.addParam(HttpParams.pNamedGraph, name);
    }
    if (params != null)
        httpQuery.merge(params);
    httpQuery.setAllowCompression(allowCompression);
    // check for service context overrides
    if (context.isDefined(Service.serviceContext)) {
        Map<String, Context> servicesContext = context.get(Service.serviceContext);
        if (servicesContext.containsKey(service)) {
            Context serviceContext = servicesContext.get(service);
            if (serviceContext.isDefined(Service.queryClient))
                client = serviceContext.get(Service.queryClient);
        }
    }
    httpQuery.setClient(client);
    HttpClientContext hcc = (httpContext == null) ? null : HttpClientContext.adapt(httpContext);
    httpQuery.setContext(hcc);
    // Apply timeouts
    if (connectTimeout > 0)
        httpQuery.setConnectTimeout((int) connectTimeoutUnit.toMillis(connectTimeout));
    if (readTimeout > 0)
        httpQuery.setReadTimeout((int) readTimeoutUnit.toMillis(readTimeout));
    return httpQuery;
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpContext(org.apache.http.protocol.HttpContext) ARQException(org.apache.jena.sparql.ARQException) HttpClientContext(org.apache.http.client.protocol.HttpClientContext)

Aggregations

HttpClientContext (org.apache.http.client.protocol.HttpClientContext)41 IOException (java.io.IOException)16 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)16 HttpHost (org.apache.http.HttpHost)14 AuthScope (org.apache.http.auth.AuthScope)14 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 CredentialsProvider (org.apache.http.client.CredentialsProvider)11 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)11 HttpGet (org.apache.http.client.methods.HttpGet)10 BasicScheme (org.apache.http.impl.auth.BasicScheme)10 HttpResponse (org.apache.http.HttpResponse)9 AuthCache (org.apache.http.client.AuthCache)9 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)9 Header (org.apache.http.Header)8 URI (java.net.URI)6 HttpEntity (org.apache.http.HttpEntity)6 HttpContext (org.apache.http.protocol.HttpContext)4 SocketTimeoutException (java.net.SocketTimeoutException)3 URISyntaxException (java.net.URISyntaxException)3