Esempio n. 1
0
  /**
   * Returns a relative path to the filter path and context root from an HttpServletRequest - use
   * this to resolve a Wicket request.
   *
   * @param request
   * @return Path requested, minus query string, context path, and filterPath. Relative, no leading
   *     '/'.
   */
  public String getRelativePath(HttpServletRequest request) {
    String path = Strings.stripJSessionId(request.getRequestURI());
    String contextPath = request.getContextPath();
    path = path.substring(contextPath.length());
    if (isServlet) {
      String servletPath = request.getServletPath();
      path = path.substring(servletPath.length());
    }

    if (path.length() > 0) {
      path = path.substring(1);
    }

    // We should always be under the rootPath, except
    // for the special case of someone landing on the
    // home page without a trailing slash.
    String filterPath = getFilterPath();
    if (!path.startsWith(filterPath)) {
      if (filterPath.equals(path + "/")) {
        path += "/";
      }
    }
    if (path.startsWith(filterPath)) {
      path = path.substring(filterPath.length());
    }

    return path;
  }
Esempio n. 2
0
  /**
   * Try to determine as fast as possible if a redirect is necessary
   *
   * @param requestURI
   * @param contextPath
   * @return null, if no redirect is necessary. Else the redirect URL
   */
  protected final String checkIfRedirectRequired(
      final String requestURI, final String contextPath) {
    // length without jsessionid (http://.../abc;jsessionid=...?param)
    int uriLength = requestURI.indexOf(';');
    if (uriLength == -1) {
      uriLength = requestURI.length();
    }

    // request.getContextPath() + "/" + filterPath. But without any trailing "/".
    int homePathLength = contextPath.length() + (filterPathLength > 0 ? 1 + filterPathLength : 0);
    if (uriLength != homePathLength) {
      // requestURI and homePath are different (in length)
      // => continue with standard request processing. No redirect.
      return null;
    }

    // Fail fast failed. Revert to "slow" but exact check
    String uri = Strings.stripJSessionId(requestURI);

    // home page without trailing slash URI
    String homePageUri = contextPath + '/' + getFilterPath();
    if (homePageUri.endsWith("/")) {
      homePageUri = homePageUri.substring(0, homePageUri.length() - 1);
    }

    // If both are equal => redirect
    if (uri.equals(homePageUri)) {
      uri += "/";
      return uri;
    }

    // no match => standard request processing; no redirect
    return null;
  }