List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine
StatusLine getStatusLine();
From source file:com.cloud.network.nicira.NiciraRestClient.java
private String responseToErrorMessage(final CloseableHttpResponse response) { String errorMessage = response.getStatusLine().toString(); if (response.containsHeader(CONTENT_TYPE) && TEXT_HTML_CONTENT_TYPE.equals(response.getFirstHeader(CONTENT_TYPE).getValue())) { try {/* ww w .j a v a 2 s. co m*/ final HttpEntity entity = response.getEntity(); final String respobnseBody = EntityUtils.toString(entity); errorMessage = respobnseBody.subSequence(0, maxResponseErrorMesageLength).toString(); } catch (final IOException e) { s_logger.debug("Could not read repsonse body. Response: " + response, e); } } return errorMessage; }
From source file:org.wso2.carbon.ml.analysis.test.DeleteAnalysesTestCase.java
/** * Test deleting an analysis by name.//from w w w. j ava 2 s . co m * * @throws MLHttpClientException * @throws IOException */ @Test(description = "Delete an analysis") public void testDeleteAnalysis() throws MLHttpClientException, IOException { CloseableHttpResponse response = mlHttpclient.doHttpDelete("/api/analyses/" + analysisId); assertEquals("Unexpected response received", Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode()); response.close(); }
From source file:fr.logfiletoes.TailerListenerUnit.java
public void saveToElasticsearch() { HttpPost elasticSearchPost = new HttpPost( unit.getElasticSearch().getUrl() + "/" + unit.getElasticSearch().getType()); try {// www. ja va2s . co m elasticSearchPost.setEntity(new StringEntity(json.toString())); CloseableHttpResponse execute = httpclient.execute(elasticSearchPost, context); if (execute.getStatusLine().getStatusCode() < 200 || execute.getStatusLine().getStatusCode() >= 300) { LOG.log(Level.SEVERE, "Add log to ElasticSearch failed : " + execute.getStatusLine().getStatusCode()); LOG.log(Level.SEVERE, inputSteamToString(execute.getEntity().getContent())); } else { LOG.log(Level.FINE, "Add log to ElasticSearch successful."); LOG.log(Level.FINER, json.toString()); } EntityUtils.consume(execute.getEntity()); execute.close(); } catch (UnsupportedEncodingException ex) { LOG.log(Level.SEVERE, "Encoding exception", ex); } catch (IOException ex) { LOG.log(Level.SEVERE, "IOException", ex); } }
From source file:org.commonjava.indy.httprox.RetrievedPomWithoutUserSuffixUntrackedTest.java
@Test public void run() throws Exception { final String testRepo = "test"; final PomRef pom = loadPom("simple.pom", Collections.<String, String>emptyMap()); final String url = server.formatUrl(testRepo, pom.path); server.expect(url, 200, pom.pom);/* w w w .ja va2 s . co m*/ final HttpGet get = new HttpGet(url); final CloseableHttpClient client = proxiedHttp(); CloseableHttpResponse response = null; InputStream stream = null; try { response = client.execute(get, proxyContext(USER, PASS)); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); stream = response.getEntity().getContent(); final String resultingPom = IOUtils.toString(stream); assertThat(resultingPom, notNullValue()); assertThat(resultingPom, equalTo(pom.pom)); } finally { IOUtils.closeQuietly(stream); HttpResources.cleanupResources(get, response, client); } final TrackedContentRecord record = this.client.module(IndyFoloAdminClientModule.class) .getRawTrackingRecord(USER); assertThat(record, nullValue()); }
From source file:org.superbiz.CdiEventRealmTest.java
@Test public void badAuthentication() throws IOException { final CloseableHttpClient client = HttpClients.createDefault(); // first authenticate with the login servlet final HttpPost httpPost = new HttpPost(webapp.toExternalForm() + "login"); final List<NameValuePair> data = new ArrayList<NameValuePair>() { {//from w w w . j a v a 2 s.co m add(new BasicNameValuePair("username", "userB")); add(new BasicNameValuePair("password", "bla bla")); } }; httpPost.setEntity(new UrlEncodedFormEntity(data)); final CloseableHttpResponse respLogin = client.execute(httpPost); try { assertEquals(401, respLogin.getStatusLine().getStatusCode()); } finally { respLogin.close(); } }
From source file:controller.NavigatorServlet.java
@Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameterMap().toString()); String page = request.getParameter("page"); String type = request.getParameter("type"); System.out.println(page);/* w w w . j a v a 2s. co m*/ System.out.println(type); CloseableHttpClient httpClient = HttpClients.createDefault(); String address = "http://127.0.0.1:8080"; HttpPost httpPost; List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if (type.equalsIgnoreCase("GetPage")) { switch (page) { case "LogIn": address += "/CloudChatUserManagerClient/loginPage.html"; break; case "LogOut": address += "/CloudChatUserManagerClient/logoutConfirm.html"; break; case "LogInSuccess": address += "/CloudChatUserManagerClient/loginSuccessPage.html"; break; case "Register": System.out.println("Got here"); address += "/CloudChatUserManagerClient/registrationPage.html"; break; case "Edit": address += "/CloudChatServer/edit.jsp"; break; case "Delete": address += "/CloudChatServer/delete.jsp"; break; case "ChatHome": address += "/CloudChatServer/chat.jsp"; break; default: address = ""; break; } } else if (type.equalsIgnoreCase("UserManager")) { address += "/CloudChatUserManagerClient/CloudChatUserManagerServlet"; nvps.add(new BasicNameValuePair("action", request.getParameter("action"))); nvps.add(new BasicNameValuePair("userID", request.getParameter("userID"))); nvps.add(new BasicNameValuePair("password", request.getParameter("password"))); } else if (type.equalsIgnoreCase("Server")) { System.out.println("Got here"); address += "/CloudChatServer/chat"; nvps.add(new BasicNameValuePair("userID", request.getParameter("userID"))); nvps.add(new BasicNameValuePair("action", request.getParameter("action"))); nvps.add(new BasicNameValuePair("messageID", request.getParameter("messageID"))); nvps.add(new BasicNameValuePair("message", request.getParameter("message"))); nvps.add(new BasicNameValuePair("category", request.getParameter("category"))); } httpPost = new HttpPost(address); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse httpResponse = httpClient.execute(httpPost); try { System.out.println(httpResponse.getStatusLine()); HttpEntity entity = httpResponse.getEntity(); BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); String line; String res = ""; while ((line = reader.readLine()) != null) { res += line; } EntityUtils.consume(entity); response.getWriter().write(res); } finally { httpResponse.close(); } }
From source file:org.opentravel.schemacompiler.repository.TestRemoteRepositoryFunctions.java
@Test public void testGetUserAuthorizationForMissingNamespace() throws ClientProtocolException, IOException { CloseableHttpResponse ret = doGet( ((RemoteRepository) testRepository.get()).getEndpointUrl() + "/service/user-authorization"); assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), ret.getStatusLine().getStatusCode()); }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.LocationsTest.java
@Test public void itGetsAListOfLocations() throws Exception { HttpGet httpGet = new HttpGet("http://localhost:3333/crs/locations"); CloseableHttpResponse response = null; try {/*from w w w.ja v a 2 s .c om*/ response = closeableHttpClient.execute(httpGet); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()); JsonNode jsonNode = objectMapper.readTree(EntityUtils.toString(response.getEntity())); assertThat(jsonNode.get("locations").get(0).asText(), not(equalTo(""))); } finally { if (response != null) response.close(); } }
From source file:org.wso2.carbon.ml.project.test.DeleteProjectsTestCase.java
/** * Test deleting a non-existing project. * @throws MLHttpClientException /* w w w . j a va 2 s.co m*/ * @throws IOException */ @Test(description = "Delete an exsisting project") public void testDeleteNonExistingProject() throws MLHttpClientException, IOException { CloseableHttpResponse response = mlHttpclient.doHttpDelete("/api/projects/" + "NonExistingProjectName"); assertEquals("Unexpected response recieved", Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode()); response.close(); }
From source file:org.wuspba.ctams.ws.ITPersonController.java
protected static void delete() throws Exception { List<String> ids = new ArrayList<>(); 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); for (Person p : doc.getPeople()) { ids.add(p.getId());/* ww w.j a v a 2 s . c om*/ } EntityUtils.consume(entity); } for (String id : ids) { 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); } } } } }