Search in sources :

Example 1 with ReplaceHandlerException

use of org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException in project wicket by apache.

the class RequestCycle method execute.

/**
 * Execute a request handler and notify registered {@link IRequestCycleListener}s.
 *
 * @param handler
 */
private void execute(IRequestHandler handler) {
    Args.notNull(handler, "handler");
    while (handler != null) {
        try {
            listeners.onRequestHandlerResolved(this, handler);
            IRequestHandler next = requestHandlerExecutor.execute(handler);
            listeners.onRequestHandlerExecuted(this, handler);
            handler = next;
        } catch (RuntimeException e) {
            ReplaceHandlerException replacer = Exceptions.findCause(e, ReplaceHandlerException.class);
            if (replacer == null) {
                throw e;
            }
            if (replacer.getRemoveScheduled()) {
                requestHandlerExecutor.schedule(null);
            }
            handler = replacer.getReplacementRequestHandler();
        }
    }
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IRequestHandler(org.apache.wicket.request.IRequestHandler) ReplaceHandlerException(org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException)

Example 2 with ReplaceHandlerException

use of org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException in project wicket by apache.

the class RequestCycleListenerTest method basicOperations.

/**
 * @throws Exception
 */
@Test
public void basicOperations() throws Exception {
    IncrementingListener incrementingListener = new IncrementingListener();
    Application.get().getRequestCycleListeners().add(incrementingListener);
    RequestCycle cycle = newRequestCycle((RuntimeException) null);
    incrementingListener.assertValues(0, 0, 0, 0, 0, 0);
    assertValues(0, 0, 0);
    cycle.processRequestAndDetach();
    // 0 exceptions mapped
    incrementingListener.assertValues(1, 1, 1, 1, 0, 1);
    // 0 exceptions mapped
    assertValues(0, 1, 1);
    // TEST WITH TWO LISTENERS
    cycle = newRequestCycle((RuntimeException) null);
    cycle.getListeners().add(incrementingListener);
    cycle.processRequestAndDetach();
    // 0 exceptions mapped, all other 2 due to two listeners
    incrementingListener.assertValues(2, 2, 2, 2, 0, 2);
    // 0 exceptions mapped
    assertValues(0, 1, 1);
    // TEST WITH TWO LISTENERS AND AN EXCEPTION DURING RESPONSE
    cycle = newRequestCycle(new RuntimeException("testing purposes only"));
    cycle.getListeners().add(incrementingListener);
    cycle.processRequestAndDetach();
    // 0 executed
    incrementingListener.assertValues(2, 2, 2, 0, 2, 2);
    // 1 exception mapped, 0 responded
    assertValues(1, 0, 1);
    // TEST A REPLACE EXCEPTION DURING RESPONSE
    IRequestHandler replacement = new IRequestHandler() {

        @Override
        public void respond(IRequestCycle requestCycle) {
            responses++;
        }

        @Override
        public void detach(IRequestCycle requestCycle) {
            detaches++;
        }
    };
    cycle = newRequestCycle(new ReplaceHandlerException(replacement, true));
    cycle.scheduleRequestHandlerAfterCurrent(new IRequestHandler() {

        @Override
        public void respond(IRequestCycle requestCycle) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void detach(IRequestCycle requestCycle) {
            throw new UnsupportedOperationException();
        }
    });
    cycle.processRequestAndDetach();
    // 2 resolved, 1 executed, 0 exception mapped
    incrementingListener.assertValues(1, 1, 2, 1, 0, 1);
    // 0 exception mapped, 1 responded, 2 detached
    assertValues(0, 1, 2);
    // TEST A REPLACE EXCEPTION DURING RESPONSE
    cycle = newRequestCycle(new ReplaceHandlerException(replacement, false));
    cycle.scheduleRequestHandlerAfterCurrent(new IRequestHandler() {

        @Override
        public void respond(IRequestCycle requestCycle) {
            responses++;
        }

        @Override
        public void detach(IRequestCycle requestCycle) {
            detaches++;
        }
    });
    cycle.processRequestAndDetach();
    // 2 resolved, 2 executed, 0 exception mapped
    incrementingListener.assertValues(1, 1, 3, 2, 0, 1);
    // 0 exception mapped, 2 responded, 3 detached
    assertValues(0, 2, 3);
}
Also used : IRequestHandler(org.apache.wicket.request.IRequestHandler) ReplaceHandlerException(org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException) IRequestCycle(org.apache.wicket.request.IRequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) Test(org.junit.Test)

Aggregations

IRequestHandler (org.apache.wicket.request.IRequestHandler)2 ReplaceHandlerException (org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 IRequestCycle (org.apache.wicket.request.IRequestCycle)1 Test (org.junit.Test)1