[gatein-commits] gatein SVN: r3727 - in portal/trunk: component/web/api/src/test/java/org/exoplatform/web and 15 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Jul 30 05:46:17 EDT 2010


Author: trong.tran
Date: 2010-07-30 05:46:15 -0400 (Fri, 30 Jul 2010)
New Revision: 3727

Added:
   portal/trunk/component/web/controller/src/main/java/conf/
   portal/trunk/component/web/controller/src/main/java/conf/portal/
   portal/trunk/component/web/controller/src/main/java/conf/portal/configuration.xml
   portal/trunk/component/web/server/src/main/java/conf/
   portal/trunk/component/web/server/src/main/java/conf/portal/
   portal/trunk/component/web/server/src/main/java/conf/portal/configuration.xml
   portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadSessionListener.java
Removed:
   portal/trunk/component/web/api/src/test/java/conf/portal/
   portal/trunk/component/web/api/src/test/java/org/exoplatform/web/command/
   portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/command/
Modified:
   portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/WebAppController.java
   portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/WebRequestHandler.java
   portal/trunk/component/web/security/src/main/java/conf/portal/configuration.xml
   portal/trunk/component/web/server/src/main/java/org/exoplatform/download/DownloadService.java
   portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadService.java
   portal/trunk/component/web/server/src/main/java/org/exoplatform/web/handler/DownloadHandler.java
   portal/trunk/component/web/server/src/main/java/org/exoplatform/web/handler/UploadHandler.java
   portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/web.xml
   portal/trunk/web/eXoResources/src/main/webapp/javascript/eXo/webui/UIUpload.js
   portal/trunk/web/portal/src/main/webapp/WEB-INF/web.xml
   portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/UIFormUploadInput.java
Log:
GTNPORTAL-1380 Upload/Download services improvements

Added: portal/trunk/component/web/controller/src/main/java/conf/portal/configuration.xml
===================================================================
--- portal/trunk/component/web/controller/src/main/java/conf/portal/configuration.xml	                        (rev 0)
+++ portal/trunk/component/web/controller/src/main/java/conf/portal/configuration.xml	2010-07-30 09:46:15 UTC (rev 3727)
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+
+    Copyright (C) 2009 eXo Platform SAS.
+    
+    This is free software; you can redistribute it and/or modify it
+    under the terms of the GNU Lesser General Public License as
+    published by the Free Software Foundation; either version 2.1 of
+    the License, or (at your option) any later version.
+    
+    This software is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+    Lesser General Public License for more details.
+    
+    You should have received a copy of the GNU Lesser General Public
+    License along with this software; if not, write to the Free
+    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+    02110-1301 USA, or see the FSF site: http://www.fsf.org.
+
+-->
+
+<configuration
+   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd http://www.exoplaform.org/xml/ns/kernel_1_0.xsd"
+   xmlns="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd">
+
+  <component>
+    <type>org.exoplatform.web.WebAppController</type>
+  </component>
+
+</configuration>

Modified: portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/WebAppController.java
===================================================================
--- portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/WebAppController.java	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/WebAppController.java	2010-07-30 09:46:15 UTC (rev 3727)
@@ -21,12 +21,10 @@
 
 import org.exoplatform.container.ExoContainer;
 import org.exoplatform.container.ExoContainerContext;
-import org.exoplatform.container.component.ComponentRequestLifecycle;
 import org.exoplatform.container.component.RequestLifeCycle;
 import org.exoplatform.services.log.ExoLogger;
 import org.exoplatform.services.log.Log;
 import org.exoplatform.web.application.Application;
-import org.exoplatform.web.command.CommandHandler;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -67,10 +65,8 @@
       applications_ = new HashMap<String, Application>();
       attributes_ = new HashMap<String, Object>();
       handlers_ = new HashMap<String, WebRequestHandler>();
-      register(new CommandHandler());
    }
 
-   @SuppressWarnings("unused")
    public Object getAttribute(String name, Object value)
    {
       return attributes_.get(name);
@@ -108,7 +104,7 @@
       for (String path : handler.getPath())
          handlers_.put(path, handler);
    }
