/**
   * @see wicket.request.compound.IExceptionResponseStrategy#respond(wicket.RequestCycle,
   *     java.lang.RuntimeException)
   */
  public final void respond(final RequestCycle requestCycle, final RuntimeException e) {
    // If application doesn't want debug info showing up for users
    final Session session = requestCycle.getSession();
    final Application application = session.getApplication();
    final IExceptionSettings settings = application.getExceptionSettings();
    final Page responsePage = requestCycle.getResponsePage();

    Page override = onRuntimeException(responsePage, e);
    if (override != null) {
      // we do not want to redirect - we want to inline the error output
      // and preserve the url so when the refresh button is pressed we
      // rerun the code that caused the error
      requestCycle.setRedirect(false);

      throw new RestartResponseException(override);
    } else if (e instanceof AuthorizationException) {
      // are authorization exceptions always thrown before the real render?
      // else we need to make a page (see below) or set it hard to a redirect.
      Class<? extends Page> accessDeniedPageClass =
          application.getApplicationSettings().getAccessDeniedPage();

      throw new RestartResponseAtInterceptPageException(accessDeniedPageClass);
    } else if (settings.getUnexpectedExceptionDisplay()
        != UnexpectedExceptionDisplay.SHOW_NO_EXCEPTION_PAGE) {
      // we do not want to redirect - we want to inline the error output
      // and preserve the url so when the refresh button is pressed we
      // rerun the code that caused the error
      requestCycle.setRedirect(false);

      // figure out which error page to show
      Class<? extends Page> internalErrorPageClass =
          application.getApplicationSettings().getInternalErrorPage();
      Class responseClass = responsePage != null ? responsePage.getClass() : null;

      if (responseClass != internalErrorPageClass
          && settings.getUnexpectedExceptionDisplay()
              == UnexpectedExceptionDisplay.SHOW_INTERNAL_ERROR_PAGE) {
        throw new RestartResponseException(internalErrorPageClass);
      } else if (responseClass != ExceptionErrorPortletPage.class) {
        // Show full details
        throw new RestartResponseException(new ExceptionErrorPortletPage(e, responsePage));
      } else {
        // give up while we're ahead!
        throw new WicketRuntimeException(
            "Internal Error: Could not render error page " + internalErrorPageClass, e);
      }
    }
  }
  /**
   * Construct.
   *
   * @param securePageSuperType The class or interface supertype that indicates that a given Page
   *     requires authorization
   * @param signInPageClass The sign in page class
   */
  public SimplePageAuthorizationStrategy(
      final Class securePageSuperType, final Class<? extends Page> signInPageClass) {
    if (securePageSuperType == null) {
      throw new IllegalArgumentException("Secure page super type must not be null");
    }

    this.securePageSuperType = securePageSuperType;

    // Handle unauthorized access to pages
    Application.get()
        .getSecuritySettings()
        .setUnauthorizedComponentInstantiationListener(
            new IUnauthorizedComponentInstantiationListener() {
              public void onUnauthorizedInstantiation(final Component component) {
                // If there is a sign in page class declared, and the
                // unauthorized component is a page, but it's not the
                // sign in page
                if (component instanceof Page) {
                  // Redirect to page to let the user sign in
                  throw new RestartResponseAtInterceptPageException(signInPageClass);
                } else {
                  // The component was not a page, so throw exception
                  throw new UnauthorizedInstantiationException(component.getClass());
                }
              }
            });
  }
  /**
   * 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);
  }