// Process Request received
  public void processRequest(jain.protocol.ip.sip.SipEvent requestReceivedEvent) {
    Request request = (Request) requestReceivedEvent.getMessage();
    long serverTransactionId = requestReceivedEvent.getTransactionId();
    System.out.println(
        "\n\nRequest received with server transaction id " + serverTransactionId + ":\n" + request);
    try {
      // If request is not an ACK then try to send an OK Response
      if ((!Request.ACK.equals(request.getMethod()))
          && (!Request.REGISTER.equals(request.getMethod()))) {

        String body = request.getBodyAsString();
        getSipProvider().sendResponse(serverTransactionId, Response.OK, body, "application", "sdp");
      }
    } catch (TransactionDoesNotExistException e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
      System.exit(-1);
    } catch (SipParseException e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
      System.exit(-1);
    } catch (SipException e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
      System.exit(-1);
    }
    System.out.println("Completed processing request!");
  }
  public void unregister() throws IOException {
    if (!isRegistered) {
      return;
    }

    cancelPendingRegistrations();
    isRegistered = false;

    if (this.registerRequest == null) {
      Log.info("Couldn't find the initial register request");
      throw new IOException("Couldn't find the initial register request");
    }

    Request unregisterRequest = (Request) registerRequest.clone();

    try {
      unregisterRequest.getExpires().setExpires(0);
      CSeqHeader cSeqHeader = (CSeqHeader) unregisterRequest.getHeader(CSeqHeader.NAME);
      // [issue 1] - increment registration cseq number
      // reported by - Roberto Tealdi <*****@*****.**>
      cSeqHeader.setSequenceNumber(cSeqHeader.getSequenceNumber() + 1);
    } catch (InvalidArgumentException e) {
      Log.info("Unable to set Expires Header " + e.getMessage());
      return;
    }

    ClientTransaction unregisterTransaction = null;

    try {
      unregisterTransaction = sipProvider.getNewClientTransaction(unregisterRequest);
    } catch (TransactionUnavailableException e) {
      throw new IOException("Unable to create a unregister transaction " + e.getMessage());
    }
    try {
      unregisterTransaction.sendRequest();
    } catch (SipException e) {
      Log.info("Faied to send unregister request " + e.getMessage());
      return;
    }
  }
  // Process Response received
  public void processResponse(jain.protocol.ip.sip.SipEvent responseReceivedEvent) {
    Response response = (Response) responseReceivedEvent.getMessage();
    long clientTransactionId = responseReceivedEvent.getTransactionId();
    System.out.println(
        "Response received with client transaction id " + clientTransactionId + ":\n" + response);

    try {
      // Get method of response
      String method = response.getCSeqHeader().getMethod();

      // Get status code of response
      int statusCode = response.getStatusCode();
      // If response is a 200 INVITE response, try to send an ACK
      if ((statusCode == Response.OK) && (method.equals(Request.INVITE))) {

        getSipProvider().sendAck(clientTransactionId);
      }
    } catch (SipException e) {
      e.printStackTrace();
      System.err.println(e.getMessage());
      System.exit(-1);
    }
  }
    /**
     * Process a request from a distant contact
     *
     * @param requestEvent the <tt>RequestEvent</tt> containing the newly received request.
     * @return <tt>true</tt> if the specified event has been handled by this processor and shouldn't
     *     be offered to other processors registered for the same method; <tt>false</tt>, otherwise
     */
    @Override
    public boolean processRequest(RequestEvent requestEvent) {
      synchronized (messageProcessors) {
        for (SipMessageProcessor listener : messageProcessors)
          if (!listener.processMessage(requestEvent)) return true;
      }

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

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

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

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

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

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

      String ctype = null;
      String cencoding = null;

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

      if (cencoding == null) cencoding = DEFAULT_MIME_ENCODING;

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

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

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

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

      return true;
    }