Click
Click Examples

Source Viewer : WEB-INF/classes/org/apache/click/examples/page/ajax/AjaxSecurePage.java

package org.apache.click.examples.page.ajax;

import org.apache.click.ActionResult;
import org.apache.click.Context;
import org.apache.click.Page;
import org.apache.click.control.ActionLink;
import org.apache.click.examples.page.BorderPage;
import org.apache.click.util.ClickUtils;

/**
 * Provides an <tt>onSecurityCheck</tt> example secure Page for handling Ajax
 * requests. Two links are presented to the user. Clicking on the first link will
 * redirect the user to this page and show an error message. Clicking on the
 * second link will show an error message without redirecting to another page.
 */
public class AjaxSecurePage extends BorderPage {

    private static final long serialVersionUID = 1L;

    private ActionLink secureLinkWithRedirect = new ActionLink("secureLinkWithRedirect");
    private ActionLink secureLinkWithMessage = new ActionLink("secureLinkWithMessage");

    /**
     * @see Page#onSecurityCheck()
     */
    @Override
    public boolean onSecurityCheck() {
        // As onSecurityCheck event occurs bfore controls are processed, we use
        // explicit binding to determine which link was clicked

        ClickUtils.bind(secureLinkWithRedirect);
        ClickUtils.bind(secureLinkWithMessage);

        boolean performRedirect = secureLinkWithRedirect.isClicked();
        boolean performShowMessage = secureLinkWithMessage.isClicked();

        Context context = getContext();
        if (performRedirect) {
            context.setFlashAttribute("flash", "You have been <b>Redirected</b> "
                + "since you don't have permission to access the link.");

            String redirectUrl = context.getPagePath(AjaxSecurePage.class);
            String contextPath = context.getRequest().getContextPath();
            redirectUrl = contextPath + redirectUrl;

            context.getResponse().setHeader("REDIRECT_URL",redirectUrl);
            return false;

        } else if (performShowMessage) {
            ActionResult result = new ActionResult("You do not have permission "
                + "to access the link.");
            result.render(context);
            return false;
        }

            return true;
    }

    @Override
    public void onInit() {
        super.onInit();
        secureLinkWithRedirect.setId("secureLinkWithRedirectId");
        secureLinkWithMessage.setId("secureLinkWithMessageId");
        addControl(secureLinkWithRedirect);
        addControl(secureLinkWithMessage);
    }
}