示例#1
0
  /**
   * Checks for certain params existence in the value, and replace them with real values obtained
   * from <tt>request</tt>.
   *
   * @param value the value of the header param
   * @param request the request we are processing
   * @return the value with replaced params
   */
  private static String processParams(String value, Request request) {
    if (value.indexOf("${from.address}") != -1) {
      FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);

      if (fromHeader != null) {
        value = value.replace("${from.address}", fromHeader.getAddress().getURI().toString());
      }
    }

    if (value.indexOf("${from.userID}") != -1) {
      FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);

      if (fromHeader != null) {
        URI fromURI = fromHeader.getAddress().getURI();
        String fromAddr = fromURI.toString();

        // strips sip: or sips:
        if (fromURI.isSipURI()) {
          fromAddr = fromAddr.replaceFirst(fromURI.getScheme() + ":", "");
        }

        // take the userID part
        int index = fromAddr.indexOf('@');
        if (index > -1) fromAddr = fromAddr.substring(0, index);

        value = value.replace("${from.userID}", fromAddr);
      }
    }

    if (value.indexOf("${to.address}") != -1) {
      ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);

      if (toHeader != null) {
        value = value.replace("${to.address}", toHeader.getAddress().getURI().toString());
      }
    }

    if (value.indexOf("${to.userID}") != -1) {
      ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);

      if (toHeader != null) {
        URI toURI = toHeader.getAddress().getURI();
        String toAddr = toURI.toString();

        // strips sip: or sips:
        if (toURI.isSipURI()) {
          toAddr = toAddr.replaceFirst(toURI.getScheme() + ":", "");
        }

        // take the userID part
        int index = toAddr.indexOf('@');
        if (index > -1) toAddr = toAddr.substring(0, index);

        value = value.replace("${to.userID}", toAddr);
      }
    }

    return value;
  }
