This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
This is sort of continuation from this thread. In my application I have a Spring controller which serves files to requests. In that controller if there is any exception I send the user to an error page (using ModelAndView of Spring). Now while writing the response JSP page, the response "hangs" at Deflater.deflateBytes method.
Edit: There is a filter applied to each request to Zip/compress the response. The Deflater.deflateBytes is called when the filter tries to compress the response. It works fine everywhere except here.
This is what my code looks like
As far as I could figure out, whenever there is an exception (which most probably is "Connection reset by peer: socket write error"), when I return an error page to the response, the thread hangs at
The problem seems to be with thread safety i.e. two thread writing to the same output stream. My doubt is that while the code is streaming the file to user, if user cancels it/closes the browser window, then it throws some IOException and after that the code tries to write a JSP error page to the same stream (which is already closed) which causes it to go nuts.
Is there a way to avoid this problem or maybe make the deflateBytes timeout somehow (highly unlikely but still )? This problem is not very easy to reproduce so its very hard to apply a fix for it
I have set the response content type (its not visible in this code). Anyway for now I've excluded this particular path from the GZip filter to solve this problem. Even though that's not a proper solution but it works for now...