Wicket
  1. Wicket
  2. WICKET-4433

Exception (Header was already written to response!) when setting response page in IRequestCycleListener

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.5.4
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      We have an IRequestCycleListener implementation that's basically:

      @Override
      public void onBeginRequest(RequestCycle cycle) {
      if (<condition>))

      { cycle.setResponsePage(SpecificPage.class); }

      }

      This results in an exception when the condition is true, and the response page is set:

      java.lang.IllegalStateException: Header was already written to response!
      at org.apache.wicket.protocol.http.HeaderBufferingWebResponse.checkHeader(HeaderBufferingWebResponse.java:64)
      at org.apache.wicket.protocol.http.HeaderBufferingWebResponse.setDateHeader(HeaderBufferingWebResponse.java:134)
      at org.apache.wicket.protocol.http.BufferedWebResponse$SetDateHeaderAction.invoke(BufferedWebResponse.java:310)
      at org.apache.wicket.protocol.http.BufferedWebResponse.writeTo(BufferedWebResponse.java:580)
      at org.apache.wicket.request.handler.render.WebPageRenderer.respond(WebPageRenderer.java:185)
      at org.apache.wicket.request.handler.RenderPageRequestHandler.respond(RenderPageRequestHandler.java:167)
      at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:781)
      at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
      at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:304)
      at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:313)

      1. myproject.zip
        27 kB
        Neil Curzon
      2. myproject.zip
        27 kB
        Neil Curzon

        Activity

        Hide
        Neil Curzon added a comment -

        Simple quickstart reproducing the issue. Just hit the main page. localhost:8080/

        Show
        Neil Curzon added a comment - Simple quickstart reproducing the issue. Just hit the main page. localhost:8080/
        Hide
        Martin Grigorov added a comment -

        I just answered you in the mailing list.
        Use RestartResponseException instead.

        Show
        Martin Grigorov added a comment - I just answered you in the mailing list. Use RestartResponseException instead.
        Hide
        Neil Curzon added a comment -

        When throwing a RestartResponseException, the result is the orange exception page with "Unexpected Runtime Exception":

        Last cause: null
        Stacktrace
        Root cause:
        org.apache.wicket.RestartResponseException

        (also replied on mailing list). Not sure if this is a different bug or related.

        Show
        Neil Curzon added a comment - When throwing a RestartResponseException, the result is the orange exception page with "Unexpected Runtime Exception": Last cause: null Stacktrace Root cause: org.apache.wicket.RestartResponseException (also replied on mailing list). Not sure if this is a different bug or related.
        Hide
        Martin Grigorov added a comment -

        Yes, it seems onBeginRequest() is not covered to handle reset handler exceptions ..
        Here is a working code that does the same as what the exception would do:

        public void onBeginRequest(RequestCycle cycle) {
        IPageProvider provider = new PageProvider(PageTwo.class);
        RenderPageRequestHandler requestHandler = new RenderPageRequestHandler(provider) {
        @Override
        public void respond(IRequestCycle requestCycle)

        { requestCycle.getResponse().reset(); super.respond(requestCycle); }

        };

        cycle.scheduleRequestHandlerAfterCurrent(requestHandler);
        }

        I'm not sure whether onBeginRequest() should be improved.

        Show
        Martin Grigorov added a comment - Yes, it seems onBeginRequest() is not covered to handle reset handler exceptions .. Here is a working code that does the same as what the exception would do: public void onBeginRequest(RequestCycle cycle) { IPageProvider provider = new PageProvider(PageTwo.class); RenderPageRequestHandler requestHandler = new RenderPageRequestHandler(provider) { @Override public void respond(IRequestCycle requestCycle) { requestCycle.getResponse().reset(); super.respond(requestCycle); } }; cycle.scheduleRequestHandlerAfterCurrent(requestHandler); } I'm not sure whether onBeginRequest() should be improved.
        Hide
        Neil Curzon added a comment -

        When I implement your solution in my attached quickstart, I do indeed get redirected to PageTwo, but the entirety of the page is written out twice.

        When I implement it in my application, nothing happens (the constructor for the other page doesn't fire and I just end up on the page I started at).

        Show
        Neil Curzon added a comment - When I implement your solution in my attached quickstart, I do indeed get redirected to PageTwo, but the entirety of the page is written out twice. When I implement it in my application, nothing happens (the constructor for the other page doesn't fire and I just end up on the page I started at).
        Hide
        Jan Riehn added a comment -

        Hello,

        Martins solution does not really fix the problem. The first request is scheduled and executed, might be that the RestartResponseException should avoid this.
        The following snippet fits better:

        public class MyRequestCycleListener extends AbstractRequestCycleListener {
        @Override
        public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler) {
        if (handler instanceof IPageRequestHandler) {
        if (condition)

        { throw new RedirectToUrlException("http://wicket.apache.org/"); }

        }
        }
        }

        Best regards,

        Jan

        Show
        Jan Riehn added a comment - Hello, Martins solution does not really fix the problem. The first request is scheduled and executed, might be that the RestartResponseException should avoid this. The following snippet fits better: public class MyRequestCycleListener extends AbstractRequestCycleListener { @Override public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler) { if (handler instanceof IPageRequestHandler) { if (condition) { throw new RedirectToUrlException("http://wicket.apache.org/"); } } } } Best regards, Jan

          People

          • Assignee:
            Unassigned
            Reporter:
            Neil Curzon
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:

              Development