views:

908

answers:

3

I use liferay 5.2 with jsf-portlet.

From the page I want to press a button to generate one PDF. In managedbean i build pdf and I want to show it in response.

In a ByteArrayOutputStream named outputStream i have my pdf built with JasperReport.

I write:

   PortletResponse portletResponse = (PortletResponse)externalCtx.getResponse(); 
   HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(portletResponse);

   ServletOutputStream out = httpResponse.getOutputStream();
   String filename="Pdf" + System.currentTimeMillis()+".pdf";
   httpResponse.reset();

   httpResponse.setContentType("application/pdf");
   httpResponse.setHeader("Content-Disposition", "attachment; filename=\""+ filename + "\"");
   httpResponse.setContentLength(outputStream.size());

   outputStream.writeTo(out);

   out.flush();
   out.close();

I do not see anything output! In jboss log i read: IllegaStateException....

What is wrong?

LOG

        11:03:19,716 INFO  [STDOUT] 11:03:19,716 ERROR [IncludeTag] Current URL /web/organo-di-governo/datawarehouse?p_p_id=1_WAR_Portlet_Datawarehouse_INSTANCE_D7s7&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-2&p_p_col_count=1&_1_WAR_Portlet_Datawarehouse_INSTANCE_D7s7_com.sun.faces.portlet.VIEW_ID=%2Fview.xhtml&_1_WAR_Portlet_Datawarehouse_INSTANCE_D7s7_com.sun.faces.portlet.NAME_SPACE=_1_WAR_Portlet_Datawarehouse_INSTANCE_D7s7_ generates exception: null
                11:03:19,717 INFO  [STDOUT] 11:03:19,717 ERROR [IncludeTag] java.lang.IllegalStateException
                    at com.liferay.portal.servlet.filters.strip.StripResponse.getWriter(StripResponse.java:85)
                    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.JspWriterImpl.write(JspWriterImpl.java:326)
                    at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:342)
                    at org.apache.jasper.runtime.JspWriterImpl.print(JspWriterImpl.java:468)
                    at com.liferay.taglib.util.ThemeUtil.includeVM(ThemeUtil.java:208)
                    at com.liferay.taglib.util.ThemeUtil.include(ThemeUtil.java:68)
                    at com.liferay.taglib.util.IncludeTag.doEndTag(IncludeTag.java:59)
                    at org.apache.jsp.html.common.themes.portal_jsp._jspx_meth_liferay_002dtheme_005finclude_005f1(portal_jsp.java:816)
                    at org.apache.jsp.html.common.themes.portal_jsp._jspx_meth_c_005fotherwise_005f0(portal_jsp.java:788)
                    at org.apache.jsp.html.common.themes.portal_jsp._jspService(portal_jsp.java:724)
                    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
                    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
                    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
                    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
                    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
                    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
                    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                11:03:19,718 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
                java.lang.IllegalStateException



        11:03:19,719 ERROR [[Main Servlet]] Servlet.service() for servlet Main Servlet threw exception
        java.lang.IllegalStateException
            at com.liferay.portal.servlet.filters.strip.StripResponse.getWriter(StripResponse.java:85)
            at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
            at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)



        11:03:19,722 INFO  [STDOUT] 11:03:19,720 ERROR [OpenSSOFilter] org.apache.jasper.JasperException: java.lang.IllegalStateException
        org.apache.jasper.JasperException: java.lang.IllegalStateException
            at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:409)
            at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
            at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)




        11:03:19,722 INFO  [STDOUT] n.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)


        Caused by: java.lang.IllegalStateException
            at com.liferay.portal.servlet.filters.strip.StripResponse.getWriter(StripResponse.java:85)
            at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
            at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
A: 

The only appropriate way to do this is by using the "RESOURCE" phase of the JSR-286 spec. Currently in your code you are in "RENDER" phase which is forcefully only allows "text/html" encoding.

In plain JSP, the tag would look something like:

<portlet:resourceURL id="/super-cool.pdf" />

Then in your portlet class you implement the serveResource(ResourceRequest req, ResourceResponse res) method and read call the req.getResourceId() method of the ResourceRequest and behave invoke your business logic (create your PDF) and subsequently write it to the output stream.

In JSF, I'm not entirely sure if this is even supported, which means that the only way would be to delegate the task to a servlet call. Look in JSF to see if JSR-286 Resource handling is implemented.

Ray
thanks Ray,but I can't believe that ...from JSF-portlet in Liferay there is not the possibility to take a pdf ..It's the same problem if I wanted to make a download ... any one zip file or a text ...there must be a solution!
Stefano
A: 

Hi Stefano Did you solve your problem? I have the same problem and i don't want to create a servlet. Regards, Angelo

Angelo
No! sorry... at the end I used a servlet... Bad solution but only solution...bye
Stefano
A: 

Hi stefano, can you please help me out on how to use the servlet for this problem?

phani