protected String getRallyXML(String apiUrl) throws Exception { String responseXML = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); Base64 base64 = new Base64(); String encodeString = new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes())); HttpGet httpGet = new HttpGet(apiUrl); httpGet.addHeader("Authorization", "Basic " + encodeString); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { InputStreamReader reader = new InputStreamReader(entity.getContent()); BufferedReader br = new BufferedReader(reader); StringBuilder sb = new StringBuilder(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } responseXML = sb.toString(); } log.debug("responseXML=" + responseXML); return responseXML; }
/** * Parse the room read response received from reservation backend. * * <p>This will request the retry if the response status code differs from 200, 201 or 4xx. * * @param response The Apache HTTP client response * @throws IOException * @throws ParseException * @throws FaultTolerantRESTRequest.RetryRequestedException */ @Override protected void parse(HttpResponse response) throws IOException, ParseException, FaultTolerantRESTRequest.RetryRequestedException { int statusCode = response.getStatusLine().getStatusCode(); logger.info("STATUS CODE: " + statusCode); if (200 == statusCode || 201 == statusCode) { // OK conference = readConferenceResponse(conference, response); result = new ApiResult(statusCode, conference); } else if ((statusCode >= 400) && (statusCode < 500)) { // Client side error, indicates that the reservation // backend do not want the resubmission ErrorResponse error = readErrorResponse(response); result = new ApiResult(statusCode, error); } else { // Unusual status code, request the retry throw new FaultTolerantRESTRequest.RetryRequestedException(); } }
/** * Parses error response. * * @param response parsed <tt>ErrorResponse</tt> * @return <tt>ErrorResponse</tt> parsed from HTTP content stream. * @throws IOException if any IO issues occur. * @throws ParseException if any issues with JSON parsing occur. */ private ErrorResponse readErrorResponse(HttpResponse response) throws IOException, ParseException { BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); jsonParser.parse(rd, errorJson); return errorJson.getResult(); }
protected String postRallyXML(String apiUrl, String requestXML) throws Exception { String responseXML = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); Base64 base64 = new Base64(); String encodeString = new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes())); HttpPost httpPost = new HttpPost(apiUrl); httpPost.addHeader("Authorization", "Basic " + encodeString); httpPost.setEntity(new StringEntity(requestXML)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); responseXML = getEntityString(entity); return responseXML; }
/** * Parses JSON string returned in HTTP response and converts it to <tt>Conference</tt> instance. * * @param conference <tt>Conference</tt> instance that contains the data returned by API endpoint. * @param response HTTP response returned by the API endpoint. * @return <tt>Conference</tt> instance that contains the data returned by API endpoint. * @throws IOException if any IO problems occur. * @throws ParseException if any problems with JSON parsing occur. */ private Conference readConferenceResponse(Conference conference, HttpResponse response) throws IOException, ParseException { BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); if (conference != null) { conferenceJson.setForUpdate(conference); } jsonParser.parse(rd, conferenceJson); if (conference == null) { conference = conferenceJson.getResult(); } logger.info("ID: " + conference.getId()); logger.info("PIN: " + conference.getPin()); logger.info("URL: " + conference.getUrl()); logger.info("SIP ID: " + conference.getSipId()); logger.info("START TIME: " + conference.getStartTime()); logger.info("DURATION: " + conference.getDuration()); return conference; }