Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Improve the documentation about RequestCycle in 1.5

RequestCycle class RequestCycle has changed in has been reworked for Wicket 1.5-M1 and WebRequestCycle has been removed.

When we want to override RequestCycle's methods and write our logic codes we can not do it like 1.4.x.

eg.

Code Block
titleReqeustCycle
borderStylesolid
public MyRequestCycle extends WebRequestCycle {
    @Override
    public void onBeginRequest() {
      //...
    }
    
    @Override
    public void onEndRequest() {
        //...
    }
    
   @Override         
    public Page onRuntimeException(Page page, RuntimeException e) {
        // ...
    }
}

In this case we'll need to extend RequestCycle and Override org.apache.wicket.Application.createRequestCycle(Request, Response) to return our class.

.

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:

Code Block
titlePageTrackingRequestCycleListener
borderStylesolid


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.The current entry point is org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetachscheduleRequestHandlerAfterCurrent(IRequestHandler)).
For now we'll have to wrap this method with try/finally to simulate onBefore/onEnd.Exception handles not like 1.4.x too we should create a new ExceptionMapper class to handle thatFor 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.).