3 Replies Latest reply on May 6, 2008 3:26 PM by viggo.navarsete

    How to stream a PDF back in the response of a Richfaces port

    viggo.navarsete

      Anyone who knows how to stream a PDF document back to the user from a backing bean using the new jboss portlet bridge?

      Code? yes:)

      public void generateReport(ActionEvent e) {
      
       System.out.println("Generate a report");
      
       // read file
       System.out.println("Loading report file...");
       ClassLoader classLoader = this.getClass().getClassLoader();
       InputStream inputStream = classLoader.getResourceAsStream("ReceivedFromAndSentTo.pdf");
      
       // get access to the response
       FacesContext fc = FacesContext.getCurrentInstance();
       RenderResponse renderResponse = (RenderResponse) fc.getExternalContext().getResponse(); // Caused by: java.lang.ClassCastException: org.jboss.portlet.JBossActionResponse
       renderResponse.setContentType("application/pdf");
      
       // write file to response
       try {
       OutputStream os = renderResponse.getPortletOutputStream();
       int nextChar;
       while ((nextChar = inputStream.read()) != -1) {
       os.write(nextChar);
       }
       inputStream.close();
       } catch (IOException ex) {
       Logger.getLogger(ReportsBean.class.getName()).log(Level.SEVERE, null, ex);
       }
       FacesContext faces = FacesContext.getCurrentInstance();
       faces.responseComplete();
       }


      The problem with this code is that you're not getting back a RenderResponse from the ExternalContext, you're getting a org.jboss.portlet.JBossActionResponse. I'm not sure how to cast it to a RenderResponse as long as I don't have access to the class from a maven repo.

      Any other better/preferred way of achiving the thing I want?

      Best regards,
      Viggo