0 Replies Latest reply on Aug 26, 2010 7:54 AM by kosala2000

    <rich:fileUpload> is not reading createTempFile=false in web.xml <filter>

    kosala2000

      Hi All,

       

      I'm rather new to richfaces and I want to use <rich:fileUpload> in a JSF page of a project created in JBoss Seam. As I read in the articles, we can control the way uploaded files are accessible to server side, by setting the createTempFiles=true/false in web.xml. What I want is get the uploaded file as a file in memory (ram), so that I can manipulate it in my listener. Here is the way I've done it.

       

      upload page (xhtml) fragment

      =====================

       

                <h:panelGrid columns="2" columnClasses="top,top">
                  <rich:fileUpload fileUploadListener="#{imageUploadBean.listener}"
                     maxFilesQuantity="#{imageUploadBean.uploadsAvailable}" id="upload"
                     immediateUpload="#{imageUploadBean.autoUpload}" acceptedTypes="jpg, gif, png, bmp"
                     allowFlash="#{imageUploadBean.useFlash}">
                     <a4j:support event="onuploadcomplete" reRender="info" />
                  </rich:fileUpload>

                  <h:panelGroup id="info">
                     <rich:panel bodyClass="info">
                        <f:facet name="header">
                           <h:outputText value="Uploaded Files Info" />
                        </f:facet>
                        <h:outputText value="No files currently uploaded" rendered="#{imageUploadBean.size==0}" />
                        <rich:dataGrid columns="1" value="#{imageUploadBean.files}" var="file" rowKeyVar="row">
                           <rich:panel bodyClass="rich-laguna-panel-no-header">
                              <h:panelGrid columns="2">
                                 <a4j:mediaOutput element="img" mimeType="#{file.mime}"
                                    createContent="#{imageUploadBean.paint}" value="#{row}" style="width:100px; height:100px;"
                                    cacheable="false">
                                    <f:param value="#{imageUploadBean.timeStamp}" name="time" />
                                 </a4j:mediaOutput>
                                 <h:panelGrid columns="2">
                                    <h:outputText value="File Name:" />
                                    <h:outputText value="#{imageFile.name}" />
                                    <h:outputText value="File Length(bytes):" />
                                    <h:outputText value="#{imageFile.length}" />
                                 </h:panelGrid>
                              </h:panelGrid>
                           </rich:panel>
                        </rich:dataGrid>
                     </rich:panel>
                     <rich:spacer height="3" />
                     <br />
                     <a4j:commandButton action="#{imageUploadBean.clearUploadData}" reRender="info, upload"
                        value="Clear Uploaded Data" rendered="#{imageUploadBean.size>0}" />
                  </h:panelGroup>
               </h:panelGrid>

       

       

       

      Action class where the listener is defined

      ==============================

       

      @Name("imageUploadBean")
      public class ImageUploadBean {

       

         /** Logger of the system. */
         @Logger
         private Log log;

       

         /** To keep the uploaded files */
         private ArrayList<UploadedImage> files = new ArrayList<UploadedImage>();

       

         /** set the number of files that can be uploaded */
         private int uploadsAvailable = 1;

       

         /**
          * set whether image is uploaded as soon as it is selected, or wait for upload button to trigger
          * the action
          */
         private boolean autoUpload = true;

       

         /** set whether flash effects are used when ploading the image */
         private boolean useFlash = false;

       

         public ImageUploadBean() {
         }

       

         /**
          * Used to render the uploaded image in the page
          *
          * @param stream
          * @param object
          * @throws IOException
          */
         public void paint(OutputStream stream,
                           Object object) throws IOException {
            stream.write(getFiles().get((Integer) object).getData());
         }

       

         public int getSize() {
            if (getFiles().size() > 0) {
               return getFiles().size();
            }
            else {
               return 0;
            }
         }

       

         /**
          * This method is the listener for richfaces image upload component. it receives the file from the
          * user's file system as a byte[], because it is defined that way in the web.xml listener tag for
          * the component. From here it should be validated and moved to permanent storage location.
          *
          * @param event
          * @throws Exception
          */
         public void listener(UploadEvent event) throws Exception {
            UploadItem item = event.getUploadItem();
            log.info("item.getData() = " + (item.getData() != null ? "not null" : "null"));
            if (item != null && item.getData() != null) {
               UploadedImage file = new UploadedImage();
               file.setLength(item.getData().length);
               file.setName(item.getFileName());
               file.setData(item.getData());
               files.add(file);

       

               log.info("Uploaded [" + file.getName() + "]");
            }

       

            uploadsAvailable--;
         }

       

         public String clearUploadData() {
            files.clear();
            setUploadsAvailable(5);
            return null;
         }

       

         public long getTimeStamp() {
            return System.currentTimeMillis();
         }

       

         public ArrayList<UploadedImage> getFiles() {
            return files;
         }

       

         public void setFiles(ArrayList<UploadedImage> files) {
            this.files = files;
         }

       

         public int getUploadsAvailable() {
            return uploadsAvailable;
         }

       

         public void setUploadsAvailable(int uploadsAvailable) {
            this.uploadsAvailable = uploadsAvailable;
         }

       

         public boolean isAutoUpload() {
            return autoUpload;
         }

       

         public void setAutoUpload(boolean autoUpload) {
            this.autoUpload = autoUpload;
         }

       

         public boolean isUseFlash() {
            return useFlash;
         }

       

         public void setUseFlash(boolean useFlash) {
            this.useFlash = useFlash;
         }
      }

       

       

       

      Class to hold the details of the uploaded file

      ================================

       

      @Name("imageFile")
      public class UploadedImage {
         private String Name;

       

         private String mime;

       

         private long length;

       

         private byte[] data;

       

         public byte[] getData() {
            return data;
         }

       

         public void setData(byte[] data) {
            this.data = data;
         }

       

         public String getName() {
            return Name;
         }

       

         public void setName(String name) {
            Name = name;
            int extDot = name.lastIndexOf('.');
            if (extDot > 0) {
               String extension = name.substring(extDot + 1);
               if ("bmp".equals(extension)) {
                  mime = "image/bmp";
               }
               else if ("jpg".equals(extension)) {
                  mime = "image/jpeg";
               }
               else if ("gif".equals(extension)) {
                  mime = "image/gif";
               }
               else if ("png".equals(extension)) {
                  mime = "image/png";
               }
               else {
                  mime = "image/unknown";
               }
            }
         }

       

         public long getLength() {
            return length;
         }

       

         public void setLength(long length) {
            this.length = length;
         }

       

         public String getMime() {
            return mime;
         }
      }

       

       

       

      web.xml

      =======

       

      <?xml version="1.0" ?>
      <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

       

         <!-- RichFaces -->

       

         <context-param>
            <param-name>org.richfaces.SKIN</param-name>
            <param-value>glassX</param-value>
         </context-param>

       

         <!-- Suppress spurious stylesheets -->

       

         <context-param>
            <param-name>org.richfaces.CONTROL_SKINNING</param-name>
            <param-value>disable</param-value>
         </context-param>

       

         <context-param>
            <param-name>org.richfaces.CONTROL_SKINNING_CLASSES</param-name>
            <param-value>disable</param-value>
         </context-param>

       

         <!-- Change load strategy to DEFAULT to disable sending scripts/styles as packs -->

       

         <context-param>
            <param-name>org.richfaces.LoadStyleStrategy</param-name>
            <param-value>ALL</param-value>
         </context-param>

       

         <context-param>
            <param-name>org.richfaces.LoadScriptStrategy</param-name>
            <param-value>ALL</param-value>
         </context-param>

       

         <!-- Seam -->

       

         <listener>
            <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
         </listener>

       

         <filter>
            <filter-name>Seam Filter</filter-name>
            <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
         </filter>

       

         <filter-mapping>
            <filter-name>Seam Filter</filter-name>
            <url-pattern>/*</url-pattern>
         </filter-mapping>

       

         <filter>
            <display-name>RichFaces Filter</display-name>
            <filter-name>Ajax4jsf</filter-name>
            <filter-class>org.ajax4jsf.Filter</filter-class>
            <init-param>
               <param-name>createTempFile</param-name>
               <param-value>false</param-value>
            </init-param>
            <init-param>
               <param-name>maxRequestSize</param-name>
               <param-value>1000000</param-value>
            </init-param>
         </filter>

       

         <filter-mapping>
            <filter-name>Ajax4jsf</filter-name>
            <servlet-name>Faces Servlet</servlet-name>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>FORWARD</dispatcher>
            <dispatcher>INCLUDE</dispatcher>
         </filter-mapping>

       

         <servlet>
            <servlet-name>Seam Resource Servlet</servlet-name>
            <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
         </servlet>

       

         <servlet-mapping>
            <servlet-name>Seam Resource Servlet</servlet-name>
            <url-pattern>/seam/resource/*</url-pattern>
         </servlet-mapping>

       

         <!-- Facelets development mode (disable in production) -->

       

         <context-param>
            <param-name>facelets.DEVELOPMENT</param-name>
            <param-value>@debug@</param-value>
         </context-param>

       

         <!-- JSF -->

       

         <context-param>
            <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
            <param-value>.xhtml</param-value>
         </context-param>

       

         <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
         </servlet>

       

         <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.page</url-pattern>
         </servlet-mapping>

       

         <security-constraint>
            <display-name>Restrict raw XHTML Documents</display-name>
            <web-resource-collection>
               <web-resource-name>XHTML</web-resource-name>
               <url-pattern>*.xhtml</url-pattern>
            </web-resource-collection>
            <auth-constraint />
         </security-constraint>

       

         <!-- uncomment <ejb-local-ref> entries when deploying to GlassFish and (optionally) JBoss AS 5 -->
         <!-- <ejb-local-ref> <ejb-ref-name>showbitz/AuthenticatorBean/local</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type>
            <local-home/> <local>com.productiongenie.showbitz.action.Authenticator</local> </ejb-local-ref> -->

       

         <!-- Add entries for each EJB session bean which is also a Seam component (not required on JBoss AS) -->

       

         <persistence-unit-ref>
            <persistence-unit-ref-name>showbitz/pu</persistence-unit-ref-name>
            <persistence-unit-name>../showbitz.jar#showbitz</persistence-unit-name>
            <!-- The relative reference doesn't work on GlassFish. Instead, set the <persistence-unit-name> to "showbitz", package
               persistence.xml in the WAR, and add a <jar-file> element in persistence.xml with value "../../showbitz.jar". <persistence-unit-name>showbitz</persistence-unit-name> -->
         </persistence-unit-ref>

       

      </web-app>

       

       

       

      My problem is, when I set the "createTempFile" to false in the web.xml filter, uploaded file still gets to the /temp folder (I'm using Ubuntu 10.04 desktop OS). And I cannot use the item.getData() because it is null. I think somehow my web.xml file's filter setting is not read by the system and it still uses the default setting which is to store the temp file. I've spent more than a day to solve this but couldn't come up with anything. Please if anyone can tell me what am i doing wrong here, I'd be very grateful. Thank you all in advance.

       

       

      Regards,

       

      Kosala.