14 Replies Latest reply on Jun 26, 2012 4:42 AM by Sarah Shalaby

    How to avoid this IlligalStateException

    Srinivasan Thoyyeti Newbie

      Error: java.lang.IllegalStateException: Servlet response already use Writer, OutputStream not possible


      xhtml code:


       <body>   
           <object data="#{docManager.showPDF()}" type="application/pdf" />   
       </body>




      manager code:


      public void showPDF() throws IOException
      {
         HttpServletResponse response = (HttpServletResponse)facesContext.getExternalContext().getResponse();
         response.setContentType(docMimeType);
         response.setHeader("Content-Disposition", "attachment; filename=" + doc.getDocumentNumber()+".pdf");
      
         OutputStream out = response.getOutputStream();  // Error on executing this line
      
      }



      how best we can deal with scenario like this?





        • 1. Re: How to avoid this IlligalStateException
          Leo van den berg Master

          Hi,


          maybe something like this works better..
                         


          
          @In Manager manager;
          
          ....
          
          DocumentData data = new ByteArrayDocumentData("report",
                                   new DocumentData.DocumentType("pdf", "application/pdf"),
                                   pdf);
          DocumentStore documentStore = DocumentStore.instance();
          String docId = documentStore.newId();
          documentStore.saveData(docId, data);
          
          String documentUrl = documentStore.preferredUrlForContent(data
                                   .getBaseName(), data.getDocumentType().getExtension(),
                                   docId);
          facesContext.getExternalContext().redirect(
                                   manager.encodeConversationId(documentUrl, null));
          // Don't process the reposne
          facesContext.responseComplete();






          Leo


          P.S: This is explained in the doc AND in the SIA book.





          • 2. Re: How to avoid this IlligalStateException
            Srinivasan Thoyyeti Newbie

            Hi,


            Thanks for the ray of hope. My scenario is I have PDF in remote place. I wan to access it and display it in xhtml.
            With above mentioned Seam API, How to access the document through xhtml View?


            Regards,
            Srini

            • 3. Re: How to avoid this IlligalStateException
              Leo van den berg Master

              Hi,


              You don't have to do anything especial. Put the peice of code in a bean (as the mentioned method) and Seam will take care of the rest for you.


              Leo

              • 4. Re: How to avoid this IlligalStateException
                Srinivasan Thoyyeti Newbie

                Yes, I understood what you have meant.


                Now I am able to see the desired link






                String documentUrl = documentStore.preferredUrlForContent(data.getBaseName(), data.getDocumentType().getExtension(),
                                                                                        docId);
                
                
                I have got the addressbar like this: context/docstore/*document.seam*?docId=1&cid=2
                
                
                
                but when I debugged data.getBaseName() = "report" and data.getDocumentType().getExtension() = "pdf"
                So I am expecting adress bar to be like this:  context/docstore/*report.pdf*?docId=1&cid=2
                
                Is it what documentStore.preferredUrlForContent(...) is supposed to do?




                • 5. Re: How to avoid this IlligalStateException
                  Leo van den berg Master

                  Hi,


                  the piece of code is an idea how you can send a binary (PDF) to the browser, it's part of our application which creates PDF's on the fly. Off-course you need additional work to get it working in your situation. If your PDF in on another server and avilable as a URL, you could include it in your page. PrimeFaces has a neat component to create an overlay (LightBox) which in its IFrame mode enables you to show an external resource.


                  Leo


                  P.S. Please read the docs carefully and look in this forum.

                  • 6. Re: How to avoid this IlligalStateException
                    Srinivasan Thoyyeti Newbie

                    yes, I read it now. documentStore.preferredUrlForContent() is not working as intended.


                    That's what I mean to ask in the above post.

                    • 7. Re: How to avoid this IlligalStateException
                      Srinivasan Thoyyeti Newbie

                      Hello Leo,


                      I have been through DocumentSource source file and understood how the method documentStore.preferredUrlForContent() behaves.
                      After that I configured every thing required to have customDocumentName.pdf.


                      So I am able embed the PDF object into xhtml.
                      Thank you for the valuable help.


                      Regards,
                      Srini


                      • 8. Re: How to avoid this IlligalStateException
                        Sarah Shalaby Newbie

                        Hi Srini

                         

                        Can you post how u managed to fix it?

                         

                        Regards

                        Sarah

                        • 9. Re: How to avoid this IlligalStateException
                          Srinivasan Thoyyeti Newbie

                          Step #1.

                          <body>   
                          <object data=''#{docManager.showPDF()}'' type=''application/pdf'' />
                           </body>

                           

                           

                          Step #2.

                          //The following Path includes context path as the URL accessed in a new tab.
                          // ''/docStore/'' routes request to Document Store Servlet 
                          String documentUrl = documentStore.preferredUrlForContent(request.getContextPath()+''/docStore/''+data.getBaseName(), data.getDocumentType().getExtension(),docId);
                          facesContext.getExternalContext().redirect(
                             manager.encodeConversationId(documentUrl,null));
                          // Don't process the response
                          facesContext.responseComplete();

                           

                          Step #3.

                          <servlet> 
                            <servlet-name>Document Store Servlet</servlet-name> 
                            <servlet-class>org.jboss.seam.document.DocumentStoreServlet</servlet-class> 
                           </servlet> 
                           <servlet-mapping> 
                            <servlet-name>Document Store Servlet</servlet-name> 
                            <url-pattern>/docStore/*</url-pattern> 
                           </servlet-mapping>

                           

                          HTH,

                          Regards,

                          Srinivasan Thoyyeti.

                          • 10. Re: How to avoid this IlligalStateException
                            Sarah Shalaby Newbie

                            Thanks. So this gets the data from the remote site?

                            I read the code for Documentstore and I see that it stores it in a hashmap

                            So we have to store it first like Leo said?

                            Do we have to do these steps?

                             


                            DocumentData data = new ByteArrayDocumentData("report",
                                                     new DocumentData.DocumentType("pdf", "application/pdf"),
                                                     pdf);
                            DocumentStore documentStore = DocumentStore.instance();
                            String docId = documentStore.newId();
                            documentStore.saveData(docId, data);

                             

                            becuse i have the pdf in a remote location just like you, I am trying to get it. I can''t access it this way.

                             

                            Thanks

                            Sarah

                            • 11. Re: How to avoid this IlligalStateException
                              Srinivasan Thoyyeti Newbie

                              Here is the full code in my application for exporting the a document:


                              HttpServletRequest request = (HttpServletRequest)facesContext.getExternalContext().getRequest();
                              
                              //Using SEAM Document API 
                              InputStream in = fileShareManager.getDocAsStream(doc.getId(),DOC_ADDR_QUERY);
                              String docMimeType = EXT_MIME_MAP.getProperty(fileShareManager.getFileExtension());
                                 
                                 DocumentData data = new ByteArrayDocumentData(doc.getDocumentNumber()+''_''+doc.getVersion(),
                              new DocumentData.DocumentType(fileShareManager.getFileExtension(), docMimeType),
                              getByteArrayFromStream(in)
                              );
                                 
                                 DocumentStore documentStore = DocumentStore.instance();
                                 documentStore.setUseExtensions(true);
                                 String docId = documentStore.newId();
                                 documentStore.saveData( docId, data);
                                 
                                 //The following Path includes context path as the URL accessed in a new tab.
                                 // ''/docStore/'' routes request to Document Store Servlet 
                                 String documentUrl = documentStore.preferredUrlForContent(request.getContextPath()+''/docStore/''+data.getBaseName(), data.getDocumentType().getExtension(),docId);
                                 facesContext.getExternalContext().redirect(
                                  manager.encodeConversationId(documentUrl,null));
                                 // Don't process the response
                                 facesContext.responseComplete();

                               

                              Regards,

                              Srinivasan Thoyyeti.

                              • 13. Re: How to avoid this IlligalStateException
                                Srinivasan Thoyyeti Newbie

                                Step 1: in source xhtml where you invoke the "show PDF" call

                                notice that we request for showPDF.xhtml

                                 

                                <s:link view=''/docs/showPdf.xhtml'' target=''_blank'' rendered=''#{_doc.documentType != 'Set of documents (SD)' and _doc.valid}''>
                                      <f:param name=''pdfTitle'' value=''#{_doc.documentNumber}_#{_doc.version}''/>
                                      <f:param name=''pdfId'' value=''#{_doc.id}''/>
                                      <h:graphicImage value=''/img/bosch/adobe.gif'' title=''#{messages.showPDF}'' style=''width : 15px; height : 15px;''/>
                                </s:link>
                                

                                 

                                 

                                Step 2: showPdf.xhtml  : The object tag generates http request for the dynamic PDF stream.

                                 <head>
                                   <title>#{docViewManager.pdfTitle}</title>
                                   <link rel=''shortcut icon'' href=''favicon.ico'' type=''image/x-icon''/>
                                </head>
                                <body>   
                                <object data=''#{docManager.showPDF()}'' type=''application/pdf'' />
                                  
                                </body>
                                </ui:composition>
                                
                                

                                 

                                Step 3. showPDF functionality I have posted above which makes redirective call to load the stream.

                                 

                                HTH

                                Regards,

                                Srinivasan Thoyyeti