Search in sources :

Example 1 with IRequestablePage

use of org.apache.wicket.request.component.IRequestablePage in project wicket by apache.

the class CsrfPreventionRequestCycleListener method onRequestHandlerResolved.

@Override
public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler) {
    if (!isEnabled()) {
        log.trace("CSRF listener is disabled, no checks performed");
        return;
    }
    handler = unwrap(handler);
    // check if the request is targeted at a page
    if (isChecked(handler)) {
        IPageRequestHandler prh = (IPageRequestHandler) handler;
        IRequestablePage targetedPage = prh.getPage();
        HttpServletRequest containerRequest = (HttpServletRequest) cycle.getRequest().getContainerRequest();
        String sourceUri = getSourceUri(containerRequest);
        // Check if the page should be CSRF protected
        if (isChecked(targetedPage)) {
            // if so check the Origin HTTP header
            checkRequest(containerRequest, sourceUri, targetedPage);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Targeted page {} was opted out of the CSRF origin checks, allowed", targetedPage.getClass().getName());
            }
            allowHandler(containerRequest, sourceUri, targetedPage);
        }
    } else {
        if (log.isTraceEnabled())
            log.trace("Resolved handler {} doesn't target an action on a page, no CSRF check performed", handler.getClass().getName());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IRequestablePage(org.apache.wicket.request.component.IRequestablePage) IPageRequestHandler(org.apache.wicket.core.request.handler.IPageRequestHandler)

Example 2 with IRequestablePage

use of org.apache.wicket.request.component.IRequestablePage in project wicket by apache.

the class WebPageRenderer method renderPage.

/**
 * Renders page to a {@link BufferedWebResponse}. All URLs in page will be rendered relative to
 * <code>targetUrl</code>
 *
 * @param targetUrl
 * @param requestCycle
 * @return BufferedWebResponse containing page body
 */
protected BufferedWebResponse renderPage(Url targetUrl, RequestCycle requestCycle) {
    // get the page before checking for a scheduled request handler because
    // the page may call setResponsePage in its constructor
    IRequestablePage requestablePage = getPage();
    IRequestHandler scheduled = requestCycle.getRequestHandlerScheduledAfterCurrent();
    if (scheduled != null) {
        // no need to render
        return null;
    }
    // keep the original response
    final WebResponse originalResponse = (WebResponse) requestCycle.getResponse();
    // buffered web response for page
    BufferedWebResponse response = new BufferedWebResponse(originalResponse);
    // keep the original base URL
    Url originalBaseUrl = requestCycle.getUrlRenderer().setBaseUrl(targetUrl);
    try {
        requestCycle.setResponse(response);
        requestablePage.renderPage();
        if (requestCycle.getRequestHandlerScheduledAfterCurrent() != null) {
            // This is a special case.
            // During page render another request handler got scheduled and will want to
            // overwrite the response, so we need to let it.
            // Just preserve the meta data headers. Clear the initial actions because they are
            // already copied into the new response's actions
            originalResponse.reset();
            response.writeMetaData(originalResponse);
            return null;
        } else {
            return response;
        }
    } finally {
        // restore original response and base URL
        requestCycle.setResponse(originalResponse);
        requestCycle.getUrlRenderer().setBaseUrl(originalBaseUrl);
    }
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) WebResponse(org.apache.wicket.request.http.WebResponse) IRequestHandler(org.apache.wicket.request.IRequestHandler) IRequestablePage(org.apache.wicket.request.component.IRequestablePage) Url(org.apache.wicket.request.Url)

Example 3 with IRequestablePage

use of org.apache.wicket.request.component.IRequestablePage in project wicket by apache.

the class ListenerRequestHandler method respond.

@Override
public void respond(final IRequestCycle requestCycle) {
    final IRequestablePage page = getPage();
    final boolean freshPage = pageComponentProvider.doesProvideNewPage();
    final boolean isAjax = ((WebRequest) requestCycle.getRequest()).isAjax();
    IRequestableComponent component;
    try {
        component = getComponent();
    } catch (ComponentNotFoundException e) {
        // either the page is stateless and the component we are looking for is not added in the
        // constructor
        // or the page is stateful+stale and a new instances was created by pageprovider
        // we denote this by setting component to null
        component = null;
    }
    if ((component == null && !freshPage) || (component != null && component.getPage() != page)) {
        throw new ComponentNotFoundException("Component '" + getComponentPath() + "' has been removed from page.");
    }
    if (page instanceof Page) {
        // initialize the page to be able to check whether it is stateless
        ((Page) page).internalInitialize();
    }
    RedirectPolicy policy = page.isPageStateless() ? RedirectPolicy.NEVER_REDIRECT : RedirectPolicy.AUTO_REDIRECT;
    boolean blockIfExpired = component != null && !component.canCallListenerAfterExpiry();
    boolean lateComponent = component == null && freshPage;
    if ((pageComponentProvider.wasExpired() && blockIfExpired) || lateComponent) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("An IRequestListener was called but its page/component({}) couldn't be resolved. " + "Scheduling re-create of the page and ignoring the listener interface...", getComponentPath());
        }
        if (isAjax) {
            policy = RedirectPolicy.ALWAYS_REDIRECT;
        }
        requestCycle.scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(new PageProvider(page), policy));
        return;
    }
    invokeListener(requestCycle, policy, isAjax);
}
Also used : RedirectPolicy(org.apache.wicket.core.request.handler.RenderPageRequestHandler.RedirectPolicy) IRequestableComponent(org.apache.wicket.request.component.IRequestableComponent) WebRequest(org.apache.wicket.request.http.WebRequest) IRequestablePage(org.apache.wicket.request.component.IRequestablePage) Page(org.apache.wicket.Page) IRequestablePage(org.apache.wicket.request.component.IRequestablePage)

