List of usage examples for org.apache.http.impl.client HttpClientBuilder create
public static HttpClientBuilder create()
From source file:com.microsoft.windowsazure.core.apache.HttpResponseInterceptorAdapterTest.java
@Test public void responseFilterShouldWork() throws Exception { ServiceResponseFilter filterFirst = new ServiceResponseFilter() { @Override//from ww w. j av a 2s . c o m public void filter(ServiceRequestContext request, ServiceResponseContext response) { response.setHeader("header1", "value1"); } }; ServiceResponseFilter filterLast = new ServiceResponseFilter() { @Override public void filter(ServiceRequestContext request, ServiceResponseContext response) { response.setHeader("header1", "value2"); } }; class DummyClient extends ServiceClient<DummyClient> { public URI baseUri; public DummyClient() { super(HttpClientBuilder.create(), null); } @Override protected DummyClient newInstance(HttpClientBuilder httpClientBuilder, ExecutorService executorService) { return new DummyClient(); } } DummyClient client = new DummyClient(); client.baseUri = new URI("http://www.microsoft.com"); addClient((ServiceClient<?>) client, new Callable<Void>() { @Override public Void call() throws Exception { return null; } }); setupTest(); client.withResponseFilterFirst(filterFirst); client.withResponseFilterFirst(filterLast); HttpGet request = new HttpGet(client.baseUri); HttpResponse response = null; try { response = client.getHttpClient().execute(request); } finally { assertEquals(response.getFirstHeader("header1").getValue(), "value1"); resetTest(); } }
From source file:com.ibm.mil.readyapps.telco.adapters.CloudantGeoAdapterResource.java
@GET @Produces(MediaType.APPLICATION_JSON)/*from ww w. j a va2 s. c om*/ @Path("/{user_id}/wifi") public Response getWifiLocations(@PathParam("user_id") String name, @QueryParam(value = "lat") double latitude, @QueryParam(value = "lon") double longitude) { GeoJsonPoint userLocation = new GeoJsonPoint(latitude, longitude); try { HttpGet httpget = new HttpGet(GeoRadiusURI.build(DEMO_LOCATION)); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpResponse httpResponse = httpClient.execute(httpget); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.OK.getCode()) { String jsonString = EntityUtils.toString(httpResponse.getEntity()); List<WifiHotspotFlat> hotspots = WifiHotspotUtils.parseAndOffsetHotspots(jsonString, userLocation); return Response.ok(new Gson().toJson(hotspots), MediaType.APPLICATION_JSON).build(); } httpClient.close(); return Response.serverError().entity(httpResponse.getStatusLine()).build(); } catch (URISyntaxException | ParseException | IOException e) { e.printStackTrace(); return Response.serverError().entity("Error").build(); } }
From source file:com.fredhopper.core.connector.index.upload.impl.RestTemplateProvider.java
public RestTemplate createTemplate(final String host, final Integer port, final String username, final String password) { Preconditions.checkArgument(StringUtils.isNotBlank(host)); Preconditions.checkArgument(port != null); Preconditions.checkArgument(StringUtils.isNotBlank(username)); Preconditions.checkArgument(StringUtils.isNotBlank(password)); final AuthScope authscope = new AuthScope(host, port.intValue()); final Credentials credentials = new UsernamePasswordCredentials(username, password); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(authscope, credentials); final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); final CloseableHttpClient httpClient = clientBuilder.build(); final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); return new RestTemplate(requestFactory); }
From source file:guru.nidi.ramltester.httpcomponents.RamlHttpClient.java
public RamlHttpClient(RamlChecker checker) { this(checker, false, new ThreadLocalReportStore(), HttpClientBuilder.create().build()); }
From source file:com.cognifide.qa.bb.provider.http.HttpClientProvider.java
/** * Produces http client and configures it with provided credentials. *///from w ww . j a v a 2s.c om @Override public CloseableHttpClient get() { HttpClientBuilder builder = HttpClientBuilder.create(); if (StringUtils.isNotBlank(login) && StringUtils.isNotBlank(password)) { builder.setDefaultCredentialsProvider(createCredentialsProvider(url, login, password)); } return builder.build(); }
From source file:com.netflix.spinnaker.halyard.config.validate.v1.security.SamlValidator.java
@Override public void validate(ConfigProblemSetBuilder p, Saml saml) { if (!saml.isEnabled()) { return;//from w w w. j av a 2s . c om } if (StringUtils.isEmpty(saml.getMetadataLocal()) && StringUtils.isEmpty(saml.getMetadataRemote())) { p.addProblem(Problem.Severity.ERROR, "No metadata file specified."); } if (StringUtils.isNotEmpty(saml.getMetadataLocal())) { try { new File(new URI("file:" + saml.getMetadataLocal())); } catch (Exception f) { p.addProblem(Problem.Severity.ERROR, f.getMessage()); } } if (StringUtils.isNotEmpty(saml.getMetadataRemote())) { try { HttpClientBuilder.create().build().execute(new HttpGet(saml.getMetadataRemote())); } catch (IOException e) { p.addProblem(Problem.Severity.WARNING, "Cannot access remote metadata.xml file: " + e.getMessage()); } } if (StringUtils.isEmpty(saml.getIssuerId())) { p.addProblem(Problem.Severity.ERROR, "No issuerId specified."); } if (StringUtils.isEmpty(saml.getKeyStore())) { p.addProblem(Problem.Severity.ERROR, "No keystore specified."); } if (StringUtils.isEmpty(saml.getKeyStorePassword())) { p.addProblem(Problem.Severity.ERROR, "No keystore password specified."); } if (StringUtils.isEmpty(saml.getKeyStoreAliasName())) { p.addProblem(Problem.Severity.ERROR, "No keystore alias specified."); } InputStream is = null; try { File f = new File(new URI("file:" + saml.getKeyStore())); is = new FileInputStream(f); val keystore = KeyStore.getInstance(KeyStore.getDefaultType()); // will throw an exception if `keyStorePassword` is invalid keystore.load(is, saml.getKeyStorePassword().toCharArray()); Collections.list(keystore.aliases()).stream() .filter(alias -> alias.equalsIgnoreCase(saml.getKeyStoreAliasName())).findFirst() .orElseThrow(() -> new RuntimeException( "Keystore does not contain alias " + saml.getKeyStoreAliasName())); } catch (Exception e) { p.addProblem(Problem.Severity.ERROR, "Keystore validation problem: " + e.getMessage()); } finally { if (is != null) { try { is.close(); } catch (Exception e) { // ignored. } } } if (saml.getServiceAddress() == null) { p.addProblem(Problem.Severity.ERROR, "No service address specified."); } else if (!saml.getServiceAddress().getProtocol().equalsIgnoreCase("https")) { p.addProblem(Problem.Severity.WARNING, "Gate should operate on HTTPS"); } }
From source file:org.keycloak.testsuite.account.AccountRestServiceCorsTest.java
@Before public void before() { client = HttpClientBuilder.create().build(); oauth.clientId("direct-grant"); executor = (JavascriptExecutor) driver; }
From source file:com.helpers.ServiceLocationsHelper.java
private static String getAccountId(String opportunityID) { // System.out.println(opportunityID); String uri = AuthenticateAuthorizeTest.baseUri + "/sobjects/Opportunity/" + opportunityID; try {//ww w .j a v a 2s. c o m HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(uri); // System.out.println("oauthHeader2: " + oauthHeader); httpGet.addHeader(AuthenticateAuthorizeTest.oauthHeader); httpGet.addHeader(AuthenticateAuthorizeTest.prettyPrintHeader); // Make the request. HttpResponse response = httpClient.execute(httpGet); String getResponse = EntityUtils.toString(response.getEntity()); // System.out.println("Query Result:\n" + getResponse); return new JSONObject(getResponse).getString("AccountId"); } catch (IOException | ParseException | JSONException e) { } return ""; }
From source file:co.tuzza.clicksend4j.http.HttpClientUtils.java
public HttpClient getHttpClient() { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setUserAgent("ClickSend4J 0.0.1"); httpClientBuilder.setConnectionManager(clientConnectionManager); httpClientBuilder.setDefaultRequestConfig(buildRequestConfig()); return httpClientBuilder.build(); }