Ejemplo n.º 1
0
  private void sendInviteOK(RequestEvent requestEvent, ServerTransaction inviteTid) {
    try {
      logger.info("sendInviteOK: " + inviteTid);
      if (inviteTid.getState() != TransactionState.COMPLETED) {
        logger.info("shootme: Dialog state before OK: " + inviteTid.getDialog().getState());

        SipProvider sipProvider = (SipProvider) requestEvent.getSource();
        Request request = requestEvent.getRequest();
        Response okResponse = protocolObjects.messageFactory.createResponse(Response.OK, request);
        ListeningPoint lp = sipProvider.getListeningPoint(protocolObjects.transport);
        int myPort = lp.getPort();

        Address address =
            protocolObjects.addressFactory.createAddress(
                "Shootme <sip:" + myAddress + ":" + myPort + ">");
        ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
        okResponse.addHeader(contactHeader);
        inviteTid.sendResponse(okResponse);
        logger.info("shootme: Dialog state after OK: " + inviteTid.getDialog().getState());
        TestHarness.assertEquals(DialogState.CONFIRMED, inviteTid.getDialog().getState());
      } else {
        logger.info("semdInviteOK: inviteTid = " + inviteTid + " state = " + inviteTid.getState());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Ejemplo n.º 2
0
  /** Process the invite request. */
  public void processInvite(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    try {
      System.out.println("shootme: got an Invite sending Trying");
      // System.out.println("shootme: " + request);
      Response response = messageFactory.createResponse(Response.RINGING, request);
      ServerTransaction st = requestEvent.getServerTransaction();

      if (st == null) {
        st = sipProvider.getNewServerTransaction(request);
      }
      dialog = st.getDialog();

      st.sendResponse(response);

      this.okResponse = messageFactory.createResponse(Response.OK, request);
      Address address =
          addressFactory.createAddress("Shootme <sip:" + myAddress + ":" + myPort + ">");
      ContactHeader contactHeader = headerFactory.createContactHeader(address);
      response.addHeader(contactHeader);
      ToHeader toHeader = (ToHeader) okResponse.getHeader(ToHeader.NAME);
      toHeader.setTag("4321"); // Application is supposed to set.
      okResponse.addHeader(contactHeader);
      this.inviteTid = st;
      // Defer sending the OK to simulate the phone ringing.
      // Answered in 1 second ( this guy is fast at taking calls)
      this.inviteRequest = request;

      new Timer().schedule(new MyTimerTask(this), 1000);
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
Ejemplo n.º 3
0
  /** Process the invite request. */
  public void processInvite(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    try {
      System.out.println("cutme: got an Invite sending Trying");
      // System.out.println("cutme: " + request);
      Response response = messageFactory.createResponse(Response.RINGING, request);
      String toTag = Integer.toString((int) (Math.random() * 10000000));
      ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
      toHeader.setTag(toTag); // Application is supposed to set.

      ServerTransaction st = requestEvent.getServerTransaction();

      if (st == null) {
        st = sipProvider.getNewServerTransaction(request);
      }
      inviteTid = st;
      inviteRequest = request;
      dialog = st.getDialog();
      //			inviteRequest = request;
      if (timeToWaitBeforeAnswer > 0) {
        Thread.sleep(timeToWaitBeforeAnswer);
      }

      st.sendResponse(response);
      // If we dont send final response this will receive cancel.
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
Ejemplo n.º 4
0
  public void processCancel(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
    Request request = requestEvent.getRequest();
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    try {
      logger.info("shootme:  got a cancel. ");
      // Because this is not an In-dialog request, you will get a null server Tx id here.
      if (serverTransactionId == null) {
        serverTransactionId = sipProvider.getNewServerTransaction(request);
      }
      Response response = protocolObjects.messageFactory.createResponse(200, request);
      serverTransactionId.sendResponse(response);

      String serverTxId = ((ViaHeader) response.getHeader(ViaHeader.NAME)).getBranch();
      ServerTransaction serverTx = (ServerTransaction) this.serverTxTable.get(serverTxId);
      if (serverTx != null
          && (serverTx.getState().equals(TransactionState.TRYING)
              || serverTx.getState().equals(TransactionState.PROCEEDING))) {
        Request originalRequest = serverTx.getRequest();
        Response resp =
            protocolObjects.messageFactory.createResponse(
                Response.REQUEST_TERMINATED, originalRequest);
        serverTx.sendResponse(resp);
      }

    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
Ejemplo n.º 5
0
    public void run() {
      Request request = requestEvent.getRequest();
      try {
        // System.out.println("shootme: got an Invite sending OK");
        Response response = messageFactory.createResponse(180, request);
        ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
        Address address =
            addressFactory.createAddress("Shootme <sip:" + myAddress + ":" + myPort + ">");
        ContactHeader contactHeader = headerFactory.createContactHeader(address);
        response.addHeader(contactHeader);

        // System.out.println("got a server tranasaction " + st);
        Dialog dialog = st.getDialog();
        /*
         * if (dialog != null) { System.out.println("Dialog " + dialog);
         * System.out.println("Dialog state " + dialog.getState()); }
         */
        st.sendResponse(response); // send 180(RING)
        response = messageFactory.createResponse(200, request);
        toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
        String toTag = new Integer((int) (Math.random() * 1000)).toString();
        toHeader.setTag(toTag); // Application is supposed to set.
        response.addHeader(contactHeader);

        st.sendResponse(response); // send 200(OK)

      } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(0);
      }
    }
Ejemplo n.º 6
0
  public void processResponse(ResponseEvent responseReceivedEvent) {

    // Log.info("Registering response...." + sipCallId);

    Response response = (Response) responseReceivedEvent.getResponse();
    int statusCode = response.getStatusCode();
    String method = ((CSeqHeader) response.getHeader(CSeqHeader.NAME)).getMethod();

    Log.debug("Got response " + response);

    if (statusCode == Response.OK) {
      isRegistered = true;

      Log.info(
          "Voice bridge successfully registered with "
              + registrar
              + " for "
              + proxyCredentials.getXmppUserName());
      PluginImpl.sipRegisterStatus = "Registered ok with " + proxyCredentials.getHost();

      sipServerCallback.removeSipListener(sipCallId);

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

      if (method.equals(Request.REGISTER)) {
        CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);

        if (cseq.getSequenceNumber() < 2) {

          ClientTransaction regTrans =
              SipService.handleChallenge(
                  response, responseReceivedEvent.getClientTransaction(), proxyCredentials);

          if (regTrans != null) {
            try {
              regTrans.sendRequest();

            } catch (Exception e) {

              Log.info("Registration failed, cannot send transaction " + e);
              PluginImpl.sipRegisterStatus = "Registration error " + e.toString();
            }

          } else {
            Log.info("Registration failed, cannot create transaction");
            PluginImpl.sipRegisterStatus = "Registration cannot create transaction";
          }

        } else {
          Log.info("Registration failed " + responseReceivedEvent);
          PluginImpl.sipRegisterStatus = "Registration failed";
        }
      }

    } else {
      Log.info("Unrecognized response:  " + response);
    }
  }
Ejemplo n.º 7
0
  /** Process the invite request. */
  public void processInvite(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    try {
      logger.info("shootme: got an Invite sending Trying");
      // logger.info("shootme: " + request);

      ServerTransaction st = requestEvent.getServerTransaction();

      if (st == null) {
        logger.info("null server tx -- getting a new one");
        st = sipProvider.getNewServerTransaction(request);
      }

      logger.info("getNewServerTransaction : " + st);

      String txId = ((ViaHeader) request.getHeader(ViaHeader.NAME)).getBranch();
      this.serverTxTable.put(txId, st);

      // Create the 100 Trying response.
      Response response = protocolObjects.messageFactory.createResponse(Response.TRYING, request);
      ListeningPoint lp = sipProvider.getListeningPoint(protocolObjects.transport);
      int myPort = lp.getPort();

      Address address =
          protocolObjects.addressFactory.createAddress(
              "Shootme <sip:" + myAddress + ":" + myPort + ">");

      // Add a random sleep to stagger the two OK's for the benifit of implementations
      // that may not be too good about handling re-entrancy.
      int timeToSleep = (int) (Math.random() * 1000);

      Thread.sleep(timeToSleep);

      st.sendResponse(response);

      Response ringingResponse =
          protocolObjects.messageFactory.createResponse(Response.RINGING, request);
      ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
      response.addHeader(contactHeader);
      ToHeader toHeader = (ToHeader) ringingResponse.getHeader(ToHeader.NAME);
      String toTag =
          actAsNonRFC3261UAS ? null : new Integer((int) (Math.random() * 10000)).toString();
      if (!actAsNonRFC3261UAS) toHeader.setTag(toTag); // Application is supposed to set.
      ringingResponse.addHeader(contactHeader);
      st.sendResponse(ringingResponse);
      Dialog dialog = st.getDialog();
      dialog.setApplicationData(st);

      this.inviteSeen = true;

      new Timer().schedule(new MyTimerTask(requestEvent, st /*,toTag*/), 1000);
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
Ejemplo n.º 8
0
  public void processResponse(ResponseEvent responseReceivedEvent) {
    System.out.println("Got a response");
    Response response = (Response) responseReceivedEvent.getResponse();
    Transaction tid = responseReceivedEvent.getClientTransaction();

    System.out.println(
        "Response received with client transaction id " + tid + ":\n" + response.getStatusCode());
    if (tid == null) {
      System.out.println("Stray response -- dropping ");
      return;
    }
    System.out.println("transaction state is " + tid.getState());
    System.out.println("Dialog = " + tid.getDialog());
    System.out.println("Dialog State is " + tid.getDialog().getState());

    try {
      if (response.getStatusCode() == Response.OK
          && ((CSeqHeader) response.getHeader(CSeqHeader.NAME))
              .getMethod()
              .equals(Request.INVITE)) {
        // Request cancel = inviteTid.createCancel();
        // ClientTransaction ct =
        //  sipProvider.getNewClientTransaction(cancel);
        // ct.sendRequest();
        Dialog dialog = tid.getDialog();
        CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
        Request ackRequest = dialog.createAck(cseq.getSeqNumber());
        System.out.println("Sending ACK");
        dialog.sendAck(ackRequest);

        // Send a Re INVITE but this time force it
        // to use UDP as the transport. Else, it will
        // Use whatever transport was used to create
        // the dialog.
        if (reInviteCount == 0) {
          Request inviteRequest = dialog.createRequest(Request.INVITE);
          ((SipURI) inviteRequest.getRequestURI()).removeParameter("transport");
          ((ViaHeader) inviteRequest.getHeader(ViaHeader.NAME)).setTransport("udp");
          inviteRequest.addHeader(contactHeader);
          try {
            Thread.sleep(100);
          } catch (Exception ex) {
          }
          ClientTransaction ct = udpProvider.getNewClientTransaction(inviteRequest);
          dialog.sendRequest(ct);
          reInviteCount++;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
    public void processResponse(ResponseEvent responseEvent) {
      try {
        Response response = responseEvent.getResponse();
        SipProvider sender = null;

        // Topmost via header is me. As it is reposne to external reqeust
        response.removeFirst(ViaHeader.NAME);

        sender = this.sipProvider;
        sender.sendResponse(response);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
Ejemplo n.º 10
0
  /**
   * Receives options requests and replies with an OK response containing methods that we support.
   *
   * @param requestEvent the incoming options request.
   * @return <tt>true</tt> if request has been successfully processed, <tt>false</tt> otherwise
   */
  @Override
  public boolean processRequest(RequestEvent requestEvent) {
    Response optionsOK = null;
    try {
      optionsOK =
          provider.getMessageFactory().createResponse(Response.OK, requestEvent.getRequest());

      // add to the allows header all methods that we support
      for (String method : provider.getSupportedMethods()) {
        // don't support REGISTERs
        if (!method.equals(Request.REGISTER))
          optionsOK.addHeader(provider.getHeaderFactory().createAllowHeader(method));
      }

      Iterable<String> knownEventsList = provider.getKnownEventsList();

      synchronized (knownEventsList) {
        for (String event : knownEventsList)
          optionsOK.addHeader(provider.getHeaderFactory().createAllowEventsHeader(event));
      }
    } catch (ParseException ex) {
      // What else could we do apart from logging?
      logger.warn("Failed to create an incoming OPTIONS request", ex);
      return false;
    }

    try {
      SipStackSharing.getOrCreateServerTransaction(requestEvent).sendResponse(optionsOK);
    } catch (TransactionUnavailableException ex) {
      // this means that we received an OPTIONS request outside the scope
      // of a transaction which could mean that someone is simply sending
      // us b****hit to keep a NAT connection alive, so let's not get too
      // excited.
      if (logger.isInfoEnabled())
        logger.info("Failed to respond to an incoming " + "transactionless OPTIONS request");
      if (logger.isTraceEnabled()) logger.trace("Exception was:", ex);
      return false;
    } catch (InvalidArgumentException ex) {
      // What else could we do apart from logging?
      logger.warn("Failed to send an incoming OPTIONS request", ex);
      return false;
    } catch (SipException ex) {
      // What else could we do apart from logging?
      logger.warn("Failed to send an incoming OPTIONS request", ex);
      return false;
    }

    return true;
  }
  public void processResponse(ResponseEvent responseReceivedEvent) {
    LOG.debug("Response received at Subscriber");
    Response response = responseReceivedEvent.getResponse();
    Transaction clientTransactionId = responseReceivedEvent.getClientTransaction();

    LOG.debug(
        "Response received with client transaction id {}:{}",
        clientTransactionId,
        response.getStatusCode());
    if (clientTransactionId == null) {
      if (LOG.isWarnEnabled()) {
        LOG.warn("Stray response -- dropping");
      }
      return;
    }
  }
Ejemplo n.º 12
0
  /** Process the invite request. */
  public void processInvite(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    try {
      System.out.println("shootme: got an Invite sending Trying");
      // System.out.println("shootme: " + request);
      Response response = messageFactory.createResponse(Response.TRYING, request);
      ServerTransaction st = requestEvent.getServerTransaction();

      if (st == null) {
        st = sipProvider.getNewServerTransaction(request);
      }
      dialog = st.getDialog();

      st.sendResponse(response);

      this.okResponse = messageFactory.createResponse(Response.BUSY_HERE, request);

      ToHeader toHeader = (ToHeader) okResponse.getHeader(ToHeader.NAME);
      toHeader.setTag("4321"); // Application is supposed to set.

      this.inviteTid = st;
      // Defer sending the OK to simulate the phone ringing.
      this.inviteRequest = request;

      new Timer().schedule(new MyTimerTask(this), 100);
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
Ejemplo n.º 13
0
  private void sendOkResponse(ServerTransaction st, Request request, int expiryTime, String eTag) {

    // return an OK message back to the publisher
    try {
      Response response = getSipMessageFactory().createResponse(Response.OK, request);

      // set contacts
      ArrayList newContacts = new ArrayList(4);
      for (Iterator it = request.getHeaders(ContactHeader.NAME); it.hasNext(); ) {
        ContactHeader contact = (ContactHeader) it.next();
        newContacts.add(contact);
      }

      Iterator it = newContacts.iterator();
      while (it.hasNext()) {
        ContactHeader contact = (ContactHeader) it.next();
        response.addHeader(contact);
      }

      // add an Expires header
      ExpiresHeader expiresHeader = getSipHeaderFactory().createExpiresHeader(expiryTime);
      response.addHeader(expiresHeader);

      // add a SIP-ETag header
      SIPETagHeader sipETagHeader = getSipHeaderFactory().createSIPETagHeader(eTag);
      response.addHeader(sipETagHeader);

      st.sendResponse(response);
      if (isTraceable(TraceLevel.FINEST)) {
        finest("sending OK response:\n" + response);
      }

    } catch (Exception e) {
      // some error occured. Try to send an error response instead
      sendErrorResponse(
          Response.SERVER_INTERNAL_ERROR,
          "An error occurred while attempting to send 200 OK response",
          st,
          request);
    }
  }
    /** Process the invite request. */
    public void processInvite(RequestEvent requestEvent, ServerTransaction serverTransaction) {
      SipProvider sipProvider = (SipProvider) requestEvent.getSource();
      Request request = requestEvent.getRequest();
      try {

        Response okResponse = messageFactory.createResponse(Response.OK, request);
        FromHeader from = (FromHeader) okResponse.getHeader(FromHeader.NAME);
        from.removeParameter("tag");
        Address address =
            addressFactory.createAddress("Shootme <sip:" + myAddress + ":" + myPort + ">");
        ContactHeader contactHeader = headerFactory.createContactHeader(address);
        ToHeader toHeader = (ToHeader) okResponse.getHeader(ToHeader.NAME);
        toHeader.setTag("4321"); // Application is supposed to set.
        okResponse.addHeader(contactHeader);
        sipProvider.sendResponse(okResponse); // Send it through the Provider.

      } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(0);
      }
    }
Ejemplo n.º 15
0
  public void processResponse(ResponseEvent responseReceivedEvent) {
    // System.out.println("Got a response");
    Response response = (Response) responseReceivedEvent.getResponse();
    Transaction tid = responseReceivedEvent.getClientTransaction();

    // System.out.println("Response received with client transaction id "
    // + tid + ":\n" + response);

    try {
      if (response.getStatusCode() == Response.OK
          && ((CSeqHeader) response.getHeader(CSeqHeader.NAME))
              .getMethod()
              .equals(Request.INVITE)) {

        Dialog dialog = tid.getDialog();
        Request request = tid.getRequest();
        dialog.sendAck(request);
      }

    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
  public synchronized void processNotify(
      RequestEvent requestEvent, ServerTransaction serverTransactionId) {
    LOG.debug("Notification received at Subscriber");
    SipProvider provider = (SipProvider) requestEvent.getSource();
    Request notify = requestEvent.getRequest();
    try {
      if (serverTransactionId == null) {
        LOG.info("ServerTransaction is null. Creating new Server transaction");
        serverTransactionId = provider.getNewServerTransaction(notify);
      }
      Dialog dialog = serverTransactionId.getDialog();

      if (dialog != subscriberDialog) {
        forkedDialog = dialog;
      }
      // Dispatch the response along the route
      dispatchExchange(notify.getContent());

      // Send back an success response
      Response response =
          sipSubscriber.getConfiguration().getMessageFactory().createResponse(200, notify);
      response.addHeader(sipSubscriber.getConfiguration().getContactHeader());
      serverTransactionId.sendResponse(response);

      SubscriptionStateHeader subscriptionState =
          (SubscriptionStateHeader) notify.getHeader(SubscriptionStateHeader.NAME);

      // Subscription is terminated?
      if (subscriptionState.getState().equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
        LOG.info("Subscription state is terminated. Deleting the current dialog");
        dialog.delete();
      }
    } catch (Exception e) {
      LOG.error("Exception thrown during Notify processing in the SipSubscriptionListener.", e);
    }
  }
  public void processOK(Response responseCloned, ClientTransaction clientTransaction) {
    try {
      logger.debug("Processing OK for SUBSCRIBE in progress...");

      ExpiresHeader expiresHeader = (ExpiresHeader) responseCloned.getHeader(ExpiresHeader.NAME);
      if (expiresHeader != null && expiresHeader.getExpires() == 0) {
        logger.debug(
            "DEBUG, IMSubscribeProcessing, processOK(), we got" + " the OK for the unsubscribe...");
      } else {

        // We have to create or update the presentity!
        PresenceManager presenceManager = imUA.getPresenceManager();
        String presentityURL = IMUtilities.getKey(responseCloned, "To");

        Dialog dialog = clientTransaction.getDialog();
        if (dialog != null) presenceManager.addPresentity(presentityURL, responseCloned, dialog);
        else {
          logger.debug(
              "ERROR, IMSubscribeProcessing, processOK(), the"
                  + " dialog for the SUBSCRIBE we sent is null!!!"
                  + " No presentity added....");
        }

        // WE have to create a new Buddy in the GUI!!!
        InstantMessagingGUI imGUI = imUA.getInstantMessagingGUI();
        BuddyList buddyList = imGUI.getBuddyList();
        if (!buddyList.hasBuddy(presentityURL)) {
          buddyList.addBuddy(presentityURL, "offline");
        } else {
          logger.debug("The buddy is already in the Buddy list...");
        }
      }
      logger.debug("Processing OK for SUBSCRIBE completed...");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Ejemplo n.º 18
0
 public void processResponse(ResponseEvent responseEvent) {
   ClientTransaction ct = responseEvent.getClientTransaction();
   Response response = responseEvent.getResponse();
   ServerTransaction st = (ServerTransaction) ct.getApplicationData();
   try {
     Response otherResponse =
         messageFactory.createResponse(response.getStatusCode(), st.getRequest());
     if (response.getStatusCode() == 200 && ct.getRequest().getMethod().equals("INVITE")) {
       Address address =
           addressFactory.createAddress("B2BUA <sip:" + myAddress + ":" + myPort + ">");
       ContactHeader contactHeader = headerFactory.createContactHeader(address);
       response.addHeader(contactHeader);
       ToHeader toHeader = (ToHeader) otherResponse.getHeader(ToHeader.NAME);
       if (toHeader.getTag() == null)
         toHeader.setTag(new Long(counter.getAndIncrement()).toString());
       otherResponse.addHeader(contactHeader);
     }
     st.sendResponse(otherResponse);
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
Ejemplo n.º 19
0
  /** This is a listener method. */
  public void processResponse(ResponseEvent responseEvent) {
    try {

      Response response = responseEvent.getResponse();
      SipProvider sipProvider = (SipProvider) responseEvent.getSource();
      ClientTransaction clientTransaction = responseEvent.getClientTransaction();

      ProxyDebug.println(
          "\n***************************************************************"
              + "\n***************************************************************"
              + "\nResponse "
              + response.getStatusCode()
              + " "
              + response.getReasonPhrase()
              + " received:\n"
              + response.toString());
      ProxyDebug.println("Processing Response in progress");

      if (ProxyDebug.debug) ProxyUtilities.printTransaction(clientTransaction);

      // Henrik - added handling of responses addressed to server
      // If we use a presenceserver, and if statuscode was OK...
      CSeqHeader cseqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME);

      if (cseqHeader.getMethod().equals("SUBSCRIBE")) {
        presenceServer.processSubscribeResponse((Response) response.clone(), clientTransaction);
      } else if (cseqHeader.getMethod().equals("NOTIFY")) {
        // presenceServer.processNotifyResponse((Response)response.clone(),
        // clientTransaction);
      }

      responseForwarding.forwardResponse(sipProvider, response, clientTransaction);

    } catch (Exception ex) {
      if (ProxyDebug.debug) {
        ProxyDebug.println("Proxy, processResponse(), internal error, " + "exception raised:");
        ProxyDebug.logException(ex);
      }
    }
  }
Ejemplo n.º 20
0
  public void processResponse(ResponseEvent responseEvent) {
    Response response = responseEvent.getResponse();
    ClientTransaction clientTransaction = responseEvent.getClientTransaction();
    CSeqHeader cseqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME);

    logger.debug("***** Response: " + response.getStatusCode() + " received by UA *****");
    logger.debug(response);
    logger.debug("***** Response dispatched *****");

    if (response.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED
        || response.getStatusCode() == Response.UNAUTHORIZED) {
      if (clientTransaction == null) {
        logger.info("Bad username ?");
      } else {
        try {
          logger.debug("IMUserAgent, processResponse(), Credentials to provide!");
          // WE start the authentication process!!!
          // Let's get the Request related to this response:
          Request clonedRequest = (Request) clientTransaction.getRequest().clone();
          if (clonedRequest == null) {
            logger.debug(
                "IMUserAgent, processResponse(), the request "
                    + " that caused the 407 has not been retrieved!!! Return cancelled!");
          } else {
            // Let's increase the Cseq:
            cseqHeader = (CSeqHeader) clonedRequest.getHeader(CSeqHeader.NAME);
            cseqHeader.setSequenceNumber(cseqHeader.getSequenceNumber() + 1);
            // Let's add a Proxy-Authorization header:
            // We send the informations stored:
            FromHeader fromHeader = (FromHeader) clonedRequest.getHeader(FromHeader.NAME);
            String fromURI = fromHeader.getAddress().getURI().toString();
            Header header;
            if (fromURI.equals(getLocalURI())) {
              header = authenticationProcess.getHeader(response);
            } else {
              String fromAddress = IMUtilities.getUsernameFromURI(fromURI);
              logger.info("Fetching credentials dor user: "******"ERROR, user not found");
                header = null;
              }
            }

            if (header == null) {
              logger.debug(
                  "IMUserAgent, processResponse(), Proxy-Authorization "
                      + " header is null, the request is not resent");
            } else {
              clonedRequest.setHeader(header);

              ClientTransaction newClientTransaction =
                  getSipProvider().getNewClientTransaction(clonedRequest);

              newClientTransaction.sendRequest();
              logger.debug(
                  "IMUserAgent, processResponse(), REGISTER "
                      + "with credentials sent:\n"
                      + clonedRequest);
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } else {
      if (cseqHeader.getMethod().equals("REGISTER")) {
        //	logger.info("REGISTER Response received");
        sipRC.processResponse(responseEvent);
      } else if (cseqHeader.getMethod().equals("ACK")) {

      } else if (cseqHeader.getMethod().equals("MESSAGE")) {
        sipMC.processResponse(responseEvent);
      }
    }
  }
  public void processResponse(ResponseEvent responseReceivedEvent) {
    /// System.out.println("=> BUNDLE: br.ufes.inf.ngn.televoto.client.logic | CLASS: LogicListener
    // | METOD: processResponse ");//By Ju
    try {
      Response myResponse = responseReceivedEvent.getResponse();
      logger.info("<<< " + myResponse.toString());

      ClientTransaction thisClientTransaction = responseReceivedEvent.getClientTransaction();

      int myStatusCode = myResponse.getStatusCode();

      switch (status) {
        case WAIT_PROV:
          if (!thisClientTransaction.equals(myClientTransaction))
            myClientTransaction = thisClientTransaction;

          if (myStatusCode < 200) {
            status = WAIT_FINAL;
            myDialog = thisClientTransaction.getDialog();
          } else if (myStatusCode < 300) {
            myDialog = thisClientTransaction.getDialog();
            CSeqHeader originalCSeq =
                (CSeqHeader) myClientTransaction.getRequest().getHeader(CSeqHeader.NAME);
            long numseq = originalCSeq.getSeqNumber();
            Request myAck = myDialog.createAck(numseq);
            myAck.addHeader(myContactHeader);
            myDialog.sendAck(myAck);
            // logger.info(">>> "+myAck.toString());
            status = ESTABLISHED;
            byte[] cont = (byte[]) myResponse.getContent();
            answerInfo = mySdpManager.getSdp(cont);

          } else {
            if (myStatusCode == 401) {

            } else {
              status = IDLE;
              CSeqHeader originalCSeq =
                  (CSeqHeader) myClientTransaction.getRequest().getHeader(CSeqHeader.NAME);
              long numseq = originalCSeq.getSeqNumber();
              Request myAck = myDialog.createAck(numseq);
              myAck.addHeader(myContactHeader);
              myDialog.sendAck(myAck);
              // logger.info(">>> "+myAck.toString());
            }
          }
          break;

        case WAIT_FINAL:
          if (!thisClientTransaction.equals(myClientTransaction))
            myClientTransaction = thisClientTransaction;

          if (myStatusCode < 200) {
            status = WAIT_FINAL;
            myDialog = thisClientTransaction.getDialog();
          } else if (myStatusCode < 300) {
            if (useQueue.equals("yes")) {
              status = IDLE;
            } else {
              status = ESTABLISHED;
            }

            myDialog = thisClientTransaction.getDialog();
            CSeqHeader originalCSeq =
                (CSeqHeader) myClientTransaction.getRequest().getHeader(CSeqHeader.NAME);
            long numseq = originalCSeq.getSeqNumber();
            Request myAck = myDialog.createAck(numseq); // Aquiiii
            myAck.addHeader(myContactHeader);
            myDialog.sendAck(myAck);
            // logger.info(">>> "+myAck.toString());
            byte[] cont = (byte[]) myResponse.getContent();
            answerInfo = mySdpManager.getSdp(cont);

          } else {
            // Cancelando requisição ao cliente
            Request myCancelRequest = myClientTransaction.createCancel();
            ClientTransaction myCancelClientTransaction =
                mySipProvider.getNewClientTransaction(myCancelRequest);
            myCancelClientTransaction.sendRequest();
            // logger.info(">>> " + myCancelRequest.toString());
            status = IDLE;
          }
          break;

        case REGISTERING:
          if (!thisClientTransaction.equals(myClientTransaction))
            myClientTransaction = thisClientTransaction;

          if (myStatusCode == 200) {
            status = IDLE;
            if (!Unregistring) {

              System.out.println(myName + ": Registrado");

              // inserir captura de timestamp
              // logger.info(myName + ";INVITE;" +  getTime());
              // Random gerador = new Random();
              // int temp = gerador.nextInt(10);
              // System.out.println(myName + ": Esperando " + temp + "segundos");
              // Thread.sleep(temp);

              System.out.println(myName + ": chamando " + destination);

              SdpInfo offerInfo = new SdpInfo();
              offerInfo.IpAddress = myIP;
              offerInfo.aport = myAudioPort;
              offerInfo.aformat = 0;
              byte[] content = mySdpManager.createSdp(offerInfo);

              // Via
              // ViaHeader comentei
              myViaHeader = myHeaderFactory.createViaHeader(myIP, myPort, "udp", null);
              myViaHeader.setRPort();
              ArrayList viaHeaders = new ArrayList();
              viaHeaders.add(myViaHeader);

              // From
              Address addressOfRecord = myAddressFactory.createAddress("<sip:" + myUserID + ">");
              FromHeader myFromHeader = myHeaderFactory.createFromHeader(addressOfRecord, "456249");

              // To
              String dstURI[] = destination.split("@");
              String dstSipAlias = dstURI[0];
              Address destinationAddress =
                  myAddressFactory.createAddress("<sip:" + destination + ">");
              javax.sip.address.URI myRequestURI = destinationAddress.getURI();
              ToHeader myToHeader = myHeaderFactory.createToHeader(destinationAddress, null);

              // MaxForwards
              MaxForwardsHeader myMaxForwardsHeader = myHeaderFactory.createMaxForwardsHeader(70);

              // Call-ID
              CallIdHeader myCallIdHeader = mySipProvider.getNewCallId();

              // CSeq
              Random random = new Random();
              CSeqHeader myCSeqHeader =
                  myHeaderFactory.createCSeqHeader(random.nextInt(1000) * 1L, "INVITE");

              // Create SIP request
              Request myRequest =
                  myMessageFactory.createRequest(
                      myRequestURI,
                      "INVITE",
                      myCallIdHeader,
                      myCSeqHeader,
                      myFromHeader,
                      myToHeader,
                      viaHeaders,
                      myMaxForwardsHeader);

              // Contact
              Address contactAddress =
                  myAddressFactory.createAddress(
                      "<sip:" + mySipAlias + "@" + myIP + ":" + myPort + ";transport=udp>");
              myContactHeader = myHeaderFactory.createContactHeader(contactAddress);
              myRequest.addHeader(myContactHeader);

              // Allow
              Allow myAllow = new Allow();
              myAllow.setMethod(
                  "INVITE, ACK, CANCEL, BYE, MESSAGE, OPTIONS, NOTIFY, PRACK, UPDATE, REFER");
              myRequest.addHeader(myAllow);

              // Privacy
              Privacy myPrivacy = new Privacy("none");
              myRequest.addHeader(myPrivacy);

              // PPreferredIdentity
              HeaderFactoryImpl myHeaderFactoryImpl = new HeaderFactoryImpl();
              PPreferredIdentityHeader myPPreferredIdentityHeader =
                  myHeaderFactoryImpl.createPPreferredIdentityHeader(
                      myAddressFactory.createAddress("sip:" + myName + '@' + myServer));
              myRequest.addHeader(myPPreferredIdentityHeader);

              // Route
              Address routeAddress =
                  myAddressFactory.createAddress("sip:orig@scscf." + myServer + ":6060;lr");
              // RouteHeader comentei
              myRouteHeader = myHeaderFactory.createRouteHeader(routeAddress);
              myRequest.addHeader(myRouteHeader);

              // Para proxy funcionar
              SipURI outboundProxyURI = myAddressFactory.createSipURI("proxy", myProxyIP);
              outboundProxyURI.setLrParam();
              outboundProxyURI.setPort(myProxyPort);
              myRouteHeader =
                  myHeaderFactory.createRouteHeader(
                      myAddressFactory.createAddress(outboundProxyURI));
              myRequest.addFirst(myRouteHeader);

              // Content Type
              ContentTypeHeader contentTypeHeader =
                  myHeaderFactory.createContentTypeHeader("application", "sdp");
              myRequest.setContent(content, contentTypeHeader);

              // ClientTransaction comentei aqui
              myClientTransaction = mySipProvider.getNewClientTransaction(myRequest);
              myClientTransaction.setRetransmitTimer(700);
              myClientTransaction.sendRequest();

              status = WAIT_PROV;

              logger.info(">>> " + myRequest.toString());

            } else {
              this.setOff(mySipStack);
            }
          } else {
            if (myStatusCode == 403) {

              System.out.println("Problemas com credenciais!!!!\n");
            } else if (myStatusCode == 401) {

              myName = (String) this.getLocalName();
              myUserID = (String) this.getLocalName() + "@" + myServer;

              Address contactAddress =
                  myAddressFactory.createAddress("sip:" + myName + '@' + myIP + ":" + myPort);
              myContactHeader = myHeaderFactory.createContactHeader(contactAddress);

              myViaHeader = myHeaderFactory.createViaHeader(myIP, myPort, "udp", null);

              fromAddress = myAddressFactory.createAddress(myName + " <sip:" + myUserID + ">");

              Address registrarAddress = myAddressFactory.createAddress("sip:" + myServer);
              Address registerToAddress = fromAddress;
              Address registerFromAddress = fromAddress;

              ToHeader myToHeader = myHeaderFactory.createToHeader(registerToAddress, null);
              FromHeader myFromHeader =
                  myHeaderFactory.createFromHeader(registerFromAddress, "647554");

              ArrayList myViaHeaders = new ArrayList();
              myViaHeaders.add(myViaHeader);

              // System.out.println("myClientTransaction.getRequest():"+
              // myClientTransaction.getRequest());
              CSeqHeader originalCSeq =
                  (CSeqHeader) myClientTransaction.getRequest().getHeader(CSeqHeader.NAME);
              long numseq = originalCSeq.getSeqNumber();
              MaxForwardsHeader myMaxForwardsHeader = myHeaderFactory.createMaxForwardsHeader(70);
              CSeqHeader myCSeqHeader = myHeaderFactory.createCSeqHeader(numseq + 1L, "REGISTER");

              CallIdHeader myCallID =
                  (CallIdHeader) myClientTransaction.getRequest().getHeader(CallIdHeader.NAME);
              CallIdHeader myCallIDHeader = myCallID;
              SipURI myRequestURI = (SipURI) registrarAddress.getURI();
              Request myRegisterRequest =
                  myMessageFactory.createRequest(
                      myRequestURI,
                      "REGISTER",
                      myCallIDHeader,
                      myCSeqHeader,
                      myFromHeader,
                      myToHeader,
                      myViaHeaders,
                      myMaxForwardsHeader);
              myRegisterRequest.addHeader(myContactHeader);

              // Expires
              ExpiresHeader myExpiresHeader;
              if (Unregistring) {
                myExpiresHeader = myHeaderFactory.createExpiresHeader(0);
                myContactHeader.setExpires(0);
              } else {
                myExpiresHeader = myHeaderFactory.createExpiresHeader(60000);
              }
              myRegisterRequest.addHeader(myExpiresHeader);

              // Allow
              Allow myAllow = new Allow();
              myAllow.setMethod(
                  "INVITE, ACK, CANCEL, BYE, MESSAGE, OPTIONS, NOTIFY, PRACK, UPDATE, REFER");
              myRegisterRequest.addHeader(myAllow);

              // Privacy
              Privacy myPrivacy = new Privacy("none");
              myRegisterRequest.addHeader(myPrivacy);

              // PPreferredIdentity
              HeaderFactoryImpl myHeaderFactoryImpl = new HeaderFactoryImpl();
              PPreferredIdentityHeader myPPreferredIdentityHeader =
                  myHeaderFactoryImpl.createPPreferredIdentityHeader(
                      myAddressFactory.createAddress("sip:" + myName + '@' + myServer));
              myRegisterRequest.addHeader(myPPreferredIdentityHeader);

              // Supported
              Supported mySupported = new Supported("path");
              myRegisterRequest.addHeader(mySupported);

              myWWWAuthenticateHeader =
                  Utils.makeAuthHeader(
                      myHeaderFactory, myResponse, myRegisterRequest, myUserID, myPassword);
              myRegisterRequest.addHeader(myWWWAuthenticateHeader);

              myClientTransaction = mySipProvider.getNewClientTransaction(myRegisterRequest);
              myClientTransaction.sendRequest();

              // logger.info(">>> "+myRegisterRequest.toString());
              // System.out.println(">>> " + myRegisterRequest.toString());
              status = REGISTERING;
            }
          }
          break;
      }
    } catch (Exception excep) {
      excep.printStackTrace();
    }
  }
  public void processRequest(RequestEvent requestReceivedEvent) {
    /// System.out.println("=> BUNDLE: br.ufes.inf.ngn.televoto.client.logic | CLASS: LogicListener
    // | METOD: processRequest ");//By Ju

    String method;
    Response myResponse;
    ToHeader myToHeader;
    Request myRequest = requestReceivedEvent.getRequest();
    method = myRequest.getMethod();
    // logger.info(myRequest.toString());
    if (!method.equals("CANCEL")) {
      myServerTransaction = requestReceivedEvent.getServerTransaction();
    }
    try {

      switch (status) {
        case WAIT_PROV:
          status = WAIT_ACK;
          System.out.println("Chamando....");
          break;

        case IDLE:
          if (method.equals("INVITE")) {
            if (myServerTransaction == null) {
              myServerTransaction = mySipProvider.getNewServerTransaction(myRequest);
            }
            byte[] cont = (byte[]) myRequest.getContent();
            offerInfo = mySdpManager.getSdp(cont);

            answerInfo.IpAddress = myIP;
            answerInfo.aport = myAudioPort;
            answerInfo.aformat = offerInfo.aformat;

            if (useQueue.equals("yes")) {
              status = ESTABLISHED;
            } else {
              // envio do PROVISIONAL 180
              myResponse = myMessageFactory.createResponse(180, myRequest);
              myResponse.addHeader(myContactHeader);
              myToHeader = (ToHeader) myResponse.getHeader("To");
              myToHeader.setTag("454326");
              myServerTransaction.sendResponse(myResponse);
              myDialog = myServerTransaction.getDialog();
              // logger.info(">>> "+myResponse.toString());
              status = WAIT_ACK;
            }
            // inicio do envio do ACK ao cliente em confirmacao ao PROV_180 e envio do SDP
            Request originalRequest = myServerTransaction.getRequest();
            myResponse = myMessageFactory.createResponse(200, originalRequest);
            // System.out.println(originalRequest.toString());
            myToHeader = (ToHeader) myResponse.getHeader("To");
            myResponse.addHeader(myContactHeader);

            // SEND ANSWER SDP

            ContentTypeHeader contentTypeHeader =
                myHeaderFactory.createContentTypeHeader("application", "sdp");
            byte[] content = mySdpManager.createSdp(answerInfo);
            myResponse.setContent(content, contentTypeHeader);

            // aguardando ACK do cliente
            myServerTransaction.sendResponse(myResponse);
            myDialog = myServerTransaction.getDialog();

            new Timer().schedule(new MyTimerTask(this), 500000); // Aqui!!!
            // logger.info(">>> " + myResponse.toString());

          }
          if (method.equals("OPTIONS")) {
            if (myServerTransaction == null) {
              myServerTransaction = mySipProvider.getNewServerTransaction(myRequest);
            }

            Request originalRequest = myServerTransaction.getRequest();
            myResponse = myMessageFactory.createResponse(200, originalRequest);
            myResponse.addHeader(myContactHeader);
            myServerTransaction.sendResponse(myResponse);
          }

          break;
        case ESTABLISHED:
          if (method.equals("BYE")) {
            // capturar o timestamp
            // logger.info(myName + ";BYE;" +  getTime());
            System.out.println(myName + ": BYE");
            myResponse = myMessageFactory.createResponse(200, myRequest);
            myResponse.addHeader(myContactHeader);
            myServerTransaction.sendResponse(myResponse);
            // logger.info(">>> "+myResponse.toString());
            if (ack == 1) {
              ack = 0;
              redial++;

              if (redial <= dialTimes) {
                // inserir captura timestamp
                logger.info(myName + ";BYE;" + getTime());
                System.out.println(myName + ": rediscagem " + redial + " para: " + destination);

                String dstURI[] = destination.split("@");
                String dstSipAlias = dstURI[0];
                Address destinationAddress =
                    myAddressFactory.createAddress(dstSipAlias + " <sip:" + destination + ">");
                javax.sip.address.URI myRequestURI = destinationAddress.getURI();

                Address addressOfRecord =
                    myAddressFactory.createAddress(
                        mySipAlias + " <sip:" + mySipAlias + "@" + myIP + ":" + myPort + ">");
                // HeaderFactory comentei
                myHeaderFactory = mySipFactory.createHeaderFactory();
                // ViaHeader comentei
                myViaHeader = myHeaderFactory.createViaHeader(myIP, myPort, "udp", null);
                ArrayList viaHeaders = new ArrayList();
                viaHeaders.add(myViaHeader);
                MaxForwardsHeader myMaxForwardsHeader = myHeaderFactory.createMaxForwardsHeader(70);
                CallIdHeader myCallIdHeader = mySipProvider.getNewCallId();
                CSeqHeader myCSeqHeader = myHeaderFactory.createCSeqHeader(1L, "INVITE");
                FromHeader myFromHeader =
                    myHeaderFactory.createFromHeader(addressOfRecord, "456249");
                myToHeader = myHeaderFactory.createToHeader(destinationAddress, null);

                myRequest =
                    myMessageFactory.createRequest(
                        myRequestURI,
                        "INVITE",
                        myCallIdHeader,
                        myCSeqHeader,
                        myFromHeader,
                        myToHeader,
                        viaHeaders,
                        myMaxForwardsHeader);

                Address contactAddress =
                    myAddressFactory.createAddress(
                        "<sip:" + mySipAlias + "@" + myIP + ":" + myPort + ">");
                myContactHeader = myHeaderFactory.createContactHeader(contactAddress);
                myRequest.addHeader(myContactHeader);

                SdpInfo offerInfo = new SdpInfo();

                offerInfo.IpAddress = myIP;
                offerInfo.aport = myAudioPort;

                offerInfo.aformat = 0;

                ContentTypeHeader contentTypeHeader =
                    myHeaderFactory.createContentTypeHeader("application", "sdp");
                byte[] content = mySdpManager.createSdp(offerInfo);
                myRequest.setContent(content, contentTypeHeader);

                // ClientTransaction Comentei aqui
                myClientTransaction = mySipProvider.getNewClientTransaction(myRequest);
                myClientTransaction.sendRequest();
                logger.info(myName + ";INVITE;" + getTime());
                // System.out.println(myRequest);
                status = WAIT_PROV;
              } else {
                status = IDLE;
                System.out.println(myName + ": pronto.");
              }
            } else ack++;
          }
          break;

        case RINGING:
          if (method.equals("CANCEL")) {
            ServerTransaction myCancelServerTransaction =
                requestReceivedEvent.getServerTransaction();
            Request originalRequest = myServerTransaction.getRequest();
            myResponse = myMessageFactory.createResponse(487, originalRequest);
            myServerTransaction.sendResponse(myResponse);
            Response myCancelResponse = myMessageFactory.createResponse(200, myRequest);
            myCancelServerTransaction.sendResponse(myCancelResponse);
            // logger.info(">>> "+myResponse.toString());
            // logger.info(">>> "+myCancelResponse.toString());
            status = IDLE;
          }
          break;

        case WAIT_ACK:
          if (method.equals("ACK")) {
            status = ESTABLISHED;
            System.out.println("Conectado ...");
            // SendMedia(offerInfo.IpAddress, offerInfo.aport);
          }

          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * @param originalRequestEvent
   * @param response
   * @param serviceUnit
   */
  @Override
  public void sendResponse(
      RequestEvent originalRequestEvent, Response response, ServiceUnit serviceUnit, String toTag) {
    /** Here we need to do some specific handling specific to the type of responses being sent. */

    /**
     * If it is a 180 RINGING response, then we need to set the To tag to the same to tag generated
     * when creating the associated B2BSession and B2BDialog.
     */
    if (isDialogCreating(response.getStatusCode())) {
      logger.fine("Sending a ringing response...");
      /** Copy the Local Tag from the B2BDialog to the SipResponse */
      logger.info("Setting the To Dialog of the response to be sent to : " + toTag);
      SIPResponse sipResponse = (SIPResponse) response;
      sipResponse.setToTag(toTag);
    }
    logger.info(
        "Sending Response for the request associated with ServiceUnit: " + serviceUnit.getName());
    /** Conveying the response sending to the UserAgent */
    logger.info("Sending Response: " + response.getReasonPhrase());
    Dialog sipDialog = userAgent.sendResponse(originalRequestEvent, response, serviceUnit);
    if (sipDialog == null) {
      logger.info(
          "The sent response " + response.getReasonPhrase() + " did not resulted in any dialog...");
    } else {
      logger.info(
          "The sent response "
              + response.getReasonPhrase()
              + " has created a new Dialog with ID: "
              + sipDialog.getDialogId());
    }
    /**
     * Set the underlying SIP Dialog of the B2BDialog for Dialog creating responses as well as the
     * local seq number and the current state of the dialog.
     */
    if (isDialogCreating(response.getStatusCode())) {
      B2BSession b2BSessions[] =
          RuntimeComponentContext.getInstance().getB2BSessions(serviceUnit.getName());
      for (B2BSession b2BSession : b2BSessions) {
        B2BDialogImpl b2BDialogImpl = null;
        if (sipDialog != null) {
          b2BDialogImpl =
              (B2BDialogImpl) b2BSession.getB2BDialog(sipDialog.getDialogId(), true, null);
        }
        if (b2BDialogImpl != null) {
          logger.info(
              "Setting the underlying SIP Dialog property of the B2BDialog: "
                  + b2BDialogImpl.getDialogId());
          b2BDialogImpl.setUnderlyingSipDialog(sipDialog);
          b2BDialogImpl.setLocalSeqNumber(sipDialog.getLocalSeqNumber());
          b2BDialogImpl.setState(sipDialog.getState().toString());
          logger.info(
              "Setting the Dialog status with ID: "
                  + b2BDialogImpl.getDialogId()
                  + " to "
                  + sipDialog.getState().toString());
          break;
        } else {
          logger.info("Unable to locate a B2BDialog with ID " + sipDialog.getDialogId());
        }
      }
    }
  }
  /**
   * Tries to steer a TI client transaction through the following scenario Calling-->Terminated.
   * Apart from state transitions, we also test, retransmissions and proper hiding/passing of
   * messages to the TU.
   */
  public void testCallingTerminatedScenario() {
    try {
      Request invite = createTiInviteRequest(null, null, null);
      ClientTransaction tran = null;
      try {
        eventCollector.collectRequestEvent(riSipProvider);
        tran = tiSipProvider.getNewClientTransaction(invite);
        tran.sendRequest();
      } catch (SipException ex) {
        throw new TiUnexpectedError("A SipExceptionOccurred while trying to send request!", ex);
      } catch (TooManyListenersException ex) {
        throw new TckInternalError("Failed to regiest a SipListener with an RI SipProvider", ex);
      }
      waitForMessage();
      RequestEvent inviteReceivedEvent = eventCollector.extractCollectedRequestEvent();
      if (inviteReceivedEvent == null || inviteReceivedEvent.getRequest() == null)
        throw new TiUnexpectedError("The invite request was not received by the RI!");
      // At this point the ClientTransaction should be CALLING!
      assertEquals(TransactionState.CALLING, tran.getState());
      // Check Request retransmission
      try {
        eventCollector.collectRequestEvent(riSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TckInternalError("Failed to regiest a SipListener with an RI SipProvider", ex);
      }
      // Wait for the retransmission timer to fire if it had not already
      // done so.
      if (tran.getRetransmitTimer() > MESSAGES_ARRIVE_FOR)
        sleep((long) tran.getRetransmitTimer() - MESSAGES_ARRIVE_FOR); // subtract
      // the
      // time
      // we
      // waited
      // for
      // the
      // invite
      // Wait for the retransmitted request to arrive
      waitForMessage();
      inviteReceivedEvent = eventCollector.extractCollectedRequestEvent();
      assertNotNull("The invite request was not retransmitted!", inviteReceivedEvent);
      assertNotNull("The invite request was not retransmitted!", inviteReceivedEvent.getRequest());
      assertEquals(Request.INVITE, inviteReceivedEvent.getRequest().getMethod());
      // At this point the ClientTransaction should STILL be CALLING!
      assertEquals(TransactionState.CALLING, tran.getState());
      // Send a 200 OK (final) response from the RI
      try {
        eventCollector.collectResponseEvent(tiSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TiUnexpectedError("Failed to register a SipListener with TI", ex);
      }
      // The OK response shouldn't trigger any ACKs so let's register
      // a listener with the RI to verify whether that is the case
      SipEventCollector ackCollector = new SipEventCollector();
      try {
        ackCollector.collectRequestEvent(riSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TckInternalError("Failed to regiest a SipListener with an RI SipProvider", ex);
      }
      Response ok = null;
      try {
        ok = riMessageFactory.createResponse(Response.OK, inviteReceivedEvent.getRequest());
        addStatus(inviteReceivedEvent.getRequest(), ok);

        // JvB: MUST add Contact too!
        ContactHeader contact =
            riHeaderFactory.createContactHeader(((ToHeader) ok.getHeader("To")).getAddress());
        ok.addHeader(contact);
        // end JvB

        riSipProvider.sendResponse(ok);
      } catch (Throwable ex) {
        throw new TckInternalError("The TCK could not send an OK response back to the TI", ex);
      }
      waitForMessage();
      // Analyze the OK response and Tran state back at the TI
      ResponseEvent responseEvent = eventCollector.extractCollectedResponseEvent();
      assertNotNull(
          "The Tested Implementation did not pass a 200 OK response to the TU!", responseEvent);
      assertNotNull(
          "The Tested Implementation did not pass a 200 OK response to the TU!",
          responseEvent.getResponse());
      assertSame(
          "The OK response was not associated with the right transaction",
          tran,
          responseEvent.getClientTransaction());
      assertSame(
          "A response different from OK was passed to the TU",
          tran,
          responseEvent.getClientTransaction());
      assertEquals(
          "The ClientTransaction did not pass in the TERMINATED state after "
              + "receiving 200 final response",
          tran.getState(),
          TransactionState.TERMINATED);
      // check whether the ackCollector has caught any fish
      RequestEvent ackReceivedEvent = ackCollector.extractCollectedRequestEvent();
      assertNull("The TI sent an ACK to an OK (this is TU's job)!", ackReceivedEvent);
      // Now let's retransmit the final response and see that it is
      // passed to the TU (again no ACKs should be sent by the TI)
      try {
        eventCollector.collectResponseEvent(tiSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TiUnexpectedError("Failed to register a SipListener with TI", ex);
      }
      // go fish the ack
      try {
        ackCollector.collectRequestEvent(riSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TckInternalError("Failed to regiest a SipListener with an RI SipProvider", ex);
      }
      try {
        riSipProvider.sendResponse((Response) ok.clone());
      } catch (Throwable ex) {
        throw new TckInternalError("The TCK could not send an OK response back to the TI", ex);
      }
      waitForMessage();
      // Did we get the 2nd OK?
      responseEvent = eventCollector.extractCollectedResponseEvent();
      /*
       * JvB: see previous comment
       *
       * assertNotNull( "The TI did not pass to the TU a retransmitted OK
       * response!", responseEvent); assertNotNull( "The TI did not pass
       * to the TU a retransmitted OK response!",
       * responseEvent.getResponse()); assertTrue( "The TI passed to the
       * TU a bad response!",
       * responseEvent.getResponse().getStatusCode()==Response.OK);
       */
      // JvB: TBD
      // assertNull( "The TI should filter the retransmitted OK response",
      // responseEvent );

      // We must still be in the terminated state.
      assertEquals(
          "The ClientTransaction mysteriously left the TERMINATED state!",
          tran.getState(),
          TransactionState.TERMINATED);
      // check whether the ackCollector has caught any fish
      ackReceivedEvent = ackCollector.extractCollectedRequestEvent();
      assertNull(
          "The TI sent an ACK request to the second OK response "
              + "(OK acks are TU's responsibility)!",
          ackReceivedEvent);
    } catch (Throwable exc) {
      exc.printStackTrace();
      fail(exc.getClass().getName() + ": " + exc.getMessage());
    }
    assertTrue(new Exception().getStackTrace()[0].toString(), true);
  }
Ejemplo n.º 25
0
  /** This is a listener method. */
  public void processRequest(RequestEvent requestEvent) {
    Request request = requestEvent.getRequest();
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    ServerTransaction serverTransaction = requestEvent.getServerTransaction();
    try {

      if (ProxyDebug.debug)
        ProxyDebug.println(
            "\n****************************************************"
                + "\nRequest "
                + request.getMethod()
                + " received:\n"
                + request.toString());

      if (ProxyDebug.debug) ProxyUtilities.printTransaction(serverTransaction);

      /** **************************************************************************** */
      /** ********************* PROXY BEHAVIOR *********************************** */
      /** **************************************************************************** */

      /*
       * RFC 3261: 16.2: For all new requests, including any with unknown
       * methods, an element intending to proxy the request MUST:
       *
       * 1. Validate the request (Section 16.3)
       *
       * 2. Preprocess routing information (Section 16.4)
       *
       * 3. Determine target(s) for the request (Section 16.5)
       *
       * 4. Forward the request to each target (Section 16.6)
       *
       * 5. Process all responses (Section 16.7)
       */

      /** **************************************************************************** */
      /** *************************** 1. Validate the request (Section 16.3) ********* */
      /** **************************************************************************** */

      /*
       * Before an element can proxy a request, it MUST verify the
       * message's validity
       */

      RequestValidation requestValidation = new RequestValidation(this);
      if (!requestValidation.validateRequest(sipProvider, request, serverTransaction)) {
        // An appropriate response has been sent back by the request
        // validation step, so we just return. The request has been
        // processed!
        if (ProxyDebug.debug)
          ProxyDebug.println(
              "Proxy, processRequest(), the request has not been"
                  + " validated, so the request is discarded "
                  + " (an error code has normally been"
                  + " sent back)");
        return;
      }

      // Let's check if the ACK is for the proxy: if there is no Route
      // header: it is mandatory for the ACK to be forwarded
      if (request.getMethod().equals(Request.ACK)) {
        ListIterator routes = request.getHeaders(RouteHeader.NAME);

        if (routes == null || !routes.hasNext()) {
          if (ProxyDebug.debug)
            ProxyDebug.println(
                "Proxy, processRequest(), "
                    + "the request is an ACK"
                    + " targeted for the proxy, we ignore it");
          return;
        }
        /* added code */
        CallID call_id = (CallID) request.getHeader(CallID.CALL_ID);
        String call_id_str = call_id.getCallId();
        TimeThreadController.Start(call_id_str);
        /* end of added code */

      }

      if (serverTransaction == null) {
        String method = request.getMethod();
        // Methods that creates dialogs, so that can
        // generate transactions
        if (method.equals(Request.INVITE) || method.equals(Request.SUBSCRIBE)) {
          try {
            serverTransaction = sipProvider.getNewServerTransaction(request);
            TransactionsMapping transactionsMapping =
                (TransactionsMapping) serverTransaction.getDialog().getApplicationData();
            if (transactionsMapping == null) {
              transactionsMapping = new TransactionsMapping(serverTransaction);
            }
          } catch (TransactionAlreadyExistsException e) {
            if (ProxyDebug.debug)
              ProxyDebug.println(
                  "Proxy, processRequest(), this request" + " is a retransmission, we drop it!");
          }
        }
      }

      /** ************************************************************************ */
      /** **** 2. Preprocess routing information (Section 16.4) ****************** */
      /** ************************************************************************ */

      /*
       * The proxy MUST inspect the Request-URI of the request. If the
       * Request-URI of the request contains a value this proxy previously
       * placed into a Record-Route header field (see Section 16.6 item
       * 4), the proxy MUST replace the Request-URI in the request with
       * the last value from the Route header field, and remove that value
       * from the Route header field. The proxy MUST then proceed as if it
       * received this modified request. ..... (idem to below:) 16.12. The
       * proxy will inspect the URI in the topmost Route header field
       * value. If it indicates this proxy, the proxy removes it from the
       * Route header field (this route node has been reached).
       */

      ListIterator routes = request.getHeaders(RouteHeader.NAME);
      if (routes != null) {
        if (routes.hasNext()) {
          RouteHeader routeHeader = (RouteHeader) routes.next();
          Address routeAddress = routeHeader.getAddress();
          SipURI routeSipURI = (SipURI) routeAddress.getURI();

          String host = routeSipURI.getHost();
          int port = routeSipURI.getPort();

          if (sipStack.getIPAddress().equals(host)) {
            Iterator lps = sipStack.getListeningPoints();
            while (lps != null && lps.hasNext()) {
              ListeningPoint lp = (ListeningPoint) lps.next();
              if (lp.getPort() == port) {
                if (ProxyDebug.debug)
                  ProxyDebug.println(
                      "Proxy, processRequest(),"
                          + " we remove the first route form "
                          + " the RouteHeader;"
                          + " it matches the proxy");
                routes.remove();
                break;
              }
            }
          }
        }
      }

      /*
       * If the Request-URI contains a maddr parameter, the proxy MUST
       * check to see if its value is in the set of addresses or domains
       * the proxy is configured to be responsible for. If the Request-URI
       * has a maddr parameter with a value the proxy is responsible for,
       * and the request was received using the port and transport
       * indicated (explicitly or by default) in the Request-URI, the
       * proxy MUST strip the maddr and any non-default port or transport
       * parameter and continue processing as if those values had not been
       * present in the request.
       */

      URI requestURI = request.getRequestURI();
      if (requestURI.isSipURI()) {
        SipURI requestSipURI = (SipURI) requestURI;
        if (requestSipURI.getMAddrParam() != null) {
          // The domain the proxy is configured to be responsible for
          // is defined
          // by stack_domain parameter in the configuration file:
          if (configuration.hasDomain(requestSipURI.getMAddrParam())) {
            if (ProxyDebug.debug)
              ProxyDebug.println(
                  "Proxy, processRequest(),"
                      + " The maddr contains a domain we are responsible for,"
                      + " we remove the mAddr parameter from the original"
                      + " request");
            // We have to strip the madr parameter:
            requestSipURI.removeParameter("maddr");
            // We have to strip the port parameter:
            if (requestSipURI.getPort() != 5060 && requestSipURI.getPort() != -1) {
              requestSipURI.setPort(5060);
            }
            // We have to strip the transport parameter:
            requestSipURI.removeParameter("transport");
          } else {
            // The Maddr parameter is not a domain we have to take
            // care of, we pass this check...
          }
        } else {
          // No Maddr parameter, we pass this check...
        }
      } else {
        // No SipURI, so no Maddr parameter, we pass this check...
      }

      /** *************************************************************************** */
      /** *********** 3. Determine target(s) for the request (Section 16.5) ********* */
      /** ************************************************************************** */
      /*
       * The set of targets will either be predetermined by the contents
       * of the request or will be obtained from an abstract location
       * service. Each target in the set is represented as a URI.
       */

      Vector targetURIList = new Vector();
      URI targetURI;

      /*
       * If the Request-URI of the request contains an maddr parameter,
       * the Request-URI MUST be placed into the target set as the only
       * target URI, and the proxy MUST proceed to Section 16.6.
       */

      if (requestURI.isSipURI()) {
        SipURI requestSipURI = (SipURI) requestURI;
        if (requestSipURI.getMAddrParam() != null) {
          targetURI = requestURI;
          targetURIList.addElement(targetURI);
          if (ProxyDebug.debug)
            ProxyDebug.println(
                "Proxy, processRequest(),"
                    + " the only target is the Request-URI (mAddr parameter)");

          // 4. Forward the request statefully:
          requestForwarding.forwardRequest(
              targetURIList, sipProvider, request, serverTransaction, true);

          return;
        }
      }

      /*
       * If the domain of the Request-URI indicates a domain this element
       * is not responsible for, the Request-URI MUST be placed into the
       * target set as the only target, and the element MUST proceed to
       * the task of Request Forwarding (Section 16.6).
       */

      if (requestURI.isSipURI()) {
        SipURI requestSipURI = (SipURI) requestURI;
        if (!configuration.hasDomain(requestSipURI.getHost())) {
          if (ProxyDebug.debug)
            ProxyDebug.println(
                "Proxy, processRequest(),"
                    + " we are not responsible for the domain: Let's check if we have"
                    + " a registration for this domain from another proxy");

          // We have to check if another proxy did not registered
          // to us, in this case we have to use the contacts provided
          // by the registered proxy to create the targets:
          if (registrar.hasDomainRegistered(request)) {
            targetURIList = registrar.getDomainContactsURI(request);
            if (targetURIList != null && !targetURIList.isEmpty()) {
              if (ProxyDebug.debug) {
                ProxyDebug.println(
                    "Proxy, processRequest(), we have"
                        + " a registration for this domain from another proxy");
              }
              // 4. Forward the request statefully:
              requestForwarding.forwardRequest(
                  targetURIList, sipProvider, request, serverTransaction, true);
              return;

            } else {
              targetURIList = new Vector();
              ProxyDebug.println(
                  "Proxy, processRequest(),"
                      + " we are not responsible for the domain: the only target"
                      + " URI is given by the request-URI");
              targetURI = requestURI;
              targetURIList.addElement(targetURI);
            }
          } else {
            ProxyDebug.println(
                "Proxy, processRequest(),"
                    + " we are not responsible for the domain: the only target"
                    + " URI is given by the request-URI");
            targetURI = requestURI;
            targetURIList.addElement(targetURI);
          }

          // 4. Forward the request statelessly:
          requestForwarding.forwardRequest(
              targetURIList, sipProvider, request, serverTransaction, false);

          return;
        } else {
          ProxyDebug.println(
              "Proxy, processRequest(),"
                  + " we are responsible for the domain... Let's find the contact...");
        }
      }

      // we use a SIP registrar:
      if (request.getMethod().equals(Request.REGISTER)) {
        if (ProxyDebug.debug) ProxyDebug.println("Incoming request Register");
        // we call the RegisterProcessing:
        registrar.processRegister(request, sipProvider, serverTransaction);
        // Henrik: let the presenceserver do some processing too
        if (isPresenceServer()) {
          presenceServer.processRegisterRequest(sipProvider, request, serverTransaction);
        }

        return;
      }

      /*
       * If we receive a subscription targeted to a user that is
       * publishing its state here, send to presence server
       */
      if (isPresenceServer() && (request.getMethod().equals(Request.SUBSCRIBE))) {
        ProxyDebug.println("Incoming request Subscribe");

        if (presenceServer.isStateAgent(request)) {
          Request clonedRequest = (Request) request.clone();
          presenceServer.processSubscribeRequest(sipProvider, clonedRequest, serverTransaction);
        } else {
          // Do we know this guy?

          targetURIList = registrar.getContactsURI(request);
          if (targetURIList == null) {
            // If not respond that we dont know him.
            ProxyDebug.println(
                "Proxy: received a Subscribe request to "
                    + " a user in our domain that was not found, "
                    + " responded 404");
            Response response = messageFactory.createResponse(Response.NOT_FOUND, request);
            if (serverTransaction != null) serverTransaction.sendResponse(response);
            else sipProvider.sendResponse(response);
            return;
          } else {
            ProxyDebug.println(
                "Trying to forward subscribe to "
                    + targetURIList.toString()
                    + "\n"
                    + request.toString());
            requestForwarding.forwardRequest(
                targetURIList, sipProvider, request, serverTransaction, false);
          }
        }
        return;
      }

      /** Received a Notify. TOADD: Check if it match active VirtualSubscriptions and update it */
      if (isPresenceServer() && (request.getMethod().equals(Request.NOTIFY))) {
        System.out.println("Incoming request Notify");

        Response response = messageFactory.createResponse(481, request);
        response.setReasonPhrase("Subscription does not exist");
        if (serverTransaction != null) serverTransaction.sendResponse(response);
        else sipProvider.sendResponse(response);
        ProxyDebug.println("Proxy: received a Notify request. Probably wrong, responded 481");
        return;
      }

      if (isPresenceServer() && (request.getMethod().equalsIgnoreCase("PUBLISH"))) {

        System.out.println("Incoming request Publish");

        ProxyDebug.println("Proxy: received a Publish request.");
        Request clonedRequest = (Request) request.clone();

        if (presenceServer.isStateAgent(clonedRequest)) {
          ProxyDebug.println("PresenceServer.isStateAgent");
        } else {
          ProxyDebug.println("PresenceServer is NOT StateAgent");
        }

        if (presenceServer.isStateAgent(clonedRequest)) {
          presenceServer.processPublishRequest(sipProvider, clonedRequest, serverTransaction);
        } else {
          Response response = messageFactory.createResponse(Response.NOT_FOUND, request);
          if (serverTransaction != null) serverTransaction.sendResponse(response);
          else sipProvider.sendResponse(response);
        }
        return;
      }

      // Forward to next hop but dont reply OK right away for the
      // BYE. Bye is end-to-end not hop by hop!
      if (request.getMethod().equals(Request.BYE)) {

        if (serverTransaction == null) {
          if (ProxyDebug.debug) ProxyDebug.println("Proxy, null server transaction for BYE");
          return;
        }

        /* added code */
        CallID call_id = (CallID) request.getHeader(CallID.CALL_ID);
        String call_id_str = call_id.getCallId();
        long end_time = TimeThreadController.Stop(call_id_str);

        To tou = (To) request.getHeader(ToHeader.NAME);
        From fromu = (From) request.getHeader(FromHeader.NAME);

        String FromUser = fromu.getUserAtHostPort();
        String ToUser = tou.getUserAtHostPort();

        StringBuffer sb = new StringBuffer(FromUser);
        int endsAt = sb.indexOf("@");
        String FromUsername = sb.substring(0, endsAt);
        sb = new StringBuffer(ToUser);
        endsAt = sb.indexOf("@");
        String ToUsername = sb.substring(0, endsAt);
        BillStrategyApply bill = new BillStrategyApply(new StandardBillPolicy());
        long start_time = TimeThreadController.getStartTime();
        java.sql.Timestamp s = new java.sql.Timestamp(start_time);
        System.out.println("START TIME POU BIKE :" + s.toString());
        BigDecimal cost = bill.executeStrategy(1, 2, start_time, end_time);
        ProcessBill.updateCallDB(FromUsername, ToUsername, start_time, end_time, cost);

        /* end of added code */

        Dialog d = serverTransaction.getDialog();
        TransactionsMapping transactionsMapping = (TransactionsMapping) d.getApplicationData();
        Dialog peerDialog = (Dialog) transactionsMapping.getPeerDialog(serverTransaction);
        Request clonedRequest = (Request) request.clone();
        FromHeader from = (FromHeader) clonedRequest.getHeader(FromHeader.NAME);
        from.removeParameter("tag");
        ToHeader to = (ToHeader) clonedRequest.getHeader(ToHeader.NAME);
        to.removeParameter("tag");
        ViaHeader via = this.getStackViaHeader();
        clonedRequest.addHeader(via);
        if (peerDialog.getState() != null) {
          ClientTransaction newct = sipProvider.getNewClientTransaction(clonedRequest);
          transactionsMapping.addMapping(serverTransaction, newct);
          peerDialog.sendRequest(newct);
          return;
        } else {
          // the peer dialog is not yet established so bail out.
          // this is a client error - client is sending BYE
          // before dialog establishment.
          if (ProxyDebug.debug) ProxyDebug.println("Proxy, bad dialog state - BYE dropped");
          return;
        }
      }

      /*
       * If the target set for the request has not been predetermined as
       * described above, this implies that the element is responsible for
       * the domain in the Request-URI, and the element MAY use whatever
       * mechanism it desires to determine where to send the request. ...
       * When accessing the location service constructed by a registrar,
       * the Request-URI MUST first be canonicalized as described in
       * Section 10.3 before being used as an index.
       */
      if (requestURI.isSipURI()) {
        SipURI requestSipURI = (SipURI) requestURI;
        Iterator iterator = requestSipURI.getParameterNames();
        if (ProxyDebug.debug)
          ProxyDebug.println("Proxy, processRequest(), we canonicalized" + " the request-URI");
        while (iterator != null && iterator.hasNext()) {
          String name = (String) iterator.next();
          requestSipURI.removeParameter(name);
        }
      }

      if (registrar.hasRegistration(request)) {

        targetURIList = registrar.getContactsURI(request);

        // We fork only INVITE
        if (targetURIList != null
            && targetURIList.size() > 1
            && !request.getMethod().equals("INVITE")) {
          if (ProxyDebug.debug)
            ProxyDebug.println(
                "Proxy, processRequest(), the request "
                    + " to fork is not an INVITE, so we will process"
                    + " it with the first target as the only target.");
          targetURI = (URI) targetURIList.firstElement();
          targetURIList = new Vector();
          targetURIList.addElement(targetURI);
          // 4. Forward the request statefully to the target:
          requestForwarding.forwardRequest(
              targetURIList, sipProvider, request, serverTransaction, true);
          return;
        }

        if (targetURIList != null && !targetURIList.isEmpty()) {
          if (ProxyDebug.debug)
            ProxyDebug.println(
                "Proxy, processRequest(), the target set"
                    + " is the set of the contacts URI from the "
                    + " location service");

          To to = (To) request.getHeader(ToHeader.NAME);
          From from = (From) request.getHeader(FromHeader.NAME);

          String FromUser = from.getUserAtHostPort();
          String ToUser = to.getUserAtHostPort();

          StringBuffer sb = new StringBuffer(FromUser);
          int endsAt = sb.indexOf("@");
          String FromUsername = sb.substring(0, endsAt);
          sb = new StringBuffer(ToUser);
          endsAt = sb.indexOf("@");
          String ToUsername = sb.substring(0, endsAt);

          if (!block.CheckBlock(FromUsername, ToUsername)) {
            Response response =
                messageFactory.createResponse(Response.TEMPORARILY_UNAVAILABLE, request);
            if (serverTransaction != null) serverTransaction.sendResponse(response);
            else sipProvider.sendResponse(response);
            return;
          }
          /*
           * // ECE355 Changes - Aug. 2005. // Call registry service,
           * get response (uri - wsdl). // if response is not null
           * then // do our staff // send to caller decline message by
           * building a decline msg // and attach wsdl uri in the
           * message body // else .. continue the logic below ...
           *
           *
           * // Lets assume that wsdl_string contains the message with
           * all the // service names and uri's for each service in
           * the required format
           *
           * // Query for web services for the receiver of INVITE //
           * Use WebServices class to get services for org
           *
           * String messageBody = "" ; WebServicesQuery wsq = null ;
           * wsq = WebServicesQuery.getInstance();
           *
           * // Get services info for receiver // A receiver is
           * represented as an organization in the Service Registry
           *
           * To to = (To)request.getHeader(ToHeader.NAME); String
           * toAddress = to.getUserAtHostPort();
           *
           * // Remove all characters after the @ sign from To address
           * StringBuffer sb = new StringBuffer(toAddress); int endsAt
           * = sb.indexOf("@"); String orgNamePattern =
           * sb.substring(0, endsAt);
           *
           *
           * Collection serviceInfoColl =
           * wsq.findServicesForOrg(orgNamePattern);
           *
           * // If services are found for this receiver (Org), build
           * DECLINE message and // send to client if (serviceInfoColl
           * != null) { if (serviceInfoColl.size()!= 0 ){
           * System.out.println("Found " + serviceInfoColl.size() +
           * " services for o rg " + orgNamePattern) ; // Build
           * message body for DECLINE message with Service Info
           * messageBody = serviceInfoColl.size()+ " -- " ;
           *
           * Iterator servIter = serviceInfoColl.iterator(); while
           * (servIter.hasNext()) { ServiceInfo servInfo =
           * (ServiceInfo)servIter.next(); messageBody = messageBody +
           * servInfo.getDescription()+ " " + servInfo.getWsdluri() +
           * " " + servInfo.getEndPoint()+ " -- ";
           *
           *
           * System.out.println("Name: " + servInfo.getName()) ;
           * System.out.println("Providing Organization: " +
           * servInfo.getProvidingOrganization()) ;
           * System.out.println("Description: " +
           * servInfo.getDescription()) ;
           * System.out.println("Service End Point " +
           * servInfo.getEndPoint()) ; System.out.println("wsdl wri "
           * + servInfo.getWsdluri()) ;
           * System.out.println("---------------------------------");
           *
           *
           *
           * }
           *
           * System.out.println("ServiceInfo - Message Body  " +
           * messageBody) ;
           *
           * // Build and send DECLINE message with web service info
           *
           * ContentTypeHeader contentTypeHeader = new ContentType(
           * "text", "plain");
           *
           * Response response = messageFactory.createResponse(
           * Response.DECLINE, request, contentTypeHeader,
           * messageBody);
           *
           *
           *
           * if (serverTransaction != null)
           * serverTransaction.sendResponse(response); else
           * sipProvider.sendResponse(response); return; } else
           * System.out.println("There are no services for org " +
           * orgNamePattern) ;
           *
           * }
           *
           * // End of ECE355 change
           */

          // 4. Forward the request statefully to each target Section
          // 16.6.:
          requestForwarding.forwardRequest(
              targetURIList, sipProvider, request, serverTransaction, true);

          return;
        } else {
          // Let's continue and try the default hop.
        }
      }

      // The registrar cannot help to decide the targets, so let's use
      // our router: the default hop!
      ProxyDebug.println(
          "Proxy, processRequest(), the registrar cannot help"
              + " to decide the targets, so let's use our router: the default hop");
      Router router = sipStack.getRouter();
      if (router != null) {
        ProxyHop hop = (ProxyHop) router.getOutboundProxy();
        if (hop != null) {
          if (ProxyDebug.debug)
            ProxyDebug.println(
                "Proxy, processRequest(), the target set" + " is the defaut hop: outbound proxy");

          // Bug fix contributed by Joe Provino
          String user = null;

          if (requestURI.isSipURI()) {
            SipURI requestSipURI = (SipURI) requestURI;
            user = requestSipURI.getUser();
          }

          SipURI hopURI = addressFactory.createSipURI(user, hop.getHost());
          hopURI.setTransportParam(hop.getTransport());
          hopURI.setPort(hop.getPort());
          targetURI = hopURI;
          targetURIList.addElement(targetURI);

          // 4. Forward the request statelessly to each target Section
          // 16.6.:
          requestForwarding.forwardRequest(
              targetURIList, sipProvider, request, serverTransaction, false);

          return;
        }
      }

      /*
       * If the target set remains empty after applying all of the above,
       * the proxy MUST return an error response, which SHOULD be the 480
       * (Temporarily Unavailable) response.
       */
      Response response = messageFactory.createResponse(Response.TEMPORARILY_UNAVAILABLE, request);
      if (serverTransaction != null) serverTransaction.sendResponse(response);
      else sipProvider.sendResponse(response);

      if (ProxyDebug.debug)
        ProxyDebug.println(
            "Proxy, processRequest(), unable to set "
                + " the targets, 480 (Temporarily Unavailable) replied:\n"
                + response.toString());

    } catch (Exception ex) {
      try {
        if (ProxyDebug.debug) {
          ProxyDebug.println("Proxy, processRequest(), internal error, " + "exception raised:");
          ProxyDebug.logException(ex);
          ex.printStackTrace();
        }

        // This is an internal error:
        // Let's return a 500 SERVER_INTERNAL_ERROR
        Response response = messageFactory.createResponse(Response.SERVER_INTERNAL_ERROR, request);
        if (serverTransaction != null) serverTransaction.sendResponse(response);
        else sipProvider.sendResponse(response);

        if (ProxyDebug.debug)
          ProxyDebug.println(
              "Proxy, processRequest(),"
                  + " 500 SERVER_INTERNAL_ERROR replied:\n"
                  + response.toString());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
Ejemplo n.º 26
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());
    }
  }
  public void processSubscribe(Request request, ServerTransaction serverTransaction) {
    logger.debug("Processing SUBSCRIBE in progress ");
    try {

      MessageFactory messageFactory = imUA.getMessageFactory();
      HeaderFactory headerFactory = imUA.getHeaderFactory();
      AddressFactory addressFactory = imUA.getAddressFactory();
      Dialog dialog = serverTransaction.getDialog();

      // ********** Terminating subscriptions **********
      ExpiresHeader expiresHeader = (ExpiresHeader) request.getHeader(ExpiresHeader.NAME);
      if (expiresHeader != null && expiresHeader.getExpires() == 0) {
        if (dialog != null) {
          // Terminating an existing subscription
          Response response = messageFactory.createResponse(Response.OK, request);
          serverTransaction.sendResponse(response);
          IMNotifyProcessing imNotifyProcessing = imUA.getIMNotifyProcessing();
          imNotifyProcessing.sendNotify(response, null, dialog);
          return;
        } else {
          // Terminating an non existing subscription
          Response response =
              messageFactory.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST, request);
          serverTransaction.sendResponse(response);
          return;
        }
      }

      // ********** Non-terminating subscriptions ************

      // send a 202 Accepted while waiting for authorization from user
      Response response = messageFactory.createResponse(Response.ACCEPTED, request);
      // Tag:
      ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
      if (toHeader.getTag() == null)
        toHeader.setTag(new Integer((int) (Math.random() * 10000)).toString());
      serverTransaction.sendResponse(response);
      logger.debug(response.toString());

      // We have to ask the user to authorize the guy to be in his buddy
      // list
      String presentityURL = IMUtilities.getKey(request, "From");
      SipProvider sipProvider = imUA.getSipProvider();
      InstantMessagingGUI imGUI = imUA.getInstantMessagingGUI();
      boolean authorization = imGUI.getAuthorizationForBuddy(presentityURL);
      if (authorization) {
        logger.debug(
            "DEBUG: SubscribeProcessing, processSubscribe(), " + " Response 202 Accepted sent.");

        // We have to create or update the subscriber!
        PresenceManager presenceManager = imUA.getPresenceManager();
        String subscriberURL = IMUtilities.getKey(request, "From");

        if (dialog != null) presenceManager.addSubscriber(subscriberURL, response, dialog);
        else {
          logger.debug(
              "ERROR, IMSubscribeProcessing, processSubscribe(), the"
                  + " dialog for the SUBSCRIBE we received is null!!! No subscriber added....");
          return;
        }

        // Let's see if this buddy is in our buddy list
        // if not let's ask to add him!
        BuddyList buddyList = imGUI.getBuddyList();
        ListenerInstantMessaging listenerIM = imGUI.getListenerInstantMessaging();
        if (!buddyList.hasBuddy(subscriberURL)) {
          // Let's ask:
          listenerIM.addContact(subscriberURL);
        }

        /** ********************** send NOTIFY ************************* */
        // We send a NOTIFY for any of our status but offline
        String localStatus = listenerIM.getLocalStatus();
        if (!localStatus.equals("offline")) {
          IMNotifyProcessing imNotifyProcessing = imUA.getIMNotifyProcessing();
          Subscriber subscriber = presenceManager.getSubscriber(subscriberURL);
          // Response okSent=subscriber.getOkSent();

          subscriberURL = subscriber.getSubscriberName();

          String contactAddress = imUA.getIMAddress() + ":" + imUA.getIMPort();

          String subStatus = listenerIM.getLocalStatus();
          String status = null;
          if (subStatus.equals("offline")) status = "closed";
          else status = "open";
          String xmlBody =
              imNotifyProcessing.xmlPidfParser.createXMLBody(
                  status, subStatus, subscriberURL, contactAddress);
          imNotifyProcessing.sendNotify(response, xmlBody, dialog);
        }
      } else {
        // User did not authorize subscription. Terminate it!
        logger.debug(
            "DEBUG, IMSubsribeProcessing, processSubscribe(), " + " Subscription declined!");
        logger.debug(
            "DEBUG, IMSubsribeProcessing, processSubscribe(), "
                + " Sending a Notify with Subscribe-state=terminated");

        IMNotifyProcessing imNotifyProcessing = imUA.getIMNotifyProcessing();
        if (dialog != null) {
          imNotifyProcessing.sendNotify(response, null, dialog);
          logger.debug(
              "DEBUG, IMSubsribeProcessing, processSubscribe(), "
                  + " Sending a Notify with Subscribe-state=terminated");
        } else {
          logger.debug(
              "ERROR, IMSubscribeProcessing, processSubscribe(), the"
                  + " dialog for the SUBSCRIBE we received is null!!! \n"
                  + "   No terminating Notify sent");
        }
        imNotifyProcessing.sendNotify(response, null, dialog);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  /**
   * Tries to steer a TI client transaction through the following scenario
   * Calling-->Completed-->Terminated. Apart from state transitions, we also test, retransmissions
   * and proper hiding/passing of messages to the TU.
   */
  public void testCallingCompletedTerminatedScenario() {
    try {
      Request invite = createTiInviteRequest(null, null, null);

      // JvB: test
      // ((MessageExt) invite).getFirstViaHeader().setParameter( "rport",
      // null );

      ClientTransaction tran = null;
      try {
        eventCollector.collectRequestEvent(riSipProvider);
        tran = tiSipProvider.getNewClientTransaction(invite);
        tran.sendRequest();
      } catch (SipException ex) {
        throw new TiUnexpectedError("A SipExceptionOccurred while trying to send request!", ex);
      } catch (TooManyListenersException ex) {
        throw new TckInternalError("Failed to regiest a SipListener with an RI SipProvider", ex);
      }
      waitForMessage();
      RequestEvent inviteReceivedEvent = eventCollector.extractCollectedRequestEvent();
      if (inviteReceivedEvent == null || inviteReceivedEvent.getRequest() == null)
        throw new TiUnexpectedError("The invite request was not received by the RI!");
      // At this point the ClientTransaction should be CALLING!
      assertEquals(TransactionState.CALLING, tran.getState());
      // Check Request retransmission
      try {
        eventCollector.collectRequestEvent(riSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TckInternalError("Failed to regiest a SipListener with an RI SipProvider", ex);
      }
      // Wait for the retransmission timer to fire if it had not already
      // done so.
      if (tran.getRetransmitTimer() > MESSAGES_ARRIVE_FOR)
        sleep((long) tran.getRetransmitTimer() - MESSAGES_ARRIVE_FOR); // subtract
      // the
      // time
      // we
      // waited
      // for
      // the
      // invite
      // Wait for the retransmitted request to arrive
      waitForMessage();
      inviteReceivedEvent = eventCollector.extractCollectedRequestEvent();
      assertNotNull("The invite request was not retransmitted!", inviteReceivedEvent);
      assertNotNull("The invite request was not retransmitted!", inviteReceivedEvent.getRequest());
      assertEquals(Request.INVITE, inviteReceivedEvent.getRequest().getMethod());
      // At this point the ClientTransaction should STILL be CALLING!
      assertEquals(TransactionState.CALLING, tran.getState());
      // Send a 486 BUSY HERE (final) response from the RI
      try {
        eventCollector.collectResponseEvent(tiSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TiUnexpectedError("Failed to register a SipListener with TI", ex);
      }
      // The BUSY_HERE response should trigger some ACKs so let's register
      // a listener with the RI
      SipEventCollector ackCollector = new SipEventCollector();
      try {
        ackCollector.collectRequestEvent(riSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TckInternalError("Failed to regiest a SipListener with an RI SipProvider", ex);
      }
      Response busyHere = null;
      try {
        busyHere =
            riMessageFactory.createResponse(Response.BUSY_HERE, inviteReceivedEvent.getRequest());
        addStatus(inviteReceivedEvent.getRequest(), busyHere);

        // JvB: set to-tag, check it against the ACK generated
        ((ToHeader) busyHere.getHeader("to")).setTag("ack-to-test");

        riSipProvider.sendResponse((Response) busyHere.clone());
      } catch (Throwable ex) {
        throw new TckInternalError(
            "The TCK could not send a BUSY HERE response back to the TI", ex);
      }
      waitForMessage();
      // Analyze the BUSY_HERE response and Tran state back at the TI
      ResponseEvent responseEvent = eventCollector.extractCollectedResponseEvent();
      assertNotNull(
          "The Tested Implementation did not pass a 300-699 response to the TU!", responseEvent);
      assertNotNull(
          "The Tested Implementation did not pass a 300-699 response to the TU!",
          responseEvent.getResponse());
      assertSame(
          "The BUSY_HERE response was not associated with the right transaction",
          tran,
          responseEvent.getClientTransaction());
      assertSame(
          "A response different from BUSY_HERE was passed to the TU",
          tran,
          responseEvent.getClientTransaction());
      assertEquals(
          "The ClientTransaction did not pass in the COMPLETED state after "
              + "receiving 300-699 final response",
          tran.getState(),
          TransactionState.COMPLETED);
      // check whether the ackCollector has caught any fish
      RequestEvent ackReceivedEvent = ackCollector.extractCollectedRequestEvent();
      assertNotNull("The TI did not send an ACK request", ackReceivedEvent);
      assertNotNull("The TI did not send an ACK request", ackReceivedEvent.getRequest());
      assertEquals(Request.ACK, ackReceivedEvent.getRequest().getMethod());
      // Try to kill remaining ACK retransmissions
      // TODO this may not always work .. should give it a specific
      // timeout value
      waitForMessage();
      // Now let's retransmit the final response. This time it shouldn't
      // be
      // passed to the TU but an ACK should still be sent
      try {
        eventCollector.collectResponseEvent(tiSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TiUnexpectedError("Failed to register a SipListener with TI", ex);
      }
      // go fish the ack
      try {
        ackCollector.collectRequestEvent(riSipProvider);
      } catch (TooManyListenersException ex) {
        throw new TckInternalError("Failed to regiest a SipListener with an RI SipProvider", ex);
      }
      try {
        riSipProvider.sendResponse((Response) busyHere.clone());
      } catch (Throwable ex) {
        throw new TckInternalError(
            "The TCK could not send a BUSY HERE response back to the TI", ex);
      }
      waitForMessage();
      // The TU shouldn't see the retransmitted BUSY_HERE response
      responseEvent = eventCollector.extractCollectedResponseEvent();
      assertNull(
          "The Tested Implementation passed a retransmitted 300-699 response "
              + "to the TU instead of just silently acknowledging it!",
          responseEvent);
      // We must still be in the completed state.
      assertEquals(
          "The ClientTransaction did not stay long enough in the COMPLETED " + "state.",
          tran.getState(),
          TransactionState.COMPLETED);
      // check whether the ackCollector has caught any fish
      ackReceivedEvent = ackCollector.extractCollectedRequestEvent();
      assertNotNull("The TI did not send an ACK request to the second response", ackReceivedEvent);
      assertNotNull(
          "The TI did not send an ACK request to the second response",
          ackReceivedEvent.getRequest());

      assertEquals(Request.ACK, ackReceivedEvent.getRequest().getMethod());

      // JvB: verify to tag
      assertEquals(
          "The To header field in the ACK MUST equal the To header field "
              + " in the response being acknowledged",
          "ack-to-test",
          ((ToHeader) ackReceivedEvent.getRequest().getHeader("to")).getTag());
    } catch (Throwable exc) {
      exc.printStackTrace();
      fail(exc.getClass().getName() + ": " + exc.getMessage());
    }
    assertTrue(new Exception().getStackTrace()[0].toString(), true);

    // Unfortunately we can't assert the TERMINATED state as timerD
    // is not exported by JAIN SIP

  }
Ejemplo n.º 29
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);
      }
    }
  }
    public void processResponse(ResponseEvent responseReceivedEvent) {
      System.out.println("Got a response");
      Response response = (Response) responseReceivedEvent.getResponse();
      ClientTransaction tid = responseReceivedEvent.getClientTransaction();
      CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);

      System.out.println(
          "Response received : Status Code = " + response.getStatusCode() + " " + cseq);

      if (tid == null) {

        // RFC3261: MUST respond to every 2xx
        if (ackRequest != null && dialog != null) {
          System.out.println("re-sending ACK");
          try {
            dialog.sendAck(ackRequest);
          } catch (SipException se) {
            se.printStackTrace();
            fail("Unxpected exception ");
          }
        }
        return;
      }
      // If the caller is supposed to send the bye
      if (callerSendsBye && !byeTaskRunning) {
        byeTaskRunning = true;
        new Timer().schedule(new ByeTask(dialog), 4000);
      }
      System.out.println("transaction state is " + tid.getState());
      System.out.println("Dialog = " + tid.getDialog());
      System.out.println("Dialog State is " + tid.getDialog().getState());

      assertSame("Checking dialog identity", tid.getDialog(), this.dialog);

      try {
        if (response.getStatusCode() == Response.OK) {
          if (cseq.getMethod().equals(Request.INVITE)) {
            System.out.println("Dialog after 200 OK  " + dialog);
            System.out.println("Dialog State after 200 OK  " + dialog.getState());
            Request ackRequest = dialog.createAck(cseq.getSeqNumber());
            System.out.println("Sending ACK");
            dialog.sendAck(ackRequest);

            // JvB: test REFER, reported bug in tag handling
            //                      Request referRequest = dialog.createRequest("REFER");
            //                      //simulating a balancer that will forward the request to the
            // recovery node
            //                      SipURI referRequestURI = addressFactory.createSipURI(null,
            // "127.0.0.1:5080");
            //                      referRequest.setRequestURI(referRequestURI);
            //                      dialog.sendRequest(
            // sipProvider.getNewClientTransaction(referRequest));
            //
          } else if (cseq.getMethod().equals(Request.CANCEL)) {
            if (dialog.getState() == DialogState.CONFIRMED) {
              // oops cancel went in too late. Need to hang up the
              // dialog.
              System.out.println("Sending BYE -- cancel went in too late !!");
              Request byeRequest = dialog.createRequest(Request.BYE);
              ClientTransaction ct = sipProvider.getNewClientTransaction(byeRequest);
              dialog.sendRequest(ct);
            }

          } else if (cseq.getMethod().equals(Request.BYE)) {
            okToByeReceived = true;
          }
        }
      } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(0);
      }
    }