use of org.apache.http.protocol.BasicHttpContext in project tutorials by eugenp.
the class HttpComponentsClientHttpRequestFactoryDigestAuth method createHttpContext.
private HttpContext createHttpContext() {
// Create AuthCache instance
final AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
final DigestScheme digestAuth = new DigestScheme();
// If we already know the realm name
digestAuth.overrideParamter("realm", "Custom Realm Name");
// digestAuth.overrideParamter("nonce", "MTM3NTU2OTU4MDAwNzoyYWI5YTQ5MTlhNzc5N2UxMGM5M2Y5M2ViOTc4ZmVhNg==");
authCache.put(host, digestAuth);
// Add AuthCache to the execution context
final BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
return localcontext;
}
use of org.apache.http.protocol.BasicHttpContext in project coastal-hazards by USGS-CIDA.
the class HttpComponentsWFSClient method getFeatureCollection.
@Override
public SimpleFeatureCollection getFeatureCollection(String typeName, Filter filter) throws IOException {
SimpleFeatureCollection collection = null;
String filterXml = null;
InputStream is = null;
OutputStream os = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
if (filter != null) {
try {
FilterTransformer transformer = new FilterTransformer();
transformer.setOmitXMLDeclaration(true);
transformer.setNamespaceDeclarationEnabled(false);
filterXml = "<ogc:Filter>" + transformer.transform(filter) + "</ogc:Filter>";
} catch (TransformerException ex) {
throw new RuntimeException("Specified filter cannot be transformed", ex);
}
} else {
filterXml = "";
}
try {
HttpPost post = new HttpPost(wfsEndpoint);
StringEntity stringEntity = new StringEntity(fillInTemplate(typeName, filterXml), ContentType.APPLICATION_XML);
post.setEntity(stringEntity);
HttpContext localContext = new BasicHttpContext();
httpClient.setReuseStrategy(new NoConnectionReuseStrategy());
HttpResponse methodResponse = httpClient.execute(post, localContext);
if (methodResponse.getStatusLine().getStatusCode() != 200) {
throw new IOException(methodResponse.getStatusLine().getReasonPhrase());
}
is = methodResponse.getEntity().getContent();
os = new FileOutputStream(tmpWfsFile);
IOUtils.copy(is, os);
} finally {
if (httpClient.getConnectionManager() != null) {
httpClient.getConnectionManager().closeExpiredConnections();
}
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
collection = new GMLStreamingFeatureCollection(tmpWfsFile);
return collection;
}
use of org.apache.http.protocol.BasicHttpContext in project epp.mpc by eclipse.
the class TransportFactoryTest method interceptRequest.
private static AbortRequestCustomizer interceptRequest(HttpClientCustomizer... customizers) throws Exception {
AbortRequestCustomizer abortRequestCustomizer = new AbortRequestCustomizer();
HttpClientCustomizer[] mergedCustomizers;
if (customizers == null || customizers.length == 0) {
mergedCustomizers = new HttpClientCustomizer[] { abortRequestCustomizer };
} else {
mergedCustomizers = new HttpClientCustomizer[customizers.length + 1];
System.arraycopy(customizers, 0, mergedCustomizers, 0, customizers.length);
mergedCustomizers[customizers.length] = abortRequestCustomizer;
}
HttpClientTransport httpClientTransport = createClient(mergedCustomizers);
HttpClient client = httpClientTransport.getClient();
HttpContext context = new BasicHttpContext();
try {
client.execute(new HttpGet("http://localhost/test"), context);
fail("Expected request execution to fail");
} catch (ConnectionClosedException ex) {
// ignore expected exception
}
return abortRequestCustomizer;
}
use of org.apache.http.protocol.BasicHttpContext in project activemq-artemis by apache.
the class UriStrategy method initAuthentication.
protected void initAuthentication() {
if (registration.getAuthenticationMechanism() != null) {
if (registration.getAuthenticationMechanism().getType() instanceof BasicAuth) {
BasicAuth basic = (BasicAuth) registration.getAuthenticationMechanism().getType();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(basic.getUsername(), basic.getPassword());
AuthScope authScope = new AuthScope(AuthScope.ANY);
((DefaultHttpClient) client).getCredentialsProvider().setCredentials(authScope, creds);
localContext = new BasicHttpContext();
// Generate BASIC scheme object and stick it to the local execution context
BasicScheme basicAuth = new BasicScheme();
localContext.setAttribute("preemptive-auth", basicAuth);
// Add as the first request interceptor
((DefaultHttpClient) client).addRequestInterceptor(new PreemptiveAuth(), 0);
executor.setHttpContext(localContext);
}
}
}
use of org.apache.http.protocol.BasicHttpContext in project k-9 by k9mail.
the class WebDavStore method getHttpClient.
public WebDavHttpClient getHttpClient() throws MessagingException {
if (httpClient == null) {
httpClient = httpClientFactory.create();
// Disable automatic redirects on the http client.
httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false);
// Setup a cookie store for forms-based authentication.
httpContext = new BasicHttpContext();
authCookies = new BasicCookieStore();
httpContext.setAttribute(ClientContext.COOKIE_STORE, authCookies);
SchemeRegistry reg = httpClient.getConnectionManager().getSchemeRegistry();
try {
Scheme s = new Scheme("https", new WebDavSocketFactory(trustManagerFactory, hostname, 443), 443);
reg.register(s);
} catch (NoSuchAlgorithmException nsa) {
Timber.e(nsa, "NoSuchAlgorithmException in getHttpClient");
throw new MessagingException("NoSuchAlgorithmException in getHttpClient: ", nsa);
} catch (KeyManagementException kme) {
Timber.e(kme, "KeyManagementException in getHttpClient");
throw new MessagingException("KeyManagementException in getHttpClient: ", kme);
}
}
return httpClient;
}
Aggregations