r1051 - in xwiki/trunk/src/main: java/com/xpn/xwiki java/com/xpn/xwiki/doc java/com/xpn/xwiki/plugin/fileupload java/com/xpn/xwiki/web resources web/templates

Nguyen Viet Chung chungnv at users.forge.objectweb.org
Thu Apr 20 04:01:14 CEST 2006


Author: chungnv
Date: 2006-04-20 04:00:55 +0200 (Thu, 20 Apr 2006)
New Revision: 1051

Modified:
   xwiki/trunk/src/main/java/com/xpn/xwiki/XWikiException.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/fileupload/FileUploadPlugin.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/web/UploadAction.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/web/Utils.java
   xwiki/trunk/src/main/resources/ApplicationResources.properties
   xwiki/trunk/src/main/web/templates/exception.vm
Log:
Fixed bug XWiki-170 . Upload big file throw Exception OutOFMemory and size file over max size file upload !

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/XWikiException.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/XWikiException.java	2006-04-17 22:03:36 UTC (rev 1050)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/XWikiException.java	2006-04-20 02:00:55 UTC (rev 1051)
@@ -169,6 +169,9 @@
     public static final int ERROR_XWIKI_APP_REDIRECT_EXCEPTION = 11010;
     public static final int ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION = 11011;
     public static final int ERROR_XWIKI_APP_SERVICE_NOT_FOUND = 11012;
+    public static final int ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE = 11013;
+    public static final int ERROR_XWIKI_APP_JAVA_HEAP_SPACE = 11014;
+    
     public static final int ERROR_XWIKI_EXPORT_XSL_FILE_NOT_FOUND = 12001;
     public static final int ERROR_XWIKI_EXPORT_PDF_FOP_FAILED = 12002;
     public static final int ERROR_XWIKI_EXPORT_XSL_FAILED = 12003;

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java	2006-04-17 22:03:36 UTC (rev 1050)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java	2006-04-20 02:00:55 UTC (rev 1051)
@@ -1459,7 +1459,7 @@
             if (bclass.getFieldList().size() > 0) {
                 docel.add(bclass.toXML(null));
             }
-
+            
             // Add Objects
             Iterator it = getxWikiObjects().values().iterator();
             while (it.hasNext()) {
@@ -1663,8 +1663,13 @@
             if (getDatabase() != null)
                 context.setDatabase(getDatabase());
 
-            context.getWiki().getAttachmentStore().saveAttachmentContent(attachment, context, true);
-        } finally {
+           context.getWiki().getAttachmentStore().saveAttachmentContent(attachment, context,true);
+        }catch(java.lang.OutOfMemoryError e){
+            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                    XWikiException.ERROR_XWIKI_APP_JAVA_HEAP_SPACE,
+                    "Out Of Memory Exception");
+        }
+        finally {
             if (database != null)
                 context.setDatabase(database);
         }
@@ -1692,8 +1697,14 @@
             // get the translated content
             if (getDatabase() != null)
                 context.setDatabase(getDatabase());
+            try{
+               context.getWiki().getAttachmentStore().deleteXWikiAttachment(attachment, context, true);
+            }catch(java.lang.OutOfMemoryError e){                
+                throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                    XWikiException.ERROR_XWIKI_APP_JAVA_HEAP_SPACE,
+                    "Out Of Memory Exception");
+            }
 
-            context.getWiki().getAttachmentStore().deleteXWikiAttachment(attachment, context, true);
         } finally {
             if (database != null)
                 context.setDatabase(database);

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/fileupload/FileUploadPlugin.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/fileupload/FileUploadPlugin.java	2006-04-17 22:03:36 UTC (rev 1050)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/fileupload/FileUploadPlugin.java	2006-04-20 02:00:55 UTC (rev 1051)
@@ -1,25 +1,25 @@
-/*
- * Copyright 2006, XpertNet SARL, and individual contributors as indicated
- * by the contributors.txt.
- *
- * 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.
- *
- * @author ludovic
- * @author sdumitriu
- */
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * 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.
+ *
+ * @author ludovic
+ * @author sdumitriu
+ */
 
 package com.xpn.xwiki.plugin.fileupload;
 
@@ -29,10 +29,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.commons.fileupload.DefaultFileItem;
-import org.apache.commons.fileupload.DiskFileUpload;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.fileupload.*;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -72,7 +69,7 @@
     public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
         return new FileUploadPluginApi((FileUploadPlugin) plugin, context);
     }
