/**
   * Handles server noninvite message
   *
   * @param msg - noninvite message
   */
  public void handleIncomingBye(final Request msg) {

    assert TransactionUtils.isTransactionExecutionThread()
        : "Code run in wrong thread. Must be run in TransactionThread. Now in "
            + Thread.currentThread();
    assert !done.get();
    assert msg != null && MessageType.SIP_BYE == MessageType.parse(msg.getMethod());

    Logger.log("Remote party has sent noninvite");
    if (!done.get()) {

      final Dialog dialog = getStackContext().getDialogStorage().findDialogForMessage(msg);
      assert dialog != null;
      assert STATED == dialog.getState();

      dialog.getMessageHistory().addMessage(msg, true);

      final TransactionManager transactionManager = getTransactionManager();
      transactionManager.addListener(
          new FirstMessageResolver(SIP_BYE_SERVER.getName(), dialog, msg, transactionManager));

      final Transaction transaction =
          transactionManager.lookUpTransaction(dialog, null, SIP_BYE_SERVER);
      runAsynchronously(transaction, TRANSACTION_TIMEOUT);
    }
  }
  /**
   * Handles server invite message
   *
   * @param msg - invite message
   */
  public void handleIncomingInvite(final Request msg) throws DialogStateException {
    assert TransactionUtils.isTransactionExecutionThread()
        : "Code run in wrong thread. Must be run in TransactionThread. Now in "
            + Thread.currentThread();

    assert !done.get();
    assert msg != null && MessageType.SIP_INVITE == MessageType.parse(msg.getMethod());

    if (!done.get()) {
      Logger.log("Remote party has sent invite");
      // ClientIdentity localParty =
      // getStackContext().getStackClientRegistry().findAddressee(msg.getTo().getUriBuilder().getShortURI());
      ClientIdentity localParty = getStackContext().getClientRouter().findAddressee(msg);
      if (localParty != null) {
        assert getStackContext().getDialogStorage().findDialogForMessage(msg) == null;
        final Dialog dialog =
            getStackContext().getDialogStorage().getDialogForIncomingMessage(localParty, msg);
        TransactionType<InviteSrvTransaction, ? extends ServerCommonInviteTransaction>
            transactionType = SIP_INVITE_SERVER;

        doHandleIncomingInvite(msg, dialog, transactionType);
      } else {
        throw new DialogStateException(null, DialogStateException.Error.ADDRESSEE_NOT_FOUND, msg);
      }
    }
  }
  public void handleIncomingCancel(Request msg) throws DialogStateException {
    assert !done.get();
    assert msg != null && MessageType.SIP_CANCEL == MessageType.parse(msg.getMethod());

    if (!done.get()) {
      Logger.log("Remote party has sent SIP_CANCEL");

      final Dialog dialog = getStackContext().getDialogStorage().findDialogForMessage(msg);

      if (dialog != null) {
        dialog.getMessageHistory().addMessage(msg, true);

        final InviteSrvTransaction transaction =
            getTransactionManager().findTransaction(dialog, SIP_INVITE_SERVER);

        if (transaction != null) {
          final DialogStateEvent<BaseSipMessage> stateEvent =
              new DefaultDialogStateEvent<BaseSipMessage>(
                  dialog, SessionState.SESSION_TERMINATED, msg);

          dialogStateListenerHolder.getNotifier().onSessionTerminated(stateEvent);
          dialogStateListenerHolder.getNotifier().onSessionEnded(stateEvent);
          // DIALOG.putCustomParameter(ParamKey.INITIAL_MESSAGE, ((Transaction)
          // transaction).getInitialMessage());

          transaction.cancel();
        } else {
          // assert false : "Transaction already terminated for msg: " + msg.shortDescription() + "
          // dialog: " + dialog;
          throw new DialogStateException(
              dialog, DialogStateException.Error.REQUEST_FOR_UNKNOWN_DIALOG, msg);
        }
      } else {
        // assert false : "Dialog is already terminated or never exist. Message :" +
        // msg.shortDescription();
        throw new DialogStateException(
            dialog, DialogStateException.Error.REQUEST_FOR_UNKNOWN_DIALOG, msg);
      }
    }
  }
  public void handleIncomingReInvite(final Request msg) throws DialogStateException {
    assert TransactionUtils.isTransactionExecutionThread()
        : "Code run in wrong thread. Must be run in TransactionThread. Now in "
            + Thread.currentThread();
    assert !done.get();
    assert msg != null && MessageType.SIP_INVITE == MessageType.parse(msg.getMethod());

    if (!done.get()) {
      Logger.log("Remote party has sent ReInvite");

      final Dialog dialog = getStackContext().getDialogStorage().findDialogForMessage(msg);
      assert dialog != null;

      checkReInvitePreconditions(dialog, msg);
      dialog.markReInviteInProgress(InitiateParty.REMOTE);

      TransactionType<InviteSrvTransaction, ? extends ServerCommonInviteTransaction>
          transactionType = SIP_REINVITE_SERVER;

      doHandleIncomingInvite(msg, dialog, transactionType);
    }
  }