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:eu.serco.dhus.request.RequestWork.java

public String call() throws Exception {
    HttpGet httpGet = new HttpGet(getUrl());

    //This class doesn't exist, it's for demonstration purpose only.

    httpGet.setHeader("Authorization", "Basic " + this.authorization);
    return HttpClientBuilder.create().build().execute(httpGet, new BasicResponseHandler());
}

From source file:juzu.impl.plugin.asset.ETagTestCase.java

@Test
public void testSatisfied() throws Exception {

    driver.get(applicationURL().toString());
    WebElement element = driver.findElement(By.tagName("script"));
    String assetURI = element.getAttribute("src");
    assertNotNull(assetURI);//from www  .  java  2  s.c om
    HttpClient client = HttpClientBuilder.create().build();

    // With no etag
    HttpGet get = new HttpGet(assetURI);
    HttpResponse response = client.execute(get);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals("a = 0;", EntityUtils.toString(response.getEntity()));
    Header[] etag = response.getHeaders("ETag");
    assertNotNull(etag);
    assertEquals(1, etag.length);

    // With correct etag
    get = new HttpGet(assetURI);
    get.setHeader("If-None-Match", etag[0].getValue());
    response = client.execute(get);
    assertEquals(304, response.getStatusLine().getStatusCode());
    assertEquals(null, response.getEntity());

    // With wrong etag
    get = new HttpGet(assetURI);
    get.setHeader("If-None-Match", "foo");
    response = client.execute(get);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals("a = 0;", EntityUtils.toString(response.getEntity()));
    etag = response.getHeaders("ETag");
    assertNotNull(etag);
}

From source file:com.predic8.membrane.servlet.test.ForwardingTest.java

@Test
public void testReachable() throws ClientProtocolException, IOException {
    String secret = "secret452363763";
    HttpClient hc = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(getBaseURL());
    post.setEntity(new StringEntity(secret));
    HttpResponse res = hc.execute(post);
    assertEquals(200, res.getStatusLine().getStatusCode());

    AssertUtils.assertContains(secret, EntityUtils.toString(res.getEntity()));
}

From source file:com.temenos.useragent.generic.http.DefaultHttpClient.java

@Override
public HttpResponse get(String url, HttpRequest request) {
    logHttpRequest(url, request);
    CloseableHttpClient client = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(DefaultHttpClientHelper.getBasicCredentialProvider()).build();
    HttpGet getRequest = new HttpGet(url);
    DefaultHttpClientHelper.buildRequestHeaders(request, getRequest);
    try {/*from w  w w .  java2 s.  c  om*/
        CloseableHttpResponse httpResponse = client.execute(getRequest);
        HttpEntity responseEntity = httpResponse.getEntity();
        return handleResponse(httpResponse, responseEntity);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:ai.serotonin.haystack.validator.Source.java

/**
 * Read a remote database via the Project-Haystack protocol.
 * /*from   w w  w.java2s .  c o  m*/
 * This method currently does not support authentication.
 * 
 * @param endpoint
 * @return the list of rows returned
 * @throws Exception
 */
public static List<HMap> remote(String endpoint) throws Exception {
    String filter = "id";
    //int limit = 10000;

    HMap map = new HMap().put("filter", filter); //.put("limit", limit);
    String entityStr = ZincWriter.gridToString(new HGrid(map));

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(endpoint + "read");
    // Set the auth as required.
    StringEntity entity = new StringEntity(entityStr, ContentType.TEXT_PLAIN);
    post.setEntity(entity);

    String responseStr = HttpUtils4.getTextContent(client, post, 1);
    HGrid response = new ZincReader(responseStr).readGrid();
    return response.getRows();
}

From source file:ca.islandora.alpaca.http.client.StaticTokenRequestInterceptor.java

/**
 * Convenience factory method.//from   www .j av  a  2  s.  co m
 * 
 * @param interceptor the interceptor to use, presumably an instance of {@link StaticTokenRequestInterceptor}
 * @return a default-configuration {@link HttpClient} that is wrapped with this interceptor
 */
public static HttpClient defaultClient(final StaticTokenRequestInterceptor interceptor) {
    final PoolingHttpClientConnectionManager connMan = new PoolingHttpClientConnectionManager();
    return HttpClientBuilder.create().setConnectionManager(connMan).setConnectionManagerShared(true)
            .addInterceptorFirst(interceptor).build();
}

From source file:com.arcbees.vcs.util.HttpClientWrapperImpl.java

private CloseableHttpClient initHttpClient() {
    RequestConfig requestConfig = getRequestConfig();

    String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    connectionManager = new PoolingHttpClientConnectionManager();

    return HttpClientBuilder.create().useSystemProperties().addInterceptorFirst(new RequestAcceptEncoding())
            .addInterceptorFirst(new ResponseContentEncoding())
            .setRetryHandler(new DefaultHttpRequestRetryHandler(RETRY_COUNT, true))
            .setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig)
            .setUserAgent("JetBrains TeamCity " + serverVersion).build();
}

From source file:com.neu.bigdata.service.PredictionService.java

private String rrsHttpPost() {

    HttpPost post;//  w w  w.j  a  v  a2  s. co m
    HttpClient client;
    StringEntity entity;
    String response = "";

    try {
        // create HttpPost and HttpClient object
        post = new HttpPost(API_URL);
        client = HttpClientBuilder.create().build();

        // setup output message by copying JSON body into 
        // apache StringEntity object along with content type
        entity = new StringEntity(jsonBody, HTTP.UTF_8);
        entity.setContentEncoding(HTTP.UTF_8);
        entity.setContentType("text/json");

        // add HTTP headers
        post.setHeader("Accept", "text/json");
        post.setHeader("Accept-Charset", "UTF-8");

        // set Authorization header based on the API key
        post.setHeader("Authorization", ("Bearer " + API_KEY));
        post.setEntity(entity);

        // Call REST API and retrieve response content
        HttpResponse authResponse = client.execute(post);
        response = EntityUtils.toString(authResponse.getEntity());

        return response;

    } catch (Exception e) {

        return e.toString();
    }

}

From source file:com.foundationdb.http.RestServiceITBase.java

@Before
public final void setUp() {
    port = serviceManager().getServiceByClass(HttpConductor.class).getPort();
    restContext = serviceManager().getServiceByClass(RestService.class).getContextPath();
    createTable(SCHEMA, TABLE, "id int not null primary key");
    client = HttpClientBuilder.create().build();
}