-    
+
     /**
      *  endRendering to make sure we don't leave files in temp directories
      * @param context Context of the request
@@ -127,8 +124,9 @@
         fileupload.setSizeMax(uploadMaxSize);
         fileupload.setSizeThreshold(uploadSizeThreashold);
         context.put("fileupload", fileupload);
+        XWikiRequest request = context.getRequest() ;
 
-        if (tempdir!=null) {
+        if (tempdir != null) {
             fileupload.setRepositoryPath(tempdir);
             (new File(tempdir)).mkdirs();
         }
@@ -137,11 +135,14 @@
         }
 
         try {
-            XWikiRequest request = context.getRequest();
             List list = fileupload.parseRequest(request.getHttpServletRequest());
-            // We store the file list in the context
+            // We store the file list in the context, throw Exception ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE
             context.put("fileuploadlist", list);
-        } catch (FileUploadException e) {
+        }catch (FileUploadBase.SizeLimitExceededException  e) {
+              throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                    XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE,
+                    "Exception uploaded file");
+        }catch(FileUploadException e){
             throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                     XWikiException.ERROR_XWIKI_APP_UPLOAD_PARSE_EXCEPTION,
                     "Exception while parsing uploaded file", e);
@@ -186,15 +187,19 @@
             return null;
 
         byte[] data = new byte[(int)fileitem.getSize()];
-        InputStream fileis = null;
-        try {
-            fileis = fileitem.getInputStream();
-            fileis.read(data);
-            fileis.close();
-        } catch (IOException e) {
+        try{
+            InputStream fileis = fileitem.getInputStream();
+            if(fileis != null){
+                fileis.read(data);
+                fileis.close();
+            }
+
+        } catch (java.lang.OutOfMemoryError e) {
             throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
-                    XWikiException.ERROR_XWIKI_APP_UPLOAD_FILE_EXCEPTION,
-                    "Exception while reading uploaded parsed file", e);
+                   XWikiException.ERROR_XWIKI_APP_JAVA_HEAP_SPACE,"Java Heap Space, Out of memory exception",e);
+        }catch(IOException ie){
+            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                    XWikiException.ERROR_XWIKI_APP_UPLOAD_FILE_EXCEPTION,"Exception while reading uploaded parsed file",ie) ;
         }
         return data;
     }

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/web/UploadAction.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/web/UploadAction.java	2006-04-17 22:03:36 UTC (rev 1050)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/web/UploadAction.java	2006-04-20 02:00:55 UTC (rev 1051)
@@ -35,46 +35,66 @@
     public boolean action(XWikiContext context) throws XWikiException {
         XWikiResponse response = context.getResponse();
         XWikiDocument doc = context.getDoc();
-
         String username = context.getUser();
-
+        Object exception = context.get("exception");
+        // check Exception File upload is large
+        if(exception != null){
+            if(exception instanceof XWikiException){
+                XWikiException exp = (XWikiException) exception ;
+                if(exp.getCode() == XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE) {
+                    context.put("message","fileuploadislarge");
+                    return true ;
+                }
+            }
+        }
         FileUploadPlugin fileupload = (FileUploadPlugin) context.get("fileuploadplugin");
+        String filename =fileupload.getFileItem("filename", context);
 
-        String filename = fileupload.getFileItem("filename", context);
         if (filename!=null) {
          if (filename.indexOf("/") != -1 || filename.indexOf("\\") != -1 || filename.indexOf(";") != -1){
              context.put("message","notsupportcharacters");
              return true ;
          }
         }
+
         byte[] data = fileupload.getFileItemData("filepath", context);
-
         if (filename==null) {
             String fname = fileupload.getFileName("filepath", context);
             int i = fname.indexOf("\\");
             if (i==-1)
-                i = fname.indexOf ("/");
+                i = fname.indexOf("/");
             filename = fname.substring(i+1);
         }
         filename = filename.replaceAll("\\+"," ");
+
+        XWikiDocument olddoc = (XWikiDocument) doc.clone();
         // Read XWikiAttachment
-        XWikiAttachment attachment = doc.getAttachment(filename);
+        XWikiAttachment attachment = olddoc.getAttachment(filename);
 
         if (attachment==null) {
             attachment = new XWikiAttachment();
-            doc.getAttachmentList().add(attachment);
+            olddoc.getAttachmentList().add(attachment);
         }
         attachment.setContent(data);
         attachment.setFilename(filename);
-
         // TODO: handle Author
         attachment.setAuthor(username);
 
         // Add the attachment to the document
-        attachment.setDoc(doc);
+        attachment.setDoc(olddoc);
 
         // Save the content and the archive
-        doc.saveAttachmentContent(attachment, context);
+        try{
+             olddoc.saveAttachmentContent(attachment, context);
+        }catch(XWikiException e){
+            // check Exception is ERROR_XWIKI_APP_JAVA_HEAP_SPACE when saving Attachment
+            if(e.getCode() == XWikiException.ERROR_XWIKI_APP_JAVA_HEAP_SPACE){
+                context.put("message","javaheapspace");
+                return true ;
+            }else{
+                throw e;
+            }
+        }
 
         // forward to attach page
         String redirect = fileupload.getFileItem("xredirect", context);

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/web/Utils.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/web/Utils.java	2006-04-17 22:03:36 UTC (rev 1050)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/web/Utils.java	2006-04-20 02:00:55 UTC (rev 1051)
@@ -429,7 +429,14 @@
             }
         }
         catch (Exception e) {
-            e.printStackTrace();
+            if( (e instanceof XWikiException) &&
+                    (((XWikiException)e).getCode() == XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE ))
+            {
+                context.put("exception",e);
+            }else {
+                 e.printStackTrace();
+            }
+
         }
         return fileupload;
     }

Modified: xwiki/trunk/src/main/resources/ApplicationResources.properties
===================================================================
--- xwiki/trunk/src/main/resources/ApplicationResources.properties	2006-04-17 22:03:36 UTC (rev 1050)
+++ xwiki/trunk/src/main/resources/ApplicationResources.properties	2006-04-20 02:00:55 UTC (rev 1051)
@@ -196,7 +196,7 @@
 attachmentdoesnotexist=The attachment does not exist.
 wikicontentcannotbeempty= The content of a wiki page is not allowed to be completely empty.
 fileuploadislarge=XWiki has a default limit of around 10Mb for attached files. This limit can be changed using the upload_maxsize parameter. Check the FAQ for more information.
-javaheapspace=Out of memory Exception
+javaheapspace=Java Heap Space Out Of Memory Exception !
 notsupportcharacters=File name does not support characters '\\' '/' ';'
 youcanclicktoedit=You can click on "Edit this Page" to create it.
 thistemplatedoesnotexist=This template does not exist

Modified: xwiki/trunk/src/main/web/templates/exception.vm
===================================================================
--- xwiki/trunk/src/main/web/templates/exception.vm	2006-04-17 22:03:36 UTC (rev 1050)
+++ xwiki/trunk/src/main/web/templates/exception.vm	2006-04-20 02:00:55 UTC (rev 1051)
@@ -1,11 +1,14 @@
 #template("xwiki.vm")
 #template("httpheader.vm")
 #template("header.vm")
-#template("baseheader.vm")
+#template("viewheader.vm")
 <div id="xwikicontent">
     #set($message = $xwiki.parseMessage())
     #if($message)
-    $message
+    <center>
+        <h2>$message</h2>
+        <br/><br/>
+    </center>
     #else
     $msg.get("problemoccured")
     #end





More information about the Xwiki-notifications mailing list