/** * Send Conference POST request to API endpoint which is used for allocating new conferences in * reservation system. * * @param ownerEmail email of new conference owner * @param mucRoomName full name of MUC room that is hosting the conference. * {room_name}@{muc.server.net} * @return <tt>ApiResult</tt> that contains system response. It will contain <tt>Conference</tt> * instance filled with data from the reservation system if everything goes OK. * @throws FaultTolerantRESTRequest.RetryExhaustedException When the number of retries to submit * the request for the conference data is reached * @throws UnsupportedEncodingException When the room data have the encoding that does not play * with UTF8 standard */ public ApiResult createNewConference(String ownerEmail, String mucRoomName) throws FaultTolerantRESTRequest.RetryExhaustedException, UnsupportedEncodingException { Conference conference = new Conference(mucRoomName, ownerEmail, new Date()); HttpPost post = new HttpPost(baseUrl + "/conference"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); Map<String, Object> jsonMap = conference.createJSonMap(); for (Map.Entry<String, Object> entry : jsonMap.entrySet()) { nameValuePairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()))); } post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF8")); logger.info("Sending post: " + jsonMap); CreateConferenceResponseParser conferenceResponseParser = new CreateConferenceResponseParser(conference); FaultTolerantRESTRequest faultTolerantRESTRequest = new FaultTolerantRESTRequest(post, conferenceResponseParser, retriesCreate, httpClient); return faultTolerantRESTRequest.submit(); }
/** * Finds conference for given ID assigned by the reservation system. * * @param id identifier of the conference to find. * @return <tt>Conference</tt> for given <tt>id</tt> or <tt>null</tt> if not found. */ private Conference findConferenceForId(Number id) { for (Conference conference : conferenceMap.values()) { if (conference.getId().equals(id)) { return conference; } } return null; }
/** {@inheritDoc} */ public synchronized Result createConference(String creator, String mucRoomName) { Conference conference = conferenceMap.get(mucRoomName); if (conference == null) { // Create new conference try { ApiResult result = api.createNewConference(creator, mucRoomName); if (result.getError() == null) { conference = result.getConference(); conferenceMap.put(mucRoomName, conference); } else if (result.getStatusCode() == 409 && result.getError().getConflictId() != null) { Number conflictId = result.getError().getConflictId(); // Conference already exists(check if we have it locally) conference = findConferenceForId(conflictId); logger.info("Conference '" + mucRoomName + "' already " + "allocated, id: " + conflictId); // do GET conflict conference if (conference == null) { ApiResult getResult = api.getConference(conflictId); if (getResult.getConference() != null) { conference = getResult.getConference(); // Fill full room name as it is not transferred // over REST API conference.setMucRoomName(mucRoomName); conferenceMap.put(mucRoomName, conference); } else { logger.error("API error: " + result); return new Result(RESULT_INTERNAL_ERROR, result.getError().getMessage()); } } } else { // Other error logger.error("API error: " + result); return new Result(RESULT_INTERNAL_ERROR, result.getError().getMessage()); } } catch (FaultTolerantRESTRequest.RetryExhaustedException e) { logger.error(e, e); return new Result(RESULT_INTERNAL_ERROR, e.getMessage()); } catch (UnsupportedEncodingException e) { logger.error(e, e); return new Result(RESULT_INTERNAL_ERROR, e.getMessage()); } } // Verify owner == creator if (creator.equals(conference.getOwner())) { return new Result(RESULT_OK); } else { logger.error( "Room " + mucRoomName + ", conflict : " + creator + " != " + conference.getOwner()); return new Result(RESULT_CONFLICT); } }
/** Deletes conference for given <tt>mucRoomName</tt> through the API. */ Result deleteConference(String mucRoomName) { Conference conference = conferenceMap.get(mucRoomName); if (conference != null) { // Delete conference Number id = conference.getId(); int result = deleteConference(id); if (result == RESULT_OK) { conferenceMap.remove(mucRoomName); } else { // Other error return new Result(result); } } return new Result(RESULT_OK); }
@Override public void run() { synchronized (RESTReservations.this) { Iterator<Conference> conferenceIterator = conferenceMap.values().iterator(); while (conferenceIterator.hasNext()) { Conference conference = conferenceIterator.next(); Date startTimeDate = conference.getStartTime(); if (startTimeDate == null) { logger.error("No 'start_time' for conference: " + conference.getName()); continue; } long startTime = startTimeDate.getTime(); long duration = conference.getDuration(); // Convert duration to millis duration = duration * 1000L; long now = System.currentTimeMillis(); if (now - startTime > duration - EXPIRE_INTERVAL) { // Destroy the conference String mucRoomName = conference.getMucRoomName(); deleteConference(conference.getId()); conferenceIterator.remove(); focusManager.destroyConference(mucRoomName, "Scheduled conference duration exceeded."); } } } }
/** * 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; }