// Register a remote location where Restcomm will send monitoring updates
 protected Response registerForUpdates(
     final String accountSid, final MultivaluedMap<String, String> data, MediaType responseType) {
   try {
     secure(daos.getAccountsDao().getAccount(accountSid), "RestComm:Read:Calls");
   } catch (final AuthorizationException exception) {
     return status(UNAUTHORIZED).build();
   }
   // Get the list of live calls from Monitoring Service
   MonitoringServiceResponse liveCalls;
   try {
     final Timeout expires = new Timeout(Duration.create(60, TimeUnit.SECONDS));
     GetLiveCalls getLiveCalls = new GetLiveCalls();
     Future<Object> future = (Future<Object>) ask(monitoringService, getLiveCalls, expires);
     liveCalls =
         (MonitoringServiceResponse) Await.result(future, Duration.create(10, TimeUnit.SECONDS));
   } catch (Exception exception) {
     return status(BAD_REQUEST).entity(exception.getMessage()).build();
   }
   if (liveCalls != null) {
     if (APPLICATION_XML_TYPE == responseType) {
       final RestCommResponse response = new RestCommResponse(liveCalls);
       return ok(xstream.toXML(response), APPLICATION_XML).build();
     } else if (APPLICATION_JSON_TYPE == responseType) {
       Response response = ok(gson.toJson(liveCalls), APPLICATION_JSON).build();
       return response;
     } else {
       return null;
     }
   } else {
     return null;
   }
 }
 protected Response pong(final String accountSid, final MediaType responseType) {
   try {
     secure(daos.getAccountsDao().getAccount(accountSid), "RestComm:Read:Calls");
   } catch (final AuthorizationException exception) {
     return status(UNAUTHORIZED).build();
   }
   CallDetailRecordFilter filterForTotal;
   try {
     filterForTotal = new CallDetailRecordFilter("", null, null, null, null, null, null, null);
   } catch (ParseException e) {
     return status(BAD_REQUEST).build();
   }
   int totalCalls = daos.getCallDetailRecordsDao().getTotalCallDetailRecords(filterForTotal);
   if (APPLICATION_XML_TYPE == responseType) {
     final RestCommResponse response = new RestCommResponse("TotalCalls: " + totalCalls);
     return ok(xstream.toXML(response), APPLICATION_XML).build();
   } else if (APPLICATION_JSON_TYPE == responseType) {
     return ok(gson.toJson("TotalCalls: " + totalCalls), APPLICATION_JSON).build();
   } else {
     return null;
   }
 }
Beispiel #3
0
 protected Response getCall(
     final String accountSid, final String sid, final MediaType responseType) {
   try {
     secure(daos.getAccountsDao().getAccount(accountSid), "RestComm:Read:Calls");
   } catch (final AuthorizationException exception) {
     return status(UNAUTHORIZED).build();
   }
   final CallDetailRecordsDao dao = daos.getCallDetailRecordsDao();
   final CallDetailRecord cdr = dao.getCallDetailRecord(new Sid(sid));
   if (cdr == null) {
     return status(NOT_FOUND).build();
   } else {
     if (APPLICATION_XML_TYPE == responseType) {
       final RestCommResponse response = new RestCommResponse(cdr);
       return ok(xstream.toXML(response), APPLICATION_XML).build();
     } else if (APPLICATION_JSON_TYPE == responseType) {
       return ok(gson.toJson(cdr), APPLICATION_JSON).build();
     } else {
       return null;
     }
   }
 }
