Example usage for org.apache.http.impl.client HttpClientBuilder addInterceptorFirst

List of usage examples for org.apache.http.impl.client HttpClientBuilder addInterceptorFirst

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClientBuilder addInterceptorFirst.

Prototype

public final HttpClientBuilder addInterceptorFirst(final HttpRequestInterceptor itcp) 

Source Link

Document

Adds this protocol interceptor to the head of the protocol processing list.

Usage

From source file:com.ibm.watson.retrieveandrank.app.rest.UtilityFunctions.java

public static HttpClientBuilder createHTTPBuilder(URI uri, String username, String password) {
    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(username, password));

    final HttpClientBuilder builder = HttpClientBuilder.create().setMaxConnTotal(128).setMaxConnPerRoute(32)
            .setDefaultRequestConfig(/* ww  w  .  ja  v  a 2  s.  c  o  m*/
                    RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build());
    builder.setDefaultCredentialsProvider(credentialsProvider);
    builder.addInterceptorFirst(new PreemptiveAuthInterceptor());
    return builder;
}

From source file:uk.org.openeyes.oink.it.ITSupport.java

public static IGenericClient buildHapiClientForFacade(Properties proxyProps) {
    String proxyUri = (String) proxyProps.get("proxy.uri");

    // Create a context and get the client factory so it can be configured
    FhirContext ctx = new FhirContext();
    IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();

    // Create an HTTP Client Builder
    HttpClientBuilder builder = HttpClientBuilder.create();

    // This interceptor adds HTTP username/password to every request
    String username = (String) proxyProps.get("proxy.username");
    String password = (String) proxyProps.get("proxy.password");
    builder.addInterceptorFirst(new HttpBasicAuthInterceptor(username, password));

    builder.addInterceptorFirst(new HttpRequestInterceptor() {

        @Override/*from ww w. ja  v  a  2 s.c o  m*/
        public void process(HttpRequest req, HttpContext context) throws HttpException, IOException {
            req.addHeader("Accept", "application/json+fhir; charset=UTF-8");
        }
    });

    // Use the new HTTP client builder
    clientFactory.setHttpClient(builder.build());

    IGenericClient client = clientFactory.newGenericClient("http://" + proxyUri);

    return client;
}

From source file:uk.org.openeyes.oink.it.ITSupport.java

public static IGenericClient buildHapiClientForProxy(Properties proxyProps) {
    // See if Patient exists
    String proxyUri = (String) proxyProps.get("proxy.uri");

    // Create a context and get the client factory so it can be configured
    FhirContext ctx = new FhirContext();
    IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();

    // Create an HTTP Client Builder
    HttpClientBuilder builder = HttpClientBuilder.create();

    // This interceptor adds HTTP username/password to every request
    String username = (String) proxyProps.get("proxy.username");
    String password = (String) proxyProps.get("proxy.password");
    builder.addInterceptorFirst(new HttpBasicAuthInterceptor(username, password));

    builder.addInterceptorFirst(new HttpRequestInterceptor() {

        @Override/* w  w w  .  j  av a 2  s  .  c  o m*/
        public void process(HttpRequest req, HttpContext context) throws HttpException, IOException {
            req.addHeader("Accept", "application/json+fhir; charset=UTF-8");
        }
    });

    // Use the new HTTP client builder
    clientFactory.setHttpClient(builder.build());

    IGenericClient client = clientFactory.newGenericClient("http://" + proxyUri);

    return client;
}

From source file:uk.org.openeyes.oink.it.multi.ITHl7v2ToOpenEyes.java

public static IGenericClient buildOpenEyesClient(Properties props) {
    String proxyUri = (String) props.get("openeyes.uri");

    // Create a context and get the client factory so it can be configured
    FhirContext ctx = new FhirContext();
    IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();

    // Create an HTTP Client Builder
    HttpClientBuilder builder = HttpClientBuilder.create();

    // This interceptor adds HTTP username/password to every request
    String username = (String) props.get("openeyes.username");
    String password = (String) props.get("openeyes.password");
    builder.addInterceptorFirst(new HttpBasicAuthInterceptor(username, password));

    builder.addInterceptorFirst(new HttpRequestInterceptor() {

        @Override// w w  w .  j av  a 2 s. co m
        public void process(HttpRequest req, HttpContext context) throws HttpException, IOException {
            req.addHeader("Accept", "application/json+fhir; charset=UTF-8");
        }
    });

    // Use the new HTTP client builder
    clientFactory.setHttpClient(builder.build());

    IGenericClient client = clientFactory.newGenericClient("http://" + proxyUri);

    return client;
}

