List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine
StatusLine getStatusLine();
From source file:org.wuspba.ctams.ws.ITBandContestController.java
protected static void delete() throws Exception { String id;/*from w w w . j a v a 2s. c om*/ 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:io.crate.integrationtests.RestSQLActionIntegrationTest.java
@Test public void testWithInvalidPayload() throws IOException { CloseableHttpResponse response = post("{\"foo\": \"bar\"}"); assertEquals(400, response.getStatusLine().getStatusCode()); String bodyAsString = EntityUtils.toString(response.getEntity()); assertThat(bodyAsString, startsWith("{\"error\":{\"message\":\"SQLActionException[Failed to parse source" + " [{\\\"foo\\\": \\\"bar\\\"}]]\",\"code\":4000},\"error_trace\":\"")); }
From source file:com.microsoft.azure.hdinsight.common.task.MultiRestTask.java
@Override public List<String> call() throws Exception { CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider) .build();// w w w .ja va 2s.c o m List<String> results = new ArrayList<>(); for (String path : paths) { HttpGet httpGet = new HttpGet(path); httpGet.addHeader("Content-Type", "application/json"); CloseableHttpResponse response = httpclient.execute(httpGet); int code = response.getStatusLine().getStatusCode(); if (code == 200 || code == 201) { results.add(EntityUtils.toString(response.getEntity())); } else { throw new HDIException(response.getStatusLine().getReasonPhrase(), code); } } return results; }
From source file:com.macrossx.wechat.http.WechatHttpClient.java
public String send(final HttpUriRequest request) { String result = ""; try {/* w w w .j a va 2 s .co m*/ CloseableHttpResponse response = HttpClients.createDefault().execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK != statusCode) { log.info("Failed to get!"); return ""; } HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity, "utf-8"); } catch (IOException e) { log.info(e.getMessage()); } return result; }
From source file:com.srotya.tau.ui.alerts.AlertReceiver.java
/** * @param ruleId//from w w w .ja v a 2 s . c o m * @return */ @SuppressWarnings("unchecked") public Queue<Map<String, Object>> getChannel(short ruleId) throws Exception { CloseableHttpClient client = Utils.buildClient(am.getBaseUrl(), am.getConnectTimeout(), am.getRequestTimeout()); HttpGet get = new HttpGet(am.getBaseUrl() + "/receive/events/" + ruleId); CloseableHttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() < 300) { String result = EntityUtils.toString(response.getEntity()); Gson gson = new Gson(); Queue<Map<String, Object>> queue = gson.fromJson(result, Queue.class); return queue; } return new LinkedList<>(); }
From source file:org.sahli.asciidoc.confluence.publisher.client.http.ConfluenceRestClientTest.java
private static CloseableHttpClient recordHttpClientForMultipleJsonAndStatusCodeResponse( List<String> jsonResponses, List<Integer> statusCodes) throws IOException { CloseableHttpResponse httpResponseMock = mock(CloseableHttpResponse.class); List<HttpEntity> httpEntities = jsonResponses.stream() .map(ConfluenceRestClientTest::recordHttpEntityForContent).collect(toList()); when(httpResponseMock.getEntity()).thenReturn(httpEntities.get(0), httpEntities.subList(1, httpEntities.size()).toArray(new HttpEntity[httpEntities.size() - 1])); List<StatusLine> statusLines = statusCodes.stream().map(ConfluenceRestClientTest::recordStatusLine) .collect(toList());//from ww w . ja va2 s . c o m when(httpResponseMock.getStatusLine()).thenReturn(statusLines.get(0), statusLines.subList(1, statusLines.size()).toArray(new StatusLine[statusLines.size() - 1])); CloseableHttpClient httpClientMock = anyCloseableHttpClient(); when(httpClientMock.execute(any(HttpPost.class), any(HttpContext.class))).thenReturn(httpResponseMock); return httpClientMock; }
From source file:io.crate.integrationtests.SQLHttpIntegrationTest.java
protected String upload(String table, String content) throws IOException { String digest = blobDigest(content); String url = Blobs.url(address, table, digest); HttpPut httpPut = new HttpPut(url); httpPut.setEntity(new StringEntity(content)); CloseableHttpResponse response = httpClient.execute(httpPut); assertThat(response.getStatusLine().getStatusCode(), is(201)); response.close();/* w w w . j a v a2 s . c o m*/ return url; }
From source file:org.restcomm.connect.java.sdk.ExceptionHandler.java
public ExceptionHandler(final CloseableHttpResponse response) { this.response = response; int StatusCode; StatusCode = response.getStatusLine().getStatusCode(); try {//from w w w. ja v a2s. c om if (StatusCode == 401) throw new AuthenticationException("The Credentials Provided Are Incorrect"); if (StatusCode == 404) throw new ResourceNotFoundException("The Requested Resource Is Not Available"); } catch (AuthenticationException e) { e.printStackTrace(); } catch (ResourceNotFoundException e) { e.printStackTrace(); } }
From source file:moxy.ProxyHttpAppTest.java
@Test public void canProxyGetRequests() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet request = new HttpGet("http://localhost:6565/healthcheck"); CloseableHttpResponse response = httpclient.execute(request); try {/*w ww. ja v a 2s . com*/ assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals("{\"deadlocks\":{\"healthy\":true}}", EntityUtils.toString(response.getEntity())); assertableListener.assertConnectionWasMadeOn(6565); } finally { httpclient.close(); } }
From source file:com.arpnetworking.kairosdb.integration.StorageIT.java
@Test public void testStoreDataPoint() throws IOException, JSONException { final Histogram histogram = new Histogram(Arrays.asList(1d, 3d, 5d, 7d, 9d, 1d, 9d)); final int timestamp = 10; final String metricName = "foo_histogram"; final HttpPost request = KairosHelper.postHistogram(timestamp, histogram, metricName); final CloseableHttpResponse response = _client.execute(request); Assert.assertEquals(204, response.getStatusLine().getStatusCode()); final HttpPost queryRequest = KairosHelper.queryFor(timestamp, timestamp, metricName); final CloseableHttpResponse lookupResponse = _client.execute(queryRequest); Assert.assertEquals(200, lookupResponse.getStatusLine().getStatusCode()); final String body = CharStreams .toString(new InputStreamReader(lookupResponse.getEntity().getContent(), Charsets.UTF_8)); final JSONObject responseJson = new JSONObject(body); final JSONObject queryObject = responseJson.getJSONArray("queries").getJSONObject(0); Assert.assertEquals(1, queryObject.getInt("sample_size")); final JSONArray result = queryObject.getJSONArray("results").getJSONObject(0).getJSONArray("values") .getJSONArray(0);//from w ww . j ava 2s. c o m Assert.assertEquals(timestamp, result.getInt(0)); final JSONObject histogramJson = result.getJSONObject(1); final Histogram returnHistogram = new Histogram(histogramJson); Assert.assertEquals(histogram, returnHistogram); }