/**
  * Handle an exception - construct a sip reply and send it back to the caller.
  *
  * @param ex The exception thrown at us by our application.
  */
 public void handleException(SIPServerException ex) {
   // Return a parse error message to the client on the other end
   // if he is still alive.
   // ex.printStackTrace();
   int rc = ex.getRC();
   Request request = (Request) ex.getSIPMessage();
   Response response;
   String msgString = ex.getMessage();
   if (rc != 0) {
     response = request.createResponse(rc, msgString);
     // messageFormatter.newResponse(rc,request,msgString);
     try {
       sendMessage(response);
     } catch (IOException ioex) {
       ServerLog.logException(ioex);
     }
   } else {
     // Assume that the message has already been formatted.
     try {
       sendMessage(msgString);
     } catch (IOException ioex) {
       ServerLog.logException(ioex);
     }
   }
 }
  /**
   * Actually process the parsed SIP message.
   *
   * @param sipMessage
   */
  public void processMessage(Message sipMessage) {

    if (sipMessage instanceof Request) {
      Request sipRequest = (Request) sipMessage;

      // This is a request - process it.
      SIPServerRequestInterface sipServerRequest = stack.newSIPServerRequest(sipRequest, this);
      // Drop it if there is no request returned
      if (sipServerRequest == null) {
        if (LogWriter.needsLogging) {
          LogWriter.logMessage("Null request interface returned");
        }
        return;
      }
      try {
        if (LogWriter.needsLogging)
          LogWriter.logMessage(
              "About to process " + sipRequest.getFirstLine() + "/" + sipServerRequest);
        sipServerRequest.processRequest(sipRequest, this);
        if (LogWriter.needsLogging)
          LogWriter.logMessage(
              "Done processing " + sipRequest.getFirstLine() + "/" + sipServerRequest);

        // So far so good -- we will commit this message if
        // all processing is OK.
        if (ServerLog.needsLogging(ServerLog.TRACE_MESSAGES)) {
          if (sipServerRequest.getProcessingInfo() == null) {
            ServerLog.logMessage(
                sipMessage,
                sipRequest.getViaHost() + ":" + sipRequest.getViaPort(),
                stack.getHostAddress() + ":" + stack.getPort(this.getTransport()),
                false,
                new Long(receptionTime).toString());
          } else {
            ServerLog.logMessage(
                sipMessage,
                sipRequest.getViaHost() + ":" + sipRequest.getViaPort(),
                stack.getHostAddress() + ":" + stack.getPort(this.getTransport()),
                sipServerRequest.getProcessingInfo(),
                false,
                new Long(receptionTime).toString());
          }
        }
      } catch (SIPServerException ex) {

        if (ServerLog.needsLogging(ServerLog.TRACE_MESSAGES)) {
          ServerLog.logMessage(
              sipMessage,
              sipRequest.getViaHost() + ":" + sipRequest.getViaPort(),
              stack.getHostAddress() + ":" + stack.getPort(this.getTransport()),
              ex.getMessage(),
              false,
              new Long(receptionTime).toString());
        }
        handleException(ex);
      }

    } else {

      // Handle a SIP Response message.
      Response sipResponse = (Response) sipMessage;
      SIPServerResponseInterface sipServerResponse = stack.newSIPServerResponse(sipResponse, this);
      try {
        if (sipServerResponse != null) {
          sipServerResponse.processResponse(sipResponse, this);
          // Normal processing of message.
        } else {
          if (LogWriter.needsLogging) {
            LogWriter.logMessage("null sipServerResponse!");
          }
        }

      } catch (SIPServerException ex) {
        if (ServerLog.needsLogging(ServerLog.TRACE_MESSAGES)) {
          this.logResponse(sipResponse, receptionTime, ex.getMessage() + "-- Dropped!");
        }

        ServerLog.logException(ex);
      }
    }
  }