Cannot forward after response has been committed

Splash Forums Rewrite Users Cannot forward after response has been committed

This topic contains 10 replies, has 4 voices, and was last updated by  Christian Kaltepoth 16 minutes ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #26998

    AndreaCatania
    Participant

    Hi all, I’ve this configuration:

    ConfigurationBuilder config = ConfigurationBuilder.begin();
    
    config.addRule()
             .when(Direction.isInbound().and(Path.matches("{excluded}{url}")))
             .perform(Forward.to("/123/123/test?url={url}" ) )
             .where("excluded").matches("(?!/wres|/123/123/test)")
             .where("url").matches(".*");

    The servlet is:

    @WebServlet(name="/123/123/test", urlPatterns={"/123/123/test"})
    public class Cppanel_ContentsGenerator extends HttpServlet{
    	
        public void doGet( HttpServletRequest request, HttpServletResponse response ){
    
            try{
                response.sendError( HttpServletResponse.SC_NOT_FOUND );
            }catch( IOException e2 ){
                e2.printStackTrace();
            }
       }
    }

    When I reach the servlet from a rewrited request i get this error:
    java.lang.IllegalStateException: Cannot forward after response has been committed

    What is the problem?

    Thanks in advance.

    #26999

    AndreaCatania
    Participant

    This is full stack trace

    [2015-03-17T09:12:10.260+0100] [glassfish 4.1] [WARNING] [] [javax.enterprise.web.core] [tid: _ThreadID=35 _ThreadName=http-listener-1(3)] [timeMillis: 1426579930260] [levelValue: 900] [[
      Servlet.service() for servlet default threw exception
    java.lang.IllegalStateException: Cannot forward after response has been committed
    	at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:448)
    	at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
    	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
    	at org.ocpsoft.rewrite.servlet.impl.HttpRewriteResultHandler.handleResult(HttpRewriteResultHandler.java:41)
    	at org.ocpsoft.rewrite.servlet.RewriteFilter.rewrite(RewriteFilter.java:268)
    	at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:188)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    	at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:873)
    	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    	at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:695)
    	at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:626)
    	at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:500)
    	at org.apache.catalina.core.StandardHostValve.dispatchToErrorPage(StandardHostValve.java:699)
    	at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:380)
    	at org.apache.catalina.core.StandardHostValve.postInvoke(StandardHostValve.java:234)
    	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:417)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    	at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    	at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    	at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    	at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    	at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    	at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    	at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    	at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    	at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    	at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    	at java.lang.Thread.run(Thread.java:745)
    ]]
    
    [2015-03-17T09:12:10.261+0100] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=35 _ThreadName=http-listener-1(3)] [timeMillis: 1426579930261] [levelValue: 900] [[
      org.apache.catalina.core.StandardHostValve@734202a8: Exception Processing ErrorPage[errorCode=404, location=/wres/errorpage/404.html]
    java.lang.IllegalStateException: Cannot forward after response has been committed
    	at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:448)
    	at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
    	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
    	at org.ocpsoft.rewrite.servlet.impl.HttpRewriteResultHandler.handleResult(HttpRewriteResultHandler.java:41)
    	at org.ocpsoft.rewrite.servlet.RewriteFilter.rewrite(RewriteFilter.java:268)
    	at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:188)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    	at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:873)
    	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    	at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:695)
    	at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:626)
    	at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:500)
    	at org.apache.catalina.core.StandardHostValve.dispatchToErrorPage(StandardHostValve.java:699)
    	at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:380)
    	at org.apache.catalina.core.StandardHostValve.postInvoke(StandardHostValve.java:234)
    	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:417)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    	at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    	at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    	at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    	at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    	at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    	at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    	at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    	at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    	at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    	at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    	at java.lang.Thread.run(Thread.java:745)
    ]]
    • This reply was modified 1 year, 8 months ago by  AndreaCatania.
    #27001

    AndreaCatania
    Participant

    Problem found, it’s in the web.xml where I’ve setted the error pages and doesn’t work properly on GlassFish

    <error-page>
    		<error-code>404</error-code>
    		<location>/wres/errorpage/404.html</location>
    	</error-page>
    	<error-page>
    		<error-code>500</error-code>
    		<location>/wres/errorpage/500.html</location>
    	</error-page>
    #27003

    I was trying to figure out what might cause this problem last night, but I have to admit that I don’t really understand what happened. Could you perhaps upload a sample application that reproduces this so that I can see what is going on?

    Is this a problem with Rewrite that is incompatible with GlassFish? Or was there an error encountered during servlet processing (after rewrite forwarded, then rewrite triggered again on the error dispatch.)

    I think this configuration might solve your problem (and allow you to keep your web.xml error config.)

    ConfigurationBuilder config = ConfigurationBuilder.begin();
    
    config.addRule()
             .when(Direction.isInbound().and(Path.matches("{excluded}{url}"))
                .andNot(DispatchType.isError())
             )
             .perform(Forward.to("/123/123/test?url={url}" ) )
             .where("excluded").matches("(?!/wres|/123/123/test)")
             .where("url").matches(".*");

    Note the exclusion of the ERROR dispatch type:

    .andNot(DispatchType.isError())

    Let me know if this works. Thanks!

    #27004

    AndreaCatania
    Participant

    I’ve try your config but doesn’t work. Here a simple project that reproduces the error: https://github.com/AndreaCatania/RewTest
    Thanks for your support.

    #27005

    I see another problem with your rule. Currently it will match on ALL urls, and “excluded” will always be empty. The reason is this:

    .where("excluded").matches("(?!/wres|/123/123/test)")

    The problem here is that the above regex is always going to be 0-width, (because the lookahead is zero width, and the regular expression has no character groups.) It should probably be:

    .where("excluded").matches("(?!/wres|/123/123/test).*")

    But this leads to another problem, where I think that will cause “excluded” to match more than what you want. In reality, I think you are trying to improperly use two parameters to match one thing. That won’t work. You should just do this all with the parameter for “url”.

    ConfigurationBuilder config = ConfigurationBuilder.begin();
    
    config.addRule()
             .when(Direction.isInbound().and(Path.matches("{url}"))
                .andNot(DispatchType.isError())
             )
             .perform(Forward.to("/123/123/test?url={url}" ) )
             .where("url").matches("(?!/wres|/123/123/test).*");

    I am about to try this myself.

    #27006

    I’m a glassfish noob and eclipse isn’t letting me actually set up a GlassFish server, so I may not get to try this soon. Will need to revisit.

    #27007

    AndreaCatania
    Participant

    Ok, thanks.. Let me know if you found something.

    #27513

    rewriteme
    Participant

    I too get this error for my app in Glassfish 4.1.1 but the same app deployed in Wildfly 10.1.0 is ok.

    Looked at ApplicationDispatcher.java:448 but could not pinpoint, the cause as mentioned in here

    catching java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:448) ~[web-core.jar:?]
    at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428) ~[web-core.jar:?]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378) ~[web-core.jar:?]
    at org.ocpsoft.rewrite.servlet.impl.HttpRewriteResultHandler.handleResult(HttpRewriteResultHandler.java:42) ~[rewrite-servlet-3.4.1.Final.jar:?]
    at org.ocpsoft.rewrite.servlet.RewriteFilter.rewrite(RewriteFilter.java:297) ~[rewrite-servlet-3.4.1.Final.jar:?]
    at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:198) ~[rewrite-servlet-3.4.1.Final.jar:?]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) ~[web-core.jar:?]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) ~[web-core.jar:?]

    • This reply was modified 5 hours, 6 minutes ago by  rewriteme.
    • This reply was modified 5 hours, 6 minutes ago by  rewriteme.
    #27516

    rewriteme
    Participant

    I had written AuthFilter which was executing chain.doFilter(request, response); this was causing the exception reported earlier. I removed the use of the Auth Filter and now the same error occurs again, but it is not directly related to my code.

    [2016-11-15T21:37:41.659-0500] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=30 _ThreadName=http-listener-1(1)] [timeMillis: 1479263861659] [levelValue: 900] [[
    org.apache.catalina.core.StandardHostValve@47bab060: Exception Processing ErrorPage[exceptionType=java.lang.Throwable, location=/WEB-INF/errorpages/error.xhtml]
    java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:448)
    at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
    at org.ocpsoft.rewrite.servlet.impl.HttpRewriteResultHandler.handleResult(HttpRewriteResultHandler.java:42)
    at org.ocpsoft.rewrite.servlet.RewriteFilter.rewrite(RewriteFilter.java:297)
    at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:198)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:873)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:695)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:626)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:500)
    at org.apache.catalina.core.StandardHostValve.dispatchToErrorPage(StandardHostValve.java:699)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:306)
    at org.apache.catalina.core.StandardHostValve.postInvoke(StandardHostValve.java:232)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:418)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)
    ]]

    #27517

    This exception basically tells you that Rewrite tries to forward the request to some internal URL (because one of your rules matched) but isn’t able to do so because someone has committed the response before. The response gets committed for example if someone calls HttpServletResponse.getOutputStream() and starts writing to the client.

    These issues are very difficult to debug. You could try to enable DEBUG/TRACE logging level for Rewrite to find out more. Or you could set a break point in Rewrite’s HttpRewriteResultHandler which performs the forward and check the properties of HttpServletResponse. You can also try to use method break points for relevant methods on HttpServletResponse to find the place where the response gets committed.

    I hope this helps

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

Comments are closed.