示例#2
0
 public void processRegister(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
   Request request = requestEvent.getRequest();
   ContactHeader contact = (ContactHeader) request.getHeader(ContactHeader.NAME);
   SipURI contactUri = (SipURI) contact.getAddress().getURI();
   FromHeader from = (FromHeader) request.getHeader(FromHeader.NAME);
   SipURI fromUri = (SipURI) from.getAddress().getURI();
   registrar.put(fromUri.getUser(), contactUri);
   try {
     Response response = this.messageFactory.createResponse(200, request);
     ServerTransaction serverTransaction = sipProvider.getNewServerTransaction(request);
     serverTransaction.sendResponse(response);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
示例#3
0
  /**
   * handles the INVITED state.
   *
   * @param response the response
   * @param clientTransaction the client transaction
   * @throws SipException SIP stack related error
   */
  private void handleCallParticipantInvited(Response response, ClientTransaction clientTransaction)
      throws ParseException, SipException, InvalidArgumentException {
    FromHeader fromHeader = (FromHeader) response.getHeader(FromHeader.NAME);

    String displayName = fromHeader.getAddress().getDisplayName();

    int statusCode = response.getStatusCode();

    Logger.println(
        "handleCallParticipantInvited "
            + cp
            + " status "
            + statusCode
            + " "
            + response.getReasonPhrase());
    Logger.println("handleCallParticipantInvited , displayname " + displayName);

    CallIdHeader callIdHeader = (CallIdHeader) response.getHeader(CallIdHeader.NAME);

    if (sipCallId.equals(callIdHeader.getCallId())
        && displayName.equals(cp.getDisplayName())
        && (statusCode == Response.OK || statusCode == Response.SESSION_PROGRESS)
        && ((CSeqHeader) response.getHeader(CSeqHeader.NAME)).getMethod().equals(Request.INVITE)) {
      if (statusCode == Response.SESSION_PROGRESS) {
        /*
         * For some calls, we never get an OK.  Instead we just get
         * SESSION_PROGRESS.  In order to handle these calls, we treat
         * SESSION_PROGRESS as OK.  If an OK arrives later, we'll
         * send an ACK.  This flag allows us to enable or
         * disable this workaround for each call.
         *
         * The problem with always treating SESSION_PROGRESS as OK
         * is that in a conference everybody will hear the ringing sound
         * which the remote call sends until the call is actually answered.
         * This can be avoided if joinConfirmation is specified.
         * The other problem is that if we treat SESSION_PROGRESS
         * as though the call has been answered, then we'll start
         * playing the treatment before a person really answers to
         * hear the treatment.
         */

        if (cp.getHandleSessionProgress() == false) {
          Logger.writeFile("Call " + cp + " Ignoring SESSION_PROGRESS");
          return;
        }

        Logger.writeFile("Call " + cp + " Treating SESSION_PROGRESS as OK");
      }

      if (response.getRawContent() == null) {
        Logger.error("Call " + cp + " no SDP in OK Response!");
        cancelRequest("SIP error!  no SDP in OK Response!");
        return;
      }

      this.clientTransaction = clientTransaction;

      if (statusCode == Response.OK) {
        gotOk = true;
        Logger.writeFile("Call " + cp + " Got OK, call answered\n" + response);
      }

      ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);

      /*
       * We got an OK response.
       *
       * send an ACK back to the CallParticipant
       */

      if (statusCode == Response.OK) {
        sipUtil.sendAck(clientTransaction);
        ackSent = true;
      }

      if (callAnswered) {
        Logger.writeFile("Call " + cp + " done processing OK");
        return;
      }

      /*
       * Remember the IP and port of where to send data to
       * the CallParticipant.
       */

      sdpBody = new String(response.getRawContent());

      SdpInfo sdpInfo;

      try {
        sdpInfo = sipUtil.getSdpInfo(sdpBody, false);

      } catch (ParseException e) {

        Logger.error("Call " + cp + " Invalid SDP in OK Response! " + e.getMessage());
        cancelRequest("SIP error!  Invalid SDP in OK Response!");
        return;
      }

      MediaInfo mediaInfo = sdpInfo.getMediaInfo();
      InetSocketAddress isa =
          new InetSocketAddress(sdpInfo.getRemoteHost(), sdpInfo.getRemotePort());
      InetSocketAddress rtcpAddress = sdpInfo.getRtcpAddress();
      setEndpointAddress(
          isa,
          mediaInfo.getPayload(),
          sdpInfo.getTransmitMediaInfo().getPayload(),
          sdpInfo.getTelephoneEventPayload(),
          rtcpAddress);

      /*
       * The CallParticipant has answered.
       * If join confirmation is required, we remain in the
       * INVITED state.  We set the callAnswered flag so that
       * if the join confirmation times out we know to
       * send a BYE rather than a CANCEL.
       */

      callAnswered = true;

      if (cp.getJoinConfirmationTimeout() == 0) {
        setState(CallState.ANSWERED);
      }

      /*
       * Start treatment if any and wait for it to finish.
       * When the treatment finishes, notification will
       * be delivered to our parent which will indicate
       * we're ready for the conference.
       *
       * If there's no treatment to be played, we're ready now
       * unless we're waiting for join confirmation..
       */
      initializeCallAnsweredTreatment();

      if (callAnsweredTreatment != null) {
        startCallAnsweredTreatment();
      } else {

        if (cp.getJoinConfirmationTimeout() == 0) {
          setState(CallState.ESTABLISHED);
        }
      }

    } else {
      Logger.writeFile("Call " + cp + " Ignoring response: " + response.getReasonPhrase());

      if (Logger.logLevel >= Logger.LOG_SIP) {
        Logger.println("Call " + cp + " Response: " + response);
      }
    }
  }
示例#4
0
  public synchronized void processResponse(ResponseEvent responseReceivedEvent) {

    try {
      Response response = (Response) responseReceivedEvent.getResponse();
      ClientTransaction clientTransaction = responseReceivedEvent.getClientTransaction();

      int statusCode = response.getStatusCode();

      FromHeader fromHeader = (FromHeader) response.getHeader(FromHeader.NAME);

      String displayName = fromHeader.getAddress().getDisplayName();

      if (Logger.logLevel >= Logger.LOG_SIP) {
        Logger.println(
            "Response:  statusCode "
                + statusCode
                + " state "
                + getCallState()
                + " fromHeader "
                + displayName
                + " call participant "
                + cp.getName());
      }

      if (reasonCallTerminated != null) {
        /*
         * Ignore OK and Request Terminated.
         * XXX what's the symbol for 487?
         */
        if (statusCode != Response.OK && statusCode != 487) {
          if (Logger.logLevel >= Logger.LOG_SIP) {
            Logger.println("Call " + cp + ":  request cancelled, ignoring response");
          }
        }

        CallIdHeader callIdHeader = (CallIdHeader) response.getHeader("Call-Id");

        String sipCallId = callIdHeader.getCallId();
        sipServerCallback.removeSipListener(sipCallId);
        return;
      }

      /*
       * Some type of global failure that prevents the
       * CallParticipant from being contacted, report failure.
       */
      if (forceGatewayError) {
        statusCode = 500;
        forceGatewayError = false;
      }

      if (statusCode >= 500 && getState() == CallState.INVITED) {
        Logger.error(
            "Call " + cp + " gateway error:  " + statusCode + " " + response.getReasonPhrase());
        cancelRequest("gateway error: " + statusCode + " " + response.getReasonPhrase());
        return;

      } else if (statusCode == Response.PROXY_AUTHENTICATION_REQUIRED
          || statusCode == Response.UNAUTHORIZED) {

        if (cp.getProxyCredentials() != null) {
          try {
            SipServer.handleChallenge(response, clientTransaction, cp.getProxyCredentials())
                .sendRequest();

          } catch (Exception e) {

            Logger.println("Proxy authentification failed  " + e);
          }
        }
        return;

      } else if (statusCode >= 400) {

        // if we get a busy or an unknown error, play busy.
        Logger.println("Call " + cp + " got status code :" + statusCode);

        cp.setCallEndTreatment(null);
        cp.setConferenceJoinTreatment(null);
        cp.setConferenceLeaveTreatment(null);

        /*
         * play busy treatment, but deallocate any resources
         * held up by ringBack first, if any.
         */
        // stopCallAnsweredTreatment();

        if (statusCode == Response.BUSY_HERE) {
          try {
            if (busyTreatment != null) {
              addTreatment(busyTreatment);
              // busyTreatment.waitForTreatment();
            } else {
              Logger.println("Unable to play busy treatment!!!");
            }
          } catch (Exception e) {
            Logger.error("can't start busy treatment!" + sdpBody);
          }

          CallEvent callEvent = new CallEvent(CallEvent.BUSY_HERE);

          callEvent.setInfo(response.getReasonPhrase());

          sendCallEventNotification(callEvent);
        }

        // sipUtil.sendBye(clientTransaction);
        cancelRequest(response.getReasonPhrase());
        return;
      }

      /* state machine */
      switch (getState()) {
          /*
           * CallParticipant picked up, send treatment if any,
           * and wait for it to finish.
           */
        case CallState.INVITED:
          if (rejectCall) {
            Logger.error(
                "Call " + cp + " gateway error:  " + statusCode + " " + response.getReasonPhrase());

            cancelRequest("gateway error: " + statusCode + " " + response.getReasonPhrase());
            return;
          }

          handleCallParticipantInvited(response, clientTransaction);
          break;

          /*
           * Call established, the ACK needs to be resent.
           * According to Ranga, this is done by the NIST SIP Stack.
           */
        case CallState.ESTABLISHED:
          if (statusCode == Response.OK) {
            gotOk = true;

            Logger.writeFile("Call " + cp + " Got OK, ESTABLISHED");

            if (ackSent == false) {
              sipUtil.sendAck(clientTransaction);
              ackSent = true;
            }
          }
          break;

        case CallState.ENDED:
          break; // ignore the response

        default:
          Logger.error("Process Response bad state " + getState() + "\n" + response);
      }
    } catch (SipException e) {
      Logger.exception("Call " + cp + " SIP Stack error ", e);

      cancelRequest("processResponse:  SIP Stack error " + e.getMessage());
    } catch (Exception e) {
      Logger.exception("processResponse:  " + cp, e);

      cancelRequest("processResponse:  SIP Stack error " + e.getMessage());
    }
  }
  private void register() throws IOException {
    Log.info("Registering with " + registrar);
    FromHeader fromHeader = getFromHeader();
    Address fromAddress = fromHeader.getAddress();

    // Request URI
    SipURI requestURI = null;
    try {
      requestURI = addressFactory.createSipURI(null, registrar);

    } catch (ParseException e) {
      throw new IOException("Bad registrar address:" + registrar + " " + e.getMessage());
    }
    // requestURI.setPort(registrarPort);

    try {
      requestURI.setTransportParam(sipProvider.getListeningPoint().getTransport());

    } catch (ParseException e) {
      throw new IOException(
          sipProvider.getListeningPoint().getTransport()
              + " is not a valid transport! "
              + e.getMessage());
    }

    CallIdHeader callIdHeader = sipProvider.getNewCallId();
    CSeqHeader cSeqHeader = null;

    try {
      cSeqHeader = headerFactory.createCSeqHeader(1, Request.REGISTER);
    } catch (ParseException e) {
      // Should never happen
      throw new IOException("Corrupt Sip Stack " + e.getMessage());
    } catch (InvalidArgumentException e) {
      // Should never happen
      throw new IOException("The application is corrupt ");
    }

    ToHeader toHeader = null;
    try {
      String proxyWorkAround = System.getProperty("com.sun.mc.softphone.REGISTRAR_WORKAROUND");

      if (proxyWorkAround != null && proxyWorkAround.toUpperCase().equals("TRUE")) {

        SipURI toURI = (SipURI) (requestURI.clone());
        toURI.setUser(System.getProperty("user.name"));

        toHeader = headerFactory.createToHeader(addressFactory.createAddress(toURI), null);
      } else {
        toHeader = headerFactory.createToHeader(fromAddress, null);
      }
    } catch (ParseException e) {
      throw new IOException(
          "Could not create a To header for address:"
              + fromHeader.getAddress()
              + " "
              + e.getMessage());
    }

    ArrayList viaHeaders = getLocalViaHeaders();
    MaxForwardsHeader maxForwardsHeader = getMaxForwardsHeader();
    Request request = null;

    try {
      request =
          messageFactory.createRequest(
              requestURI,
              Request.REGISTER,
              callIdHeader,
              cSeqHeader,
              fromHeader,
              toHeader,
              viaHeaders,
              maxForwardsHeader);
    } catch (ParseException e) {
      throw new IOException("Could not create the register request! " + e.getMessage());
    }

    ExpiresHeader expHeader = null;

    for (int retry = 0; retry < 2; retry++) {
      try {
        expHeader = headerFactory.createExpiresHeader(expires);
      } catch (InvalidArgumentException e) {
        if (retry == 0) {
          continue;
        }
        throw new IOException(
            "Invalid registrations expiration parameter - " + expires + " " + e.getMessage());
      }
    }

    request.addHeader(expHeader);
    ContactHeader contactHeader = getRegistrationContactHeader();
    request.addHeader(contactHeader);

    try {
      SipURI routeURI =
          (SipURI) addressFactory.createURI("sip:" + proxyCredentials.getProxy() + ";lr");
      RouteHeader routeHeader =
          headerFactory.createRouteHeader(addressFactory.createAddress(routeURI));
      request.addHeader(routeHeader);

    } catch (Exception e) {

      Log.error("Creating registration route error ", e);
    }

    ClientTransaction regTrans = null;
    try {
      regTrans = sipProvider.getNewClientTransaction(request);
    } catch (TransactionUnavailableException e) {
      throw new IOException(
          "Could not create a register transaction!\n"
              + "Check that the Registrar address is correct! "
              + e.getMessage());
    }

    try {
      sipCallId = callIdHeader.getCallId();
      sipServerCallback.addSipListener(sipCallId, this);
      registerRequest = request;
      regTrans.sendRequest();

      Log.debug("Sent register request " + registerRequest);

      if (expires > 0) {
        scheduleReRegistration();
      }
    } catch (Exception e) {
      throw new IOException("Could not send out the register request! " + e.getMessage());
    }

    this.registerRequest = request;
  }
示例#6
0
    @Override
    public void run() {
      try {
        logger.logEntry();

        // From
        FromHeader fromHeader = null;
        try {
          // this keep alive task only makes sense in case we have
          // a registrar so we deliberately use our AOR and do not
          // use the getOurSipAddress() method.
          fromHeader =
              provider
                  .getHeaderFactory()
                  .createFromHeader(
                      provider.getRegistrarConnection().getAddressOfRecord(),
                      SipMessageFactory.generateLocalTag());
        } catch (ParseException ex) {
          // this should never happen so let's just log and bail.
          logger.error("Failed to generate a from header for " + "our register request.", ex);
          return;
        }

        // Call ID Header
        CallIdHeader callIdHeader = provider.getDefaultJainSipProvider().getNewCallId();

        // CSeq Header
        CSeqHeader cSeqHeader = null;
        try {
          cSeqHeader =
              provider.getHeaderFactory().createCSeqHeader(getNextCSeqValue(), Request.OPTIONS);
        } catch (ParseException ex) {
          // Should never happen
          logger.error("Corrupt Sip Stack", ex);
          return;
        } catch (InvalidArgumentException ex) {
          // Should never happen
          logger.error("The application is corrupt", ex);
          return;
        }

        // To Header
        ToHeader toHeader = null;
        try {
          // this request isn't really going anywhere so we put our
          // own address in the To Header.
          toHeader = provider.getHeaderFactory().createToHeader(fromHeader.getAddress(), null);
        } catch (ParseException ex) {
          logger.error("Could not create a To header for address:" + fromHeader.getAddress(), ex);
          return;
        }

        // MaxForwardsHeader
        MaxForwardsHeader maxForwardsHeader = provider.getMaxForwardsHeader();
        // Request
        Request request = null;
        try {
          // create a host-only uri for the request uri header.
          String domain = ((SipURI) toHeader.getAddress().getURI()).getHost();

          // request URI
          SipURI requestURI = provider.getAddressFactory().createSipURI(null, domain);

          // Via Headers
          ArrayList<ViaHeader> viaHeaders = provider.getLocalViaHeaders(requestURI);

          request =
              provider
                  .getMessageFactory()
                  .createRequest(
                      requestURI,
                      Request.OPTIONS,
                      callIdHeader,
                      cSeqHeader,
                      fromHeader,
                      toHeader,
                      viaHeaders,
                      maxForwardsHeader);

          if (logger.isDebugEnabled()) logger.debug("Created OPTIONS request " + request);
        } catch (ParseException ex) {
          logger.error("Could not create an OPTIONS request!", ex);
          return;
        }

        Iterator<String> supportedMethods = provider.getSupportedMethods().iterator();

        // add to the allows header all methods that we support
        while (supportedMethods.hasNext()) {
          String method = supportedMethods.next();

          // don't support REGISTERs
          if (method.equals(Request.REGISTER)) continue;

          request.addHeader(provider.getHeaderFactory().createAllowHeader(method));
        }

        Iterator<String> events = provider.getKnownEventsList().iterator();

        synchronized (provider.getKnownEventsList()) {
          while (events.hasNext()) {
            String event = events.next();

            request.addHeader(provider.getHeaderFactory().createAllowEventsHeader(event));
          }
        }

        // Transaction
        ClientTransaction optionsTrans = null;
        try {
          optionsTrans = provider.getDefaultJainSipProvider().getNewClientTransaction(request);
        } catch (TransactionUnavailableException ex) {
          logger.error("Could not create options transaction!\n", ex);
          return;
        }
        try {
          optionsTrans.sendRequest();
          if (logger.isDebugEnabled()) logger.debug("sent request= " + request);
        } catch (SipException ex) {
          logger.error("Could not send out the options request!", ex);

          if (ex.getCause() instanceof IOException) {
            // IOException problem with network
            disconnect();
          }

          return;
        }
      } catch (Exception ex) {
        logger.error("Cannot send OPTIONS keep alive", ex);
      }
    }
    /**
     * Process a request from a distant contact
     *
     * @param requestEvent the <tt>RequestEvent</tt> containing the newly received request.
     * @return <tt>true</tt> if the specified event has been handled by this processor and shouldn't
     *     be offered to other processors registered for the same method; <tt>false</tt>, otherwise
     */
    @Override
    public boolean processRequest(RequestEvent requestEvent) {
      synchronized (messageProcessors) {
        for (SipMessageProcessor listener : messageProcessors)
          if (!listener.processMessage(requestEvent)) return true;
      }

      // get the content
      String content = null;
      Request req = requestEvent.getRequest();
      try {

        content = new String(req.getRawContent(), getCharset(req));
      } catch (UnsupportedEncodingException ex) {
        if (logger.isDebugEnabled()) logger.debug("failed to convert the message charset");
        content = new String(requestEvent.getRequest().getRawContent());
      }

      // who sent this request ?
      FromHeader fromHeader = (FromHeader) requestEvent.getRequest().getHeader(FromHeader.NAME);

      if (fromHeader == null) {
        logger.error("received a request without a from header");
        return false;
      }

      Contact from =
          opSetPersPresence.resolveContactID(fromHeader.getAddress().getURI().toString());

      ContentTypeHeader ctheader = (ContentTypeHeader) req.getHeader(ContentTypeHeader.NAME);

      String ctype = null;
      String cencoding = null;

      if (ctheader == null) {
        ctype = DEFAULT_MIME_TYPE;
      } else {
        ctype = ctheader.getContentType() + "/" + ctheader.getContentSubType();
        cencoding = ctheader.getParameter("charset");
      }

      if (cencoding == null) cencoding = DEFAULT_MIME_ENCODING;

      Message newMessage = createMessage(content, ctype, cencoding, null);

      if (from == null) {
        if (logger.isDebugEnabled())
          logger.debug(
              "received a message from an unknown contact: "
                  + fromHeader.getAddress().getURI().toString());
        // create the volatile contact
        from = opSetPersPresence.createVolatileContact(fromHeader.getAddress().getURI().toString());
      }

      // answer ok
      try {
        Response ok =
            sipProvider.getMessageFactory().createResponse(Response.OK, requestEvent.getRequest());
        SipStackSharing.getOrCreateServerTransaction(requestEvent).sendResponse(ok);
      } catch (ParseException exc) {
        logger.error("failed to build the response", exc);
      } catch (SipException exc) {
        logger.error("failed to send the response : " + exc.getMessage(), exc);
      } catch (InvalidArgumentException exc) {
        if (logger.isDebugEnabled())
          logger.debug("Invalid argument for createResponse : " + exc.getMessage(), exc);
      }

      // fire an event
      MessageReceivedEvent msgReceivedEvt =
          new MessageReceivedEvent(newMessage, from, System.currentTimeMillis());
      fireMessageEvent(msgReceivedEvt);

      return true;
    }