Beispiel #4
0
  /**
   * @param request
   * @param client
   * @param toClient
   * @throws IOException
   */
  public static boolean redirectToB2BUA(
      final SipServletRequest request,
      final Client client,
      Client toClient,
      DaoManager storage,
      SipFactory sipFactory)
      throws IOException {
    request.getSession().setAttribute("lastRequest", request);
    if (logger.isInfoEnabled()) {
      logger.info("B2BUA (p2p proxy): Got request:\n" + request.getMethod());
      logger.info(
          String.format(
              "B2BUA: Proxying a session between %s and %s", client.getUri(), toClient.getUri()));
    }

    if (daoManager == null) {
      daoManager = storage;
    }

    String user = ((SipURI) request.getTo().getURI()).getUser();

    final RegistrationsDao registrations = daoManager.getRegistrationsDao();
    final Registration registration = registrations.getRegistration(user);
    if (registration != null) {
      final String location = registration.getLocation();
      SipURI to;
      SipURI from;
      try {
        to = (SipURI) sipFactory.createURI(location);
        from =
            (SipURI)
                sipFactory.createURI(
                    (registrations.getRegistration(client.getLogin())).getLocation());

        final SipSession incomingSession = request.getSession();
        // create and send the outgoing invite and do the session linking
        incomingSession.setAttribute(B2BUA_LAST_REQUEST, request);
        SipServletRequest outRequest =
            sipFactory.createRequest(
                request.getApplicationSession(),
                request.getMethod(),
                request.getFrom().getURI(),
                request.getTo().getURI());
        outRequest.setRequestURI(to);

        if (request.getContent() != null) {
          final byte[] sdp = request.getRawContent();
          String offer = null;
          if (request.getContentType().equalsIgnoreCase("application/sdp")) {
            // Issue 308: https://telestax.atlassian.net/browse/RESTCOMM-308
            String externalIp = request.getInitialRemoteAddr();
            // Issue 306: https://telestax.atlassian.net/browse/RESTCOMM-306
            final String initialIpBeforeLB = request.getHeader("X-Sip-Balancer-InitialRemoteAddr");
            try {
              if (initialIpBeforeLB != null && !initialIpBeforeLB.isEmpty()) {
                offer = patch(sdp, initialIpBeforeLB);
              } else {
                offer = patch(sdp, externalIp);
              }
            } catch (SdpException e) {
              logger.error("Unexpected exception while patching sdp ", e);
            }
          }
          if (offer != null) {
            outRequest.setContent(offer, request.getContentType());
          } else {
            outRequest.setContent(sdp, request.getContentType());
          }
        }

        final SipSession outgoingSession = outRequest.getSession();
        if (request.isInitial()) {
          incomingSession.setAttribute(B2BUA_LINKED_SESSION, outgoingSession);
          outgoingSession.setAttribute(B2BUA_LINKED_SESSION, incomingSession);
        }
        outgoingSession.setAttribute(B2BUA_LAST_REQUEST, outRequest);
        request.createResponse(100).send();
        // Issue #307: https://telestax.atlassian.net/browse/RESTCOMM-307
        request.getSession().setAttribute("toInetUri", to);
        outRequest.send();
        outRequest.getSession().setAttribute("fromInetUri", from);

        final CallDetailRecord.Builder builder = CallDetailRecord.builder();
        builder.setSid(Sid.generate(Sid.Type.CALL));
        builder.setDateCreated(DateTime.now());
        builder.setAccountSid(client.getAccountSid());
        builder.setTo(toClient.getFriendlyName());
        builder.setCallerName(client.getFriendlyName());
        builder.setFrom(client.getFriendlyName());
        // builder.setForwardedFrom(callInfo.forwardedFrom());
        // builder.setPhoneNumberSid(phoneId);
        builder.setStatus(CallStateChanged.State.QUEUED.name());
        builder.setDirection("Client-To-Client");
        builder.setApiVersion(client.getApiVersion());
        builder.setPrice(new BigDecimal("0.00"));
        // TODO implement currency property to be read from Configuration
        builder.setPriceUnit(Currency.getInstance("USD"));
        final StringBuilder buffer = new StringBuilder();
        buffer.append("/").append(client.getApiVersion()).append("/Accounts/");
        buffer.append(client.getAccountSid().toString()).append("/Calls/");
        buffer.append(client.getSid().toString());
        final URI uri = URI.create(buffer.toString());
        builder.setUri(uri);

        CallDetailRecordsDao records = daoManager.getCallDetailRecordsDao();
        CallDetailRecord callRecord = builder.build();
        records.addCallDetailRecord(callRecord);

        incomingSession.setAttribute(CDR_SID, callRecord.getSid());
        outgoingSession.setAttribute(CDR_SID, callRecord.getSid());

        return true; // successfully proxied the SIP request between two registered clients
      } catch (ServletParseException badUriEx) {
        if (logger.isInfoEnabled()) {
          logger.info(
              String.format("B2BUA: Error parsing Client Contact URI: %s", location), badUriEx);
        }
      }
    }
    return false;
  }
