use of org.apache.http.auth.AuthScope 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.auth.AuthScope in project jmeter by apache.
the class HTTPHC4Impl method setupClient.
private CloseableHttpClient setupClient(URL url) {
Map<HttpClientKey, CloseableHttpClient> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get();
final String host = url.getHost();
String proxyHost = getProxyHost();
int proxyPort = getProxyPortInt();
String proxyPass = getProxyPass();
String proxyUser = getProxyUser();
// static proxy is the globally define proxy eg command line or properties
boolean useStaticProxy = isStaticProxy(host);
// dynamic proxy is the proxy defined for this sampler
boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort);
boolean useProxy = useStaticProxy || useDynamicProxy;
// if both dynamic and static are used, the dynamic proxy has priority over static
if (!useDynamicProxy) {
proxyHost = PROXY_HOST;
proxyPort = PROXY_PORT;
proxyUser = PROXY_USER;
proxyPass = PROXY_PASS;
}
// Lookup key - must agree with all the values used to create the HttpClient.
HttpClientKey key = new HttpClientKey(url, useProxy, proxyHost, proxyPort, proxyUser, proxyPass);
CloseableHttpClient httpClient = null;
boolean concurrentDwn = this.testElement.isConcurrentDwn();
if (concurrentDwn) {
httpClient = (CloseableHttpClient) JMeterContextService.getContext().getSamplerContext().get(HTTPCLIENT_TOKEN);
}
if (httpClient == null) {
httpClient = mapHttpClientPerHttpClientKey.get(key);
}
if (httpClient != null && resetSSLContext && HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(url.getProtocol())) {
((AbstractHttpClient) httpClient).clearRequestInterceptors();
((AbstractHttpClient) httpClient).clearResponseInterceptors();
httpClient.getConnectionManager().closeIdleConnections(1L, TimeUnit.MICROSECONDS);
httpClient = null;
JsseSSLManager sslMgr = (JsseSSLManager) SSLManager.getInstance();
sslMgr.resetContext();
resetSSLContext = false;
}
if (httpClient == null) {
// One-time init for this client
HttpParams clientParams = new DefaultedHttpParams(new BasicHttpParams(), DEFAULT_HTTP_PARAMS);
DnsResolver resolver = this.testElement.getDNSResolver();
if (resolver == null) {
resolver = SystemDefaultDnsResolver.INSTANCE;
}
MeasuringConnectionManager connManager = new MeasuringConnectionManager(createSchemeRegistry(), resolver, TIME_TO_LIVE, VALIDITY_AFTER_INACTIVITY_TIMEOUT);
// to be realistic JMeter must set an higher value to DefaultMaxPerRoute
if (concurrentDwn) {
try {
int maxConcurrentDownloads = Integer.parseInt(this.testElement.getConcurrentPool());
connManager.setDefaultMaxPerRoute(Math.max(maxConcurrentDownloads, connManager.getDefaultMaxPerRoute()));
} catch (NumberFormatException nfe) {
// no need to log -> will be done by the sampler
}
}
httpClient = new DefaultHttpClient(connManager, clientParams) {
@Override
protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
return new StandardHttpRequestRetryHandler(RETRY_COUNT, REQUEST_SENT_RETRY_ENABLED);
}
};
if (IDLE_TIMEOUT > 0) {
((AbstractHttpClient) httpClient).setKeepAliveStrategy(IDLE_STRATEGY);
}
// see https://issues.apache.org/jira/browse/HTTPCORE-397
((AbstractHttpClient) httpClient).setReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE);
((AbstractHttpClient) httpClient).addResponseInterceptor(RESPONSE_CONTENT_ENCODING);
// HACK
((AbstractHttpClient) httpClient).addResponseInterceptor(METRICS_SAVER);
((AbstractHttpClient) httpClient).addRequestInterceptor(METRICS_RESETTER);
// Override the default schemes as necessary
SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
if (SLOW_HTTP != null) {
schemeRegistry.register(SLOW_HTTP);
}
// Set up proxy details
if (useProxy) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
clientParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (proxyUser.length() > 0) {
((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUser, proxyPass, LOCALHOST, PROXY_DOMAIN));
}
}
// Bug 52126 - we do our own cookie handling
clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookieSpecs.IGNORE_COOKIES);
if (log.isDebugEnabled()) {
log.debug("Created new HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
}
// save the agent for next time round
mapHttpClientPerHttpClientKey.put(key, httpClient);
} else {
if (log.isDebugEnabled()) {
log.debug("Reusing the HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
}
}
if (concurrentDwn) {
JMeterContextService.getContext().getSamplerContext().put(HTTPCLIENT_TOKEN, httpClient);
}
// TODO - should this be done when the client is created?
// If so, then the details need to be added as part of HttpClientKey
setConnectionAuthorization(httpClient, url, getAuthManager(), key);
return httpClient;
}
use of org.apache.http.auth.AuthScope in project jena by apache.
the class TestAuth method update_with_auth_10.
@Test
public void update_with_auth_10() throws URISyntaxException {
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
ue.setClient(withBasicAuth(new AuthScope("localhost", authPort), "allowed", "password"));
ue.execute();
}
use of org.apache.http.auth.AuthScope in project jena by apache.
the class TestAuth method query_with_auth_09.
@Test
public void query_with_auth_09() throws URISyntaxException {
QueryEngineHTTP qe = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(authServiceQuery, "ASK { }");
// Auth credentials for valid user with correct password
qe.setClient(withBasicAuth(new AuthScope("localhost", authPort), "allowed", "password"));
Assert.assertTrue(qe.execAsk());
}
use of org.apache.http.auth.AuthScope in project jena by apache.
the class TestAuth method query_with_auth_13.
@Test
public void query_with_auth_13() throws URISyntaxException {
QueryEngineHTTP qe = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(authServiceQuery, "ASK { }");
// Auth credentials for valid user with correct password and scoped to
// base URI of the actual service URL
qe.setClient(withBasicAuth(new AuthScope("localhost", authPort), "allowed", "password"));
Assert.assertTrue(qe.execAsk());
}
Aggregations