@Override protected void doInvite(SipServletRequest request) throws ServletException, IOException { if (logger.isInfoEnabled()) { logger.info("Simple Servlet: Got request:\n" + request.getMethod()); } SipServletResponse sipServletResponse = request.createResponse(SipServletResponse.SC_RINGING); sipServletResponse.send(); sipServletResponse = request.createResponse(SipServletResponse.SC_OK); sipServletResponse.send(); if (CALLEE_SEND_BYE.equalsIgnoreCase(((SipURI) request.getTo().getURI()).getUser())) { TimerService timer = (TimerService) getServletContext().getAttribute(TIMER_SERVICE); timer.createTimer( request.getApplicationSession(), byeDelay, false, request.getSession().getId()); } }
protected void doRegister(SipServletRequest req) throws ServletException, IOException { logger.info("Received register request: " + req.getTo()); int response = SipServletResponse.SC_OK; SipServletResponse resp = req.createResponse(response); HashMap<String, String> users = (HashMap<String, String>) getServletContext().getAttribute("registeredUsersMap"); if (users == null) users = new HashMap<String, String>(); getServletContext().setAttribute("registeredUsersMap", users); Address address = req.getAddressHeader(CONTACT_HEADER); String fromURI = req.getFrom().getURI().toString(); int expires = address.getExpires(); if (expires < 0) { expires = req.getExpires(); } if (expires == 0) { users.remove(fromURI); logger.info("User " + fromURI + " unregistered"); } else { resp.setAddressHeader(CONTACT_HEADER, address); users.put(fromURI, address.getURI().toString()); logger.info("User " + fromURI + " registered with an Expire time of " + expires); } resp.send(); }
public void connectionOpenRequest(MsConnectionEvent event, SipServletRequest inviteRequest) { logger.info("connection opened " + event); String sdp = event.getConnection().getLocalDescriptor(); SipServletResponse sipServletResponse = inviteRequest.createResponse(SipServletResponse.SC_OK); try { sipServletResponse.setContent(sdp, "application/sdp"); sipServletResponse.send(); } catch (Exception e) { logger.error(e); } provider = ConferenceCenter.getInstance().getProvider(); MsConnection connection = event.getConnection(); MsEndpoint endpoint = connection.getEndpoint(); MsSession session = connection.getSession(); String callerName = inviteRequest.getFrom().getURI().toString(); ConferenceParticipant participant = new EndpointConferenceParticipant(callerName, endpoint, session, inviteRequest, connection); String key = ((SipURI) inviteRequest.getTo().getURI()).getUser(); ConferenceCenter.getInstance().getConference(key).joinParticipant(participant); }
public void timeout(ServletTimer timer, SipSession sipSession) { serverEntryLog(); String reasonPhrase; if (sipSession.getAttribute("cancelled") != null) { reasonPhrase = "got cancelled"; } else { logger.error("***ERROR: didn't receive notification of CANCEL***"); reasonPhrase = "didn't get cancelled"; } try { SipServletRequest invite = (SipServletRequest) sipSession.getAttribute("invite"); SipServletResponse resp = invite.createResponse(500, reasonPhrase); try { resp.send(); } catch (IOException ex) { logger.error("***failed to send subsequent request***", ex); } logger.error( "***ERROR: sending final response after CANCEL didn't " + "cause IllegalStateException***"); } catch (IllegalStateException ex) { logger.info( "===SUCCESS: sending final response after CANCEL " + "cause IllegalStateException==="); } }
@Observer("connectionOpen") public void doConnectionOpen(MsConnectionEvent event) throws IOException { conferenceEndpointName = event.getConnection().getEndpoint().getLocalName(); SipServletRequest request = (SipServletRequest) sipSession.getAttribute("inviteRequest"); SipServletResponse response = request.createResponse(200); response.setContent(event.getConnection().getLocalDescriptor(), "application/sdp"); response.send(); }
protected synchronized void doInvite(final Map<String, String> headers) throws IOException { if (_cstate == SIPCall.State.INVITING) { setSIPCallState(SIPCall.State.RINGING); final SipServletResponse res = _invite.createResponse(SipServletResponse.SC_RINGING); SIPHelper.addHeaders(res, headers); res.send(); } }
protected synchronized void doPrack(final SipServletRequest req) throws IOException { if (_cstate == SIPCall.State.PROGRESSED) { final SipServletResponse res = req.createResponse(SipServletResponse.SC_OK); if (getLocalSDP() != null) { res.setContent(getLocalSDP(), "application/sdp"); } res.send(); } }
protected void doInvite(SipServletRequest req) throws ServletException, IOException { serverEntryLog(); logger.debug("---doInvite---"); SipServletResponse resp = req.createResponse(180); resp.send(); // just set timer - we expect a CANCEL to arrive... SipSession sipSession = req.getSession(); sipSession.setAttribute("invite", req); timerService.createTimer( req.getApplicationSession(), 5000, true, UasCancelServlet.class.getName()); }
@Override public synchronized void accept(final Map<String, String> headers) throws SignalException { this.checkState(); _accepted = true; final SipServletResponse res = _req.createResponse(SipServletResponse.SC_OK); SIPHelper.addHeaders(res, headers); try { res.send(); } catch (final IOException e) { throw new SignalException(e); } }
@Override public synchronized void reject(final Reason reason, final Map<String, String> headers) throws SignalException { this.checkState(); _rejected = true; final SipServletResponse res = _req.createResponse(reason == null ? Reason.DECLINE.getCode() : reason.getCode()); SIPHelper.addHeaders(res, headers); try { res.send(); } catch (final IOException e) { throw new SignalException(e); } }
@Override protected void doBye(SipServletRequest request) throws ServletException, IOException { logger.info("Got bye"); SipSession session = request.getSession(); SipSession linkedSession = (SipSession) session.getAttribute("LinkedSession"); if (linkedSession != null) { SipServletRequest bye = linkedSession.createRequest("BYE"); logger.info("Sending bye to " + linkedSession.getRemoteParty()); bye.send(); } CallStatusContainer calls = (CallStatusContainer) getServletContext().getAttribute("activeCalls"); calls.removeCall(request.getFrom().getURI().toString(), request.getTo().getURI().toString()); calls.removeCall(request.getTo().getURI().toString(), request.getFrom().getURI().toString()); SipServletResponse ok = request.createResponse(SipServletResponse.SC_OK); ok.send(); }
@Override public synchronized void reject(final Reason reason, final Map<String, String> headers) throws SignalException { checkState(); _rejected = true; setSIPCallState(SIPCall.State.REJECTED); terminate(CallCompleteEvent.Cause.DECLINE, null, headers); try { final SipServletResponse res = _invite.createResponse(reason == null ? Reason.DECLINE.getCode() : reason.getCode()); SIPHelper.addHeaders(res, headers); res.send(); } catch (final IOException e) { throw new SignalException(e); } }
private void doBye(SipSession session, SipServletRequest request) { // send bye to its linked user session and notify SipSession reqSession = request.getSession(false); if (null == reqSession.getAttribute(Constant.INVITE_USER_REQUEST)) { logger.info("callee ok"); SipServletRequest bye = session.createRequest(Constant.BYE); try { bye.send(); } catch (IOException e) { e.printStackTrace(); } } // send 200 OK to the session SipServletResponse resp = request.createResponse(SipServletResponse.SC_OK); try { resp.send(); } catch (IOException e) { e.printStackTrace(); } }
@Override public synchronized void redirect(final Endpoint o, final Map<String, String> headers) throws SignalException { checkState(); _redirected = true; setSIPCallState(SIPCall.State.REDIRECTED); terminate(CallCompleteEvent.Cause.REDIRECT, null, headers); if (o instanceof SIPEndpoint) { final SipServletResponse res = _invite.createResponse(SipServletResponse.SC_MOVED_TEMPORARILY); res.setHeader("Contact", ((SIPEndpoint) o).getURI().toString()); SIPHelper.addHeaders(res, headers); try { res.send(); } catch (final IOException e) { throw new SignalException(e); } } else { throw new IllegalArgumentException("Unable to redirect the call to a non-SIP participant."); } }
@Override public synchronized void onEvent(final SdpPortManagerEvent event) { if (getSIPCallState() == SIPCall.State.PROGRESSING) { try { final byte[] sdp = event.getMediaServerSdp(); this.setLocalSDP(sdp); final SipServletResponse res = getSipInitnalRequest().createResponse(SipServletResponse.SC_SESSION_PROGRESS); res.setContent(sdp, "application/sdp"); try { res.sendReliably(); } catch (final Rel100Exception e) { LOG.warn("", e); res.send(); } setSIPCallState(SIPCall.State.PROGRESSED); this.notifyAll(); } catch (final IOException e) { LOG.warn("", e); } } super.onEvent(event); }
@Override protected void handleReinviteResponse( final SIPCallImpl call, final SipServletResponse res, final Map<String, String> headers) { if (res.getRequest().getAttribute(SIPCallDelegate.SIPCALL_HOLD_REQUEST) != null || res.getRequest().getAttribute(SIPCallDelegate.SIPCALL_UNHOLD_REQUEST) != null || res.getRequest().getAttribute(SIPCallDelegate.SIPCALL_DEAF_REQUEST) != null) { try { res.createAck().send(); if (call.getHoldState() == HoldState.Holding) { call.setHoldState(HoldState.Held); } else if (call.getHoldState() == HoldState.UnHolding) { call.setHoldState(HoldState.None); } else if (call.getDeafState() == HoldState.Deafing) { call.setDeafState(HoldState.Deafed); } else if (call.getDeafState() == HoldState.Undeafing) { call.setDeafState(HoldState.None); } } catch (IOException e) { LOG.error("IOException when sending back ACK.", e); call.setHoldState(HoldState.None); call.setDeafState(HoldState.None); call.fail(e); } finally { call.notify(); } } else if (res.getRequest().getAttribute(SIPCallDelegate.SIPCALL_MUTE_REQUEST) != null || res.getRequest().getAttribute(SIPCallDelegate.SIPCALL_UNMUTE_REQUEST) != null) { // send ACK. try { res.createAck().send(); // send the received SDP to peer. final SIPCallImpl peer = (SIPCallImpl) call.getLastPeer(); synchronized (peer) { if (call.getMuteState() == HoldState.Muting) { peer.setDeafState(HoldState.Deafing); } else { peer.setDeafState(HoldState.Undeafing); } SipServletRequest reInvite = peer.getSipSession().createRequest("INVITE"); reInvite.setAttribute(SIPCallDelegate.SIPCALL_DEAF_REQUEST, "true"); reInvite.setContent(res.getRawContent(), "application/sdp"); reInvite.send(); while (peer.getDeafState() != HoldState.Deafed && peer.getDeafState() != HoldState.None) { try { peer.wait(); } catch (InterruptedException e) { LOG.warn( "InterruptedException when wait make peer deaf, the peer's DeafState " + peer.getDeafState()); } } // set call deaf state if (call.getMuteState() == HoldState.Muting) { peer.setDeafState(HoldState.Deafed); call.setMuteState(HoldState.Muted); } else if (call.getMuteState() == HoldState.UnMuting) { peer.setDeafState(HoldState.None); call.setMuteState(HoldState.None); } } } catch (IOException e1) { LOG.error("IOException", e1); call.setMuteState(HoldState.None); call.fail(e1); } finally { call.notify(); } } else { try { final SipServletRequest req = res.getRequest(); final SipServletRequest newReq = (SipServletRequest) SIPHelper.getLinkSIPMessage(req); if (newReq != null) { SIPHelper.unlinkSIPMessage(req); final SipServletResponse newRes = newReq.createResponse(res.getStatus(), res.getReasonPhrase()); SIPHelper.addHeaders(newRes, headers); SIPHelper.copyContent(res, newRes); if (SIPHelper.isReinvite(newRes)) { newRes.getSession().setAttribute(REINVITE_PEER_RES, res); } newRes.send(); } } catch (final Exception e) { LOG.warn("", e); return; } } }
@Override public void execute(final Object message) throws Exception { if (logger.isInfoEnabled()) { logger.info("UssdInterpreter Processing INFO request"); } final NotificationsDao notifications = storage.getNotificationsDao(); SipServletRequest info = (SipServletRequest) message; SipServletResponse okay = info.createResponse(200); okay.send(); UssdInfoRequest ussdInfoRequest = new UssdInfoRequest(info); String ussdText = ussdInfoRequest.getMessage(); if (ussdCollectAction != null && !ussdCollectAction.isEmpty() && ussdText != null) { URI target = null; try { target = URI.create(ussdCollectAction); } catch (final Exception exception) { final Notification notification = notification(ERROR_NOTIFICATION, 11100, ussdCollectAction + " is an invalid URI."); notifications.addNotification(notification); sendMail(notification); final StopInterpreter stop = new StopInterpreter(); source.tell(stop, source); return; } final URI base = request.getUri(); final URI uri = UriUtils.resolve(base, target); // Parse "method". String method = "POST"; Attribute attribute = null; try { attribute = verb.attribute("method"); } catch (Exception e) { } if (attribute != null) { method = attribute.value(); if (method != null && !method.isEmpty()) { if (!"GET".equalsIgnoreCase(method) && !"POST".equalsIgnoreCase(method)) { final Notification notification = notification( WARNING_NOTIFICATION, 14104, method + " is not a valid HTTP method for <Gather>"); notifications.addNotification(notification); method = "POST"; } } else { method = "POST"; } } final List<NameValuePair> parameters = parameters(); parameters.add(new BasicNameValuePair("Digits", ussdText)); request = new HttpRequestDescriptor(uri, method, parameters); downloader.tell(request, source); ussdCollectTag = null; ussdLanguageTag = null; ussdMessageTags = new LinkedBlockingQueue<Tag>(); return; } else if (ussdInfoRequest .getUssdMessageType() .equals(UssdMessageType.unstructuredSSNotify_Response)) { UssdRestcommResponse ussdRestcommResponse = new UssdRestcommResponse(); ussdRestcommResponse.setErrorCode("1"); ussdRestcommResponse.setIsFinalMessage(true); ussdCall.tell(ussdRestcommResponse, source); return; } // Ask the parser for the next action to take. final GetNextVerb next = GetNextVerb.instance(); parser.tell(next, self()); }
/** * @param response * @throws IOException */ public static void forwardResponse(final SipServletResponse response) throws IOException { if (logger.isInfoEnabled()) { logger.info(String.format("B2BUA: Got response: \n %s", response)); } CallDetailRecordsDao records = daoManager.getCallDetailRecordsDao(); // container handles CANCEL related responses no need to forward them if (response.getStatus() == 487 || (response.getStatus() == 200 && response.getMethod().equalsIgnoreCase("CANCEL"))) { if (logger.isDebugEnabled()) { logger.debug("response to CANCEL not forwarding"); } // Update CallDetailRecord SipServletRequest request = (SipServletRequest) getLinkedSession(response).getAttribute(B2BUA_LAST_REQUEST); CallDetailRecord callRecord = records.getCallDetailRecord((Sid) request.getSession().getAttribute(CDR_SID)); if (callRecord != null) { logger.info("CDR found! Updating"); callRecord = callRecord.setStatus(CallStateChanged.State.CANCELED.name()); final DateTime now = DateTime.now(); callRecord = callRecord.setEndTime(now); final int seconds = (int) (DateTime.now().getMillis() - callRecord.getStartTime().getMillis()) / 1000; callRecord = callRecord.setDuration(seconds); records.updateCallDetailRecord(callRecord); } return; } // forward the response response.getSession().setAttribute(B2BUA_LAST_RESPONSE, response); SipServletRequest request = (SipServletRequest) getLinkedSession(response).getAttribute(B2BUA_LAST_REQUEST); SipServletResponse resp = request.createResponse(response.getStatus()); CallDetailRecord callRecord = records.getCallDetailRecord((Sid) request.getSession().getAttribute(CDR_SID)); if (response.getContent() != null) { final byte[] sdp = response.getRawContent(); String offer = null; if (response.getContentType().equalsIgnoreCase("application/sdp")) { // Issue 306: https://telestax.atlassian.net/browse/RESTCOMM-306 Registration registration = daoManager.getRegistrationsDao().getRegistration(callRecord.getTo()); final String externalIp = registration.getLocation().split(":")[1].split("@")[1]; try { logger.debug("Got original address from Registration :" + externalIp); offer = patch(sdp, externalIp); } catch (SdpException e) { logger.error("Unexpected exception while patching sdp ", e); } if (offer != null) { resp.setContent(offer, response.getContentType()); } else { resp.setContent(sdp, response.getContentType()); } } } resp.send(); // CallDetailRecord callRecord = records.getCallDetailRecord((Sid) // request.getSession().getAttribute(CDR_SID)); if (callRecord != null) { logger.info("CDR found! Updating"); if (!request.getMethod().equalsIgnoreCase("BYE")) { if (response.getStatus() == 100 || response.getStatus() == 180) { callRecord = callRecord.setStatus(CallStateChanged.State.RINGING.name()); } else if (response.getStatus() == 200 || response.getStatus() == 202) { callRecord = callRecord.setStatus(CallStateChanged.State.IN_PROGRESS.name()); callRecord = callRecord.setAnsweredBy(((SipURI) response.getTo().getURI()).getUser()); final DateTime now = DateTime.now(); callRecord = callRecord.setStartTime(now); } else if (response.getStatus() == 486 || response.getStatus() == 600) { callRecord = callRecord.setStatus(CallStateChanged.State.BUSY.name()); } else if (response.getStatus() > 400) { callRecord = callRecord.setStatus(CallStateChanged.State.FAILED.name()); } } else { callRecord = callRecord.setStatus(CallStateChanged.State.COMPLETED.name()); final DateTime now = DateTime.now(); callRecord = callRecord.setEndTime(now); final int seconds = (int) ((DateTime.now().getMillis() - callRecord.getStartTime().getMillis()) / 1000); callRecord = callRecord.setDuration(seconds); } records.updateCallDetailRecord(callRecord); } }