Search in sources :

Example 16 with BasicHttpContext

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;
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Example 17 with BasicHttpContext

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStream(java.io.InputStream) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) StringEntity(org.apache.http.entity.StringEntity) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) GMLStreamingFeatureCollection(gov.usgs.cida.gml.GMLStreamingFeatureCollection) FileOutputStream(java.io.FileOutputStream) FilterTransformer(org.geotools.filter.FilterTransformer) TransformerException(javax.xml.transform.TransformerException)

Example 18 with BasicHttpContext

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;
}
Also used : HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) ConnectionClosedException(org.apache.http.ConnectionClosedException)

Example 19 with BasicHttpContext

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);
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicAuth(org.apache.activemq.artemis.rest.queue.push.xml.BasicAuth) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) AuthScope(org.apache.http.auth.AuthScope) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 20 with BasicHttpContext

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;
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Scheme(org.apache.http.conn.scheme.Scheme) MessagingException(com.fsck.k9.mail.MessagingException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException)

Aggregations

BasicHttpContext (org.apache.http.protocol.BasicHttpContext)60 HttpContext (org.apache.http.protocol.HttpContext)37 IOException (java.io.IOException)24 HttpResponse (org.apache.http.HttpResponse)21 HttpGet (org.apache.http.client.methods.HttpGet)19 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)14 HttpHost (org.apache.http.HttpHost)11 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)11 HttpEntity (org.apache.http.HttpEntity)10 HttpPost (org.apache.http.client.methods.HttpPost)10 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)10 Header (org.apache.http.Header)8 BasicScheme (org.apache.http.impl.auth.BasicScheme)8 InputStream (java.io.InputStream)7 AuthScope (org.apache.http.auth.AuthScope)7 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)7 URI (java.net.URI)6 ArrayList (java.util.ArrayList)6 GZIPInputStream (java.util.zip.GZIPInputStream)6 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)6