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());
}
}
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);
}
}
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);
}
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;
}
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;
}
Aggregations