Ejemplo n.º 1
0
  /**
   * Return the child Container that should be used to process this Request, based upon its
   * characteristics. If no such child Container can be identified, return <code>null</code>
   * instead.
   *
   * @param request Request being processed
   * @param update Update the Request to reflect the mapping selection?
   * @exception IllegalArgumentException if the relative portion of the path cannot be URL decoded
   */
  public Container map(Request request, boolean update) {

    int debug = context.getDebug();

    // Has this request already been mapped?
    if (update && (request.getWrapper() != null)) return (request.getWrapper());

    // Identify the context-relative URI to be mapped
    String contextPath = ((HttpServletRequest) request.getRequest()).getContextPath();
    String requestURI = ((HttpRequest) request).getDecodedRequestURI();
    String relativeURI = requestURI.substring(contextPath.length());

    // Apply the standard request URI mapping rules from the specification
    Wrapper wrapper = null;
    String servletPath = relativeURI;
    String pathInfo = null;
    String name = null;

    // Rule 1 -- Exact Match
    if (wrapper == null) {
      name = context.findServletMapping(requestURI);
      if (name == null) {
        if (!(relativeURI.equals("/"))) name = context.findServletMapping(relativeURI);
      }
      if (name != null) wrapper = (Wrapper) context.findChild(name);
      if (wrapper != null) {
        servletPath = relativeURI;
        pathInfo = null;
      }
    }

    // Rule 2 -- Prefix Match
    if (wrapper == null) {
      servletPath = relativeURI;
      while (true) {
        name = context.findServletMapping(servletPath + "/*");
        if (name != null) wrapper = (Wrapper) context.findChild(name);
        if (wrapper != null) {
          pathInfo = relativeURI.substring(servletPath.length());
          if (pathInfo.length() == 0) pathInfo = null;
          break;
        }
        int slash = servletPath.lastIndexOf('/');
        if (slash < 0) break;
        servletPath = servletPath.substring(0, slash);
      }
    }

    // Rule 3 -- Extension Match
    if (wrapper == null) {
      int slash = relativeURI.lastIndexOf('/');
      if (slash >= 0) {
        String last = relativeURI.substring(slash);
        int period = last.lastIndexOf('.');
        if (period >= 0) {
          String pattern = "*" + last.substring(period);
          name = context.findServletMapping(pattern);
          if (name != null) wrapper = (Wrapper) context.findChild(name);
          if (wrapper != null) {
            servletPath = relativeURI;
            pathInfo = null;
          }
        }
      }
    }

    // Rule 4 -- Default Match
    if (wrapper == null) {
      name = context.findServletMapping("/");
      if (name != null) wrapper = (Wrapper) context.findChild(name);
      if (wrapper != null) {
        servletPath = relativeURI;
        pathInfo = null;
      }
    }

    // Update the Request (if requested) and return this Wrapper
    if ((debug >= 1) && (wrapper != null))
      if (update) {
        request.setWrapper(wrapper);
        ((HttpRequest) request).setServletPath(servletPath);
        ((HttpRequest) request).setPathInfo(pathInfo);
      }
    return (wrapper);
  }