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:ch.ralscha.extdirectspring_itest.RawJsonControllerTest.java

@SuppressWarnings("unchecked")
private static void testAndCheck(String action, String method, Integer total, boolean success)
        throws IOException, JsonParseException, JsonMappingException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {// w ww.  j  a v a2  s  .  c  o m
        HttpPost post = new HttpPost("http://localhost:9998/controller/router");

        StringEntity postEntity = new StringEntity("{\"action\":\"" + action + "\",\"method\":\"" + method
                + "\",\"data\":[],\"type\":\"rpc\",\"tid\":1}", "UTF-8");
        post.setEntity(postEntity);
        post.setHeader("Content-Type", "application/json; charset=UTF-8");

        response = client.execute(post);
        HttpEntity entity = response.getEntity();
        assertThat(entity).isNotNull();
        String responseString = EntityUtils.toString(entity);

        assertThat(responseString).isNotNull();
        assertThat(responseString).startsWith("[").endsWith("]");

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> rootAsMap = mapper
                .readValue(responseString.substring(1, responseString.length() - 1), Map.class);
        assertEquals(5, rootAsMap.size());

        assertEquals(method, rootAsMap.get("method"));
        assertEquals("rpc", rootAsMap.get("type"));
        assertEquals(action, rootAsMap.get("action"));
        assertEquals(1, rootAsMap.get("tid"));

        Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result");
        if (total != null) {
            assertEquals(3, result.size());
            assertThat((Integer) result.get("total")).isEqualTo(total);
        } else {
            assertEquals(2, result.size());
        }
        assertThat((Boolean) result.get("success")).isEqualTo(success);

        List<Map<String, Object>> records = (List<Map<String, Object>>) result.get("records");
        assertEquals(2, records.size());

        assertEquals("4cf8e5b8924e23349fb99454", ((Map<String, Object>) records.get(0).get("_id")).get("$oid"));
        assertEquals("4cf8e5b8924e2334a0b99454", ((Map<String, Object>) records.get(1).get("_id")).get("$oid"));
    } finally {
        IOUtils.closeQuietly(response);
        IOUtils.closeQuietly(client);
    }
}

From source file:guru.nidi.ramltester.loader.UrlRamlLoader.java

public UrlRamlLoader(String base, UrlFetcher fetcher, CloseableHttpClient httpClient) {
    this.base = base;
    this.fetcher = fetcher;
    this.client = httpClient == null ? HttpClientBuilder.create().build() : httpClient;
}

From source file:ar.edu.ubp.das.src.chat.actions.LogoutAction.java

@Override
public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, RuntimeException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        String authToken = String.valueOf(request.getSession().getAttribute("token"));

        HttpPost httpPost = new HttpPost("http://25.136.78.82:8080/logout/");
        httpPost.addHeader("Authorization", "BEARER " + authToken);
        httpPost.addHeader("accept", "application/json");

        CloseableHttpResponse postResponse = httpClient.execute(httpPost);

        HttpEntity responseEntity = postResponse.getEntity();
        StatusLine responseStatus = postResponse.getStatusLine();
        String restResp = EntityUtils.toString(responseEntity);

        if (responseStatus.getStatusCode() != 200) {
            throw new RuntimeException(restResp);
        }// w w w .j  ava  2s  . co m

        request.getSession().removeAttribute("token");

        return mapping.getForwardByName("success");

    } catch (IOException | RuntimeException e) {
        request.setAttribute("message", "Error al realizar logout: " + e.getMessage());
        response.setStatus(400);
        return mapping.getForwardByName("failure");
    }
}

From source file:com.themodernway.server.core.support.spring.network.CoreNativeHttpRequestFactory.java

public CoreNativeHttpRequestFactory(final int route, final int total) {
    this(HttpClientBuilder.create().setMaxConnPerRoute(route).setMaxConnTotal(total));
}

From source file:com.restfiddle.handler.http.builder.RfHttpClientBuilder.java

public CloseableHttpClient build(RfRequestDTO requestDTO, HttpUriRequest request) {
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    //Set Digest Authentication
    digestAuthHandler.setCredentialsProvider(requestDTO, clientBuilder);

    CloseableHttpClient httpClient = clientBuilder.build();
    return httpClient;
}

From source file:at.yawk.buycraft.BuycraftApiImpl.java

public BuycraftApiImpl(BuycraftClientImpl client) {
    this.client = client;

    httpClient = HttpClientBuilder.create().disableCookieManagement().build();
}

From source file:com.comcast.video.dawg.DawgClient.java

private static HttpClient defaultClient(CookieStore cookieStore) {
    return HttpClientBuilder.create().setDefaultCookieStore(cookieStore)
            .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(RestClient.DEFAULT_TIMEOUT).build())
            .disableContentCompression().build();
}

From source file:poisondog.net.ApacheHttpPost.java

public ApacheHttpPost() {
    mHttpClient = HttpClientBuilder.create().build();
    mBuilder = MultipartEntityBuilder.create();
}

From source file:cr.ac.siua.tec.config.RestTemplateConfig.java

@Bean
public HttpClient httpClient() {
    return HttpClientBuilder.create().build();
}

From source file:com.cloud.utils.rest.HttpClientHelper.java

public static CloseableHttpClient createHttpClient(final int maxRedirects)
        throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
    final BasicCookieStore cookieStore = new BasicCookieStore();
    return HttpClientBuilder.create()
            .setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry))
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT)
                    .setMaxRedirects(maxRedirects).build())
            .setDefaultCookieStore(cookieStore).setRetryHandler(new StandardHttpRequestRetryHandler()).build();
}