From source file:com.offbytwo.jenkins.client.JenkinsHttpClient.java

protected static HttpClientBuilder addAuthentication(HttpClientBuilder builder, URI uri, String username,
        String password) {/*w  w  w. j  a va2 s  .  c  o  m*/
    if (isNotBlank(username)) {
        CredentialsProvider provider = new BasicCredentialsProvider();
        AuthScope scope = new AuthScope(uri.getHost(), uri.getPort(), "realm");
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        provider.setCredentials(scope, credentials);
        builder.setDefaultCredentialsProvider(provider);

        builder.addInterceptorFirst(new PreemptiveAuth());
    }
    return builder;
}

From source file:io.fabric8.kubernetes.jolokia.BearerTokenAuthenticator.java

/** {@inheritDoc} */
public void authenticate(HttpClientBuilder pBuilder, String pUser, String pPassword) {
    pBuilder.addInterceptorFirst(new PreemptiveBearerInterceptor(pUser));
}

From source file:org.commonjava.indy.client.core.auth.OAuth20BearerTokenAuthenticator.java

@Override
public HttpClientBuilder decorateClientBuilder(HttpClientBuilder builder) throws JHttpCException {
    builder.addInterceptorFirst(new HttpRequestInterceptor() {
        @Override/*from  ww  w .j  a v a  2 s  .c o  m*/
        public void process(HttpRequest httpRequest, HttpContext httpContext)
                throws HttpException, IOException {
            final Header header = new BasicHeader(AUTHORIZATION_HEADER, String.format(BEARER_FORMAT, token));
            httpRequest.addHeader(header);
        }
    });

    return builder;
}

From source file:org.meteogroup.jbrotli.httpclient.apache.HttpClientExample.java

public String downloadFileAsString(String url) throws IOException {
    BrotliLibraryLoader.loadBrotli();// www.  j a v a  2  s.  c  o  m
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.addInterceptorFirst(httpRequestInterceptor);
    httpClientBuilder.addInterceptorFirst(httpResponseInterceptor);
    try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
        String entity = downloadFileAsString(httpClient, url);
        if (entity != null)
            return entity;
    }
    return null;
}

From source file:org.osgpfoundation.osgp.webdemoapp.infra.platform.SoapRequestHelper.java

/**
 * Creates a HttpComponentsMessageSender for communication with the
 * platform.//from   w  w  w . j a va  2  s . com
 *
 * @return HttpComponentsMessageSender
 */
private HttpComponentsMessageSender createHttpMessageSender() {

    final HttpComponentsMessageSender sender = new HttpComponentsMessageSender();

    final HttpClientBuilder builder = HttpClients.custom();
    builder.addInterceptorFirst(new ContentLengthHeaderRemoveInterceptor());
    try {
        final SSLContext sslContext = new SSLContextBuilder()
                .loadKeyMaterial(this.keyStoreHelper.getKeyStore(), this.keyStoreHelper.getKeyStorePwAsChar())
                .loadTrustMaterial(this.keyStoreHelper.getTrustStore()).build();
        final SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(sslContext);
        builder.setSSLSocketFactory(sslConnectionFactory);
        sender.setHttpClient(builder.build());
    } catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException
            | KeyStoreException e) {
        e.printStackTrace();
    }

    return sender;
}

From source file:com.github.sardine.AuthenticationTest.java

@Test
public void testBasicPreemptiveAuthHeader() throws Exception {
    final HttpClientBuilder client = HttpClientBuilder.create();
    client.addInterceptorFirst(new HttpRequestInterceptor() {
        public void process(final HttpRequest r, final HttpContext context) throws HttpException, IOException {
            assertNotNull(r.getHeaders(HttpHeaders.AUTHORIZATION));
            assertEquals(1, r.getHeaders(HttpHeaders.AUTHORIZATION).length);
        }//from  ww  w  .ja v  a2  s  . c  o m
    });
    Sardine sardine = new SardineImpl(client);
    sardine.setCredentials("anonymous", null);
    // mod_dav supports Range headers for PUT
    final URI url = URI.create("http://sardine.googlecode.com/svn/trunk/README.html");
    sardine.enablePreemptiveAuthentication(url.getHost());
    assertTrue(sardine.exists(url.toString()));
}