List of usage examples for org.apache.http.impl.client HttpClientBuilder build
public CloseableHttpClient build()
From source file:com.shenit.commons.utils.HttpUtils.java
/** * Get?//from w w w. j av a2 s. com * * @param url * ? * @return */ public static String getAsString(String url) { HttpGet request = new HttpGet(url); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); CloseableHttpClient client = clientBuilder.build(); LOG.info("[getAsString] execute url >> {}", url); try { CloseableHttpResponse resp = client.execute(request); int status = resp.getStatusLine().getStatusCode(); if (status == 200) { String result = EntityUtils.toString(resp.getEntity(), ENC_UTF8); LOG.info("[getAsString] execute result for {} >> {}", url, result); return StringUtils.trim(result); } else { LOG.error("execute with incorrect status code >> {}", status); } } catch (IOException e) { LOG.error("execute with exception >> {}", e.getMessage()); } return null; }
From source file:org.fao.geonet.utils.GeonetHttpRequestFactory.java
public ClientHttpResponse execute(HttpUriRequest request, Function<HttpClientBuilder, Void> configurator) throws IOException { final HttpClientBuilder clientBuilder = getDefaultHttpClientBuilder(); configurator.apply(clientBuilder);/*from ww w.j a v a2 s. c om*/ CloseableHttpClient httpClient = clientBuilder.build(); return new AdaptingResponse(httpClient, httpClient.execute(request)); }
From source file:net.officefloor.plugin.servlet.container.integrate.HttpServletIntegrateTest.java
/** * Ensure can handle authenticated {@link HttpRequest}. *///from www. j a v a 2s. com public void testAuthenticatedRequest() throws Exception { // Specify servicing setServicing(new Servicer() { @Override public String service(HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Determine if authenticated String remoteUser = req.getRemoteUser(); if (remoteUser == null) { // Challenge for authentication resp.setStatus(HttpStatus.SC_UNAUTHORIZED); resp.setHeader("WWW-Authenticate", "Basic realm=\"TestRealm\""); return "Challenge"; // challenge constructed } // Send response to user return "Hello " + req.getRemoteUser(); } }); // Provide preemptive authentication HttpClientBuilder builder = HttpClientBuilder.create(); HttpTestUtil.configureCredentials(builder, "TestRealm", null, "Daniel", "password"); try (CloseableHttpClient client = builder.build()) { // Send request HttpGet request = new HttpGet(this.getServerUrl()); HttpResponse response = client.execute(request); // Validate the response assertHttpResponse(response, 200, "Hello Daniel"); } }
From source file:org.fao.geonet.utils.GeonetHttpRequestFactory.java
public ClientHttpResponse execute(HttpUriRequest request, Function<HttpClientBuilder, Void> configurator, AbstractHttpRequest r) throws IOException { final HttpClientBuilder clientBuilder = getDefaultHttpClientBuilder(); configurator.apply(clientBuilder);/*from w w w. ja va 2s.c o m*/ CloseableHttpClient httpClient = clientBuilder.build(); if (r.isPreemptiveBasicAuth() || r.getHttpClientContext() != null) { return new AdaptingResponse(httpClient, httpClient.execute(request, r.getHttpClientContext())); } else { return new AdaptingResponse(httpClient, httpClient.execute(request)); } }
From source file:eu.vital.TrustManager.connectors.ppi.PPIManager.java
private JSONArray getPPIObservations(String ppi_url, String body) throws ParseException { JSONArray data = new JSONArray(); HttpClientBuilder builder = HttpClientBuilder.create(); try {// www.j a va2s . co m final CloseableHttpClient client = builder.build(); HttpPost post = new HttpPost(ppi_url); post.setHeader(HTTP.CONTENT_TYPE, "application/json"); HttpEntity entity = new StringEntity(body); post.setEntity(entity); HttpResponse clientresponse = client.execute(post); if (clientresponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) return null; final String sdata = EntityUtils.toString(clientresponse.getEntity(), StandardCharsets.UTF_8); data = new JSONArray(sdata); } catch (IOException ioe) { //logger.error(ioe); } return data; }
From source file:fr.treeptik.cloudunit.utils.JSONClient.java
public CloseableHttpClient buildSecureHttpClient(Boolean httpRequired) throws IOException { if (isTLSActivated && !httpRequired) { org.apache.http.impl.client.HttpClientBuilder builder = HttpClients.custom(); HttpClientConnectionManager manager = getConnectionFactory(certsDirPath, 10); builder.setConnectionManager(manager); return builder.build(); } else {/* w w w.j a va 2 s .c o m*/ return HttpClients.createDefault(); } }
From source file:com.github.segator.scaleway.api.ScalewayClient.java
public ScalewayClient(String accessToken, String organizationToken, ScalewayComputeRegion region) { this.accessToken = accessToken; this.organizationToken = organizationToken; this.region = region; HttpClientBuilder httpBuilder = HttpClients.custom(); httpBuilder.setUserAgent(ScalewayConstants.USER_AGENT); this.httpclient = httpBuilder.build(); }
From source file:com.srotya.collectd.storm.StormNimbusMetrics.java
public void fetchTopologyMetrics(String url, String topologyId, ValueList values, HttpClientBuilder builder, Gson gson) throws ClientProtocolException, IOException { CloseableHttpClient client = builder.build(); HttpGet get = new HttpGet(url + "/api/v1/topology/" + topologyId + "?window=600"); CloseableHttpResponse result = client.execute(get, context); if (result.getStatusLine().getStatusCode() == 200) { String metrics = EntityUtils.toString(result.getEntity()); JsonObject topologyMetrics = gson.fromJson(metrics, JsonObject.class); if (topologyMetrics.has(SPOUTS)) { JsonArray spouts = topologyMetrics.get(SPOUTS).getAsJsonArray(); for (JsonElement spoutElement : spouts) { for (String field : SPOUT_PROPS) { addDataSourceAndValue(spoutElement, SPOUT_ID, field, values); }//w ww. j a v a 2s .c o m } } else { Collectd.logError("Topology:" + topologyId + " has no Spouts"); } if (topologyMetrics.has(BOLTS)) { JsonArray bolts = topologyMetrics.get(BOLTS).getAsJsonArray(); for (JsonElement boltElement : bolts) { for (String field : BOLT_PROPS) { addDataSourceAndValue(boltElement, BOLT_ID, field, values); } } } else { Collectd.logError("Topology:" + topologyId + " has no Bolts"); } } else { if (result.getStatusLine().getStatusCode() == 401) { Collectd.logError( "Looks like supplied Kerberos account can't read topology metrics for:" + topologyId); } else { Collectd.logError("Failed to fetch topology metrics:" + result.getStatusLine() + "\t" + EntityUtils.toString(result.getEntity())); } } }
From source file:ee.ria.xroad.proxy.serverproxy.HttpClientCreator.java
private void build() throws HttpClientCreatorException { RegistryBuilder<ConnectionSocketFactory> sfr = RegistryBuilder.create(); sfr.register("http", PlainConnectionSocketFactory.INSTANCE); try {/*from ww w . j a v a 2 s. c om*/ sfr.register("https", createSSLSocketFactory()); } catch (Exception e) { throw new HttpClientCreatorException("Creating SSL Socket Factory failed", e); } connectionManager = new PoolingHttpClientConnectionManager(sfr.build()); connectionManager.setMaxTotal(CLIENT_MAX_TOTAL_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(CLIENT_MAX_CONNECTIONS_PER_ROUTE); connectionManager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build()); RequestConfig.Builder rb = RequestConfig.custom(); rb.setConnectTimeout(CLIENT_TIMEOUT); rb.setConnectionRequestTimeout(CLIENT_TIMEOUT); rb.setSocketTimeout(CLIENT_TIMEOUT); HttpClientBuilder cb = HttpClients.custom(); cb.setDefaultRequestConfig(rb.build()); cb.setConnectionManager(connectionManager); // Disable request retry cb.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); httpClient = cb.build(); }
From source file:net.siegmar.japtproxy.fetcher.HttpClientConfigurer.java
public CloseableHttpClient build() throws InitializationException { final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(MAX_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(MAX_CONNECTIONS); final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectTimeout) .setSocketTimeout(socketTimeout).build(); final HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig); if (configuration.getHttpProxy() != null) { configureProxy(httpClientBuilder, configuration.getHttpProxy()); }/*from w w w . j a va 2 s . co m*/ return httpClientBuilder.build(); }