You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

RequestCycle class has been reworked for Wicket 1.5.

General

Now it is better suited for composition by using org.apache.wicket.request.cycle.IRequestCycleListener so it can be extended by several independent libraries. In Wicket 1.4 the user application had to extend (Web)RequestCycle to use the hooks and this made it hard for third party libraries because they don't know about the other and both can extend only WebRequestCycle. Now libraries should register their own IRequestCycleListener (or better AbstractRequestCycleListener) through org.apache.wicket.Application.getRequestCycleListeners().add(listener).

Configuring custom RequestCycle

Extending of the new RequestCycle is still possible. To register custom RequestCycle you have to use org.apache.wicket.Application.setRequestCycleProvider(IRequestCycleProvider).

Tracking requested and response pages

The new RequestCycle implementation doesn't provide methods for getting the page for the current request but this information can be easily tracked with the following IRequestCycleProvider implementation:

PageTrackingRequestCycleListener

import org.apache.wicket.page.IManageablePage;
import org.apache.wicket.request.IRequestHandler;
import org.apache.wicket.request.component.IRequestablePage;
import org.apache.wicket.request.cycle.AbstractRequestCycleListener;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.handler.IPageRequestHandler;

public class PageTrackingRequestCycleListener extends AbstractRequestCycleListener
{

	public static final MetaDataKey<IRequestablePage> REQUESTED_PAGE = new MetaDataKey<IRequestablePage>()
	{
		private static final long serialVersionUID = 1L;
	};

	public static final MetaDataKey<IRequestablePage> RESPONSE_PAGE = new MetaDataKey<IRequestablePage>()
	{
		private static final long serialVersionUID = 1L;
	};

	private boolean isFirst = true;

	@Override
	public void onRequestHandlerScheduled(RequestCycle cycle, IRequestHandler handler)
	{
		super.onRequestHandlerScheduled(cycle, handler);

		checkForPage(cycle, handler);
	}

	@Override
	public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler)
	{
		super.onRequestHandlerResolved(cycle, handler);

		checkForPage(cycle, handler);
	}

	@Override
	public void onExceptionRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler,
		Exception exception)
	{
		super.onExceptionRequestHandlerResolved(cycle, handler, exception);

		checkForPage(cycle, handler);
	}

	private void checkForPage(RequestCycle cycle, IRequestHandler handler)
	{
		if (handler instanceof IPageRequestHandler)
		{
			IPageRequestHandler pageRequestHandler = (IPageRequestHandler)handler;
			IRequestablePage page = pageRequestHandler.getPage();
			if (isFirst)
			{
				cycle.setMetaData(REQUESTED_PAGE, page);
				cycle.setMetaData(RESPONSE_PAGE, page);
				isFirst = false;
			}
			else
			{
				cycle.setMetaData(RESPONSE_PAGE, page);
			}
		}
	}
}

To get the requested page you can do: RequestCycle.get().getMetaData(PageTrackingRequestCycleListener.REQUESTED_PAGE).

During processing of the request another IRequestHandler can be scheduled (e.g. with org.apache.wicket.request.cycle.RequestCycle.setResponsePage(Class<? extends IRequestablePage>, PageParameters) method, or directly with org.apache.wicket.request.cycle.RequestCycle.scheduleRequestHandlerAfterCurrent(IRequestHandler)).For each of these a notification will be send to all registered IRequestCycleListeners and they can track the page that is going to be rendered (the response page).

Exception handling

Whenever an exception occurs org.apache.wicket.request.cycle.IRequestCycleListener.onException(RequestCycle, Exception) will be called for all registered IRequestCycleListeners. The first one that knows how to handle this kind of exception can return IRequestHandler implementation that will be used to proceed the processing of the request. If none of the IRequestCycleListeners can handle it (i.e. all of them return null) then Wicket will use the configured org.apache.wicket.request.IExceptionMapper. The default one will show the configured error pages (internal error, page expired, access denied, etc.).

  • No labels