Beispiel #5
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);
    }
  }
Beispiel #6
0
  // Issue 139: https://bitbucket.org/telestax/telscale-restcomm/issue/139
  @SuppressWarnings("unchecked")
  protected Response updateCall(
      final String sid,
      final String callSid,
      final MultivaluedMap<String, String> data,
      final MediaType responseType) {
    final Sid accountSid = new Sid(sid);
    try {
      secure(daos.getAccountsDao().getAccount(accountSid), "RestComm:Modify:Calls");
    } catch (final AuthorizationException exception) {
      return status(UNAUTHORIZED).build();
    }

    final Timeout expires = new Timeout(Duration.create(60, TimeUnit.SECONDS));

    final CallDetailRecordsDao dao = daos.getCallDetailRecordsDao();
    final CallDetailRecord cdr = dao.getCallDetailRecord(new Sid(callSid));

    final String url = data.getFirst("Url");
    String method = data.getFirst("Method");
    final String status = data.getFirst("Status");
    final String fallBackUrl = data.getFirst("FallbackUrl");
    String fallBackMethod = data.getFirst("FallbackMethod");
    final String statusCallBack = data.getFirst("StatusCallback");
    String statusCallbackMethod = data.getFirst("StatusCallbackMethod");
    // Restcomm-  Move connected call leg (if exists) to the new URL
    Boolean moveConnectedCallLeg = Boolean.valueOf(data.getFirst("MoveConnectedCallLeg"));

    String callPath = null;
    final ActorRef call;
    final CallInfo callInfo;

    try {
      callPath = cdr.getCallPath();
      Future<Object> future = (Future<Object>) ask(callManager, new GetCall(callPath), expires);
      call = (ActorRef) Await.result(future, Duration.create(10, TimeUnit.SECONDS));

      future = (Future<Object>) ask(call, new GetCallInfo(), expires);
      CallResponse<CallInfo> response =
          (CallResponse<CallInfo>) Await.result(future, Duration.create(10, TimeUnit.SECONDS));
      callInfo = response.get();
    } catch (Exception exception) {
      return status(INTERNAL_SERVER_ERROR).entity(exception.getMessage()).build();
    }

    if (method == null) method = "POST";

    if (url != null && status != null) {
      // Throw exception. We can either redirect a running call using Url or change the state of a
      // Call with Status
      final String errorMessage =
          "You can either redirect a running call using \"Url\" or change the state of a Call with \"Status\"";
      return status(javax.ws.rs.core.Response.Status.CONFLICT).entity(errorMessage).build();
    }

    // Modify state of a call
    if (status != null) {
      if (status.equalsIgnoreCase("canceled")) {
        if (callInfo.state().name().equalsIgnoreCase("queued")
            || callInfo.state().name().equalsIgnoreCase("ringing")) {
          if (call != null) {
            call.tell(new Hangup(), null);
          }
        } else {
          // Do Nothing. We can only cancel Queued or Ringing calls
        }
      }

      if (status.equalsIgnoreCase("completed")) {
        // Specifying "completed" will attempt to hang up a call even if it's already in progress.
        if (call != null) {
          call.tell(new Hangup(), null);
        }
      }
    }

    if (url != null && call != null) {
      try {
        final String version = getApiVersion(data);
        final URI uri = (new URL(url)).toURI();

        URI fallbackUri = (fallBackUrl != null) ? (new URL(fallBackUrl)).toURI() : null;
        fallBackMethod = (fallBackMethod == null) ? "POST" : fallBackMethod;
        URI callbackUri = (statusCallBack != null) ? (new URL(statusCallBack)).toURI() : null;
        statusCallbackMethod = (statusCallbackMethod == null) ? "POST" : statusCallbackMethod;

        final UpdateCallScript update =
            new UpdateCallScript(
                call,
                accountSid,
                version,
                uri,
                method,
                fallbackUri,
                fallBackMethod,
                callbackUri,
                statusCallbackMethod,
                moveConnectedCallLeg);
        callManager.tell(update, null);
      } catch (Exception exception) {
        return status(INTERNAL_SERVER_ERROR).entity(exception.getMessage()).build();
      }
    }

    if (APPLICATION_JSON_TYPE == responseType) {
      return ok(gson.toJson(cdr), APPLICATION_JSON).build();
    } else if (APPLICATION_XML_TYPE == responseType) {
      return ok(xstream.toXML(new RestCommResponse(cdr)), APPLICATION_XML).build();
    } else {
      return null;
    }
  }
