List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine
StatusLine getStatusLine();
From source file:pl.wavesoftware.wfirma.api.simple.mapper.SimpleGateway.java
private String getContent(CloseableHttpResponse response) throws WFirmaException, IOException { switch (response.getStatusLine().getStatusCode()) { case 200:// w ww. j ava 2 s. c om return handleCode200OK(response); case 403: throw new WFirmaSecurityException("Auth failed for user: `%s`", credentials.getConsumerKey()); default: StatusLine status = response.getStatusLine(); throw new WFirmaException("Connection error: %d - %s", status.getStatusCode(), status.getReasonPhrase()); } }
From source file:ar.edu.ubp.das.src.chat.actions.DashboardAction.java
@Override public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request, HttpServletResponse response) throws SQLException, RuntimeException { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { String url = "http://25.136.78.82:8080/salas"; HttpGet getRequest = new HttpGet(url); String authToken = String.valueOf(request.getSession().getAttribute("token")); getRequest.addHeader("Authorization", "BEARER " + authToken); getRequest.addHeader("accept", "application/json; charset=ISO-8859-1"); CloseableHttpResponse getResponse = httpClient.execute(getRequest); HttpEntity responseEntity = getResponse.getEntity(); StatusLine responseStatus = getResponse.getStatusLine(); String restResp = EntityUtils.toString(responseEntity); if (responseStatus.getStatusCode() != 200) { throw new RuntimeException(restResp); }//from ww w . j a va2s . c om Gson gson = new Gson(); Type listType = new TypeToken<LinkedList<SalaBean>>() { }.getType(); List<SalaBean> salas = gson.fromJson(restResp, listType); request.getSession().setAttribute("salas", salas); return mapping.getForwardByName("success"); } catch (IOException | RuntimeException e) { request.setAttribute("message", "Error al intentar listar Salas " + e.getMessage()); response.setStatus(400); return mapping.getForwardByName("failure"); } }
From source file:com.github.kaklakariada.aws.sam.PluginTest.java
private String getWebServiceResult(final String url) throws IOException, ClientProtocolException { final CloseableHttpClient httpclient = HttpClients.createDefault(); final CloseableHttpResponse response = httpclient.execute(new HttpGet(url)); assertEquals(200, response.getStatusLine().getStatusCode()); return readStream(response.getEntity().getContent()); }
From source file:com.thoughtworks.go.util.HttpServiceTest.java
@Test public void shouldPostArtifactsAlongWithMD5() throws IOException, URISyntaxException { File uploadingFile = mock(File.class); java.util.Properties checksums = new java.util.Properties(); String uploadUrl = "http://url"; HttpPost mockPostMethod = mock(HttpPost.class); CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); when(httpClient.execute(mockPostMethod)).thenReturn(response); when(uploadingFile.exists()).thenReturn(true); when(httpClientFactory.createPost(uploadUrl)).thenReturn(mockPostMethod); when(mockPostMethod.getURI()).thenReturn(new URI(uploadUrl)); service.upload(uploadUrl, 100L, uploadingFile, checksums); verify(mockPostMethod).setHeader(GO_ARTIFACT_PAYLOAD_SIZE, "100"); verify(mockPostMethod).setHeader("Confirm", "true"); verify(mockPostMethod).setHeader("X-Agent-GUID", "some-guid"); verify(mockPostMethod).setHeader("Authorization", "some-token"); verify(httpClientFactory).createMultipartRequestEntity(uploadingFile, checksums); verify(httpClient).execute(mockPostMethod); }
From source file:com.ibm.devops.dra.AbstractDevOpsAction.java
/** * Get a list of toolchains using given token and organization name. * @param token//from w ww . j a va 2s. c o m * @param orgName * @return */ public static ListBoxModel getToolchainList(String token, String orgName, String environment, Boolean debug_mode) { LOGGER.setLevel(Level.INFO); if (debug_mode) { LOGGER.info("#######################"); LOGGER.info("TOKEN:" + token); LOGGER.info("ORG:" + orgName); LOGGER.info("ENVIRONMENT:" + environment); } String orgId = getOrgId(token, orgName, environment, debug_mode); ListBoxModel emptybox = new ListBoxModel(); emptybox.add("", "empty"); if (orgId == null) { return emptybox; } CloseableHttpClient httpClient = HttpClients.createDefault(); String toolchains_url = chooseToolchainsUrl(environment); if (debug_mode) { LOGGER.info("GET TOOLCHAIN LIST URL:" + toolchains_url + orgId); } HttpGet httpGet = new HttpGet(toolchains_url + orgId); httpGet = addProxyInformation(httpGet); httpGet.setHeader("Authorization", token); CloseableHttpResponse response = null; try { response = httpClient.execute(httpGet); String resStr = EntityUtils.toString(response.getEntity()); if (debug_mode) { LOGGER.info("RESPONSE FROM TOOLCHAINS API:" + response.getStatusLine().toString()); } if (response.getStatusLine().toString().contains("200")) { // get 200 response JsonParser parser = new JsonParser(); JsonElement element = parser.parse(resStr); JsonObject obj = element.getAsJsonObject(); JsonArray items = obj.getAsJsonArray("items"); ListBoxModel toolchainList = new ListBoxModel(); for (int i = 0; i < items.size(); i++) { JsonObject toolchainObj = items.get(i).getAsJsonObject(); String toolchainName = String.valueOf(toolchainObj.get("name")).replaceAll("\"", ""); String toolchainID = String.valueOf(toolchainObj.get("toolchain_guid")).replaceAll("\"", ""); toolchainList.add(toolchainName, toolchainID); } if (debug_mode) { LOGGER.info("TOOLCHAIN LIST:" + toolchainList); LOGGER.info("#######################"); } if (toolchainList.isEmpty()) { if (debug_mode) { LOGGER.info("RETURNED NO TOOLCHAINS."); } return emptybox; } return toolchainList; } else { LOGGER.info( "RETURNED STATUS CODE OTHER THAN 200. RESPONSE: " + response.getStatusLine().toString()); return emptybox; } } catch (Exception e) { e.printStackTrace(); } return emptybox; }
From source file:org.opennms.protocols.http.HttpUrlConnectionIT.java
/** * Test the Servlet with a simple POST Request based on XML Data. * * @throws Exception the exception//from ww w . j a va 2 s. c o m */ @Test @JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") }) public void testServlet() throws Exception { String xml = "<person><firstName>Alejandro</firstName></person>"; final HttpClientWrapper clientWrapper = HttpClientWrapper.create(); try { StringEntity entity = new StringEntity(xml, ContentType.APPLICATION_XML); HttpPost method = new HttpPost("http://localhost:10342/junit/test/sample"); method.setEntity(entity); CloseableHttpResponse response = clientWrapper.execute(method); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); Assert.assertEquals("OK!", EntityUtils.toString(response.getEntity())); } finally { IOUtils.closeQuietly(clientWrapper); } }
From source file:com.thoughtworks.go.util.HttpServiceTest.java
@Test public void shouldSetTheAcceptHeaderWhilePostingProperties() throws Exception { HttpPost post = mock(HttpPost.class); String url = "http://url"; when(httpClientFactory.createPost(url)).thenReturn(post); when(post.getURI()).thenReturn(new URI(url)); CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); when(httpClient.execute(post)).thenReturn(response); ArgumentCaptor<UrlEncodedFormEntity> entityCaptor = ArgumentCaptor.forClass(UrlEncodedFormEntity.class); service.postProperty(url, "value"); verify(post).setHeader("Confirm", "true"); verify(post).setEntity(entityCaptor.capture()); UrlEncodedFormEntity expected = new UrlEncodedFormEntity( Arrays.asList(new BasicNameValuePair("value", "value"))); UrlEncodedFormEntity actual = entityCaptor.getValue(); assertEquals(IOUtils.toString(expected.getContent()), IOUtils.toString(actual.getContent())); assertEquals(expected.getContentLength(), expected.getContentLength()); assertEquals(expected.getContentType(), expected.getContentType()); assertEquals(expected.getContentEncoding(), expected.getContentEncoding()); assertEquals(expected.isChunked(), expected.isChunked()); }
From source file:nl.salp.warcraft4j.io.HttpDataReader.java
private InputStream getResponseStream(String url, CloseableHttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() > 300) { throw new DataParsingException(String.format("Error opening HTTP data reader for %s: error %d: %s", url, statusLine.getStatusCode(), statusLine.getReasonPhrase())); }//from w w w . j a va2s . c o m HttpEntity entity = response.getEntity(); if (entity == null) { throw new DataParsingException(format("HTTP data reader received no response from for %s", url)); } length = entity.getContentLength(); return entity.getContent(); }
From source file:org.commonjava.indy.promote.callback.PromotionCallbackHelper.java
private <T extends AbstractPromoteResult> boolean execute(CloseableHttpClient client, HttpEntityEnclosingRequestBase req, CallbackTarget target, T ret) throws IOException { addHeadersAndSetEntity(req, target, ret); CloseableHttpResponse response = client.execute(req); return isCallbackOk(response.getStatusLine().getStatusCode()); }
From source file:org.apache.hawq.ranger.integration.service.tests.common.RESTClient.java
private String executeRequest(HttpUriRequest request) throws IOException { LOG.debug("--> request URI = " + request.getURI()); request.setHeader("Authorization", AUTH_HEADER); request.setHeader("Content-Type", ContentType.APPLICATION_JSON.toString()); CloseableHttpResponse response = httpClient.execute(request); String payload = null;// w ww . ja va 2 s . com try { int responseCode = response.getStatusLine().getStatusCode(); LOG.debug("<-- response code = " + responseCode); HttpEntity entity = response.getEntity(); if (entity != null) { payload = EntityUtils.toString(response.getEntity()); } LOG.debug("<-- response payload = " + payload); if (responseCode == HttpStatus.SC_NOT_FOUND) { throw new ResourceNotFoundException(); } else if (responseCode >= 300) { throw new ClientProtocolException("Unexpected HTTP response code = " + responseCode); } } finally { response.close(); } return payload; }