Example #1
0
 List<NameValuePair> parameters() {
   final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
   final String callSid = callInfo.sid().toString();
   parameters.add(new BasicNameValuePair("CallSid", callSid));
   final String accountSid = accountId.toString();
   parameters.add(new BasicNameValuePair("AccountSid", accountSid));
   final String from = (callInfo.from());
   parameters.add(new BasicNameValuePair("From", from));
   final String to = (callInfo.to());
   parameters.add(new BasicNameValuePair("To", to));
   final String state = callState.toString();
   parameters.add(new BasicNameValuePair("CallStatus", state));
   parameters.add(new BasicNameValuePair("ApiVersion", version));
   final String direction = callInfo.direction();
   parameters.add(new BasicNameValuePair("Direction", direction));
   final String callerName = callInfo.fromName();
   parameters.add(new BasicNameValuePair("CallerName", callerName));
   final String forwardedFrom = callInfo.forwardedFrom();
   parameters.add(new BasicNameValuePair("ForwardedFrom", forwardedFrom));
   // logger.info("Type " + callInfo.type());
   if (CreateCall.Type.SIP == callInfo.type()) {
     // Adding SIP OUT Headers and SipCallId for
     // https://bitbucket.org/telestax/telscale-restcomm/issue/132/implement-twilio-sip-out
     SipServletResponse lastResponse = callInfo.lastResponse();
     // logger.info("lastResponse " + lastResponse);
     if (lastResponse != null) {
       final int statusCode = lastResponse.getStatus();
       final String method = lastResponse.getMethod();
       // See https://www.twilio.com/docs/sip/receiving-sip-headers
       // Headers on the final SIP response message (any 4xx or 5xx message or the final BYE/200)
       // are posted to the
       // Dial action URL.
       if ((statusCode >= 400 && "INVITE".equalsIgnoreCase(method))
           || (statusCode >= 200 && statusCode < 300 && "BYE".equalsIgnoreCase(method))) {
         final String sipCallId = lastResponse.getCallId();
         parameters.add(new BasicNameValuePair("DialSipCallId", sipCallId));
         parameters.add(new BasicNameValuePair("DialSipResponseCode", "" + statusCode));
         Iterator<String> headerIt = lastResponse.getHeaderNames();
         while (headerIt.hasNext()) {
           String headerName = headerIt.next();
           if (headerName.startsWith("X-")) {
             parameters.add(
                 new BasicNameValuePair(
                     "DialSipHeader_" + headerName, lastResponse.getHeader(headerName)));
           }
         }
       }
     }
   }
   return parameters;
 }
Example #2
0
  /**
   * @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);
    }
  }
 /** Returns one-line description of the specified response object. */
 private static String getSummary(final SipServletResponse resp) {
   return "" + resp.getStatus() + "/" + resp.getMethod();
 }