Beispiel #7
0
  @SuppressWarnings("unchecked")
  protected Response putCall(
      final String accountSid,
      final MultivaluedMap<String, String> data,
      final MediaType responseType) {
    final Sid accountId = new Sid(accountSid);
    try {
      secure(daos.getAccountsDao().getAccount(accountSid), "RestComm:Create:Calls");
    } catch (final AuthorizationException exception) {
      return status(UNAUTHORIZED).build();
    }
    try {
      validate(data);
      if (normalizePhoneNumbers) normalize(data);
    } catch (final RuntimeException exception) {
      return status(BAD_REQUEST).entity(exception.getMessage()).build();
    }
    final String from = data.getFirst("From");
    final String to = data.getFirst("To");
    final String username = data.getFirst("Username");
    final String password = data.getFirst("Password");
    final Integer timeout = getTimeout(data);
    final Timeout expires = new Timeout(Duration.create(60, TimeUnit.SECONDS));
    CreateCall create = null;
    try {
      if (to.contains("@")) {
        create =
            new CreateCall(
                from,
                to,
                username,
                password,
                true,
                timeout != null ? timeout : 30,
                CreateCall.Type.SIP,
                accountId,
                null);
      } else if (to.startsWith("client")) {
        create =
            new CreateCall(
                from,
                to,
                username,
                password,
                true,
                timeout != null ? timeout : 30,
                CreateCall.Type.CLIENT,
                accountId,
                null);
      } else {
        create =
            new CreateCall(
                from,
                to,
                username,
                password,
                true,
                timeout != null ? timeout : 30,
                CreateCall.Type.PSTN,
                accountId,
                null);
      }
      create.setCreateCDR(false);
      if (callManager == null)
        callManager =
            (ActorRef) context.getAttribute("org.mobicents.servlet.restcomm.telephony.CallManager");
      Future<Object> future = (Future<Object>) ask(callManager, create, expires);
      Object object = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
      Class<?> klass = object.getClass();
      if (CallManagerResponse.class.equals(klass)) {
        final CallManagerResponse<ActorRef> managerResponse =
            (CallManagerResponse<ActorRef>) object;
        if (managerResponse.succeeded()) {
          final ActorRef call = managerResponse.get();
          future = (Future<Object>) ask(call, new GetCallInfo(), expires);
          object = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
          klass = object.getClass();
          if (CallResponse.class.equals(klass)) {
            final CallResponse<CallInfo> callResponse = (CallResponse<CallInfo>) object;
            if (callResponse.succeeded()) {
              final CallInfo callInfo = callResponse.get();
              // Execute the call script.
              final String version = getApiVersion(data);
              final URI url = getUrl("Url", data);
              final String method = getMethod("Method", data);
              final URI fallbackUrl = getUrl("FallbackUrl", data);
              final String fallbackMethod = getMethod("FallbackMethod", data);
              final URI callback = getUrl("StatusCallback", data);
              final String callbackMethod = getMethod("StatusCallbackMethod", data);
              final ExecuteCallScript execute =
                  new ExecuteCallScript(
                      call,
                      accountId,
                      version,
                      url,
                      method,
                      fallbackUrl,
                      fallbackMethod,
                      callback,
                      callbackMethod);
              callManager.tell(execute, null);
              // Create a call detail record for the call.
              //                            final CallDetailRecord.Builder builder =
              // CallDetailRecord.builder();
              //                            builder.setSid(callInfo.sid());
              //                            builder.setDateCreated(callInfo.dateCreated());
              //                            builder.setAccountSid(accountId);
              //                            builder.setTo(to);
              //                            builder.setCallerName(callInfo.fromName());
              //                            builder.setFrom(from);
              //                            builder.setForwardedFrom(callInfo.forwardedFrom());
              //                            builder.setStatus(callInfo.state().toString());
              //                            final DateTime now = DateTime.now();
              //                            builder.setStartTime(now);
              //                            builder.setDirection(callInfo.direction());
              //                            builder.setApiVersion(version);
              //                            final StringBuilder buffer = new StringBuilder();
              //                            buffer.append("/").append(version).append("/Accounts/");
              //                            buffer.append(accountId.toString()).append("/Calls/");
              //                            buffer.append(callInfo.sid().toString());
              //                            final URI uri = URI.create(buffer.toString());
              //                            builder.setUri(uri);

              CallDetailRecord cdr =
                  daos.getCallDetailRecordsDao().getCallDetailRecord(callInfo.sid());
              //
              //                            builder.setCallPath(call.path().toString());
              //
              //                            final CallDetailRecord cdr = builder.build();
              //                            daos.getCallDetailRecordsDao().addCallDetailRecord(cdr);
              if (APPLICATION_JSON_TYPE == responseType) {
                return ok(gson.toJson(cdr), APPLICATION_JSON).build();
              } else if (APPLICATION_XML_TYPE == responseType) {
                return ok(xstream.toXML(new RestCommResponse(cdr)), APPLICATION_XML).build();
              } else {
                return null;
              }
            }
          }
        } else {
          return status(INTERNAL_SERVER_ERROR)
              .entity(managerResponse.cause() + " : " + managerResponse.error())
              .build();
        }
      }
      return status(INTERNAL_SERVER_ERROR).build();
    } catch (final Exception exception) {
      return status(INTERNAL_SERVER_ERROR).entity(exception.getMessage()).build();
    }
  }
