Ejemplo n.º 1
0
 // 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;
   }
 }
Ejemplo n.º 2
0
 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;
   }
 }
Ejemplo n.º 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;
     }
   }
 }
Ejemplo n.º 4
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;
    }
  }
Ejemplo n.º 5
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();
    }
  }
Ejemplo n.º 6
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;
    }
  }