List of usage examples for org.apache.http.impl.client HttpClientBuilder create
public static HttpClientBuilder create()
From source file:edu.emory.bmi.medicurator.tcia.TciaQuery.java
private HttpClient getInsecureClient() throws Exception { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; }/*from w ww.j ava 2s . co m*/ }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); HttpClientBuilder builder = HttpClientBuilder.create(); builder.setSSLSocketFactory(sslsf); if (Constants.PROXY_HOST != null && Constants.PROXY_PORT != null) { HttpHost proxy = new HttpHost(Constants.PROXY_HOST, Constants.PROXY_PORT, "http"); builder.setProxy(proxy); } if (Constants.PROXY_USERNAME != null && Constants.PROXY_PASSWORD != null) { Credentials credentials = new UsernamePasswordCredentials(Constants.PROXY_USERNAME, Constants.PROXY_PASSWORD); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, credentials); builder.setDefaultCredentialsProvider(credsProvider); } return builder.build(); }
From source file:juzu.impl.plugin.asset.AbstractAssetTestCase.java
@Test public void testSatisfied() throws Exception { URL url = applicationURL();/* www . j a va2s . c o m*/ driver.get(url.toString()); List<WebElement> scripts = driver.findElements(getFindBy()); String expected = getExpectedAsset(); if (expected != null) { if (scripts.size() != 1) { throw failure("Was expecting scripts to match the single asset " + expected + " instead of being " + scripts); } else { String src = scripts.get(0).getAttribute("src"); assertTrue("Was expecting " + src + " to end with " + expected, src.endsWith(expected)); url = new URL(url, src); HttpGet get = new HttpGet(url.toURI()); HttpResponse response = HttpClientBuilder.create().build().execute(get); assertEquals(200, response.getStatusLine().getStatusCode()); assertNotNull(response.getEntity()); Header[] headers = response.getHeaders("Cache-Control"); assertEquals(1, headers.length); assertEquals(getExpectedCacheControl(), headers[0].getValue()); assertEquals(getExpectedContent(), EntityUtils.toString(response.getEntity())); } } else { assertEquals(Collections.emptyList(), scripts); } }
From source file:com.angelmmg90.consumerservicespotify.configuration.SpringWebConfig.java
@Bean public static RestTemplate getTemplate() throws IOException { if (template == null) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(PROXY_HOST, PROXY_PORT), new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD)); Header[] h = new Header[3]; h[0] = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"); h[1] = new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + ACCESS_TOKEN); List<Header> headers = new ArrayList<>(Arrays.asList(h)); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.useSystemProperties(); clientBuilder.setProxy(new HttpHost(PROXY_HOST, PROXY_PORT)); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); clientBuilder.setDefaultHeaders(headers).build(); String SAMPLE_URL = "https://api.spotify.com/v1/users/yourUserName/playlists/7HHFd1tNiIFIwYwva5MTNv"; HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build(); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); CloseableHttpClient client = clientBuilder.build(); client.execute(request);//from www . ja v a 2 s. c o m HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(client); template = new RestTemplate(); template.setRequestFactory(factory); } return template; }
From source file:org.ado.biblio.desktop.server.ServerPullingService.java
@Override protected Task<BookMessageDTO[]> createTask() { return new Task<BookMessageDTO[]>() { @Override/*from w w w. jav a 2s . c o m*/ protected BookMessageDTO[] call() throws Exception { final String requestUrl = String.format(SERVER_PULL_URL, AppConfiguration.getConfigurationProperty("server.url"), clientId); try { HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(requestUrl); HttpResponse response = client.execute(request); LOGGER.info("Response code [{}]", response.getStatusLine().getStatusCode()); String responseContent = IOUtils.toString(response.getEntity().getContent()); final BookMessageDTO[] bookMessageDTOs = new Gson().fromJson(responseContent, BookMessageDTO[].class); return bookMessageDTOs; } catch (Exception e) { LOGGER.error(String.format("Unable to pull server %s", requestUrl), e); pause(15); throw e; } } }; }
From source file:org.obm.sync.push.client.OPClientTest.java
@Before public void setUp() { httpClient = HttpClientBuilder.create().build(); String loginAtDomain = "log@domain"; char[] password = "pwd".toCharArray(); DeviceId devId = new DeviceId("devId"); String devType = "devType"; String userAgent = "userAgent"; String url = "url"; opClient = new OPClient(httpClient, loginAtDomain, password, devId, devType, userAgent, url, ProtocolVersion.V121) {//from www. ja v a 2s . co m @Override public Document postXml(String namespace, Document doc, String cmd, String policyKey, boolean multipart) throws TransformerException, WBXmlException, IOException, HttpRequestException { throw new RuntimeException("this testing OPClient cannot performs request"); } @Override public <T> Future<T> postASyncXml(Async async, String namespace, Document doc, String cmd, String policyKey, boolean multipart, ResponseTransformer<T> documentHandler) throws TransformerException, WBXmlException, IOException, HttpRequestException { throw new RuntimeException("this testing OPClient cannot performs request"); } }; }
From source file:org.devdom.commons.util.Utils.java
public static InputStream httpGet(String url, Map<String, String> headers) throws Exception { InputStream result = null;/* w w w. ja v a 2s .c o m*/ HttpGet get = new HttpGet(url); if (headers != null) { for (Entry<String, String> header : headers.entrySet()) { get.addHeader(header.getKey(), header.getValue()); } } HttpClient client = HttpClientBuilder.create().build(); HttpResponse httpResponse = client.execute(get); if (httpResponse.getEntity() != null) { result = httpResponse.getEntity().getContent(); } return result; }
From source file:com.esri.geoportal.harvester.gptsrc.GptBroker.java
@Override public void initialize(InitContext context) throws DataProcessorException { definition.override(context.getParams()); td = context.getTask().getTaskDefinition(); CloseableHttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build(); if (context.getTask().getTaskDefinition().isIgnoreRobotsTxt()) { client = new Client(httpClient, definition.getHostUrl(), definition.getCredentials(), definition.getIndex());/*w w w . ja v a 2s .c o m*/ } else { Bots bots = BotsUtils.readBots(definition.getBotsConfig(), httpClient, definition.getHostUrl()); client = new Client(new BotsHttpClient(httpClient, bots), definition.getHostUrl(), definition.getCredentials(), definition.getIndex()); } }
From source file:org.hawkular.component.availcreator.AvailPublisher.java
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public void sendToMetricsViaRest(List<SingleAvail> availabilities) { // Send it to metrics via rest HttpClient client = HttpClientBuilder.create().build(); for (SingleAvail avr : availabilities) { String rid = avr.id;/* w w w. j a v a2s . c om*/ String tenantId = avr.tenantId; HttpPost request = new HttpPost(METRICS_BASE_URI + "/availability/" + rid + "/data"); request.addHeader("Hawkular-Tenant", tenantId); Availability availability = new Availability(avr.timestamp, avr.avail.toLowerCase()); List<Availability> list = new ArrayList<>(1); list.add(availability); String payload = new Gson().toJson(list); request.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON)); try { HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() > 399) { Log.LOG.wAvailPostStatus(response.getStatusLine().toString()); } } catch (IOException e) { Log.LOG.wAvailPostStatus(e.getMessage()); } } }