Search code examples
grailsgrails-2.2grails-filters

Grails Cannot create a session after the response has been committed?


I added a grails filter to redirect urls with www to non www url. After this change many errors have been triggered of this nature.

The change was to add a filter as shown below

class DomainFilters {
    def filters = {
        wwwCheck(uri:'/**') {
            before = {
                if (request.getServerName().toLowerCase().startsWith("www.")) {
                    int port = request.getServerPort();

                    if (request.getScheme().equalsIgnoreCase("http") && port == 80) {
                        port = -1;
                    }

                    URL redirectURL = new URL(request.getScheme(), request.getServerName().replaceFirst("www.",""), port, request.forwardURI);

                    response.setStatus(301)
                    response.setHeader("Location", redirectURL.toString())
                    response.flushBuffer()
                }
            }
        }
    }
}

The point where error occurs is

session['products-ids'] = sizes.join(",")

and the error is as follows

ERROR 2021-07-15 13:48:48,478 [ajp-bio-8109-exec-720] errors.GrailsExceptionResolver: IllegalStateException occurred when processing request: [GET] /payment/productsPurchaseSummary/976634
Cannot create a session after the response has been committed. Stacktrace follows:
java.lang.IllegalStateException: Cannot create a session after the response has been committed
    at registration.PaymentController.productsPurchaseSummary(PaymentController.groovy:621)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

I think the cause is linked to the added filter but i am not sure what is causing the cannot create session error. I appreciate any insights. Thanks!

UPDATE:

In the logs at the point of error is this request which was 301 redirected because of the filter above.

185.191.171.18 - - [15/Jul/2021:13:48:48 -0600] "GET /payment/productsPurchaseSummary/976634 HTTP/1.1" 301 950
185.191.171.5 - - [15/Jul/2021:13:48:49 -0600] "GET /payment/productsPurchaseSummary/976634 HTTP/1.1" 200 6341

Solution

  • It seems to have fixed it after adding return false. I think if there is no return then it will continue to execute the controller action.

    class DomainFilters {
        def filters = {
            wwwCheck(uri:'/**') {
                before = {
                    if (request.getServerName().toLowerCase().startsWith("www.")) {
    
    
                            int port = request.getServerPort();
    
                            if (request.getScheme().equalsIgnoreCase("http") && port == 80) {
                                port = -1;
                            }
    
                            URL redirectURL = new URL(request.getScheme(), request.getServerName().replaceFirst("www.", ""), port, request.forwardURI);
    
                            response.setStatus(301)
                            response.setHeader("Location", redirectURL.toString())
                            response.flushBuffer()
                            return false
    
    
    
                    }
                }
            }
        }
    }