Beispiel #8
0
  // Issue 153: https://bitbucket.org/telestax/telscale-restcomm/issue/153
  // Issue 110: https://bitbucket.org/telestax/telscale-restcomm/issue/110
  protected Response getCalls(final String accountSid, UriInfo info, MediaType responseType) {

    try {
      secure(daos.getAccountsDao().getAccount(accountSid), "RestComm:Read:Calls");
    } catch (final AuthorizationException exception) {
      return status(UNAUTHORIZED).build();
    }

    String pageSize = info.getQueryParameters().getFirst("PageSize");
    String page = info.getQueryParameters().getFirst("Page");
    // String afterSid = info.getQueryParameters().getFirst("AfterSid");
    String recipient = info.getQueryParameters().getFirst("To");
    String sender = info.getQueryParameters().getFirst("From");
    String status = info.getQueryParameters().getFirst("Status");
    String startTime = info.getQueryParameters().getFirst("StartTime");
    String parentCallSid = info.getQueryParameters().getFirst("ParentCallSid");

    if (pageSize == null) {
      pageSize = "50";
    }

    if (page == null) {
      page = "0";
    }

    int limit = Integer.parseInt(pageSize);
    int offset =
        (page == "0")
            ? 0
            : (((Integer.parseInt(page) - 1) * Integer.parseInt(pageSize))
                + Integer.parseInt(pageSize));

    CallDetailRecordsDao dao = daos.getCallDetailRecordsDao();

    CallDetailRecordFilter filterForTotal;
    try {
      filterForTotal =
          new CallDetailRecordFilter(
              accountSid, recipient, sender, status, startTime, parentCallSid, null, null);
    } catch (ParseException e) {
      return status(BAD_REQUEST).build();
    }

    final int total = dao.getTotalCallDetailRecords(filterForTotal);

    if (Integer.parseInt(page) > (total / limit)) {
      return status(javax.ws.rs.core.Response.Status.BAD_REQUEST).build();
    }

    CallDetailRecordFilter filter;
    try {
      filter =
          new CallDetailRecordFilter(
              accountSid, recipient, sender, status, startTime, parentCallSid, limit, offset);
    } catch (ParseException e) {
      return status(BAD_REQUEST).build();
    }

    final List<CallDetailRecord> cdrs = dao.getCallDetailRecords(filter);

    listConverter.setCount(total);
    listConverter.setPage(Integer.parseInt(page));
    listConverter.setPageSize(Integer.parseInt(pageSize));
    listConverter.setPathUri(info.getRequestUri().getPath());

    if (APPLICATION_XML_TYPE == responseType) {
      final RestCommResponse response = new RestCommResponse(new CallDetailRecordList(cdrs));
      return ok(xstream.toXML(response), APPLICATION_XML).build();
    } else if (APPLICATION_JSON_TYPE == responseType) {
      return ok(gson.toJson(new CallDetailRecordList(cdrs)), APPLICATION_JSON).build();
    } else {
      return null;
    }
  }