Example 4 with IRequestablePage

use of org.apache.wicket.request.component.IRequestablePage in project wicket by apache.

the class PageAndComponentProvider method getComponent.

@Override
public IRequestableComponent getComponent() {
    if (component == null) {
        IRequestablePage page = getPageInstance();
        component = page != null ? page.get(componentPath) : null;
        if (component == null) {
            // make sure this page instance was just created so the page can be stateless
            if (page.isPageStateless()) {
                Page p = (Page) page;
                p.internalInitialize();
                // preparation of feedbacks is delayed into the render phase
                try (FeedbackDelay delay = new FeedbackDelay(p.getRequestCycle())) {
                    p.beforeRender();
                    p.markRendering(false);
                // note: no invocation of delay.onBeforeRender()
                }
                component = page.get(componentPath);
            }
        }
    }
    if (component == null) {
        throw new ComponentNotFoundException("Could not find component '" + componentPath + "' on page '" + getPageClass());
    }
    return component;
}
Also used : IRequestablePage(org.apache.wicket.request.component.IRequestablePage) FeedbackDelay(org.apache.wicket.feedback.FeedbackDelay) Page(org.apache.wicket.Page) IRequestablePage(org.apache.wicket.request.component.IRequestablePage)

Example 5 with IRequestablePage

use of org.apache.wicket.request.component.IRequestablePage in project wicket by apache.

the class AbstractBookmarkableMapper method mapHandler.

