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

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

Introduction

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

Prototype

public static HttpClientBuilder create() 

Source Link

Usage

From source file:org.ambraproject.wombat.service.remote.AbstractRemoteService.java

private CloseableHttpClient createClient() {
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    if (connectionManager.isPresent()) {
        clientBuilder = clientBuilder.setConnectionManager(connectionManager.get());
    }/*  w w w  .ja v a 2 s  .  co m*/
    return clientBuilder.build();
}

From source file:org.mitre.openid.connect.client.UserInfoFetcher.java

public UserInfoFetcher() {
    this(HttpClientBuilder.create().useSystemProperties().build());
}

From source file:com.linkedin.flashback.smartproxy.FlashbackRunnerTest.java

License:asdf

@Test()
public void testReplayHttp() throws InterruptedException, IOException {
    URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR);
    String rootPath = flashbackScene.getPath();
    SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, HTTP_SCENE);
    try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE).sceneAccessLayer(
            new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest()))
            .build()) {//  www . j a  va  2 s. c om
        flashbackRunner.start();
        HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT);
        String url = "http://www.example.org/";
        HttpClient client = HttpClientBuilder.create().setProxy(host).build();
        HttpGet request = new HttpGet(url);
        HttpResponse httpResponse0 = client.execute(request);
        Assert.assertTrue(EntityUtils.toString(httpResponse0.getEntity())
                .contains("I am from Flashback scene, not http://example.org"));

        url = "http://www.nba.com/";
        request = new HttpGet(url);
        HttpResponse httpResponse1 = client.execute(request);
        Assert.assertTrue(EntityUtils.toString(httpResponse1.getEntity())
                .contains("I am from Flashback scene, not http://www.nba.com"));

        url = "http://www.notexist.org/";
        request = new HttpGet(url);
        HttpResponse httpResponse2 = client.execute(request);
        Assert.assertEquals(httpResponse2.getStatusLine().getStatusCode(), 400);
        Assert.assertTrue(EntityUtils.toString(httpResponse2.getEntity()).contains("No Matching Request"));

    }
}

From source file:org.apache.sling.launchpad.SmokeIT.java

private CloseableHttpClient newClient() {

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
    credsProvider.setCredentials(new AuthScope("localhost", LAUNCHPAD_PORT), creds);

    return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
}

From source file:com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfRetriever.java

/**
 * Create a client that ignores all ssl errors.
 * Temporary solution until TrustStore service is ready
 *  https://jira-hzn.eng.vmware.com/browse/VSYM-1838
 * @return//w w  w.j av  a  2s  .  c om
 */
public static CloseableHttpClient newInsecureClient() {
    return HttpClientBuilder.create().setHostnameVerifier(newNaiveVerifier())
            .setSslcontext(newNaiveSslContext()).setMaxConnPerRoute(4).setMaxConnTotal(8).build();
}

From source file:org.apache.ignite.console.agent.handlers.RestHandler.java

/**
 * Start HTTP client for communication with node via REST.
 */
public void start() {
    httpClient = HttpClientBuilder.create().build();
}

From source file:ie.nuim.cs.dri.metadata.WebSearch.java

/**
 * Searches the Scopus database//  ww w .  jav a 2 s.  c  o  m
 *
 * @param title the title of the research object
 * @return returns an xml response of Scopus
 */
public String searchScopus(String title) {
    String xmlString = "";
    Configuration config = new Configuration();
    String scopusAPI = config.getScopusAPIKey();

    try {
        // create default HTTP Client
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // Create new getRequest with below mentioned URL
        String query = title;
        //uncomment the following line if you want to make an exact search, very restrictive
        //query = buildScopusSExactearchTitle(query);
        query = buildScopusSearchTitle(query);
        HttpGet getRequest = new HttpGet(
                "http://api.elsevier.com/content/search/index:scopus?query=" + query + "&count=50");
        //System.out.println("http://api.elsevier.com/content/search/index:scopus?query=" + query + "&count=50");

        // Add additional header to getRequest which accepts application/xml data
        getRequest.addHeader("X-ELS-APIKey", scopusAPI);
        getRequest.addHeader("X-ELS-ResourceVersion", "XOCS");
        getRequest.addHeader("accept", "application/xml");

        // Execute your request and catch response
        HttpResponse response = httpClient.execute(getRequest);

        // Check for HTTP response code: 200 = success
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

        // Get-Capture Complete application/xml body response
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
        String output;
        while ((output = br.readLine()) != null) {
            xmlString += output;

        }
        httpClient.close();

    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    //System.out.println("Scopus Search Result=\n" + xmlString);
    return xmlString;
}

From source file:com.foundationdb.server.service.security.RestSecurityIT.java

private int openRestURL(String request, String query, String userInfo, boolean post) throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpRequestBase httpRequest;//from  w  w w.  ja va  2 s  .  co  m
    if (post) {
        httpRequest = new HttpPost(getRestURL(request, "", userInfo));
        ((HttpPost) httpRequest).setEntity(new ByteArrayEntity(query.getBytes("UTF-8")));
    } else {
        httpRequest = new HttpGet(getRestURL(request, query, userInfo));
    }
    HttpResponse response = client.execute(httpRequest);
    int code = response.getStatusLine().getStatusCode();
    EntityUtils.consume(response.getEntity());
    client.close();
    return code;
}

From source file:com.complexible.clearbit.Clearbit.java

public Clearbit(final String theKey, final boolean theUseStreamingAPI) {
    mKey = Preconditions.checkNotNull(theKey);
    mStreaming = theUseStreamingAPI;//from  ww  w  . j a v  a2s . com

    mClient = HttpClientBuilder.create().setUserAgent("clearbit.api")
            .setConnectionManager(new PoolingHttpClientConnectionManager()).build();
}

From source file:org.opennms.opennms.pris.plugins.defaults.source.HttpRequisitionSource.java

@Override
public Object dump() throws Exception {
    Requisition requisition = null;/*w ww. j a  v  a 2 s . com*/

    if (getUrl() != null) {
        HttpClientBuilder builder = HttpClientBuilder.create();

        // If username and password was found, inject the credentials
        if (getUserName() != null && getPassword() != null) {

            CredentialsProvider provider = new BasicCredentialsProvider();

            // Create the authentication scope
            AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);

            // Create credential pair
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(getUserName(),
                    getPassword());

            // Inject the credentials
            provider.setCredentials(scope, credentials);

            // Set the default credentials provider
            builder.setDefaultCredentialsProvider(provider);
        }

        HttpClient client = builder.build();
        HttpGet request = new HttpGet(getUrl());
        HttpResponse response = client.execute(request);
        try {

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));
            JAXBContext jaxbContext = JAXBContext.newInstance(Requisition.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            requisition = (Requisition) jaxbUnmarshaller.unmarshal(bufferedReader);

        } catch (JAXBException e) {
            LOGGER.error("The response did not contain a valid requisition as xml.", e);
        }
        LOGGER.debug("Got Requisition {}", requisition);
    } else {
        LOGGER.error("Parameter requisition.url is missing in requisition.properties");
    }
    if (requisition == null) {
        LOGGER.error("Requisition is null for unknown reasons");
        return null;
    }
    LOGGER.info("HttpRequisitionSource delivered for requisition '{}'", requisition.getNodes().size());
    return requisition;
}