-
+   
    public void unregister(String[] paths)
    {
       for (String path : paths)

Modified: portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/WebRequestHandler.java
===================================================================
--- portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/WebRequestHandler.java	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/WebRequestHandler.java	2010-07-30 09:46:15 UTC (rev 3727)
@@ -19,6 +19,8 @@
 
 package org.exoplatform.web;
 
+import org.exoplatform.container.component.BaseComponentPlugin;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -31,9 +33,9 @@
  * In case of portal the path is /portal but you could return your own from the getPath() method and hence the 
  * WebAppController would use your own handler
  * 
- * The execute method is to be overideen and the buisness logic should be handled here
+ * The execute method is to be overrided and the buisness logic should be handled here
  */
-abstract public class WebRequestHandler
+abstract public class WebRequestHandler extends BaseComponentPlugin
 {
 
    public void onInit(WebAppController controller) throws Exception

Modified: portal/trunk/component/web/security/src/main/java/conf/portal/configuration.xml
===================================================================
--- portal/trunk/component/web/security/src/main/java/conf/portal/configuration.xml	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/component/web/security/src/main/java/conf/portal/configuration.xml	2010-07-30 09:46:15 UTC (rev 3727)
@@ -24,32 +24,7 @@
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd http://www.exoplaform.org/xml/ns/kernel_1_0.xsd"
    xmlns="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd">
-  <component>
-    <type>org.exoplatform.upload.UploadService</type>
-    <init-params>
-      <value-param>
-        <name>upload.limit.size</name>
-        <description>Maximum size of the file to upload in MB</description>
-        <value>10</value>
-      </value-param>
-    </init-params>  
-  </component>
-  
-  <component>
-    <type>org.exoplatform.download.DownloadService</type>
-    <init-params>
-      <value-param>
-        <name>download.resource.cache.size</name>
-        <description>Maximun number of the download can be in the cache</description>
-        <value>500</value>
-      </value-param>
-    </init-params>
-  </component>
 
-  <component>
-    <type>org.exoplatform.web.WebAppController</type>
-  </component>
-
 	<component>
 		<key>org.exoplatform.web.application.javascript.JavascriptConfigService</key>
 		<type>org.exoplatform.web.application.javascript.JavascriptConfigService</type>

Added: portal/trunk/component/web/server/src/main/java/conf/portal/configuration.xml
===================================================================
--- portal/trunk/component/web/server/src/main/java/conf/portal/configuration.xml	                        (rev 0)
+++ portal/trunk/component/web/server/src/main/java/conf/portal/configuration.xml	2010-07-30 09:46:15 UTC (rev 3727)
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+   <!--
+
+      Copyright (C) 2009 eXo Platform SAS. This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
+      License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This software is
+      distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+      PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with
+      this software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF site:
+      http://www.fsf.org.
+   -->
+
+<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd http://www.exoplaform.org/xml/ns/kernel_1_0.xsd"
+   xmlns="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd">
+   <component>
+      <type>org.exoplatform.upload.UploadService</type>
+      <init-params>
+         <value-param>
+            <name>upload.limit.size</name>
+            <description>Maximum size of the file to upload in MB</description>
+            <value>10</value>
+         </value-param>
+      </init-params>
+   </component>
+
+   <component>
+      <type>org.exoplatform.download.DownloadService</type>
+      <init-params>
+         <value-param>
+            <name>download.resource.cache.size</name>
+            <description>Maximun number of the download can be in the cache</description>
+            <value>500</value>
+         </value-param>
+      </init-params>
+   </component>
+
+   <external-component-plugins>
+      <target-component>org.exoplatform.services.listener.ListenerService</target-component>
+      <component-plugin>
+         <name>org.exoplatform.web.GenericHttpListener.sessionDestroyed</name>
+         <set-method>addListener</set-method>
+         <type>org.exoplatform.upload.UploadSessionListener</type>
+      </component-plugin>
+   </external-component-plugins>
+
+   <external-component-plugins>
+      <target-component>org.exoplatform.web.WebAppController</target-component>
+      <component-plugin>
+         <name>UploadHandler</name>
+         <set-method>register</set-method>
+         <type>org.exoplatform.web.handler.UploadHandler</type>
+      </component-plugin>
+   </external-component-plugins>
+
+   <external-component-plugins>
+      <target-component>org.exoplatform.web.WebAppController</target-component>
+      <component-plugin>
+         <name>DownloadHandler</name>
+         <set-method>register</set-method>
+         <type>org.exoplatform.web.handler.DownloadHandler</type>
+      </component-plugin>
+   </external-component-plugins>
+</configuration>

Modified: portal/trunk/component/web/server/src/main/java/org/exoplatform/download/DownloadService.java
===================================================================
--- portal/trunk/component/web/server/src/main/java/org/exoplatform/download/DownloadService.java	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/component/web/server/src/main/java/org/exoplatform/download/DownloadService.java	2010-07-30 09:46:15 UTC (rev 3727)
@@ -35,11 +35,13 @@
 public class DownloadService
 {
 
-   private Cache downloadResources_;
+   private Cache downloadResources_;    
 
    private Map<String, DownloadResource> defaultResources_;
 
    private PortalContainerInfo pinfo_;
+   
+   public static final String DOWNLOAD_HANDLER_PATH = "download";
 
    public DownloadService(PortalContainerInfo pinfo, InitParams params) throws Exception
    {
@@ -82,8 +84,8 @@
 
    public String getDownloadLink(String id)
    {
-      return "/" + pinfo_.getContainerName() + "/command?"
-         + "type=org.exoplatform.web.command.handler.DownloadHandler&amp;resourceId=" + id;
+      return "/" + pinfo_.getContainerName() + "/" + DOWNLOAD_HANDLER_PATH + "?"
+         + "resourceId=" + id;
    }
 
    @SuppressWarnings("serial")

Modified: portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadService.java
===================================================================
--- portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadService.java	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadService.java	2010-07-30 09:46:15 UTC (rev 3727)
@@ -19,29 +19,29 @@
 
 package org.exoplatform.upload;
 
-import org.exoplatform.container.xml.InitParams;
-import org.exoplatform.container.xml.PortalContainerInfo;
-import org.gatein.common.logging.Logger;
-import org.gatein.common.logging.LoggerFactory;
-
 import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
 
-/**
- * Created by The eXo Platform SARL
- * Author : Tuan Nguyen
- *          tuan.nguyen at exoplatform.com
- * Dec 8, 2006  
- */
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.fileupload.ProgressListener;
+import org.apache.commons.fileupload.disk.DiskFileItem;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.container.xml.PortalContainerInfo;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+
 public class UploadService
 {
-
    /** . */
    private static final Logger log = LoggerFactory.getLogger(UploadService.class);
 
@@ -53,6 +53,8 @@
 
    private Map<String, Integer> uploadLimitsMB_ = new LinkedHashMap<String, Integer>();
 
+   public static String UPLOAD_RESOURCES_STACK = "uploadResourcesStack";
+
    public UploadService(PortalContainerInfo pinfo, InitParams params) throws Exception
    {
       String tmpDir = System.getProperty("java.io.tmpdir");
@@ -67,167 +69,129 @@
    }
 
    /**
-    * Create UploadResource for HttpServletRequest.
-    * If Upload size greater than limit upload size, do not create UploadResource
-    * @param request
-    *             the webapp's {@link javax.servlet.http.HttpServletRequest}
-    * @throws IOException 
-    *             any io exception
+    * Create UploadResource for HttpServletRequest
+    * 
+    * @param requestow
+    *           the webapp's {@link javax.servlet.http.HttpServletRequest}
+    * @throws FileUploadException
     */
-   public void createUploadResource(HttpServletRequest request) throws IOException
+   @SuppressWarnings("unchecked")
+   public void createUploadResource(HttpServletRequest request) throws FileUploadException
    {
       String uploadId = request.getParameter("uploadId");
-      // by default, use the limit set in the service
-      //int limitMB = defaultUploadLimitMB_;
-      // if the limit is set in the request (specific for this upload) then use this value instead of the default one
-      //if (uploadLimitsMB_.containsKey(uploadId)) limitMB = uploadLimitsMB_.get(uploadId).intValue() ;
-      int limitMB = uploadLimitsMB_.get(uploadId).intValue();
+      UploadResource upResource = new UploadResource(uploadId);
+      upResource.setFileName("");// Avoid NPE in UploadHandler
+      uploadResources.put(upResource.getUploadId(), upResource);
+      
+      putToStackInSession(request.getSession(true), uploadId);
 
-      UploadResource upResource = new UploadResource(uploadId);
-      RequestStreamReader reader = new RequestStreamReader(upResource);
-      int estimatedSizeMB = (request.getContentLength() / 1024) / 1024;
-      if (limitMB > 0 && estimatedSizeMB > limitMB)
-      { // a limit set to 0 means unlimited
+      double contentLength = request.getContentLength();
+      upResource.setEstimatedSize(contentLength);
+      if (isLimited(upResource, contentLength))
+      {
          upResource.setStatus(UploadResource.FAILED_STATUS);
-         //upResource.setLimitMB(limitMB);
-         uploadResources.put(uploadId, upResource);
-         log.debug("Upload cancelled because file bigger than size limit : " + estimatedSizeMB + " MB > "
-            + limitMB + " MB");
-         //    	WebuiRequestContext ctx = WebuiRequestContext.getCurrentInstance();
-         //        UIApplication uiApp = ctx.getUIApplication();
-         //        uiApp.addMessage(new ApplicationMessage("The file must be < "+limitMB+" MB.", null, ApplicationMessage.WARNING));
          return;
       }
-      // TODO : display error message, terminate upload correctly
 
-      String headerEncoding = request.getCharacterEncoding();
-      Map<String, String> headers = reader.parseHeaders(request.getInputStream(), headerEncoding);
-
-      String fileName = reader.getFileName(headers);
-      if (fileName == null)
-         fileName = uploadId;
-      fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
-
-      upResource.setFileName(fileName);
-      upResource.setMimeType(headers.get(RequestStreamReader.CONTENT_TYPE));
-      upResource.setStoreLocation(uploadLocation_ + "/" + uploadId + "." + fileName);
-      upResource.setEstimatedSize(request.getContentLength());
-
-      uploadResources.put(upResource.getUploadId(), upResource);
-
-      File fileStore = new File(upResource.getStoreLocation());
-      if (!fileStore.exists())
-         fileStore.createNewFile();
-      FileOutputStream output = new FileOutputStream(fileStore);
-      reader.readBodyData(request, output);
-
-      if (upResource.getStatus() == UploadResource.UPLOADING_STATUS)
+      ServletFileUpload servletFileUpload = makeServletFileUpload(upResource);
+      // parse request
+      List<FileItem> itemList = servletFileUpload.parseRequest(request);
+      if (itemList == null || itemList.size() != 1 || itemList.get(0).isFormField())
       {
-         upResource.setStatus(UploadResource.UPLOADED_STATUS);
+         log.debug("Please upload 1 file per request");
          return;
       }
 
-      uploadResources.remove(uploadId);
-      fileStore.delete();
-   }
+      DiskFileItem fileItem = (DiskFileItem)itemList.get(0);
+      String fileName = fileItem.getName();
+      String storeLocation = uploadLocation_ + "/" + uploadId + "." + fileName;
 
-   /**
-    * Create UploadResource for uploadId.
-    * If Upload size greater than limit upload size, do not create UploadResource.
-    * @param uploadId
-    *          the uploadId will be use to create UploadResource
-    * @param encoding
-    *           type of encode
-    * @param contentType
-    *          type of upload content
-    * @param contentLength
-    *          size of upload content
-    * @param inputStream
-    *          java.io.InputStream
-    * @throws Exception
-    */
-   public void createUploadResource(String uploadId, String encoding, String contentType, double contentLength,
-      InputStream inputStream) throws Exception
-   {
-      UploadResource upResource = new UploadResource(uploadId);
-      RequestStreamReader reader = new RequestStreamReader(upResource);
-      int limitMB = uploadLimitsMB_.get(uploadId).intValue();
-      int estimatedSizeMB = (int)contentLength / 1024 / 1024;
-      if (limitMB > 0 && estimatedSizeMB > limitMB)
-      { // a limit set to 0 means unlimited
-         upResource.setStatus(UploadResource.FAILED_STATUS);
-         uploadResources.put(uploadId, upResource);
-         log.debug("Upload cancelled because file bigger than size limit : " + estimatedSizeMB + " MB > "
-            + limitMB + " MB");
-         return;
-      }
-      Map<String, String> headers = reader.parseHeaders(inputStream, encoding);
+      // commons-fileupload will store the temp file with name *.tmp
+      // we need to rename it to our desired name
+      fileItem.getStoreLocation().renameTo(new File(storeLocation));
 
-      String fileName = reader.getFileName(headers);
-      if (fileName == null)
-         fileName = uploadId;
-      fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
-
       upResource.setFileName(fileName);
-      upResource.setMimeType(headers.get(RequestStreamReader.CONTENT_TYPE));
-      upResource.setStoreLocation(uploadLocation_ + "/" + uploadId + "." + fileName);
-      upResource.setEstimatedSize(contentLength);
-      uploadResources.put(upResource.getUploadId(), upResource);
-      File fileStore = new File(upResource.getStoreLocation());
-      if (!fileStore.exists())
-         fileStore.createNewFile();
-      FileOutputStream output = new FileOutputStream(fileStore);
-      reader.readBodyData(inputStream, contentType, output);
+      upResource.setMimeType(fileItem.getContentType());
+      upResource.setStoreLocation(storeLocation);
+      upResource.setStatus(UploadResource.UPLOADED_STATUS);
+   }
 
-      if (upResource.getStatus() == UploadResource.UPLOADING_STATUS)
+   @SuppressWarnings("unchecked")
+   private void putToStackInSession(HttpSession session, String uploadId)
+   {
+      Set<String> uploadResouceIds = (Set<String>)session.getAttribute(UploadService.UPLOAD_RESOURCES_STACK);
+      if (uploadResouceIds == null)
       {
-         upResource.setStatus(UploadResource.UPLOADED_STATUS);
-         return;
+         uploadResouceIds = new HashSet();
       }
-
-      uploadResources.remove(uploadId);
-      fileStore.delete();
+      uploadResouceIds.add(uploadId);
+      session.setAttribute(UploadService.UPLOAD_RESOURCES_STACK, uploadResouceIds);
    }
 
    /**
     * Get UploadResource by uploadId
+    * 
     * @param uploadId
-    *          uploadId of UploadResource
+    *           uploadId of UploadResource
     * @return org.exoplatform.upload.UploadResource of uploadId
     */
    public UploadResource getUploadResource(String uploadId)
-   {//throws Exception 
-      UploadResource upResource = uploadResources.get(uploadId);
-      return upResource;
+   {
+      return uploadResources.get(uploadId);
    }
 
    /**
-    * Remove UploadResource by uploadId, Delete temp file of UploadResource.
-    * If uploadId is null or UploadResource is null or Store Location of UploadResource is null, do nothing
+    * Clean up temporary files that are uploaded in the Session but not removed yet
+    * 
+    * @param session
+    */
+   public void cleanUp(HttpSession session)
+   {
+      log.debug("Cleaning up uploaded files for temporariness");
+      Set<String> uploadIds = (Set<String>)session.getAttribute(UploadService.UPLOAD_RESOURCES_STACK);
+      if (uploadIds != null)
+      {
+         for (String id : uploadIds)
+         {
+            removeUploadResource(id);
+            uploadLimitsMB_.remove(id);
+         }
+      }
+   }
+
+   /**
+    * Remove UploadResource by uploadId, Delete temp file of UploadResource. If
+    * uploadId is null or UploadResource is null, do nothing
+    * 
     * @param uploadId
-    *          uploadId of UploadResource will be removed
+    *           uploadId of UploadResource will be removed
     */
-   public void removeUpload(String uploadId)
+   public void removeUploadResource(String uploadId)
    {
       if (uploadId == null)
          return;
       UploadResource upResource = uploadResources.get(uploadId);
-      if (upResource == null)
-         return;
-      if (upResource.getStoreLocation() == null)
-         return;
-      File file = new File(upResource.getStoreLocation());
-      file.delete();
-      uploadResources.remove(uploadId);
-      //uploadLimitsMB_.remove(uploadId);
+      if (upResource != null)
+      {
+         uploadResources.remove(uploadId);
+
+         if (upResource.getStoreLocation() != null)
+         {
+            File file = new File(upResource.getStoreLocation());
+            file.delete();
+         }
+      }
+
+      // uploadLimitsMB_.remove(uploadId);
    }
 
    /**
-    * Registry upload limit size for uploadLimitsMB_.
-    * If limitMB is null, defaultUploadLimitMB_ will be registried
+    * Registry upload limit size for uploadLimitsMB_. If limitMB is null,
+    * defaultUploadLimitMB_ will be registried
+    * 
     * @param uploadId
     * @param limitMB
-    *          upload limit size
+    *           upload limit size
     */
    public void addUploadLimit(String uploadId, Integer limitMB)
    {
@@ -239,11 +203,63 @@
 
    /**
     * Get all upload limit sizes
-    * @return 
-    *     all upload limit sizes
+    * 
+    * @return all upload limit sizes
     */
    public Map<String, Integer> getUploadLimitsMB()
    {
       return uploadLimitsMB_;
    }
-}
\ No newline at end of file
+
+   private ServletFileUpload makeServletFileUpload(UploadResource upResource)
+   {
+      // Create a factory for disk-based file items
+      DiskFileItemFactory factory = new DiskFileItemFactory();
+
+      // Set factory constraints
+      factory.setSizeThreshold(0);
+      factory.setRepository(new File(uploadLocation_));
+
+      // Create a new file upload handler
+      ServletFileUpload upload = new ServletFileUpload(factory);
+      upload.setProgressListener(makeProgressListener(upResource));
+      return upload;
+   }
+
+   private ProgressListener makeProgressListener(final UploadResource upResource)
+   {
+      return new ProgressListener()
+      {
+         public void update(long pBytesRead, long pContentLength, int pItems)
+         {
+            if (pBytesRead == upResource.getUploadedSize())
+               return;
+            upResource.addUploadedBytes(pBytesRead - upResource.getUploadedSize());
+         }
+      };
+   }
+
+   private boolean isLimited(UploadResource upResource, double contentLength)
+   {
+      // by default, use the limit set in the service
+      int limitMB = defaultUploadLimitMB_;
+      // if the limit is set in the request (specific for this upload) then use
+      // this value instead of the default one
+      if (uploadLimitsMB_.containsKey(upResource.getUploadId()))
+      {
+         limitMB = uploadLimitsMB_.get(upResource.getUploadId()).intValue();
+      }
+
+      int estimatedSizeMB = (int)((contentLength / 1024) / 1024);
+      if (limitMB > 0 && estimatedSizeMB > limitMB)
+      { // a limit set to 0 means unlimited         
+         if (log.isDebugEnabled())
+         {
+            log.debug("Upload cancelled because file bigger than size limit : " + estimatedSizeMB + " MB > " + limitMB
+               + " MB");
+         }
+         return true;
+      }
+      return false;
+   }
+}

Added: portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadSessionListener.java
===================================================================
--- portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadSessionListener.java	                        (rev 0)
+++ portal/trunk/component/web/server/src/main/java/org/exoplatform/upload/UploadSessionListener.java	2010-07-30 09:46:15 UTC (rev 3727)
@@ -0,0 +1,30 @@
+package org.exoplatform.upload;
+
+import org.exoplatform.container.PortalContainer;
+import org.exoplatform.services.listener.Event;
+import org.exoplatform.services.listener.Listener;
+
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionEvent;
+
+/**
+ * This listener for the purpose of cleaning up temporary files that are uploaded to the server
+ * but not removed by specific actions from user
+ * 
+ * The listener is triggered when a session is destroyed
+ * 
+ * @author <a href="mailto:trongtt at gmail.com">Tran The Trong</a>
+ * @version $Revision$
+ */
+public class UploadSessionListener extends Listener<PortalContainer, HttpSessionEvent>
+{
+   @Override
+   public void onEvent(Event<PortalContainer, HttpSessionEvent> event) throws Exception
+   {
+      PortalContainer container = event.getSource();
+      HttpSession session = event.getData().getSession();
+
+      UploadService uploadService = (UploadService)container.getComponentInstanceOfType(UploadService.class);
+      uploadService.cleanUp(session);
+   }
+}

Modified: portal/trunk/component/web/server/src/main/java/org/exoplatform/web/handler/DownloadHandler.java
===================================================================
--- portal/trunk/component/web/server/src/main/java/org/exoplatform/web/handler/DownloadHandler.java	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/component/web/server/src/main/java/org/exoplatform/web/handler/DownloadHandler.java	2010-07-30 09:46:15 UTC (rev 3727)
@@ -24,7 +24,7 @@
 import org.exoplatform.download.DownloadResource;
 import org.exoplatform.download.DownloadService;
 import org.exoplatform.web.WebAppController;
-import org.exoplatform.web.command.Command;
+import org.exoplatform.web.WebRequestHandler;
 
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -39,14 +39,18 @@
  *          thuy.le at exoplatform.com
  * Dec 9, 2006  
  */
-public class DownloadHandler extends Command
+public class DownloadHandler extends WebRequestHandler
 {
 
-   private String resourceId;
+   @Override
+   public String[] getPath()
+   {
+      return new String[]{"/download"};
+   }
 
-   @SuppressWarnings("unused")
    public void execute(WebAppController controller, HttpServletRequest req, HttpServletResponse res) throws Exception
    {
+      String resourceId = req.getParameter("resourceId");
       res.setHeader("Cache-Control", "private max-age=600, s-maxage=120");
       ExoContainer container = ExoContainerContext.getCurrentContainer();
       DownloadService dservice = (DownloadService)container.getComponentInstanceOfType(DownloadService.class);
@@ -87,11 +91,6 @@
       }
    }
 
-   public String getResourceId()
-   {
-      return resourceId;
-   }
-
    private static void optimalRead(InputStream is, OutputStream os) throws Exception
    {
       int bufferLength = 1024; //TODO: Better to compute bufferLength in term of -Xms, -Xmx properties

Modified: portal/trunk/component/web/server/src/main/java/org/exoplatform/web/handler/UploadHandler.java
===================================================================
--- portal/trunk/component/web/server/src/main/java/org/exoplatform/web/handler/UploadHandler.java	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/component/web/server/src/main/java/org/exoplatform/web/handler/UploadHandler.java	2010-07-30 09:46:15 UTC (rev 3727)
@@ -20,11 +20,12 @@
 package org.exoplatform.web.handler;
 
 import org.exoplatform.container.ExoContainer;
+
 import org.exoplatform.container.ExoContainerContext;
 import org.exoplatform.upload.UploadResource;
 import org.exoplatform.upload.UploadService;
 import org.exoplatform.web.WebAppController;
-import org.exoplatform.web.command.Command;
+import org.exoplatform.web.WebRequestHandler;
 
 import java.io.Writer;
 import java.net.URLEncoder;
@@ -38,30 +39,24 @@
  *          nhudinhthuan at exoplatform.com
  * Dec 9, 2006  
  */
-public class UploadHandler extends Command
+public class UploadHandler extends WebRequestHandler
 {
 
    static enum UploadServiceAction {
       PROGRESS, UPLOAD, DELETE, ABORT
    }
 
-   private String action;
-
-   private String[] uploadId;
-
-   public void setAction(String action)
+   @Override
+   public String[] getPath()
    {
-      this.action = action;
+      return new String[]{"/upload"};
    }
 
-   public void setUploadId(String[] uploadId)
-   {
-      this.uploadId = uploadId;
-   }
-
-   @SuppressWarnings("unused")
    public void execute(WebAppController controller, HttpServletRequest req, HttpServletResponse res) throws Exception
    {
+      String action = req.getParameter("action");
+      String[] uploadId = req.getParameterValues("uploadId");
+      
       res.setHeader("Cache-Control", "no-cache");
 
       ExoContainer container = ExoContainerContext.getCurrentContainer();
@@ -84,6 +79,7 @@
                continue;
             if (upResource.getStatus() == UploadResource.FAILED_STATUS)
             {
+               
                int limitMB = service.getUploadLimitsMB().get(uploadId[i]).intValue();
                value.append("\n    \"").append(uploadId[i]).append("\": {");
                value.append("\n      \"status\":").append('\"').append("failed").append("\",");
@@ -113,14 +109,11 @@
       }
       else if (uploadActionService == UploadServiceAction.DELETE)
       {
-         service.removeUpload(uploadId[0]);
+         service.removeUploadResource(uploadId[0]);
       }
       else if (uploadActionService == UploadServiceAction.ABORT)
       {
-         //TODO: dang.tung - we don't need 2 statements because it'll show error when we reload browser
-         //UploadResource upResource = service.getUploadResource(uploadId[0]);
-         //if(upResource != null) upResource.setStatus(UploadResource.UPLOADED_STATUS) ;
-         service.removeUpload(uploadId[0]);
+         service.removeUploadResource(uploadId[0]);
       }
    }
 

Modified: portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/web.xml
===================================================================
--- portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/web.xml	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/examples/portal/war/src/main/webapp/WEB-INF/web.xml	2010-07-30 09:46:15 UTC (rev 3727)
@@ -220,10 +220,14 @@
 		<url-pattern>/service</url-pattern>
 	</servlet-mapping>
 	<servlet-mapping>
-		<servlet-name>portal</servlet-name>
-		<url-pattern>/command/*</url-pattern>
+	  <servlet-name>portal</servlet-name> 
+	  <url-pattern>/upload/*</url-pattern> 
 	</servlet-mapping>
 	<servlet-mapping>
+	  <servlet-name>portal</servlet-name> 
+	  <url-pattern>/download/*</url-pattern> 
+	</servlet-mapping>
+	<servlet-mapping>
 		<servlet-name>RestServer</servlet-name>
 		<url-pattern>/rest/*</url-pattern>
 	</servlet-mapping>

Modified: portal/trunk/web/eXoResources/src/main/webapp/javascript/eXo/webui/UIUpload.js
===================================================================
--- portal/trunk/web/eXoResources/src/main/webapp/javascript/eXo/webui/UIUpload.js	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/web/eXoResources/src/main/webapp/javascript/eXo/webui/UIUpload.js	2010-07-30 09:46:15 UTC (rev 3727)
@@ -28,8 +28,8 @@
  * @param {boolean} isAutoUpload auto upload or none
  */
 UIUpload.prototype.initUploadEntry = function(uploadId, isAutoUpload) {
-	var url = eXo.env.server.context + "/command?" ;
-	url += "type=org.exoplatform.web.command.handler.UploadHandler&action=progress&uploadId="+uploadId ;
+	var url = eXo.env.server.context + "/upload?" ;
+	url += "action=progress&uploadId="+uploadId ;
 	var responseText = ajaxAsyncGetRequest(url, false);
 	
 	var response;
@@ -51,9 +51,8 @@
 UIUpload.prototype.createUploadEntry = function(uploadId, isAutoUpload) {
   var iframe = document.getElementById(uploadId+'uploadFrame');
   var idoc = iframe.contentWindow.document ;
-  var uploadAction = eXo.env.server.context + "/command?" ;
-  uploadAction += "type=org.exoplatform.web.command.handler.UploadHandler";
-  uploadAction += "&uploadId=" + uploadId+"&action=upload" ;
+  var uploadAction = eXo.env.server.context + "/upload?" ;
+  uploadAction += "uploadId=" + uploadId+"&action=upload" ;
   idoc.open();
 	idoc.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>");
   idoc.write("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='" +eXo.core.I18n.lang+ "' dir='" +eXo.core.I18n.dir+ "'>");
@@ -85,8 +84,8 @@
 UIUpload.prototype.refeshProgress = function(elementId) {
   var list =  eXo.webui.UIUpload.listUpload;
   if(list.length < 1) return;
-  var url = eXo.env.server.context + "/command?" ;
-	url += "type=org.exoplatform.web.command.handler.UploadHandler&action=progress" ;
+  var url = eXo.env.server.context + "/upload?" ;
+	url += "action=progress" ;
 //  var url =  eXo.env.server.context + "/upload?action=progress";  
   for(var i = 0; i < list.length; i++){
     url = url + "&uploadId=" + list[i];
@@ -165,8 +164,8 @@
  */
 UIUpload.prototype.abortUpload = function(id) {
   eXo.webui.UIUpload.listUpload.remove(id);
-  var url = eXo.env.server.context + "/command?" ;
-	url += "type=org.exoplatform.web.command.handler.UploadHandler&uploadId=" +id+"&action=abort" ;
+  var url = eXo.env.server.context + "/upload?" ;
+	url += "uploadId=" +id+"&action=abort" ;
 //  var url = eXo.env.server.context + "/upload?uploadId=" +id+"&action=abort" ;
   var request =  eXo.core.Browser.createHttpRequest();
   request.open('GET', url, false);
@@ -197,8 +196,8 @@
  * @param {String} id upload identifier
  */
 UIUpload.prototype.deleteUpload = function(id) {
-	var url = eXo.env.server.context + "/command?";
-	url += "type=org.exoplatform.web.command.handler.UploadHandler&uploadId=" +id+"&action=delete" ;
+	var url = eXo.env.server.context + "/upload?";
+	url += "uploadId=" +id+"&action=delete" ;
 //  var url = eXo.env.server.context + "/upload?uploadId=" +id+"&action=delete" ;
   var request =  eXo.core.Browser.createHttpRequest();
   request.open('GET', url, false);

Modified: portal/trunk/web/portal/src/main/webapp/WEB-INF/web.xml
===================================================================
--- portal/trunk/web/portal/src/main/webapp/WEB-INF/web.xml	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/web/portal/src/main/webapp/WEB-INF/web.xml	2010-07-30 09:46:15 UTC (rev 3727)
@@ -267,8 +267,12 @@
 	</servlet-mapping>
 	<servlet-mapping>
 	  <servlet-name>portal</servlet-name> 
-	  <url-pattern>/command/*</url-pattern> 
+	  <url-pattern>/upload/*</url-pattern> 
 	</servlet-mapping>
+	<servlet-mapping>
+	  <servlet-name>portal</servlet-name> 
+	  <url-pattern>/download/*</url-pattern> 
+	</servlet-mapping>
   <servlet-mapping>
     <servlet-name>RestServer</servlet-name>
     <url-pattern>/rest/*</url-pattern>

Modified: portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/UIFormUploadInput.java
===================================================================
--- portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/UIFormUploadInput.java	2010-07-30 08:48:02 UTC (rev 3726)
+++ portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/UIFormUploadInput.java	2010-07-30 09:46:15 UTC (rev 3727)
@@ -54,11 +54,6 @@
     */
    private boolean isAutoUpload = false;
 
-   /**
-    * The maximum size of the file to upload, in MB
-    */
-   //private int limitMB_;
-
    public UIFormUploadInput(String name, String bindingExpression)
    {
       super(name, bindingExpression, String.class);
@@ -127,9 +122,8 @@
       WebuiRequestContext pcontext = (WebuiRequestContext)context.getParentAppRequestContext();
       if (pcontext == null)
          pcontext = context;
-      String uploadAction = pcontext.getRequestContextPath() + "/command?";
-      uploadAction += "type=org.exoplatform.web.command.handler.UploadHandler";
-      uploadAction += "&uploadId=" + uploadId_ + "&action=upload";
+      String uploadAction = pcontext.getRequestContextPath() + "/upload?";
+      uploadAction += "uploadId=" + uploadId_ + "&action=upload";
       return uploadAction;
    }
 
@@ -147,9 +141,4 @@
    {
       this.isAutoUpload = isAutoUpload;
    }
-
-   //  public void setLimit(int size) { limitMB_ = size; }
-   //  
-   //  public int getLimit() { return limitMB_; }
-
 }
\ No newline at end of file



More information about the gatein-commits mailing list