List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine
StatusLine getStatusLine();
From source file:org.wuspba.ctams.ws.ITSoloContestEntryController.java
private static void add() throws Exception { ITVenueController.add();/*from w w w . j ava2s . co m*/ ITJudgeController.add(); ITPersonController.add(); ITSoloContestController.add(); CTAMSDocument doc = new CTAMSDocument(); doc.getVenues().add(TestFixture.INSTANCE.venue); doc.getJudges().add(TestFixture.INSTANCE.judgeAndy); doc.getJudges().add(TestFixture.INSTANCE.judgeJamie); doc.getJudges().add(TestFixture.INSTANCE.judgeBob); doc.getJudges().add(TestFixture.INSTANCE.judgeEoin); doc.getSoloContests().add(TestFixture.INSTANCE.soloContest); doc.getPeople().add(TestFixture.INSTANCE.elaine); doc.getSoloContestEntry().add(TestFixture.INSTANCE.soloContestEntry); String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try { httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); TestFixture.INSTANCE.soloContestEntry.setId(doc.getSoloContestEntry().get(0).getId()); 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); } } } }
From source file:utils.APIImporter.java
/** * Adding the API documents to the published API * @param folderPath folder path imported API folder * @param accessToken access token/* www . j av a 2 s . c o m*/ * @param uuid uuid of the created API */ private static void addAPIDocuments(String folderPath, String accessToken, String uuid) { String docSummaryLocation = folderPath + ImportExportConstants.DOCUMENT_FILE_LOCATION; try { //getting the list of documents String jsonContent = FileUtils.readFileToString(new File(docSummaryLocation)); JSONParser parser = new JSONParser(); JSONObject jsonObject = (JSONObject) parser.parse(jsonContent); JSONArray array = (JSONArray) jsonObject.get(ImportExportConstants.DOCUMENT_LIST); if (array.size() == 0) { String message = "Imported API doesn't have any documents to be publish"; log.warn(message); } else { for (Object anArray : array) { JSONObject obj = (JSONObject) anArray; //publishing each document String url = config.getPublisherUrl() + "apis/" + uuid + "/documents"; CloseableHttpClient client = HttpClientGenerator.getHttpClient(config.getCheckSSLCertificate()); HttpPost request = new HttpPost(url); request.setEntity(new StringEntity(obj.toString(), ImportExportConstants.CHARSET)); request.setHeader(HttpHeaders.AUTHORIZATION, ImportExportConstants.CONSUMER_KEY_SEGMENT + " " + accessToken); request.setHeader(HttpHeaders.CONTENT_TYPE, ImportExportConstants.CONTENT_JSON); CloseableHttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == Response.Status.CREATED.getStatusCode()) { String responseString = EntityUtils.toString(response.getEntity(), ImportExportConstants.CHARSET); String sourceType = ImportExportUtils.readJsonValues(responseString, ImportExportConstants.SOURCE_TYPE); if (sourceType.equalsIgnoreCase(ImportExportConstants.FILE_DOC_TYPE) || sourceType.equalsIgnoreCase(ImportExportConstants.INLINE_DOC_TYPE)) { addDocumentContent(folderPath, uuid, responseString, accessToken); } } else { String message = "Error occurred while publishing the API document " + obj.get(ImportExportConstants.DOC_NAME) + " " + response.getStatusLine(); log.warn(message); } } } } catch (IOException e) { errorMsg = "error occurred while adding the API documents"; log.error(errorMsg, e); } catch (ParseException e) { errorMsg = "error occurred importing the API documents"; log.error(errorMsg, e); } }
From source file:com.aliyun.oss.common.comm.DefaultServiceClient.java
protected static ResponseMessage buildResponse(ServiceClient.Request request, CloseableHttpResponse httpResponse) throws IOException { assert (httpResponse != null); ResponseMessage response = new ResponseMessage(request); response.setUrl(request.getUri());/*w w w .jav a 2 s .com*/ response.setHttpResponse(httpResponse); if (httpResponse.getStatusLine() != null) { response.setStatusCode(httpResponse.getStatusLine().getStatusCode()); } if (httpResponse.getEntity() != null) { if (response.isSuccessful()) { response.setContent(httpResponse.getEntity().getContent()); } else { readAndSetErrorResponse(httpResponse.getEntity().getContent(), response); } } for (Header header : httpResponse.getAllHeaders()) { if (HttpHeaders.CONTENT_LENGTH.equals(header.getName())) { response.setContentLength(Long.parseLong(header.getValue())); } response.addHeader(header.getName(), header.getValue()); } HttpUtil.convertHeaderCharsetFromIso88591(response.getHeaders()); return response; }
From source file:utils.APIImporter.java
/** * Publishing each imported APIs// w w w . j a v a 2s . c o m * @param apiFolder path to the imported folder * @param token access token * @throws APIImportException */ private static void publishAPI(String apiFolder, String token) throws APIImportException { String apiId = null; try { //getting api.json of imported API String pathToJSONFile = apiFolder + ImportExportConstants.JSON_FILE_LOCATION; String jsonContent = FileUtils.readFileToString(new File(pathToJSONFile)); //building API id apiId = ImportExportUtils.readJsonValues(jsonContent, ImportExportConstants.API_PROVIDER) + "-" + ImportExportUtils.readJsonValues(jsonContent, ImportExportConstants.API_NAME) + "-" + ImportExportUtils.readJsonValues(jsonContent, ImportExportConstants.API_VERSION); String url = config.getPublisherUrl() + "apis"; CloseableHttpClient client = HttpClientGenerator.getHttpClient(config.getCheckSSLCertificate()); HttpPost request = new HttpPost(url); request.setEntity(new StringEntity(jsonContent, ImportExportConstants.CHARSET)); request.setHeader(HttpHeaders.AUTHORIZATION, ImportExportConstants.CONSUMER_KEY_SEGMENT + " " + token); request.setHeader(HttpHeaders.CONTENT_TYPE, ImportExportConstants.CONTENT_JSON); CloseableHttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == Response.Status.CONFLICT.getStatusCode()) { if (config.getUpdateApi()) { updateApi(jsonContent, apiId, token, apiFolder); } else { errorMsg = "API " + apiId + " already exists. "; System.out.println(errorMsg); } } else if (response.getStatusLine().getStatusCode() == Response.Status.CREATED.getStatusCode()) { System.out.println("creating API " + apiId); //getting created API's uuid HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, ImportExportConstants.CHARSET); String uuid = ImportExportUtils.readJsonValues(responseString, ImportExportConstants.UUID); if (StringUtils.isNotBlank( ImportExportUtils.readJsonValues(jsonContent, ImportExportConstants.THUMBNAIL))) { addAPIImage(apiFolder, token, uuid); } //Adding API documentations addAPIDocuments(apiFolder, token, uuid); //addAPISequences(pathToArchive, importedApi); // addAPISpecificSequences(pathToArchive, importedApi); // addAPIWsdl(pathToArchive, importedApi); System.out.println("Importing API " + apiId + " was successful"); } else { errorMsg = response.getStatusLine().toString(); log.error(errorMsg); throw new APIImportException(errorMsg); } } catch (IOException e) { String errorMsg = "cannot find details of " + apiId + " for import"; log.error(errorMsg, e); throw new APIImportException(errorMsg, e); } }
From source file:org.wuspba.ctams.ws.ITBandRegistrationController.java
protected static void delete() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("id", TestFixture.INSTANCE.bandRegistration.getId()).build(); HttpDelete httpDelete = new HttpDelete(uri); CloseableHttpResponse response = null; try {// w w w.ja v a 2 s .c om 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); } } } ITBandController.delete(); }
From source file:org.wuspba.ctams.ws.ITSoloRegistrationController.java
protected static void delete() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("id", TestFixture.INSTANCE.soloRegistration.getId()).build(); HttpDelete httpDelete = new HttpDelete(uri); CloseableHttpResponse response = null; try {/*from ww w .j a v a 2 s. c o m*/ 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); } } } ITPersonController.delete(); }
From source file:org.wuspba.ctams.ws.ITBandMemberController.java
private static void add(BandMember m) throws Exception { CTAMSDocument doc = new CTAMSDocument(); doc.getPeople().add(m.getPerson());/*w w w . ja v a2s .co m*/ doc.getBandMembers().add(m); String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try { httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); m.setId(doc.getBandMembers().get(0).getId()); 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); } } } }
From source file:com.bacic5i5j.framework.toolbox.web.WebUtils.java
/** * ??//from www .ja v a 2s.com * * @param params * @param url * @return */ public static String post(Map<String, String> params, String url) { String result = ""; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); List<NameValuePair> nvps = generateURLParams(params); try { httpPost.setEntity(new UrlEncodedFormEntity(nvps)); } catch (UnsupportedEncodingException e) { log.error(e.getMessage() + " : " + e.getCause()); } CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); } catch (IOException e) { log.error(e.getMessage() + " : " + e.getCause()); } if (response != null) { StatusLine statusLine = response.getStatusLine(); log.info("??: " + statusLine.getStatusCode()); if (statusLine.getStatusCode() == 200 || statusLine.getStatusCode() == 302) { try { InputStream is = response.getEntity().getContent(); int count = is.available(); byte[] buffer = new byte[count]; is.read(buffer); result = new String(buffer); } catch (IOException e) { log.error("???: " + e.getMessage()); } } } return result; }
From source file:org.wuspba.ctams.ws.ITVenueController.java
protected static void add() throws Exception { CTAMSDocument doc = new CTAMSDocument(); doc.getVenues().add(TestFixture.INSTANCE.venue); String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try {/*from ww w .j a v a 2 s. com*/ httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); TestFixture.INSTANCE.venue.setId(doc.getVenues().get(0).getId()); 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); } } } }
From source file:org.wuspba.ctams.ws.ITHiredJudgeController.java
protected static void add() throws Exception { ITJudgeController.add();//ww w. j a v a 2 s. co m CTAMSDocument doc = new CTAMSDocument(); doc.getPeople().add(TestFixture.INSTANCE.andy); doc.getPeople().add(TestFixture.INSTANCE.jamie); doc.getPeople().add(TestFixture.INSTANCE.bob); doc.getPeople().add(TestFixture.INSTANCE.eoin); doc.getJudges().add(TestFixture.INSTANCE.judgeAndy); doc.getJudges().add(TestFixture.INSTANCE.judgeJamie); doc.getJudges().add(TestFixture.INSTANCE.judgeBob); doc.getJudges().add(TestFixture.INSTANCE.judgeEoin); doc.getHiredJudges().add(TestFixture.INSTANCE.hiredJudgeAndy); doc.getHiredJudges().add(TestFixture.INSTANCE.hiredJudgeJamie); doc.getHiredJudges().add(TestFixture.INSTANCE.hiredJudgeBob); doc.getHiredJudges().add(TestFixture.INSTANCE.hiredJudgeEoin); String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try { httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); for (HiredJudge j : doc.getHiredJudges()) { if (j.getJudge().getId().equals(TestFixture.INSTANCE.judgeAndy.getId())) { TestFixture.INSTANCE.hiredJudgeAndy.setId(j.getId()); } else if (j.getJudge().getId().equals(TestFixture.INSTANCE.judgeBob.getId())) { TestFixture.INSTANCE.hiredJudgeBob.setId(j.getId()); } else if (j.getJudge().getId().equals(TestFixture.INSTANCE.judgeJamie.getId())) { TestFixture.INSTANCE.hiredJudgeJamie.setId(j.getId()); } else if (j.getJudge().getId().equals(TestFixture.INSTANCE.judgeEoin.getId())) { TestFixture.INSTANCE.hiredJudgeEoin.setId(j.getId()); } } 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); } } } }