/** Set the default values in the request URI if necessary. */
 protected void setDefaults() {
   // The request line may be unparseable (set to null by the
   // exception handler.
   if (requestLine == null) return;
   String method = requestLine.getMethod();
   // The requestLine may be malformed!
   if (method == null) return;
   GenericURI u = (GenericURI) requestLine.getUri();
   if (u == null) return;
   if (method.compareTo(Request.REGISTER) == 0 || method.compareTo(Request.INVITE) == 0) {
     if (u instanceof SipUri) {
       SipUri sipUri = (SipUri) u;
       sipUri.setUserParam(DEFAULT_USER);
       try {
         sipUri.setTransportParam(DEFAULT_TRANSPORT);
       } catch (ParseException ex) {
       }
     }
   }
 }
  /**
   * Check header for constraints. (1) Invite options and bye requests can only have SIP URIs in the
   * contact headers. (2) Request must have cseq, to and from and via headers. (3) Method in request
   * URI must match that in CSEQ.
   */
  public void checkHeaders() throws ParseException {
    String prefix = "Missing a required header : ";

    /* Check for required headers */

    if (getCSeq() == null) {
      throw new ParseException(prefix + CSeqHeader.NAME, 0);
    }
    if (getTo() == null) {
      throw new ParseException(prefix + ToHeader.NAME, 0);
    }

    if (this.callIdHeader == null
        || this.callIdHeader.getCallId() == null
        || callIdHeader.getCallId().equals("")) {
      throw new ParseException(prefix + CallIdHeader.NAME, 0);
    }
    if (getFrom() == null) {
      throw new ParseException(prefix + FromHeader.NAME, 0);
    }
    if (getViaHeaders() == null) {
      throw new ParseException(prefix + ViaHeader.NAME, 0);
    }
    if (getMaxForwards() == null) {
      throw new ParseException(prefix + MaxForwardsHeader.NAME, 0);
    }

    if (getTopmostVia() == null) throw new ParseException("No via header in request! ", 0);

    if (getMethod().equals(Request.NOTIFY)) {
      if (getHeader(SubscriptionStateHeader.NAME) == null)
        throw new ParseException(prefix + SubscriptionStateHeader.NAME, 0);

      if (getHeader(EventHeader.NAME) == null)
        throw new ParseException(prefix + EventHeader.NAME, 0);

    } else if (getMethod().equals(Request.PUBLISH)) {
      /*
       * For determining the type of the published event state, the EPA MUST include a
       * single Event header field in PUBLISH requests. The value of this header field
       * indicates the event package for which this request is publishing event state.
       */
      if (getHeader(EventHeader.NAME) == null)
        throw new ParseException(prefix + EventHeader.NAME, 0);
    }

    /*
     * RFC 3261 8.1.1.8 The Contact header field MUST be present and contain exactly one SIP
     * or SIPS URI in any request that can result in the establishment of a dialog. For the
     * methods defined in this specification, that includes only the INVITE request. For these
     * requests, the scope of the Contact is global. That is, the Contact header field value
     * contains the URI at which the UA would like to receive requests, and this URI MUST be
     * valid even if used in subsequent requests outside of any dialogs.
     *
     * If the Request-URI or top Route header field value contains a SIPS URI, the Contact
     * header field MUST contain a SIPS URI as well.
     */
    if (requestLine.getMethod().equals(Request.INVITE)
        || requestLine.getMethod().equals(Request.SUBSCRIBE)
        || requestLine.getMethod().equals(Request.REFER)) {
      if (this.getContactHeader() == null) {
        // Make sure this is not a target refresh. If this is a target
        // refresh its ok not to have a contact header. Otherwise
        // contact header is mandatory.
        if (this.getToTag() == null) throw new ParseException(prefix + ContactHeader.NAME, 0);
      }

      if (requestLine.getUri() instanceof SipUri) {
        String scheme = ((SipUri) requestLine.getUri()).getScheme();
        if ("sips".equalsIgnoreCase(scheme)) {
          SipUri sipUri = (SipUri) this.getContactHeader().getAddress().getURI();
          if (!sipUri.getScheme().equals("sips")) {
            throw new ParseException("Scheme for contact should be sips:" + sipUri, 0);
          }
        }
      }
    }

    /*
     * Contact header is mandatory for a SIP INVITE request.
     */
    if (this.getContactHeader() == null
        && (this.getMethod().equals(Request.INVITE)
            || this.getMethod().equals(Request.REFER)
            || this.getMethod().equals(Request.SUBSCRIBE))) {
      throw new ParseException("Contact Header is Mandatory for a SIP INVITE", 0);
    }

    if (requestLine != null
        && requestLine.getMethod() != null
        && getCSeq().getMethod() != null
        && requestLine.getMethod().compareTo(getCSeq().getMethod()) != 0) {
      throw new ParseException("CSEQ method mismatch with  Request-Line ", 0);
    }
  }