private Url encodeRenderUrl(Url url, boolean forceRenderUrl) {
    if ((!forceRenderUrl) && (ThreadPortletContext.isResourceRequest())) {
      return encodeResourceUrl(url);
    }

    if (url != null) {
      String qualifiedPath = getQualifiedPath(url.toString());

      PortletResponse portletResponse = ThreadPortletContext.getPortletResponse();
      if ((portletResponse != null) && (portletResponse instanceof MimeResponse)) {
        try {
          PortletURL portletUrl = ((MimeResponse) portletResponse).createRenderURL();
          portletUrl.setParameter(
              WicketPortlet.WICKET_URL_PORTLET_PARAMETER
                  + ThreadPortletContext.getPortletMode().toString(),
              qualifiedPath);
          qualifiedPath = portletUrl.toString();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }

      url = parseUrl(qualifiedPath);
    }

    return url;
  }
 /**
  * Get the absolute bookmarkable path of a page
  *
  * @param pageClass Page
  * @param pageParameters Optional page parameters
  * @return Bookmarkable path
  */
 public static String absoluteMountPathForPage(
     Class<? extends Page> pageClass, PageParameters pageParameters) {
   HttpServletRequest req = getHttpServletRequest();
   RequestCycle requestCycle = RequestCycle.get();
   Url url = requestCycle.mapUrlFor(pageClass, pageParameters);
   String renderedUrl = url.toString();
   renderedUrl = Strings.isEmpty(renderedUrl) ? "." : renderedUrl;
   return RequestUtils.toAbsolutePath(
       HttpUtils.getWebappContextUrl(req),
       requestCycle.getOriginalResponse().encodeURL(renderedUrl));
 }
 private Url encryptUrl(final Url url) {
   if (url.getSegments().isEmpty() && url.getQueryParameters().isEmpty()) {
     return url;
   }
   final String encryptedUrlString = this.getCrypt().encryptUrlSafe(url.toString());
   final Url encryptedUrl = new Url(url.getCharset());
   encryptedUrl.getSegments().add(encryptedUrlString);
   final int numberOfSegments = url.getSegments().size();
   final HashedSegmentGenerator generator = new HashedSegmentGenerator(encryptedUrlString);
   for (int segNo = 0; segNo < numberOfSegments; ++segNo) {
     encryptedUrl.getSegments().add(generator.next());
   }
   return encryptedUrl;
 }
  /**
   * Merges the query parameters of the url with the named parameters from the {@link
   * PageParameters}. The page parameters override the query parameters.
   *
   * @param url the url with the original parameters
   * @param params the page parameters to merge
   * @return an Url with merged parameters
   */
  public static Url mergeParameters(final Url url, final PageParameters params) {
    if (params == null) {
      return url;
    }

    Charset charset = url.getCharset();

    Url mergedUrl = Url.parse(url.toString(), charset);

    UrlEncoder urlEncoder = UrlEncoder.QUERY_INSTANCE;

    Set<String> setParameters = new HashSet<String>();

    int indexedCount = params.getIndexedCount();
    if (indexedCount > 0) {
      String jsessionidString = null;
      List<String> segments = mergedUrl.getSegments();
      if (segments.size() > 0) {
        String lastSegment = segments.get(segments.size() - 1);
        int jsessionidIndex = lastSegment.indexOf(";jsessionid=");
        if (jsessionidIndex != -1) {
          segments.set(segments.size() - 1, lastSegment.substring(0, jsessionidIndex));
          jsessionidString = lastSegment.substring(jsessionidIndex);
        }
      }
      for (int i = 0; i < indexedCount; i++) {
        segments.add(params.get(i).toString());
      }
      if (jsessionidString != null) {
        segments.set(
            segments.size() - 1, segments.get(segments.size() - 1).concat(jsessionidString));
      }
    }

    for (INamedParameters.NamedPair pair : params.getAllNamed()) {
      String key = urlEncoder.encode(pair.getKey(), charset);
      String value = urlEncoder.encode(pair.getValue(), charset);

      if (setParameters.contains(key)) {
        mergedUrl.addQueryParameter(key, value);
      } else {
        mergedUrl.setQueryParameter(key, value);
        setParameters.add(key);
      }
    }

    return mergedUrl;
  }
  private Url encodeResourceUrl(Url url) {
    if (url != null) {
      String qualifiedPath = getQualifiedPath(url.toString());

      PortletResponse portletResponse = ThreadPortletContext.getPortletResponse();
      if ((portletResponse != null) && (portletResponse instanceof MimeResponse)) {
        try {
          ResourceURL resourceUrl = ((MimeResponse) portletResponse).createResourceURL();
          resourceUrl.setResourceID(qualifiedPath);
          qualifiedPath = resourceUrl.toString();

          // resource URLs preserve all request render parameters (at
          // least for Liferay even all POST parameters), so we have
          // to remove all Wicket parameters (that have the portlet
          // namespace), otherwise we would have all submited values
          // in the URL
          int queryStringSeparator = qualifiedPath.indexOf('?');
          if (queryStringSeparator > 0) {
            Map<String, String[]> parameterMap =
                Utils.parseQueryString(qualifiedPath.substring(queryStringSeparator + 1));

            boolean changed = false;
            Iterator<Map.Entry<String, String[]>> it = parameterMap.entrySet().iterator();
            String namespace = ThreadPortletContext.getNamespace();
            while (it.hasNext()) {
              if (it.next().getKey().startsWith(namespace)) {
                changed = true;
                it.remove();
              }
            }
            if (changed) {
              qualifiedPath =
                  qualifiedPath.substring(0, queryStringSeparator)
                      + '?'
                      + Utils.buildQueryString(parameterMap);
            }
          }
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }

      url = parseUrl(qualifiedPath);
    }

    return url;
  }
  private Url encodeSharedResourceUrl(Url url) {
    if (url != null) {
      Request request = RequestCycle.get().getRequest();

      StringBuilder urlBuilder = new StringBuilder();
      urlBuilder.append(request.getContextPath());
      urlBuilder.append(request.getFilterPath());
      urlBuilder.append(PortletFilter.SHARED_RESOURCE_URL_PORTLET_WINDOW_ID_PREFIX);
      urlBuilder.append(
          Base64.encodeBase64URLSafeString(ThreadPortletContext.getWindowID().getBytes()));
      urlBuilder.append('/');
      urlBuilder.append(url.toString());

      url = Url.parse(urlBuilder.toString());
    }

    return url;
  }