[ 
https://issues.apache.org/jira/browse/WICKET-609?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12523889
 ] 

Marat Radchenko commented on WICKET-609:
----------------------------------------

Solution, take two (how handles pages in different directories):

{code:java}
  /**
   * Calculates absolute path to url relative to another absolute url.
   *
   * @param requestPath      absolute path.
   * @param relativePagePath path, relative to requestPath
   * @return absolute path for given url
   */
  private static String toAbsolutePath(final String requestPath, final String 
relativePagePath) {
    final StringBuffer result;
    if (requestPath.endsWith("/")) {
      result = new StringBuffer(requestPath);
    } else {
      //Remove everything after last slash (but not slash itself)
      result = new StringBuffer(requestPath.substring(0, 
requestPath.lastIndexOf('/') + 1));
    }
    if (relativePagePath.startsWith("../")) {
      StringBuffer tempRelative = new StringBuffer(relativePagePath);
      //Go up through hierarchy until we find most common directory for both 
pathes.
      while (tempRelative.indexOf("../") == 0) {
        //Delete ../ from relative path
        tempRelative.delete(0, 3);
        //Delete last slash from result
        result.setLength(result.length() - 1);
        //Delete everyting up to last slash
        result.delete(result.lastIndexOf("/") + 1, result.length());
      }
      result.append(tempRelative);
    } else {
      //Pages are in the same directory
      result.append(relativePagePath);
    }
    return result.toString();
  }
{code}

Testing (should print 'true' three times [and prints]):
{code:java}
  public static void main(String[] args) {
    System.out.println(toAbsolutePath("http://aif.ru/test/test";, 
"../blah/zzz").equals("http://aif.ru/blah/zzz";));
    System.out.println(toAbsolutePath("http://aif.ru/test";, 
"blah/zzz").equals("http://aif.ru/blah/zzz";));
    System.out.println(toAbsolutePath("http://aif.ru/test/";, 
"../blah/zzz").equals("http://aif.ru/blah/zzz";));
  }
{code}

Usage:
{code:java}
HttpServletRequest req = ... 
final String relativePagePath= RequestCycle.get().urlFor(...); 
final String absolutePageUrl = toAbsolutePath(req.getRequestURL().toString(), 
relativePagePath);
{code}

> Wicket should provide an easy method to generate absolute urls to 
> bookmarkable pages
> ------------------------------------------------------------------------------------
>
>                 Key: WICKET-609
>                 URL: https://issues.apache.org/jira/browse/WICKET-609
>             Project: Wicket
>          Issue Type: Bug
>    Affects Versions: 1.3.0-beta1
>            Reporter: Ryan Crumley
>            Assignee: Alastair Maw
>            Priority: Minor
>             Fix For: 1.3.0-rc1
>
>
> Currently there is no easy way to generate an absolute url for a bookmarkable 
> page. Something like getFullyQualifiedUrl(String contextRoot, Class page, 
> PageParameters parameters) would be ideal. 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to