/** 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);
    }
  }
Exemple #3
0
  /**
   * Return addresses for default proxy to forward the request to. The list is organized in the
   * following priority. If the requestURI refers directly to a host, the host and port information
   * are extracted from it and made the next hop on the list. If the default route has been
   * specified, then it is used to construct the next element of the list. <code>
   * RouteHeader firstRoute = (RouteHeader) req.getHeader( RouteHeader.NAME );
   * if (firstRoute!=null) {
   *   URI uri = firstRoute.getAddress().getURI();
   *    if (uri.isSIPUri()) {
   *       SipURI nextHop = (SipURI) uri;
   *       if ( nextHop.hasLrParam() ) {
   *           // OK, use it
   *       } else {
   *           nextHop = fixStrictRouting( req );        <--- Here, make the modifications as per RFC3261
   *       }
   *   } else {
   *       // error: non-SIP URI not allowed in Route headers
   *       throw new SipException( "Request has Route header with non-SIP URI" );
   *   }
   * } else if (outboundProxy!=null) {
   *   // use outbound proxy for nextHop
   * } else if ( req.getRequestURI().isSipURI() ) {
   *   // use request URI for nextHop
   * }
   *
   * </code>
   *
   * @param request is the sip request to route.
   */
  public Hop getNextHop(Request request) throws SipException {

    SIPRequest sipRequest = (SIPRequest) request;

    RequestLine requestLine = sipRequest.getRequestLine();
    if (requestLine == null) {
      return defaultRoute;
    }
    javax.sip.address.URI requestURI = requestLine.getUri();
    if (requestURI == null) throw new IllegalArgumentException("Bad message: Null requestURI");

    RouteList routes = sipRequest.getRouteHeaders();

    /*
     * In case the topmost Route header contains no 'lr' parameter (which
     * means the next hop is a strict router), the implementation will
     * perform 'Route Information Postprocessing' as described in RFC3261
     * section 16.6 step 6 (also known as "Route header popping"). That is,
     * the following modifications will be made to the request:
     *
     * The implementation places the Request-URI into the Route header field
     * as the last value.
     *
     * The implementation then places the first Route header field value
     * into the Request-URI and removes that value from the Route header
     * field.
     *
     * Subsequently, the request URI will be used as next hop target
     */

    if (routes != null) {

      // to send the request through a specified hop the application is
      // supposed to prepend the appropriate Route header which.
      Route route = (Route) routes.getFirst();
      URI uri = route.getAddress().getURI();
      if (uri.isSipURI()) {
        SipURI sipUri = (SipURI) uri;
        if (!sipUri.hasLrParam()) {

          fixStrictRouting(sipRequest);
          if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("Route post processing fixed strict routing");
        }

        Hop hop = createHop(sipUri, request);
        if (sipStack.isLoggingEnabled())
          sipStack.getStackLogger().logDebug("NextHop based on Route:" + hop);
        return hop;
      } else {
        throw new SipException("First Route not a SIP URI");
      }

    } else if (requestURI.isSipURI() && ((SipURI) requestURI).getMAddrParam() != null) {
      Hop hop = createHop((SipURI) requestURI, request);
      if (sipStack.isLoggingEnabled())
        sipStack
            .getStackLogger()
            .logDebug("Using request URI maddr to route the request = " + hop.toString());

      // JvB: don't remove it!
      // ((SipURI) requestURI).removeParameter("maddr");

      return hop;

    } else if (defaultRoute != null) {
      if (sipStack.isLoggingEnabled())
        sipStack
            .getStackLogger()
            .logDebug("Using outbound proxy to route the request = " + defaultRoute.toString());
      return defaultRoute;
    } else if (requestURI.isSipURI()) {
      Hop hop = createHop((SipURI) requestURI, request);
      if (hop != null && sipStack.isLoggingEnabled())
        sipStack.getStackLogger().logDebug("Used request-URI for nextHop = " + hop.toString());
      else if (sipStack.isLoggingEnabled()) {
        sipStack.getStackLogger().logDebug("returning null hop -- loop detected");
      }
      return hop;

    } else {
      // The internal router should never be consulted for non-sip URIs.
      InternalErrorHandler.handleException(
          "Unexpected non-sip URI", this.sipStack.getStackLogger());
      return null;
    }
  }