0 Replies Latest reply on May 28, 2010 10:09 AM by Adam Kowalski

    a4j:keepAlive does not work with a4j:mediaOutput - why?

    Adam Kowalski Newbie

      Hi All,

       

      I've got a problem with simple combination of a4j:keepAlive and a4j:mediaOutput tags.

      My target is to make a simple page that allows user to upload JPG file and display it just after upload has finished.

      The simplest way should be keep image content in bean and then use it to render a4j:mediaOutput.

      However I cannot force my bean to be kept alive and as a temporary workaround I have to read content form disk when I render it.

      If I remove a4j:mediaOutput from page everything works (a4j:keepAlive is applied to bean).

      What am I doing wrong? Any help appreciated.

       

       

      Best regards

      Melmack.

       

      My environment:

      JSF - 1.2 (Mojarra)

      RichFaces - 3.3.3

      Facelets - 1.1.15

      Apache Tomcat 6.0.18

      JDK 1.6.0.20

       

      Code snippet - page:

       

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:rich="http://richfaces.org/rich"
          xmlns:a4j="http://richfaces.org/a4j">
        <head>
            <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
            <title>Galeria v1.0</title>
            <link rel="stylesheet" type="text/css" href="res/style.css"/>
        </head>
        <body>
            <a4j:keepAlive beanName="uploadBean"/>
            <f:view>
                <a4j:form id="form">
                    <rich:fileUpload
                        fileUploadListener="#{uploadBean.listener}"
                        maxFilesQuantity="1"
                        id="upload"
                        immediateUpload="true"
                        addControlLabel="Dodaj"
                        cancelEntryControlLabel="Anuluj"
                        clearAllControlLabel="Wyczysc wszystko"
                        clearControlLabel="Wyczysc"
                        doneLabel="Gotowe"
                        progressLabel="Trwa wysylanie..."
                        sizeErrorLabel="Blad rozmiaru pliku!"
                        stopControlLabel="Zatrzymaj wszystkie"
                        stopEntryControlLabel="Zatrzymaj"
                        transferErrorLabel="Blad transmisji!"
                        uploadControlLabel="Wyslij">
                        <a4j:support event="onuploadcomplete" reRender="form"/>
                    </rich:fileUpload>
                    <a4j:mediaOutput
                        id="picture"
                        element="img"
                        cacheable="false"
                        session="true"
                        createContent="#{uploadBean.paint}"
                        value="#{uploadBean.data}"
                        mimeType="image/jpeg">
                    </a4j:mediaOutput>
                </a4j:form>
            </f:view>
        </body>
      </html>
      

       

      Code snippet - bean:

       

       

      package galeria.web;
      
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.io.Serializable;
      import java.util.HashMap;
      
      import javax.imageio.ImageIO;
      
      import org.richfaces.event.UploadEvent;
      import org.richfaces.model.UploadItem;
      
      public class UploadBean implements Serializable {
      
          private static final long serialVersionUID = 1L;
      
          private static int counter = 0;
          private static HashMap<Integer, String> map = new HashMap<Integer, String>();
      
          private int imageId = -1;
      
          public UploadBean() {
              System.out.println("UploadBean()");
          }
      
          public void listener(UploadEvent event) throws Exception {
              try {
                  File f = new File(".");
                  System.out.println("pwd -> " + f.getCanonicalPath());
                  UploadItem item = event.getUploadItem();
                  String name = "uploads" + File.separatorChar + item.getFileName();
                  if (item.isTempFile()) {
                      byte[] buf = new byte[1024];
                      int read = -1;
                      System.out.println("listener() -> tempFile");
                      File tempFile = item.getFile();
                      FileInputStream fis = new FileInputStream(tempFile);
                      FileOutputStream fos = new FileOutputStream(name);
                      while ((read = fis.read(buf)) != -1) {
                          fos.write(buf, 0, read);
                      }
                      fis.close();
                      fos.close();
                  } else {
                      System.out.println("listener() -> bytes");
                      byte[] data = item.getData();
                      FileOutputStream fos = new FileOutputStream(name);
                      fos.write(data);
                      fos.close();
                  }
                  imageId = counter++;
                  map.put(imageId, name);
              } catch (Exception e) {
                  e.printStackTrace();
                  throw e;
              }
      
          }
      
          public Integer getData() {
              System.out.println("getData -> " + imageId);
              return imageId;
          }
      
          public void paint(OutputStream out, Object data) throws IOException {
              System.out.println("getData -> " + imageId);
              if (data instanceof Integer) {
                  Integer i = (Integer)data;
                  String name = map.get(i);
                  if (name != null) {
                      FileInputStream fis = new FileInputStream(name);
                      BufferedImage img = ImageIO.read(fis);
                      ImageIO.write(img, "jpeg", out);
                      fis.close();
                  }
              }
          }
      }