Follow a flow exception thrown while the request is mapped RequestCycle followed a ReplaceHandlerException (AbortWithHttpErrorCodeException, RestartResponseException, RedirectToUrlException and the like) only while a request handler executed. One thrown while the request was mapped, before there was a handler, went to the onException listeners and the exception mapper like any other exception, and ended on the error page or as a 500. That is how the 400 for an ajax request without a base url (WICKET-4841) got lost. ServletWebRequest#getClientUrl() throws it, and since WICKET-5071 BookmarkableMapper and PageInstanceMapper call getClientUrl() while they map the request, so for an ajax request it is first called during mapping and the client got a 500. The test added with WICKET-4841 only checks the exception getClientUrl() throws, so it kept passing. GitHub pull request #1673, which answers a malformed base url the same way, runs into the same problem. A flow exception thrown while the request is mapped now replaces the handler the same way as one thrown while a handler executes, including dropping the scheduled handler when the exception asks for it. An ajax request without a base url gets its 400 again. An application that throws a flow exception from a request mapper, or from anything a mapper calls, now has it followed: a RestartResponseException renders its page, a RedirectToUrlException redirects, an AbortWithHttpErrorCodeException answers its status code. Its onException listeners and its exception mapper no longer see these exceptions, and onRequestHandlerResolved is called for the replacement handler. Any other exception thrown while mapping is handled as before.
diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/cycle/RequestCycleListenerTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/cycle/RequestCycleListenerTest.java index 2700395..b0ddfe4 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/cycle/RequestCycleListenerTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/cycle/RequestCycleListenerTest.java
@@ -83,6 +83,12 @@ private RequestCycle newRequestCycle(final Consumer<IRequestCycle> cycleConsumer) { + return newRequestCycle(() -> {}, cycleConsumer); + } + + private RequestCycle newRequestCycle(final Runnable mapping, + final Consumer<IRequestCycle> cycleConsumer) + { final Response originalResponse = newResponse(); Request request = new MockWebRequest(Url.parse("http://wicket.apache.org")); handler = new IRequestHandler() @@ -106,6 +112,7 @@ @Override public IRequestHandler mapRequest(Request request) { + mapping.run(); return handler; } @@ -244,6 +251,86 @@ /** */ @Test + void replaceExceptionDuringMapping() + { + IncrementingListener incrementingListener = new IncrementingListener(); + Application.get().getRequestCycleListeners().add(incrementingListener); + + IRequestHandler replacement = new IRequestHandler() + { + @Override + public void respond(IRequestCycle requestCycle) + { + responses++; + } + + @Override + public void detach(IRequestCycle requestCycle) + { + detaches++; + } + }; + + RequestCycle cycle = newRequestCycle(() -> { + throw new ReplaceHandlerException(replacement, true); + }, rc -> {}); + cycle.scheduleRequestHandlerAfterCurrent(new IRequestHandler() + { + @Override + public void respond(IRequestCycle requestCycle) + { + throw new UnsupportedOperationException(); + } + + @Override + public void detach(IRequestCycle requestCycle) + { + throw new UnsupportedOperationException(); + } + }); + assertTrue(cycle.processRequestAndDetach()); + incrementingListener.assertValues(1, 1, 1, 1, 0, 1); + assertValues(0, 1, 1); + + cycle = newRequestCycle(() -> { + throw new ReplaceHandlerException(replacement, false); + }, rc -> {}); + cycle.scheduleRequestHandlerAfterCurrent(new IRequestHandler() + { + @Override + public void respond(IRequestCycle requestCycle) + { + responses++; + } + + @Override + public void detach(IRequestCycle requestCycle) + { + detaches++; + } + }); + assertTrue(cycle.processRequestAndDetach()); + incrementingListener.assertValues(1, 1, 2, 2, 0, 1); + assertValues(0, 2, 2); + } + + /** */ + @Test + void exceptionDuringMappingIsMapped() + { + IncrementingListener incrementingListener = new IncrementingListener(); + Application.get().getRequestCycleListeners().add(incrementingListener); + + RequestCycle cycle = newRequestCycle(() -> { + throw new RuntimeException("testing purposes only"); + }, rc -> {}); + assertFalse(cycle.processRequestAndDetach()); + incrementingListener.assertValues(1, 1, 0, 0, 1, 1); + assertValues(1, 0, 0); + } + + /** */ + @Test void exceptionIsHandledByRegisteredHandler() { IncrementingListener incrementingListener = new IncrementingListener();
diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java index 5aa0350..b928dd8 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
@@ -25,6 +25,8 @@ import org.apache.wicket.MarkupContainer; import org.apache.wicket.Page; +import org.apache.wicket.ajax.markup.html.AjaxLink; +import org.apache.wicket.ajax.markup.html.ajaxLink.AjaxLinkPage; import org.apache.wicket.markup.IMarkupResourceStreamProvider; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.mock.MockApplication; @@ -169,6 +171,25 @@ } } + @Test + void ajaxRequestWithoutBaseUrlAnswersBadRequest() + { + WicketTester tester = new WicketTester(); + try + { + AjaxLinkPage page = tester.startPage(AjaxLinkPage.class); + tester.getRequest().setUrl(tester.urlFor((AjaxLink<?>)page.get("ajaxLink"))); + tester.getRequest().setHeader(WebRequest.HEADER_AJAX, "true"); + tester.processRequest(); + + assertEquals(HttpServletResponse.SC_BAD_REQUEST, tester.getLastResponse().getStatus()); + } + finally + { + tester.destroy(); + } + } + /** * Tests that {@link ServletWebRequest#getClientUrl()} returns the current url + the query * string when this is not forward dispatched request. When the request is error dispatched it
diff --git a/wicket-core/src/main/java/org/apache/wicket/request/cycle/RequestCycle.java b/wicket-core/src/main/java/org/apache/wicket/request/cycle/RequestCycle.java index 8df80b8..548ee5d 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/cycle/RequestCycle.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/cycle/RequestCycle.java
@@ -186,7 +186,8 @@ } /** - * Resolves current request to a {@link IRequestHandler}. + * Resolves current request to a {@link IRequestHandler}. A {@link ReplaceHandlerException} + * thrown while resolving replaces the handler, just like one thrown while a handler executes. * * @return RequestHandler instance */ @@ -242,7 +243,15 @@ set(this); listeners.onBeginRequest(this); onBeginRequest(); - IRequestHandler handler = resolveRequestHandler(); + IRequestHandler handler; + try + { + handler = resolveRequestHandler(); + } + catch (RuntimeException e) + { + handler = replacementOrRethrow(e); + } if (handler == null) { // Did not find any suitable handler, thus not executing the request @@ -298,23 +307,28 @@ } catch (RuntimeException e) { - ReplaceHandlerException replacer = Exceptions.findCause(e, ReplaceHandlerException.class); - - if (replacer == null) - { - throw e; - } - - if (replacer.getRemoveScheduled()) - { - requestHandlerExecutor.schedule(null); - } - - handler = replacer.getReplacementRequestHandler(); + handler = replacementOrRethrow(e); } } } + private IRequestHandler replacementOrRethrow(RuntimeException e) + { + ReplaceHandlerException replacer = Exceptions.findCause(e, ReplaceHandlerException.class); + + if (replacer == null) + { + throw e; + } + + if (replacer.getRemoveScheduled()) + { + requestHandlerExecutor.schedule(null); + } + + return replacer.getReplacementRequestHandler(); + } + /** * Process the given exception. *