List of usage examples for org.apache.http.client.methods CloseableHttpResponse toString
public String toString()
From source file:com.ontotext.s4.gdbaas.createRepo.service.GdbaasClient.java
/** * Sends a test request to the service// w w w .jav a 2 s. co m * * @return true if the service is available */ public boolean testEndpoint() { HttpGet get = new HttpGet(""); CloseableHttpResponse response = null; try { response = httpClient.execute(get, ctx); StatusLine sl = response.getStatusLine(); int statusCode = sl.getStatusCode(); if (statusCode != 200) { System.out.println("Error communicating with endpoint."); System.out.println(response.toString()); System.out.println(getContent(response)); return false; } else { System.out.println("Endpoint returned status SUCCESS."); System.out.println(response.toString()); System.out.println("Response body: "); System.out.println(getContent(response)); return true; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { } } return false; }
From source file:ConnectionProtccol.HttpClient.java
/** * * @param urlParams/* w w w. j ava 2 s .c o m*/ * @param postParams * */ public boolean httpPost(String url, MultipartEntityBuilder builder, UrlEncodedFormEntity formParams) { loggerObj.log(Level.INFO, "Inside httpsPost method"); CloseableHttpClient httpclient = HttpClients.createDefault(); try { // HttpPost httppost = new HttpPost("https://reportsapi.zoho.com/api/harshavardhan.r@zohocorp.com/Test/Name_password?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=7ed717b94bc30455aad11ce5d31d34f9&ZOHO_API_VERSION=1.0"); loggerObj.log(Level.INFO, "Going to post to the zoho reports api"); HttpPost httppost = new HttpPost(url); //Need to understand the difference betwween multipartentitybuilder and urlencodedformentity// HttpEntity entity = null; if (builder != null) { entity = builder.build(); } if (formParams != null) { entity = formParams; } httppost.setEntity(entity); loggerObj.log(Level.INFO, "executing request"); System.out.println("executing request " + httppost.getRequestLine()); CloseableHttpResponse response = null; try { response = httpclient.execute(httppost); } catch (IOException ex) { loggerObj.log(Level.INFO, "Cannot connect to reports.zoho.com" + ex.toString()); return false; } try { loggerObj.log(Level.INFO, "----------------------------------------"); loggerObj.log(Level.INFO, response.toString()); response.getStatusLine(); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { loggerObj.log(Level.INFO, "Response content length: " + resEntity.getContentLength()); BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot read the response 1 from reports.zoho.com" + ex.toString()); return false; } catch (UnsupportedOperationException ex) { loggerObj.log(Level.INFO, "cannot read the response 2" + ex.toString()); return false; } String line = ""; try { loggerObj.log(Level.INFO, "reading response line"); while ((line = rd.readLine()) != null) { System.out.println(line); loggerObj.log(Level.INFO, line); } } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot read the response 3" + ex.toString()); return false; } } try { EntityUtils.consume(resEntity); } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot read the response 4" + ex.toString()); return false; } } finally { try { response.close(); } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot close the response" + ex.toString()); return false; } } } finally { try { httpclient.close(); } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot read the https clinet" + ex.toString()); return false; } } return true; }
From source file:ConnectionProtccol.HttpsClient.java
/** * * @param urlParams//from w ww . j a va2 s . c om * @param postParams * */ public boolean httpsPost(String url, MultipartEntityBuilder builder, UrlEncodedFormEntity formParams) { loggerObj.log(Level.INFO, "Inside httpsPost method"); CloseableHttpClient httpclient = HttpClients.createDefault(); try { // HttpPost httppost = new HttpPost("https://reportsapi.zoho.com/api/harshavardhan.r@zohocorp.com/Test/Name_password?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=7ed717b94bc30455aad11ce5d31d34f9&ZOHO_API_VERSION=1.0"); loggerObj.log(Level.INFO, "Going to post to the zoho reports api"); HttpPost httppost = new HttpPost(url); //Need to understand the difference betwween multipartentitybuilder and urlencodedformentity// HttpEntity entity = null; if (builder != null) entity = builder.build(); if (formParams != null) entity = formParams; httppost.setEntity(entity); loggerObj.log(Level.INFO, "executing request"); System.out.println("executing request " + httppost.getRequestLine()); CloseableHttpResponse response = null; try { response = httpclient.execute(httppost); } catch (IOException ex) { loggerObj.log(Level.INFO, "Cannot connect to reports.zoho.com" + ex.toString()); return false; } try { loggerObj.log(Level.INFO, "----------------------------------------"); loggerObj.log(Level.INFO, response.toString()); response.getStatusLine(); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { loggerObj.log(Level.INFO, "Response content length: " + resEntity.getContentLength()); BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot read the response 1 from reports.zoho.com" + ex.toString()); return false; } catch (UnsupportedOperationException ex) { loggerObj.log(Level.INFO, "cannot read the response 2" + ex.toString()); return false; } String line = ""; try { loggerObj.log(Level.INFO, "reading response line"); while ((line = rd.readLine()) != null) { System.out.println(line); loggerObj.log(Level.INFO, line); } } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot read the response 3" + ex.toString()); return false; } } try { EntityUtils.consume(resEntity); } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot read the response 4" + ex.toString()); return false; } } finally { try { response.close(); } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot close the response" + ex.toString()); return false; } } } finally { try { httpclient.close(); } catch (IOException ex) { loggerObj.log(Level.INFO, "cannot read the https clinet" + ex.toString()); return false; } } return true; }
From source file:com.ontotext.s4.gdbaas.createRepo.service.GdbaasClient.java
/** * Serialize a ProcessingRequest and send it to Self Service Semantic Suite * Online Processing Service// ww w . j ava2s . c o m * * @param pr * the processing request to send * @param acceptType * the type of output we want to produce * @throws URISyntaxException */ private String processRequest(String message, String acceptType, String contentType, HttpRequestBase request, String contentTypeHeader, String acceptTypeHeader) throws URISyntaxException { request.setHeader("Accept", acceptTypeHeader); request.setHeader("Content-Type", contentTypeHeader); request.setHeader("Accept-Encoding", "gzip"); if (request instanceof HttpEntityEnclosingRequestBase) { System.out.println("POST body is:"); System.out.println(message); ((HttpEntityEnclosingRequestBase) request) .setEntity(new StringEntity(message, Charset.forName("UTF-8"))); } CloseableHttpResponse response = null; try { response = httpClient.execute(request, ctx); StatusLine sl = response.getStatusLine(); int statusCode = sl.getStatusCode(); switch (statusCode) { case 200: { // Request was processed successfully System.out.println("SUCCESS"); System.out.println(response.toString()); return getContent(response); } case 204: { // Request was processed successfully System.out.println("SUCCESS"); return response.toString(); } case 400: { // Bad request, there is some problem with user input System.out.println("Bad request"); System.out.println(response.toString()); System.out.println(getContent(response)); break; } case 403: { // Problem with user authentication System.out.println("Error during authentication"); System.out.println(response.toString()); System.out.println(getContent(response)); break; } case 404: { // Not found System.out.println("Not found, check endpoint URL"); System.out.println(response.toString()); System.out.println(getContent(response)); break; } case 406: { // Not Accepted System.out.println("The request was not accepted. Check Accept header"); System.out.println(response.toString()); System.out.println(getContent(response)); break; } case 408: { // Processing this request took too long System.out.println("Could not process document in time"); System.out.println(response.toString()); System.out.println(getContent(response)); break; } case 415: { // Unsupported media type System.out.println("Invalid value in Content-Type header"); System.out.println(response.toString()); System.out.println(getContent(response)); break; } case 500: { // Internal server error System.out.println("Error during processing"); System.out.println(response.toString()); System.out.println(getContent(response)); break; } default: { System.out.println("Could not process request"); System.out.println(response.toString()); System.out.println(getContent(response)); break; } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { } } return null; }
From source file:atc.otn.ckan.client.CKANClient.java
/*** * /*from ww w.j a v a2 s .c om*/ * @param dTankDatasetURL */ public boolean getDatatankTransform(String dTankDatasetURL) { //********************** Variables ********************** CloseableHttpClient httpclient = HttpClients.createDefault(); ; HttpGet httpGet; CloseableHttpResponse response = null; //************************ Action ************************* try { httpGet = new HttpGet(dTankDatasetURL + ".geojson"); httpGet.setHeader("Content-Type", "application/json"); httpGet.setHeader("Accept", "application/json"); //call the service response = httpclient.execute(httpGet); logger.info( "Datatank Trnsformation Effort for: " + dTankDatasetURL + " produced " + response.toString()); if (response.getStatusLine().getStatusCode() == 200) { return true; } } catch (Exception e) { logger.error(e.getMessage()); return false; } finally { if (response != null) { try { response.close(); httpclient.close(); } catch (IOException e) { // TODO Auto-generated catch block logger.error(e.getMessage()); } } //end if } //end finally return false; }
From source file:org.alfresco.provision.BMService.java
public JSONObject getTestProperty(String testName, String propertyName) throws IOException { CloseableHttpResponse httpResponse = null; JSONObject response = null;//w w w.java 2s. com try { String url = "http://" + hostname + ":9080/alfresco-benchmark-server/api/v1/tests/" + testName + "/props/" + propertyName; // System.out.println("Getting property " + testName + ", " + // propertyName + ", " + url); HttpGet getProperty = new HttpGet(url); httpResponse = client.execute(getProperty); StatusLine status = httpResponse.getStatusLine(); // Expecting "OK" status if (status.getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = httpResponse.getEntity(); response = readStream(entity); } else if (status.getStatusCode() == HttpStatus.SC_NOT_FOUND) { System.err.println("Property " + propertyName + " for test " + testName + " does not exist"); } else { System.err.println("Unexpected response " + httpResponse.toString()); } return response; } finally { httpResponse.close(); } }
From source file:org.alfresco.provision.BMService.java
@SuppressWarnings("unchecked") public void setTestProperty(String testName, String propertyName, Object propertyValue) throws IOException { JSONObject json = getTestProperty(testName, propertyName); if (json != null) { Long currentVersion = (Long) json.get("version"); CloseableHttpResponse httpResponse = null; try {/*from w w w. ja v a2 s .c o m*/ String url = "http://" + hostname + ":9080/alfresco-benchmark-server/api/v1/tests/" + testName + "/props/" + propertyName; JSONObject putJson = new JSONObject(); putJson.put("version", currentVersion); putJson.put("value", propertyValue); HttpPut updateProperty = new HttpPut(url); StringEntity content = setMessageBody(putJson); updateProperty.setEntity(content); httpResponse = client.execute(updateProperty); StatusLine status = httpResponse.getStatusLine(); // Expecting "OK" status if (status.getStatusCode() == HttpStatus.SC_NOT_FOUND) { System.err.println("Property does not exist"); } else if (status.getStatusCode() != HttpStatus.SC_OK) { System.err.println("Unexpected response " + httpResponse.toString()); } } finally { httpResponse.close(); } } else { System.err.println("Property " + propertyName + " for test " + testName + " does not exist"); } }
From source file:org.sdntest.app.SDNTest.java
private boolean flowAllowed() { // get login info from file // TODO: get from somewhere else, or setup certs BufferedReader br = null;/*from w ww .j a v a 2 s . c o m*/ String uname; String password; try { br = new BufferedReader(new FileReader(GLOBUS_LOGIN_FILE)); uname = br.readLine(); password = br.readLine(); } catch (IOException e) { log.info("Error reading globus login file: {}", GLOBUS_LOGIN_FILE); return false; } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { log.info("Error closing globus login file"); } } if (uname == null || password == null) { log.info("Error: could not get globus login from file"); return false; } else { log.info("Got globus login un: {}, pw: {}", uname, password); } // use login info to get authentication token UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(uname, password); CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpGet authGet = new HttpGet(GLOBUS_AUTH_URL); try { authGet.addHeader(new BasicScheme().authenticate(credentials, authGet)); } catch (AuthenticationException e1) { e1.printStackTrace(); } authGet.addHeader("User-Agent", "onos"); log.info(authGet.toString()); CloseableHttpResponse response1; try { response1 = httpclient.execute(authGet); } catch (ClientProtocolException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } String authTok = ""; try { log.info(response1.toString()); String resStr = EntityUtils.toString(response1.getEntity()); log.info(resStr); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(resStr); authTok = (String) jsonObject.get("access_token"); } catch (IOException e) { e.printStackTrace(); return false; } catch (ParseException e) { e.printStackTrace(); return false; } catch (org.json.simple.parser.ParseException e) { e.printStackTrace(); } finally { try { response1.close(); } catch (IOException e) { e.printStackTrace(); } } log.info("Token: {}", authTok); return true; }
From source file:org.alfresco.provision.BMService.java
@SuppressWarnings("unchecked") public void runTest(String testName, String testRunName, String testRunDescription) throws IOException { CloseableHttpResponse httpResponse = null; try {/* w w w.j av a 2s. com*/ String url = "http://" + hostname + ":9080/alfresco-benchmark-server/api/v1/tests/" + testName + "/runs"; JSONObject postJson = new JSONObject(); postJson.put("name", testRunName); postJson.put("description", testRunDescription); HttpPost createTestRun = new HttpPost(url); StringEntity content = setMessageBody(postJson); createTestRun.setEntity(content); httpResponse = client.execute(createTestRun); StatusLine status = httpResponse.getStatusLine(); httpResponse.close(); // Expecting "OK" status if (status.getStatusCode() == HttpStatus.SC_OK) { url = "http://" + hostname + ":9080/alfresco-benchmark-server/api/v1/tests/" + testName + "/runs/" + testRunName + "/schedule"; postJson = new JSONObject(); postJson.put("version", 1); postJson.put("scheduled", System.currentTimeMillis()); postJson.put("duration", Integer.MAX_VALUE); HttpPost scheduleTestRun = new HttpPost(url); content = setMessageBody(postJson); scheduleTestRun.setEntity(content); httpResponse = client.execute(scheduleTestRun); status = httpResponse.getStatusLine(); } else if (status.getStatusCode() == HttpStatus.SC_NOT_FOUND) { System.err.println("Property does not exist"); } else { System.err.println("Unexpected response " + httpResponse.toString()); } } finally { httpResponse.close(); } }
From source file:atc.otn.ckan.client.CKANClient.java
public String addToDataTank(JSONObject dataset, String format, String dTankTitle) { //********************** Variables ********************** CloseableHttpClient httpclient = HttpClients.createDefault(); ;/*from w w w . j av a 2s . co m*/ String ckanAPIKey = ""; HttpPost httppost; StringEntity dataSetEntity; CloseableHttpResponse response = null; JSONObject jsonResp; String responsedTank = ""; HttpPut httpput; //************************ Action ************************* try { //get the user's api key /*ckanAPIKey = getUserAPIKey(liferayID);*/ // logger.info("ckan url:"+CKAN_BASEURL+ "package_create"); /*httppost = new HttpPost(CKAN_BASEURL+ "package_create"); httppost.setHeader("Content-Type", "application/json"); httppost.setHeader("Accept", "application/json"); httppost.setHeader("Authorization", ckanAPIKey); dataSetEntity = new StringEntity(dataset.toString()); httppost.setEntity(dataSetEntity); response = httpclient.execute(httppost); //get the response and convert it to json jsonResp = new JSONObject(EntityUtils.toString(response.getEntity())); logger.info("resp:"+jsonResp);*/ String credentials = "admin:admin"; String encoding = org.apache.commons.codec.binary.Base64 .encodeBase64URLSafeString(credentials.getBytes()); httpput = new HttpPut(DATATANK_BASEURL + "api/definitions/" + format + "/" + dTankTitle); httpput.setHeader("Content-Type", "application/tdt.definition+json"); //httpput.setHeader("Accept", "application/json"); httpput.setHeader("Authorization", "Basic " + encoding); dataSetEntity = new StringEntity(dataset.toString(), StandardCharsets.UTF_8); httpput.setEntity(dataSetEntity); response = httpclient.execute(httpput); //get the response and convert it to json //jsonResp = new JSONObject(EntityUtils.toString(response.getEntity())); if (response.getStatusLine().getStatusCode() == 200) { responsedTank = DATATANK_BASEURL + format + "/" + dTankTitle; } else { responsedTank = "error"; JSONObject dTankespo = new JSONObject(EntityUtils.toString(response.getEntity())); logger.info("Datatank JSONObject Response:" + dTankespo.toString()); if (dTankespo.has("error")) { responsedTank = dTankespo.getJSONObject("error").getString("message"); } } logger.info("Datatank Raw Response:" + response.toString()); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return responsedTank; }