Beispiel #1
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 #2
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();
    }
  }