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);
}
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;
}
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();
}
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;
}
}
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;
}
Aggregations