List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine
StatusLine getStatusLine();
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.DeliveryServicesTest.java
@Test public void itReturnsNotFoundForNonexistentDeliveryService() throws Exception { String encodedUrl = URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff", "utf-8"); HttpGet httpGet = new HttpGet("http://localhost:3333/crs/deliveryservices?url=" + encodedUrl); CloseableHttpResponse response = null; try {//from www.ja v a2 s . com response = closeableHttpClient.execute(httpGet); assertThat(response.getStatusLine().getStatusCode(), equalTo(404)); } finally { if (response != null) response.close(); } }
From source file:com.anjlab.logback.hipchat.HipChatRoom.java
/** * https://www.hipchat.com/docs/apiv2/method/send_room_notification * //from ww w . j av a 2 s .co m * @param message */ public void sendMessage(HipChatMessage message) { String json = gson.toJson(message); HttpPost request = new HttpPost(roomUri); request.addHeader(contentTypeHeader); request.addHeader(authorizationHeader); request.setEntity(new StringEntity(json, Charset.defaultCharset())); CloseableHttpResponse response = null; try { response = closeableHttpClient.execute(request); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) { logError(message, response.getStatusLine()); } } catch (IOException e) { logError(message, e); } finally { IOUtils.closeQuietly(response); } }
From source file:com.anrisoftware.simplerest.core.AbstractRepeatSimpleGetWorker.java
@Override protected T repeatRequest(CloseableHttpResponse previouslyResponse, HttpRequest previouslyRequest, StatusLine previouslyStatusLine) throws SimpleRestException { int repeateCount = requestsRepeateCount; int repeateMax = requestsRepeateMax; Duration repeateWait = requestsRepeateWait; if (repeateCount == repeateMax) { throw parseErrorMessage(previouslyRequest, previouslyResponse, previouslyStatusLine); }/* w w w .j av a 2s . c o m*/ try { log.repeatRequestsSleep(parent, repeateCount, repeateWait); Thread.sleep(repeateWait.getMillis()); } catch (InterruptedException e) { return null; } CloseableHttpClient httpclient = createHttpClient(); HttpGet httpget = createHttpGet(); CloseableHttpResponse response = executeRequest(httpclient, httpget); StatusLine statusLine = response.getStatusLine(); repeateCount++; repeateWait = repeateWait.plus(requestsRepeateWaitDuration); this.requestsRepeateCount = repeateCount; this.requestsRepeateWait = repeateWait; return parseResponse(response, httpget, statusLine); }
From source file:com.networknt.traceability.TraceabilityHandlerTest.java
@Test public void testGetWithoutTid() throws Exception { String url = "http://localhost:8080/get"; CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); try {/*from ww w.ja v a2 s . c om*/ CloseableHttpResponse response = client.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); Assert.assertEquals(200, statusCode); if (statusCode == 200) { String s = IOUtils.toString(response.getEntity().getContent(), "utf8"); Assert.assertNotNull(s); Assert.assertEquals("get", s); Header tid = response.getFirstHeader(Constants.TRACEABILITY_ID); Assert.assertNull(tid); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.xx_dev.apn.proxy.test.TestProxyWithHttpClient.java
private void test(String uri, int exceptCode, String exceptHeaderName, String exceptHeaderValue) { ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(2000);//from w w w .j a va 2 s . c o m cm.setDefaultMaxPerRoute(40); cm.setDefaultConnectionConfig(connectionConfig); CloseableHttpClient httpClient = HttpClients.custom() .setUserAgent("Mozilla/5.0 xx-dev-web-common httpclient/4.x").setConnectionManager(cm) .disableContentCompression().disableCookieManagement().build(); HttpHost proxy = new HttpHost("127.0.0.1", ApnProxyConfig.getConfig().getPort()); RequestConfig config = RequestConfig.custom().setProxy(proxy).setExpectContinueEnabled(true) .setConnectionRequestTimeout(5000).setConnectTimeout(10000).setSocketTimeout(10000) .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); HttpGet request = new HttpGet(uri); request.setConfig(config); try { CloseableHttpResponse httpResponse = httpClient.execute(request); Assert.assertEquals(exceptCode, httpResponse.getStatusLine().getStatusCode()); if (StringUtils.isNotBlank(exceptHeaderName) && StringUtils.isNotBlank(exceptHeaderValue)) { Assert.assertEquals(exceptHeaderValue, httpResponse.getFirstHeader(exceptHeaderName).getValue()); } ResponseHandler<String> responseHandler = new BasicResponseHandler(); responseHandler.handleResponse(httpResponse); httpResponse.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } }
From source file:org.apache.camel.component.hipchat.HipchatProducer.java
protected StatusLine post(String urlPath, Map<String, String> postParam) throws IOException { HttpPost httpPost = new HttpPost(getConfig().hipChatUrl() + urlPath); httpPost.setEntity(new StringEntity(MAPPER.writeValueAsString(postParam), ContentType.APPLICATION_JSON)); CloseableHttpResponse closeableHttpResponse = HTTP_CLIENT.execute(httpPost); try {/*from ww w . j a va 2s . c o m*/ return closeableHttpResponse.getStatusLine(); } finally { closeableHttpResponse.close(); } }
From source file:com.networknt.limit.LimitHandlerTest.java
@Test public void testOneRequest() throws Exception { String url = "http://localhost:8080"; CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); try {/*w w w .j a v a 2s . com*/ CloseableHttpResponse response = client.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); String body = IOUtils.toString(response.getEntity().getContent(), "utf8"); Assert.assertEquals(200, statusCode); if (statusCode == 200) { Assert.assertNotNull(body); Assert.assertEquals("OK", body); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.ibm.devops.dra.AbstractDevOpsAction.java
/** * Get a list of policies that belong to an org * @param token//from ww w . j a v a2s. co m * @param orgName * @return */ public static ListBoxModel getPolicyList(String token, String orgName, String toolchainName, String environment, Boolean debug_mode) { // get all jenkins job ListBoxModel emptybox = new ListBoxModel(); emptybox.add("", "empty"); String url = choosePoliciesUrl(environment); try { url = url.replace("{org_name}", URLEncoder.encode(orgName, "UTF-8").replaceAll("\\+", "%20")); url = url.replace("{toolchain_name}", URLEncoder.encode(toolchainName, "UTF-8").replaceAll("\\+", "%20")); if (debug_mode) { LOGGER.info("GET POLICIES URL:" + url); } CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); httpGet = addProxyInformation(httpGet); httpGet.setHeader("Authorization", token); CloseableHttpResponse response = null; response = httpClient.execute(httpGet); String resStr = EntityUtils.toString(response.getEntity()); if (debug_mode) { LOGGER.info("RESPONSE FROM GET POLICIES URL:" + response.getStatusLine().toString()); } if (response.getStatusLine().toString().contains("200")) { // get 200 response JsonParser parser = new JsonParser(); JsonElement element = parser.parse(resStr); JsonArray jsonArray = element.getAsJsonArray(); ListBoxModel model = new ListBoxModel(); for (int i = 0; i < jsonArray.size(); i++) { JsonObject obj = jsonArray.get(i).getAsJsonObject(); String name = String.valueOf(obj.get("name")).replaceAll("\"", ""); model.add(name, name); } if (debug_mode) { LOGGER.info("POLICY LIST:" + model); LOGGER.info("#######################"); } return model; } else { if (debug_mode) { LOGGER.info("RETURNED STATUS CODE OTHER THAN 200. RESPONSE: " + response.getStatusLine().toString()); } return emptybox; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return emptybox; }
From source file:com.networknt.traceability.TraceabilityHandlerTest.java
@Test public void testGetWithTid() throws Exception { String url = "http://localhost:8080/get"; CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); httpGet.setHeader(Constants.TRACEABILITY_ID, "12345"); try {/*from w w w .j a v a2 s . c om*/ CloseableHttpResponse response = client.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); Assert.assertEquals(200, statusCode); if (statusCode == 200) { String s = IOUtils.toString(response.getEntity().getContent(), "utf8"); Assert.assertNotNull(s); Assert.assertEquals("get", s); Header tid = response.getFirstHeader(Constants.TRACEABILITY_ID); Assert.assertEquals("12345", tid.getValue()); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.yahoo.gondola.container.client.ApacheHttpComponentProxyClient.java
private Response getResponse(CloseableHttpResponse proxiedResponse) throws IOException { Response.ResponseBuilder builder = Response.status(proxiedResponse.getStatusLine().getStatusCode()); if (proxiedResponse.getEntity() != null) { builder.entity(EntityUtils.toString(proxiedResponse.getEntity())); for (Header header : proxiedResponse.getAllHeaders()) { builder.header(header.getName(), header.getValue()); }//from ww w . ja va2s . com } return builder.build(); }