List of usage examples for org.apache.http.client.methods CloseableHttpResponse close
public void close() throws IOException;
From source file:eu.diacron.crawlservice.app.Util.java
public static String getCrawlid(URL urltoCrawl) { String crawlid = ""; System.out.println("start crawling page"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME, Configuration.REMOTE_CRAWLER_PASS)); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try {// w w w . j av a 2 s . c o m //HttpPost httppost = new HttpPost("http://diachron.hanzoarchives.com/crawl"); HttpPost httppost = new HttpPost(Configuration.REMOTE_CRAWLER_URL_CRAWL_INIT); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("name", UUID.randomUUID().toString())); urlParameters.add(new BasicNameValuePair("scope", "page")); urlParameters.add(new BasicNameValuePair("seed", urltoCrawl.toString())); httppost.setEntity(new UrlEncodedFormEntity(urlParameters)); System.out.println("Executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println("----------------------------------------"); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String inputLine; while ((inputLine = in.readLine()) != null) { crawlid = inputLine; } in.close(); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } finally { try { httpclient.close(); } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } } return crawlid; }
From source file:org.wuspba.ctams.ws.ITBandContestController.java
protected static void delete() throws Exception { String id;//from ww w .j a v a 2 s . c o m CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); id = doc.getBandContests().get(0).getId(); EntityUtils.consume(entity); } httpclient = HttpClients.createDefault(); uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).setParameter("id", id) .build(); HttpDelete httpDelete = new HttpDelete(uri); CloseableHttpResponse response = null; try { response = httpclient.execute(httpDelete); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } ITVenueController.delete(); ITHiredJudgeController.delete(); ITJudgeController.delete(); }
From source file:org.wuspba.ctams.ws.ITJudgeController.java
private static void add(Judge j) throws Exception { CTAMSDocument doc = new CTAMSDocument(); doc.getPeople().add(j.getPerson());//from ww w. j a va 2 s . c om doc.getJudgeQualifications().addAll(j.getQualifications()); doc.getJudges().add(j); String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try { httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); j.setId(doc.getJudges().get(0).getId()); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } }
From source file:com.gemstone.gemfire.rest.internal.web.controllers.RestAPIsAndInterOpsDUnitTest.java
public static void doQueryOpsUsingRestApis(String restEndpoint) { String currentQueryOp = null; try {//from w w w.ja v a 2s. com // Query TestCase-1 :: Prepare parameterized Queries { currentQueryOp = "findAllPeopleQuery"; CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost post = new HttpPost(restEndpoint + findAllPeopleQuery); post.addHeader("Content-Type", "application/json"); post.addHeader("Accept", "application/json"); CloseableHttpResponse createNamedQueryResponse = httpclient.execute(post); assertEquals(createNamedQueryResponse.getStatusLine().getStatusCode(), 201); assertNotNull(createNamedQueryResponse.getEntity()); createNamedQueryResponse.close(); post = new HttpPost(restEndpoint + findPeopleByGenderQuery); post.addHeader("Content-Type", "application/json"); post.addHeader("Accept", "application/json"); createNamedQueryResponse = httpclient.execute(post); assertEquals(createNamedQueryResponse.getStatusLine().getStatusCode(), 201); assertNotNull(createNamedQueryResponse.getEntity()); createNamedQueryResponse.close(); post = new HttpPost(restEndpoint + findPeopleByLastNameQuery); post.addHeader("Content-Type", "application/json"); post.addHeader("Accept", "application/json"); createNamedQueryResponse = httpclient.execute(post); assertEquals(createNamedQueryResponse.getStatusLine().getStatusCode(), 201); assertNotNull(createNamedQueryResponse.getEntity()); createNamedQueryResponse.close(); } // Query TestCase-2 :: List all parameterized queries { currentQueryOp = "listAllQueries"; HttpGet get = new HttpGet(restEndpoint + "/queries"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse listAllQueriesResponse = httpclient.execute(get); assertEquals(listAllQueriesResponse.getStatusLine().getStatusCode(), 200); assertNotNull(listAllQueriesResponse.getEntity()); HttpEntity entity = listAllQueriesResponse.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; StringBuffer sb = new StringBuffer(); while ((line = reader.readLine()) != null) { sb.append(line); } listAllQueriesResponse.close(); // Check whether received response contains expected query IDs. JSONObject jsonObject = new JSONObject(sb.toString()); JSONArray jsonArray = jsonObject.getJSONArray("queries"); for (int i = 0; i < jsonArray.length(); i++) { assertTrue("PREPARE_PARAMETERIZED_QUERY: function IDs are not matched", Arrays .asList(PARAM_QUERY_IDS_ARRAY).contains(jsonArray.getJSONObject(i).getString("id"))); } } // Query TestCase-3 :: Run the specified named query passing in scalar values for query parameters. { currentQueryOp = "filterByLastName"; CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost post = new HttpPost(restEndpoint + "/queries/filterByLastName"); post.addHeader("Content-Type", "application/json"); post.addHeader("Accept", "application/json"); StringEntity entity = new StringEntity(QUERY_ARGS); post.setEntity(entity); CloseableHttpResponse runNamedQueryResponse = httpclient.execute(post); assertEquals(200, runNamedQueryResponse.getStatusLine().getStatusCode()); assertNotNull(runNamedQueryResponse.getEntity()); } } catch (Exception e) { throw new RuntimeException("unexpected exception", e); } }
From source file:org.eclipse.wst.json.core.internal.download.HttpClientProvider.java
private static long getLastModified(URL url) { CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; try {// www .j ava2 s . co m HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); Builder builder = RequestConfig.custom(); HttpHost proxy = getProxy(target); if (proxy != null) { builder = builder.setProxy(proxy); } RequestConfig config = builder.build(); HttpHead request = new HttpHead(url.toURI()); request.setConfig(config); response = httpclient.execute(target, request); Header[] s = response.getHeaders("last-modified"); if (s != null && s.length > 0) { String lastModified = s[0].getValue(); return new Date(lastModified).getTime(); } } catch (Exception e) { logWarning(e); return -1; } finally { if (response != null) { try { response.close(); } catch (IOException e) { } } try { httpclient.close(); } catch (IOException e) { } } return -1; }
From source file:org.opennms.minion.core.impl.ScvEnabledRestClientImpl.java
@Override public void ping() throws Exception { // Setup a client with pre-emptive authentication HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(username, password)); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try {/*from w w w. ja va 2 s . c o m*/ AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); // Issue a simple GET against the Info endpoint HttpGet httpget = new HttpGet(url.toExternalForm() + "/rest/info"); CloseableHttpResponse response = httpclient.execute(target, httpget, localContext); response.close(); } finally { httpclient.close(); } }
From source file:callbacks.AuthenticationCallback.java
private void closeGracefully(final CloseableHttpResponse response) { if (response != null) { try {//from w w w . jav a 2 s .c o m response.close(); } catch (IOException e) { log.debug("Could not close "); } } }
From source file:com.ibm.streamsx.topology.internal.streaminganalytics.RestUtils.java
public static JsonObject getGsonResponse(CloseableHttpClient httpClient, HttpRequestBase request) throws IOException, ClientProtocolException { request.addHeader("accept", ContentType.APPLICATION_JSON.getMimeType()); CloseableHttpResponse response = httpClient.execute(request); JsonObject jsonResponse;//w ww .jav a 2s.com try { HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { final String errorInfo; if (entity != null) errorInfo = " -- " + EntityUtils.toString(entity); else errorInfo = ""; throw new IllegalStateException( "Unexpected HTTP resource from service:" + response.getStatusLine().getStatusCode() + ":" + response.getStatusLine().getReasonPhrase() + errorInfo); } if (entity == null) throw new IllegalStateException("No HTTP resource from service"); Reader r = new InputStreamReader(entity.getContent()); jsonResponse = new Gson().fromJson(r, JsonObject.class); EntityUtils.consume(entity); } finally { response.close(); } return jsonResponse; }
From source file:org.wuspba.ctams.ws.ITBandRegistrationController.java
protected static void add() throws Exception { ITBandController.add();/* w ww .ja v a 2s . c om*/ CTAMSDocument doc = new CTAMSDocument(); doc.getBands().add(TestFixture.INSTANCE.skye); doc.getBandRegistrations().add(TestFixture.INSTANCE.bandRegistration); String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try { httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); TestFixture.INSTANCE.bandRegistration.setId(doc.getBandRegistrations().get(0).getId()); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } }
From source file:org.wuspba.ctams.ws.ITSoloRegistrationController.java
private static void add() throws Exception { ITPersonController.add();//from www. jav a 2s . c o m CTAMSDocument doc = new CTAMSDocument(); doc.getPeople().add(TestFixture.INSTANCE.elaine); doc.getSoloRegistrations().add(TestFixture.INSTANCE.soloRegistration); String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try { httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); TestFixture.INSTANCE.soloRegistration.setId(doc.getSoloRegistrations().get(0).getId()); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } }