9 Replies Latest reply on Jun 3, 2010 8:57 AM by Shervin Asgari

    download problem Servlet response already use stream, Writer not possible

    wagied davids Newbie
      Hi,

      I want to download a file, but am encountering the problem below.
      From reading the forum, it seems to be a previously reported problem about a double call
      to the servlet.

      My set-up:
      JBoss 4.2
      SEAM -2.1.2
      Richfaces: richfaces 3.3.1.GA






      "`    <rich:column>
                                      <f:facet name="header">Action</f:facet>
                                      <s:link id="download" value="Download"
                                          action="#{experimentDownloadAction.downloadExperiment}">
                                          <f:param name="experimentId" value="#{experiment.id}" />
                                      </s:link>
                                  </rich:column>`



      `@Name("experimentDownloadAction")
      public class ExperimentDownloadAction
              {
                      @Logger
                      private Log log;
                      @In
                      StatusMessages statusMessages;

                      @In(value = "#{facesContext.externalContext}")
                      private ExternalContext extCtx;

                      @In(value = "#{facesContext}")
                      private FacesContext facesContext;

                      @In
                      private EntityManager entityManager;

                      @RequestParameter
                      private Long experimentId;

                      public void downloadExperiment()
                              {
                                      // implement your business logic here
                                      log.info("Downloading Experiment ...");
                                      statusMessages.add("download ...");

                                      Experiment experiment = this.entityManager.find(Experiment.class, experimentId);

                                      if (experiment != null)
                                              {

                                                      HttpServletResponse response = (HttpServletResponse) extCtx.getResponse();
                                                      response.setContentType(experiment.getContentType());
                                                      response.setHeader("Content-disposition", "attachment; filename=\"" + experiment.getName() + "\"");
                                             
                                                      try
                                                              {
                                                                      ServletOutputStream os = response.getOutputStream();
                                                                      os.write(experiment.getFileData());
                                                                      os.flush();
                                                                      os.close();
                                                                      facesContext.responseComplete();
                                                              }
                                                      catch (Exception e)
                                                              {
                                                                      log.error("\nFailure : " + e.toString() + "\n");
                                                              }
                                              }
                                      return;
                              }

              }`






      "13:19:25,192 ERROR [STDERR] Oct 15, 2009 1:19:25 PM com.sun.facelets.FaceletViewHandler handleRenderException
      SEVERE: Error Rendering View[/experimentInfo.xhtml]
      java.lang.IllegalStateException: Servlet response already use stream, Writer not possible
              at org.ajax4jsf.webapp.FilterServletResponseWrapper.getWriter(FilterServletResponseWrapper.java:207)
              at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
              at com.sun.facelets.FaceletViewHandler.createResponseWriter(FaceletViewHandler.java:414)
              at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:571)
              at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
              at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
              at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
              at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
              at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
              at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
              at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
              at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
              at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
              at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
              at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
              at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
              at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
              at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
              at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
              at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
              at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
              at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
              at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
              at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
              at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
              at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
              at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
              at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
              at java.lang.Thread.run(Thread.java:636)""



        • 1. Re: download problem Servlet response already use stream, Writer not possible
          damendra Newbie

          Hi,


          I have tried the same code and get the following response:



          java.util.MissingResourceException: Can't find resource for bundle org.ajax4jsf.Messages$EmptyResourceBundle, key CREATE_STREAM_INFO



          Has anyone seen this and have a fix?


          Regards


          • 2. Re: download problem Servlet response already use stream, Writer not possible
            wagied davids Newbie

            Hi,


            More specifics - these are my richfaces libraries:


            richfaces-api-3.3.1.GA.jar
            richfaces-impl-3.3.1.GA.jar
            richfaces-ui-3.3.1.GA.jar



            • 3. Re: download problem Servlet response already use stream, Writer not possible
              Nikolay Elenkov Master

              Try getting the FacesContext directly:


              FacesContext fc = FacesContext.getCurrentInstance();
              HttpServletResponse response = (HttpServletResponse) fc
                              .getExternalContext().getResponse();
              
              OutputStream out = response.getOutputStream();
              out.write(file);
              out.flush();
              out.close();
              
              FacesContext.getCurrentInstance().responseComplete();
              


              1 of 1 people found this helpful
              • 4. Re: download problem Servlet response already use stream, Writer not possible
                wagied davids Newbie
                Hi Nikolay,

                Same problem.


                Adjusted version:
                ------------------

                @Name("experimentDownloadAction")
                public class ExperimentDownloadAction
                     {
                          @Logger
                          private Log log;

                          @In
                          private EntityManager entityManager;

                          @RequestParameter
                          private Long experimentId;

                          /**
                           *
                           */
                          public void downloadExperiment()
                               {
                                    Experiment experiment = this.entityManager.find(Experiment.class, experimentId);
                                    if (experiment != null)
                                         {
                                              try
                                                   {
                                                        FacesContext facesContext= FacesContext.getCurrentInstance();
                                                        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
                                                        response.setContentType(experiment.getContentType());
                                                        response.setHeader("Content-disposition", "attachment; filename=\"" + experiment.getName() + "\"");

                                                        OutputStream os = response.getOutputStream();
                               
                                                        os.write(experiment.getFileData());
                                                        os.flush();
                                                        os.close();

                                                        FacesContext.getCurrentInstance().responseComplete();
                                                   }
                                              catch (Exception e)
                                                   {
                                                        log.error("\nFailure : " + e.toString() + "\n");
                                                   }
                                         }
                                    return;
                               }
                     }


                java.lang.IllegalStateException: Servlet response already use stream, Writer not possible
                     at org.ajax4jsf.webapp.FilterServletResponseWrapper.getWriter(FilterServletResponseWrapper.java:207)
                     at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
                     at com.sun.facelets.FaceletViewHandler.createResponseWriter(FaceletViewHandler.java:414)
                     at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:571)
                     at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
                     at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
                     at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
                     at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                     at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
                     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                     at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                     at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
                     at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                     at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
                     at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                     at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                     at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                     at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                     at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                     at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                     at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                     at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
                     at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
                     at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
                     at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                     at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
                     at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                     at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
                     at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                     at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                     at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
                     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
                     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
                     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                     at java.lang.Thread.run(Thread.java:636)
                • 5. Re: download problem Servlet response already use stream, Writer not possible
                  Nikolay Elenkov Master

                  According to RF-4415, this might be related to a JSF bug. Try upgrading your JSF libs.
                  They say it works with, 1.2_09 and, IIRC, the one with JBoss 4.2 is quite old. You could try with JBoss 4.2.3, it should have 1.2_09, again, IIRC.


                  • 6. Re: download problem Servlet response already use stream, Writer not possible
                    wagied davids Newbie

                    Hi Nikolay,


                    Thanks for your reply.


                    However, the original example I based the download action code on .. ...works perfectly,
                    using my existing set-up.


                    http://seamframework.org/Documentation/HowToUploadAndDownloadFilesInSeam




                    I did, also try to close the servlet stream before hand ( Iknow dont try this), and the problem disappeared..
                    ..EXCEPT with one obvious  mistake....the file downloaded ...but it copied the current html page.


                    Seems to me like an unclosed iostream in FacesServlet.


                    -W


                    • 7. Re: download problem Servlet response already use stream, Writer not possible
                      wagied davids Newbie
                      OK...tracked down the problem -  rich:fileupload.

                      Replaced the rich:fileupload, with s:fileupload, downloading works fine.
                      Most likely an unclosed filestream in richfaces fileupload facility.

                      s:fileupload
                      -----------------
                      <s:decorate id="fileUploadDecoration" template="layout/edit.xhtml">
                                          <ui:define name="label">Attachment</ui:define>
                                          <s:fileUpload id="file"
                                                    data="#{experimentHome.instance.fileData}"
                                                    contentType="#{experimentHome.instance.contentType}"
                                                    fileName="#{experimentHome.instance.filename}"
                                                    fileSize="#{experimentHome.instance.fileSize}" />
                      </s:decorate>

                      the previous rich:fileupload
                      -------------------------------
                             <s:decorate id="experimentFileUploadFieldEdit" template="layout/edit.xhtml">
                                          <ui:define name="label">File Upload</ui:define>
                                          <rich:fileUpload fileUploadListener="#{experimentHome.uploadListener}"
                                                           maxFilesQuantity="1"
                                                           immediateUpload="true"
                                                           disableDefault="false"
                                                           listWidth="300px" listHeight="100px" >
                                              <f:facet name="label">
                                                  <h:outputText value="{_KB}KB from {KB}KB uploaded --- {mm}:{ss}" />
                                              </f:facet>
                                   </rich:fileUpload>                  
                      </s:decorate>
                      • 8. Re: download problem Servlet response already use stream, Writer not possible
                        Mita Bhushan Newbie

                        I am getting the same error. Did u find a solution.
                        Please help!

                        • 9. Re: download problem Servlet response already use stream, Writer not possible
                          Shervin Asgari Master

                          Hmmm we upgraded from JBoss 4 to JBoss 5 and been using that for a while, then suddenly we couldn't download files. I get this same exception:


                          Can't find resource for bundle org.ajax4jsf.Messages$EmptyResourceBundle, key CREATESTREAMINFO


                          Don't understand what it means.