/**
   * Find the transaction corresponding to a given request.
   *
   * @param sipMessage request for which to retrieve the transaction.
   * @param isServer search the server transaction table if true.
   * @return the transaction object corresponding to the request or null if no such mapping exists.
   */
  public SIPTransaction findTransaction(SIPMessage sipMessage, boolean isServer) {
    SIPTransaction retval = null;

    if (isServer) {
      Via via = sipMessage.getTopmostVia();
      if (via.getBranch() != null) {
        String key = sipMessage.getTransactionId();

        synchronized (this.serverTransactionTable) {
          retval = (SIPTransaction) serverTransactionTable.get(key);
          if (LogWriter.needsLogging) logMessage("looking for key " + key);
          if (retval != null && retval.isMessagePartOfTransaction(sipMessage)) return retval;
        }
      }
      // Need to scan the table for old style transactions (RFC 2543
      // style)
      synchronized (this.serverTransactions) {
        Iterator<SIPServerTransaction> it = serverTransactions.iterator();
        while (it.hasNext()) {
          SIPServerTransaction sipServerTransaction = (SIPServerTransaction) it.next();
          if (sipServerTransaction.isMessagePartOfTransaction(sipMessage))
            return sipServerTransaction;
        }
      }
    } else {
      Via via = sipMessage.getTopmostVia();
      if (via.getBranch() != null) {
        String key = sipMessage.getTransactionId();
        synchronized (this.clientTransactionTable) {
          retval = (SIPTransaction) clientTransactionTable.get(key);
          if (retval != null && retval.isMessagePartOfTransaction(sipMessage)) return retval;
        }
      }
      // Need to scan the table for old style transactions (RFC 2543
      // style)
      synchronized (this.clientTransactions) {
        Iterator<SIPClientTransaction> it = clientTransactions.iterator();
        while (it.hasNext()) {
          SIPClientTransaction clientTransaction = (SIPClientTransaction) it.next();
          if (clientTransaction.isMessagePartOfTransaction(sipMessage)) return clientTransaction;
        }
      }
    }
    return null;
  }
 /**
  * Get the host from the topmost via header.
  *
  * @return the string representation of the host from the topmost via header.
  */
 public String getViaHost() {
   Via via = (Via) this.getViaHeaders().getFirst();
   return via.getHost();
 }
 /**
  * Create a new default SIPRequest from the original request. Warning: the newly created
  * SIPRequest, shares the headers of this request but we generate any new headers that we need to
  * modify so the original request is umodified. However, if you modify the shared headers after
  * this request is created, then the newly created request will also be modified. If you want to
  * modify the original request without affecting the returned Request make sure you clone it
  * before calling this method.
  *
  * <p>Only required headers are copied.
  *
  * <ul>
  *   <li>Contact headers are not included in the newly created request. Setting the appropriate
  *       sequence number is the responsibility of the caller.
  *   <li>RouteList is not copied for ACK and CANCEL
  *   <li>Note that we DO NOT copy the body of the argument into the returned header. We do not
  *       copy the content type header from the original request either. These have to be added
  *       seperately and the content length has to be correctly set if necessary the content length
  *       is set to 0 in the returned header.
  *   <li>Contact List is not copied from the original request.
  *   <li>RecordRoute List is not included from original request.
  *   <li>Via header is not included from the original request.
  * </ul>
  *
  * @param requestLine is the new request line.
  * @param switchHeaders is a boolean flag that causes to and from headers to switch (set this to
  *     true if you are the server of the transaction and are generating a BYE request). If the
  *     headers are switched, we generate new From and To headers otherwise we just use the
  *     incoming headers.
  * @return a new Default SIP Request which has the requestLine specified.
  */
 public SIPRequest createSIPRequest(RequestLine requestLine, boolean switchHeaders) {
   SIPRequest newRequest = new SIPRequest();
   newRequest.requestLine = requestLine;
   Iterator headerIterator = this.getHeaders();
   while (headerIterator.hasNext()) {
     SIPHeader nextHeader = (SIPHeader) headerIterator.next();
     // For BYE and cancel set the CSeq header to the
     // appropriate method.
     if (nextHeader instanceof CSeq) {
       CSeq newCseq = (CSeq) nextHeader.clone();
       nextHeader = newCseq;
       try {
         newCseq.setMethod(requestLine.getMethod());
       } catch (ParseException e) {
       }
     } else if (nextHeader instanceof ViaList) {
       Via via = (Via) (((ViaList) nextHeader).getFirst().clone());
       via.removeParameter("branch");
       nextHeader = via;
       // Cancel and ACK preserve the branch ID.
     } else if (nextHeader instanceof To) {
       To to = (To) nextHeader;
       if (switchHeaders) {
         nextHeader = new From(to);
         ((From) nextHeader).removeTag();
       } else {
         nextHeader = (SIPHeader) to.clone();
         ((To) nextHeader).removeTag();
       }
     } else if (nextHeader instanceof From) {
       From from = (From) nextHeader;
       if (switchHeaders) {
         nextHeader = new To(from);
         ((To) nextHeader).removeTag();
       } else {
         nextHeader = (SIPHeader) from.clone();
         ((From) nextHeader).removeTag();
       }
     } else if (nextHeader instanceof ContentLength) {
       ContentLength cl = (ContentLength) nextHeader.clone();
       try {
         cl.setContentLength(0);
       } catch (InvalidArgumentException e) {
       }
       nextHeader = cl;
     } else if (!(nextHeader instanceof CallID) && !(nextHeader instanceof MaxForwards)) {
       // Route is kept by dialog.
       // RR is added by the caller.
       // Contact is added by the Caller
       // Any extension headers must be added
       // by the caller.
       continue;
     }
     try {
       newRequest.attachHeader(nextHeader, false);
     } catch (SIPDuplicateHeaderException e) {
       e.printStackTrace();
     }
   }
   if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) {
     newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
   }
   return newRequest;
 }
 /**
  * Get the port from the topmost via header.
  *
  * @return the port from the topmost via header (5060 if there is no port indicated).
  */
 public int getViaPort() {
   Via via = (Via) this.getViaHeaders().getFirst();
   if (via.hasPort()) return via.getPort();
   else return 5060;
 }