/**
  * Encode a shared resource target.
  *
  * <p>If you override this method to behave different then also {@link
  * #addResourceParameters(Request, RequestParameters)} should be overridden to by in sync with
  * that behaviour.
  *
  * @param requestCycle the current request cycle
  * @param requestTarget the target to encode
  * @return the encoded url
  */
 protected CharSequence encode(
     RequestCycle requestCycle, ISharedResourceRequestTarget requestTarget) {
   final CharSequence prefix = urlPrefix(requestCycle);
   final String sharedResourceKey = requestTarget.getResourceKey();
   if ((sharedResourceKey == null) || (sharedResourceKey.trim().length() == 0)) {
     return prefix;
   } else {
     final AppendingStringBuffer buffer =
         new AppendingStringBuffer(sharedResourceKey.length() + prefix.length() + 11);
     buffer.append(prefix);
     if ((buffer.length() > 0) && buffer.charAt(buffer.length() - 1) == '/') {
       buffer.append("resources/");
     } else {
       buffer.append("/resources/");
     }
     buffer.append(sharedResourceKey);
     Map<String, ? extends Object> map = requestTarget.getRequestParameters().getParameters();
     if (map != null && map.size() > 0) {
       buffer.append('?');
       for (String key : map.keySet()) {
         buffer.append(key);
         buffer.append('=');
         buffer.append(map.get(key));
         buffer.append('&');
       }
       buffer.setLength(buffer.length() - 1);
     }
     return requestCycle.getOriginalResponse().encodeURL(buffer);
   }
 }
  /**
   * Encode a listener interface target.
   *
   * <p>If you override this method to behave different then also {@link
   * #addInterfaceParameters(Request, RequestParameters)} should be overridden to by in sync with
   * that behaviour.
   *
   * @param requestCycle the current request cycle
   * @param requestTarget the target to encode
   * @return the encoded url
   */
  protected CharSequence encode(
      RequestCycle requestCycle, IListenerInterfaceRequestTarget requestTarget) {
    final RequestListenerInterface rli = requestTarget.getRequestListenerInterface();

    // Start string buffer for url
    final AppendingStringBuffer url = new AppendingStringBuffer(64);
    url.append(urlPrefix(requestCycle));
    url.append('?');
    url.append(INTERFACE_PARAMETER_NAME);
    url.append('=');

    // Get component and page for request target
    final Component component = requestTarget.getTarget();
    final Page page = component.getPage();

    // Add pagemap
    final PageMap pageMap = page.getPageMap();
    if (!pageMap.isDefault()) {
      url.append(pageMap.getName());
    }
    url.append(Component.PATH_SEPARATOR);

    // Add path to component
    url.append(component.getPath());
    url.append(Component.PATH_SEPARATOR);

    // Add version
    final int versionNumber = component.getPage().getCurrentVersionNumber();
    if (!rli.getRecordsPageVersion()) {
      url.append(Page.LATEST_VERSION);
    } else if (versionNumber > 0) {
      url.append(versionNumber);
    }
    url.append(Component.PATH_SEPARATOR);

    // Add listener interface
    final String listenerName = rli.getName();
    if (!IRedirectListener.INTERFACE.getName().equals(listenerName)) {
      url.append(listenerName);
    }

    return requestCycle.getOriginalResponse().encodeURL(url);
  }
  /**
   * Encode a page class target.
   *
   * <p>If you override this method to behave different then also {@link
   * #addBookmarkablePageParameters(Request, RequestParameters)} should be overridden to by in sync
   * with that behaviour.
   *
   * @param requestCycle the current request cycle
   * @param requestTarget the target to encode
   * @return the encoded url
   */
  protected CharSequence encode(
      RequestCycle requestCycle, IBookmarkablePageRequestTarget requestTarget) {
    // Begin encoding URL
    final AppendingStringBuffer url = new AppendingStringBuffer(64);
    url.append(urlPrefix(requestCycle));

    // Get page Class
    final Class pageClass = requestTarget.getPageClass();
    final Application application = Application.get();

    // Find pagemap name
    String pageMapName = requestTarget.getPageMapName();
    if (pageMapName == null) {
      IRequestTarget currentTarget = requestCycle.getRequestTarget();
      if (currentTarget instanceof IPageRequestTarget) {
        Page currentPage = ((IPageRequestTarget) currentTarget).getPage();
        final PageMap pageMap = currentPage.getPageMap();
        if (pageMap.isDefault()) {
          pageMapName = "";
        } else {
          pageMapName = pageMap.getName();
        }
      } else {
        pageMapName = "";
      }
    }

    boolean firstParameter = true;
    if (!application.getHomePage().equals(pageClass)
        || !"".equals(pageMapName)
        || requestTarget instanceof BookmarkableListenerInterfaceRequestTarget) {
      firstParameter = false;
      url.append('?');
      url.append(WebRequestCodingStrategy.BOOKMARKABLE_PAGE_PARAMETER_NAME);
      url.append('=');

      // Add <page-map-name>:<bookmarkable-page-class>
      url.append(pageMapName + Component.PATH_SEPARATOR + pageClass.getName());
    }

    // Is it a bookmarkable interface listener?
    if (requestTarget instanceof BookmarkableListenerInterfaceRequestTarget) {
      BookmarkableListenerInterfaceRequestTarget listenerTarget =
          (BookmarkableListenerInterfaceRequestTarget) requestTarget;
      if (firstParameter == true) {
        url.append("?");
      } else {
        url.append("&");
      }
      firstParameter = false;
      url.append(INTERFACE_PARAMETER_NAME);
      url.append("=");
      url.append(Component.PATH_SEPARATOR);
      url.append(listenerTarget.getComponentPath());
      url.append(Component.PATH_SEPARATOR);
      url.append(Component.PATH_SEPARATOR);
      url.append(listenerTarget.getInterfaceName());
    }

    // Get page parameters
    final PageParameters parameters = requestTarget.getPageParameters();
    if (parameters != null) {
      for (Object element : parameters.keySet()) {
        final String key = (String) element;
        final String value = parameters.getString(key);
        if (value != null) {
          String escapedValue = value;
          try {
            escapedValue =
                URLEncoder.encode(
                    escapedValue,
                    application.getRequestCycleSettings().getResponseRequestEncoding());
          } catch (UnsupportedEncodingException ex) {
            log.error(ex.getMessage(), ex);
          }
          if (!firstParameter) {
            url.append('&');
          } else {
            firstParameter = false;
            url.append('?');
          }
          url.append(key);
          url.append('=');
          url.append(escapedValue);
        }
      }
    }
    return requestCycle.getOriginalResponse().encodeURL(url);
  }