Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struts2 - java.lang.IllegalStateException occurs intermittently while action calls result stream to download PDF file

I have an action called GetFile which directly opens a file download box to open PDF.

Below is the struts.xml configuration and the action class for the same. I am using result type as stream to achieve this. I can see that the pdf download box opens all the times and i am also able to download the file.

But I find that there were some requests last night that had fired the action and it has produced the below error.

    org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:570)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
        at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:159)
        at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
        at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:374)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:278)
        at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at deshaw.irweb.web.interceptor.AuthInterceptor.intercept(AuthInterceptor.java:60)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
        at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510)
        at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
        at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:294)
        at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:183)
        at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:169)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at org.apache.catalina.connector.Response.getWriter(Response.java:636)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:205)
        at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
        at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
        at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
        at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:182)
        at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:123)
        at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
        at org.apache.jsp.jsp.ServerError_jsp._jspService(ServerError_jsp.java:157)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
        ... 38 more     

Can someone tell me what could have caused the problem ? I am not able to reproduce the problem currently. I read somewhere that it could be due to closing the download dialog, but it was not the case even after i tried from multiple browsers. Also checked getOutputStream() has already been called for this response , but could not relate directly to my problem as there is no redirection to jsp as the struts2 action directly leads to result stream.

struts.xml

<action name="GetFile" class="Class" method="DownloadFile">
            <result name="success" type="stream">
            <param name="contentType">contentType</param>
            <param name="contentDisposition">fileName</param>
            <param name="bufferSize">1024</param>
            <param name="inputName">inputStream</param> 
            </result>
</action>

Action Class

public String DownloadFile ()
{
        // create the object variable pdfdoc which is a custom object.
        // You can assume that the pdfdoc is properly constructed 
        // and getFileContent does what it is required to do, so
        // that the final pdf is generated.
        ...
        ...
        inputStream = new ByteArrayInputStream(pdfdoc.getFileContent());
        contentType = pdfdoc.getContentType();
        fileName = pdfdoc.getFileName();
        contentDisposition = doc.getContentDisposition();
        bufferSize = 1024;
        return "success";

}
like image 463
Paweł Hajdan Avatar asked Jun 01 '12 13:06

Paweł Hajdan


1 Answers

I had a similar problem when using Internet Explorer and fixed it by adding this in the response header:

response.setHeader("Expires","0");
response.setHeader("Pragma","cache");
response.setHeader("Cache-Control","private");

Maybe the requests that you see in your log where generated by Internet Explorer.

Have a look here and here!

Update: To print the browser version in your log you can do something like this:

System.out.println(request.getHeader("User-Agent"));
like image 100
tibtof Avatar answered Nov 01 '22 22:11

tibtof