@Override
public Url mapHandler(IRequestHandler requestHandler) {
    // eliminate the need for IRequestHandlerDelegate
    while (requestHandler instanceof IRequestHandlerDelegate) {
        requestHandler = ((IRequestHandlerDelegate) requestHandler).getDelegateHandler();
    }
    if (requestHandler instanceof BookmarkablePageRequestHandler) {
        // simple bookmarkable URL with no page instance information
        BookmarkablePageRequestHandler handler = (BookmarkablePageRequestHandler) requestHandler;
        if (!checkPageClass(handler.getPageClass())) {
            return null;
        }
        PageInfo info = new PageInfo();
        UrlInfo urlInfo = new UrlInfo(new PageComponentInfo(info, null), handler.getPageClass(), handler.getPageParameters());
        return buildUrl(urlInfo);
    } else if (requestHandler instanceof RenderPageRequestHandler) {
        // possibly hybrid URL - bookmarkable URL with page instance information
        // but only allowed if the page was created by bookmarkable URL
        RenderPageRequestHandler handler = (RenderPageRequestHandler) requestHandler;
        if (!checkPageClass(handler.getPageClass())) {
            return null;
        }
        if (handler.getPageProvider().isNewPageInstance()) {
            // no existing page instance available, don't bother creating new page instance
            PageInfo info = new PageInfo();
            UrlInfo urlInfo = new UrlInfo(new PageComponentInfo(info, null), handler.getPageClass(), handler.getPageParameters());
            return buildUrl(urlInfo);
        }
        IRequestablePage page = handler.getPage();
        if (checkPageInstance(page) && (!pageMustHaveBeenCreatedBookmarkable() || page.wasCreatedBookmarkable())) {
            PageInfo info = getPageInfo(handler);
            PageComponentInfo pageComponentInfo = new PageComponentInfo(info, null);
            UrlInfo urlInfo = new UrlInfo(pageComponentInfo, page.getClass(), handler.getPageParameters());
            return buildUrl(urlInfo);
        } else {
            return null;
        }
    } else if (requestHandler instanceof BookmarkableListenerRequestHandler) {
        // request listener URL with page class information
        BookmarkableListenerRequestHandler handler = (BookmarkableListenerRequestHandler) requestHandler;
        Class<? extends IRequestablePage> pageClass = handler.getPageClass();
        if (!checkPageClass(pageClass)) {
            return null;
        }
        Integer renderCount = null;
        if (handler.includeRenderCount()) {
            renderCount = handler.getRenderCount();
        }
        PageInfo pageInfo = getPageInfo(handler);
        ComponentInfo componentInfo = new ComponentInfo(renderCount, handler.getComponentPath(), handler.getBehaviorIndex());
        PageParameters parameters = getRecreateMountedPagesAfterExpiry() ? new PageParameters(handler.getPage().getPageParameters()).mergeWith(handler.getPageParameters()) : handler.getPageParameters();
        UrlInfo urlInfo = new UrlInfo(new PageComponentInfo(pageInfo, componentInfo), pageClass, parameters);
        return buildUrl(urlInfo);
    }
    return null;
}
Also used : PageInfo(org.apache.wicket.request.mapper.info.PageInfo) RenderPageRequestHandler(org.apache.wicket.core.request.handler.RenderPageRequestHandler) BookmarkablePageRequestHandler(org.apache.wicket.core.request.handler.BookmarkablePageRequestHandler) BookmarkableListenerRequestHandler(org.apache.wicket.core.request.handler.BookmarkableListenerRequestHandler) IRequestablePage(org.apache.wicket.request.component.IRequestablePage) ComponentInfo(org.apache.wicket.request.mapper.info.ComponentInfo) PageComponentInfo(org.apache.wicket.request.mapper.info.PageComponentInfo) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) IRequestHandlerDelegate(org.apache.wicket.request.IRequestHandlerDelegate) PageComponentInfo(org.apache.wicket.request.mapper.info.PageComponentInfo)

Aggregations

IRequestablePage (org.apache.wicket.request.component.IRequestablePage)59 Url (org.apache.wicket.request.Url)50 IRequestHandler (org.apache.wicket.request.IRequestHandler)47 Test (org.junit.Test)46 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)31 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)18 ListenerRequestHandler (org.apache.wicket.core.request.handler.ListenerRequestHandler)15 BookmarkableListenerRequestHandler (org.apache.wicket.core.request.handler.BookmarkableListenerRequestHandler)14 IPageRequestHandler (org.apache.wicket.core.request.handler.IPageRequestHandler)6 Request (org.apache.wicket.request.Request)6 PageComponentInfo (org.apache.wicket.request.mapper.info.PageComponentInfo)5 PageProvider (org.apache.wicket.core.request.handler.PageProvider)4 PageInfo (org.apache.wicket.request.mapper.info.PageInfo)4 IPageProvider (org.apache.wicket.core.request.handler.IPageProvider)3 ComponentInfo (org.apache.wicket.request.mapper.info.ComponentInfo)3 Page (org.apache.wicket.Page)2 AccessControlException (java.security.AccessControlException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ForbiddenException (javax.ws.rs.ForbiddenException)1 Login (org.apache.syncope.client.console.pages.Login)1