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();
  }
 @Override
 public synchronized void forwardTo(final Endpoint endpoint, final Map<String, String> headers)
     throws SignalException, IllegalStateException {
   URI target = null;
   try {
     target = _ctx.getSipFactory().createURI(endpoint.getURI().toString());
   } catch (final ServletParseException e) {
     throw new IllegalArgumentException(e);
   }
   this.checkState();
   _forwarded = true;
   final SipServletRequest req =
       _ctx.getSipFactory()
           .createRequest(
               _req.getApplicationSession(), _req.getMethod(), _req.getFrom(), _req.getTo());
   req.setRequestURI(target);
   SIPHelper.addHeaders(req, headers);
   SIPHelper.copyContent(_req, req);
   SIPHelper.linkSIPMessage(_req, req);
   try {
     req.send();
   } catch (final IOException e) {
     throw new SignalException(e);
   }
 }
  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);
  }
  private boolean isSipTrunk(SipServletRequest request) {
    String deviceAddress = request.getFrom().getValue();
    if (deviceAddress.contains("sip_trunk")) {
      return true;
    }

    return false;
  }
 @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();
 }
示例#6
0
  @Override
  protected void doBye(SipServletRequest req) {
    SipSession session = req.getSession(false);
    SipSession linkedSession = (SipSession) session.getAttribute(Constant.LINKED_SESSION);

    logger.info("SIP BYE From : " + req.getFrom().toString());

    // caller session
    if (null != session.getAttribute(Constant.INVITE_USER_REQUEST)) {
      doBye(session, req);
    }
    // callee session
    else {
      logger.info("callee do bye!");
      doBye(linkedSession, req);
    }
  }
示例#7
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;
  }