1 Reply Latest reply on Dec 3, 2007 10:26 AM by mschmidke

    Download binary files

    mschmidke

      Currently, I am searching the forums how to serve downloadable binary data in a Seam application.

      I found out so far that there are at least two possibilities:

      1. By action as described in http://wiki.jboss.org/wiki/Wiki.jsp?page=SeamResourceLoader

      2. By Seam's built in resource mechanism based on AbstractResource class.

      Which way should I prefer? I think (2) is much more elegant, but I need to access my Persistence Context which does not seem to be possible (at least I do not understand enough of the BypassInterceptors-beginRequest-endRequest-stuff I copied from CaptchaImage :-) )


      Marcus.

        • 1. Re: Download binary files
          mschmidke

          Hummm.

          I removed the things I do not understand (as mentioned, beginRequest and endRequest). With this, I was able to remove BypassInterceptors, and without BypassInterceptors I can have injected my hibernate session.

          At the first sight, it works perfectly. Of course, I don't know if I am breaking something in the interns of seam or if I am creating memory leaks or whatever.

          @Startup
          @Scope(ScopeType.APPLICATION)
          @Name("dateidownload")
          public class Dateidownload extends AbstractResource {
           @In
           Session session;
          
           @Override
           public void getResource(HttpServletRequest request,
           HttpServletResponse response) throws ServletException, IOException {
           // ServletLifecycle.beginRequest(request);
           try {
           String id = request.getQueryString();
           Anhang_Dokument ad = (Anhang_Dokument) session.get(
           Anhang_Dokument.class, Long.parseLong(id));
           if (ad == null)
           throw new ObjectNotFoundException(id);
           response.setHeader("Cache-Control", "no-store");
           response.setHeader("Pragma", "no-cache");
           response.setDateHeader("Expires", 0);
           response.setContentType(ad.getDatei().getMimeType());
           response.getOutputStream().write(ad.getDatei().getDaten());
           response.getOutputStream().flush();
           response.getOutputStream().close();
           } catch (Exception e) {
           response.sendError(HttpServletResponse.SC_NOT_FOUND);
           return;
           } finally {
           // ServletLifecycle.endRequest(request);
           }
          }
          
           @Override
           public String getResourcePath() {
           return "/download";
           }
          
          }
          


          There are other things in CaptchaImage I don't understand, for example the meaning of @Startup and @Install together. But as told before, I simply deleted things I did not understand, and for the moment, it works ...


          Marcus.