4 Replies Latest reply on Feb 23, 2013 7:24 AM by mbasovni

    How to download dynamicly created file?

    mbasovni

      Hello,

       

      I have a problem with downloading dynamicly created files. I found examples, how to do it for classical JSF application (not for portletbridge).

       

      http://stackoverflow.com/questions/10451095/downloading-a-csv-file-using-jsf

      http://stackoverflow.com/questions/3428039/download-a-file-with-jsf

       

      But gatein throws:

      Caused by: javax.faces.FacesException: java.lang.ClassCastException: org.gatein.pc.portlet.impl.jsr168.api.ActionResponseImpl cannot be cast to javax.servlet.http.HttpServletResponse

       

      That is apparently due to ActionResponse... i need ResourceResponse, yes?

       

      I tried something like this:

       

      FacesContext fc = FacesContext.getCurrentInstance();

      Object response = fc.getExternalContext().getResponse();

       

      if (response instanceof ResourceResponse) {}

       

      But I do not know how to generate ResourceRequest through RequestURL and e.g .

       

      <h:commandLink

                              action="#{calendarController.exportCalendar(event.calendar)}"

                              value="#{event.calendar.name}" />

       

      Can somebody help me how can I download dynamicly generated file dor example *.xml? Is somewhere a simple example, how to do it? In porltet documentation I did not found what I need.

       

      Thank you!

       

      Martin

        • 1. Re: How to download dynamicly created file?
          kenfinni

          I'm confused as to whether you mean download from the browser to server or download a file generated by server to the user in the browser?

           

          The stackoverflow links you provided refer to the latter, so I will assume that scenario.

           

          You should be able to add an f:ajax inside the h:commandLink and that will convert the portal url to be a ResourceRequest instead of an ActionRequest.

          1 of 1 people found this helpful
          • 2. Re: How to download dynamicly created file?
            mbasovni

            Thank you for your answer, Ken! Now it WORKS but I did it a little bit differently.

             

            <a4j:mediaOutput element="a" cacheable="false" createContent="#{calendarController.exportCalendar}"

                                        value="#{event.calendar}" mimeType="text/Calendar">

                                        <h:outputText value="#{event.calendar.name}" />

            </a4j:mediaOutput>

             

            public void exportCalendar(OutputStream os, Object calendar) {

             

                 ... // handle calendar  to  outputStr

             

                 FacesContext fc = FacesContext.getCurrentInstance();

                 Object response = fc.getExternalContext().getResponse();

                 ...

                 ResourceResponse rresp = (ResourceResponse) response;

                 rresp.reset();

                 rresp.setContentType("text/Calendar");

                 rresp.setProperty("Content-disposition", "attachment; filename=\"" + filename + "\"");

                 ...

                

                 try {

                       os.write(outputStr.getBytes(Charset.forName("UTF-8")));

                      os.flush();

                 } finally {

                      os.close();    

                 }

                ...

            }

             

            I did not use command fc.responseComplete();, is it ok?

             

            2nd possible solution:

             

            When I tried the way similar to http://stackoverflow.com/questions/10451095/downloading-a-csv-file-using-jsf again with "<h:commandLink ....><f:ajax /></h:commandLink>",

            I have now ResourceResponse but  file was not returned to user. My code was very similar to the code in this URL, but instead of HTTPServletResponse I used object of type ResourceResponse. Do you know why it does not work? There is no exception after running my code...

             

            But the first solution works! I am just interested in why the second solution does not work...

             

            Thx!

            • 3. Re: How to download dynamicly created file?
              kenfinni

              Glad to hear it works.

               

              I would think you do need to call responseComplete() on FacesContext, otherwise the remainder of the JSF lifecycle will execute when you don't want it to.

               

              As for why the 2nd solution doesn't work, I'm not certain what the issue might be, as it should work.

              • 4. Re: How to download dynamicly created file?
                mbasovni

                Ok, thank you very much, Ken!