Author: scottbw
Date: Thu Feb 23 23:39:20 2012
New Revision: 1293031

URL: http://svn.apache.org/viewvc?rev=1293031&view=rev
Log:
Only allow .wgt files to be uploaded using WidgetFileUtils, and cleaned up code 
formatting

Modified:
    incubator/wookie/trunk/src/org/apache/wookie/util/WidgetFileUtils.java

Modified: incubator/wookie/trunk/src/org/apache/wookie/util/WidgetFileUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/util/WidgetFileUtils.java?rev=1293031&r1=1293030&r2=1293031&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/util/WidgetFileUtils.java 
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/util/WidgetFileUtils.java Thu 
Feb 23 23:39:20 2012
@@ -15,7 +15,6 @@ package org.apache.wookie.util;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.Iterator;
 import java.util.List;
 
 import javax.servlet.http.HttpServletRequest;
@@ -28,100 +27,125 @@ import org.apache.log4j.Logger;
 import org.apache.wookie.w3c.util.WidgetPackageUtils;
 
 /**
- * Utility for working with Widget files in the Wookie server: uploading, 
moving, and deleting
+ * Utility for working with Widget files in the Wookie server: uploading,
+ * moving, and deleting
  */
 public class WidgetFileUtils {
 
-       static Logger _logger = 
Logger.getLogger(WidgetFileUtils.class.getName());
-
-       /**
-        * Upload a widget archive
-        * @param uploadPath the path to upload files to
-        * @param request the servlet request
-        * @return the widget file that was uploaded
-        * @throws Exception if the file could not be uploaded
-        */
-       public static File upload(String uploadPath, HttpServletRequest 
request) throws Exception {
-               String serverPath = 
WidgetPackageUtils.convertPathToPlatform(uploadPath);
-
-               // Create a factory for disk-based file items
-               DiskFileItemFactory factory = new DiskFileItemFactory();
-
-               // Create a new file upload handler
-               ServletFileUpload upload = new ServletFileUpload(factory);
-
-               // maximum size before a FileUploadException will be thrown
-               upload.setSizeMax(1024 * 1024 * 1024);
-               
-               // process upload request
-               List<?> fileItems = upload.parseRequest(request);
-
-               _logger.debug(serverPath);
-
-               if (!fileItems.isEmpty()) {
-                       Iterator<?> i = fileItems.iterator();
-                       FileItem fi = (FileItem) i.next();
-                       if (!fi.isFormField())
-                               return write(fi, serverPath);
-               }
-               return null;
-       }
-       
-       /**
-        * Write a FileItem to a file prefixed with the given path
-        * @param item
-        * @param serverPath
-        * @return
-        * @throws Exception
-        */
-       private static File write(FileItem item, String path) throws Exception{
-               File file = new 
File(WidgetPackageUtils.convertPathToPlatform(item.getName()));
-               String archiveFileName = file.getName();
-               File uFile = new File(path + File.separator + archiveFileName);
-               item.write(uFile);
-               _logger.debug("Upload completed successfully" +  "[" 
//$NON-NLS-1$ //$NON-NLS-2$
-                               + archiveFileName + "]-" //$NON-NLS-1$
-                               + (item.isInMemory() ? "M" : "D")); 
//$NON-NLS-1$ //$NON-NLS-2$
-
-               return uFile;
-       }
-       
-       /**
-        * Moves a file to the specified path
-        * @param uploadPath
-        * @param file
-        * @return
-        * @throws IOException
-        */
-       public static File dealWithDroppedFile(String uploadPath, File file) 
throws IOException{        
-               String serverPath = 
WidgetPackageUtils.convertPathToPlatform(uploadPath);
-               File uFile = new File(serverPath + File.separator + 
file.getName());
-               FileUtils.copyFile(file, uFile);
-               file.delete();
-               return uFile;
-       }
-       
-
-       /**
-        * Delete a widget and its resources
-        * @param WIDGETFOLDER
-        * @param widgetGuid
-        * @return
-        */
-       public static boolean removeWidgetResources(String WIDGETFOLDER, String 
widgetGuid){
-               String folder = 
WidgetPackageUtils.convertIdToFolderName(widgetGuid);
-               String serverPath = WIDGETFOLDER + File.separator + folder;
-               File pFolder = new 
File(WidgetPackageUtils.convertPathToPlatform(serverPath));
-               try {
-                       _logger.debug("Deleting 
folder:"+pFolder.getCanonicalFile().toString()); //$NON-NLS-1$
-                       if (pFolder.getParent() != null) // never call on a 
root folder
-                               FileUtils.deleteDirectory(pFolder);
-               }
-               catch (Exception ex) {
-                       _logger.error(ex);
-               }
-               return true;
-       }
+  static Logger _logger = Logger.getLogger(WidgetFileUtils.class.getName());
 
+  /**
+   * Upload a widget archive
+   * 
+   * @param uploadPath
+   *          the path to upload files to
+   * @param request
+   *          the servlet request
+   * @return the widget file that was uploaded
+   * @throws Exception
+   *           if the file could not be uploaded
+   */
+  public static File upload(String uploadPath, HttpServletRequest request)
+      throws Exception {
+    String serverPath = WidgetPackageUtils.convertPathToPlatform(uploadPath);
+
+    //
+    // Create a factory for disk-based file items
+    //
+    DiskFileItemFactory factory = new DiskFileItemFactory();
+
+    //
+    // Create a new file upload handler
+    //
+    ServletFileUpload upload = new ServletFileUpload(factory);
+
+    //
+    // maximum size before a FileUploadException will be thrown
+    //
+    upload.setSizeMax(1024 * 1024 * 1024);
+
+    //
+    // process upload request
+    //
+    @SuppressWarnings("unchecked")
+    List<FileItem> fileItems = upload.parseRequest(request);
+
+    _logger.debug(serverPath);
+
+    //
+    // Only save .wgt files and ignore any others in the POST
+    //
+    if (!fileItems.isEmpty()) {
+      for (FileItem item : fileItems) {
+        if (!item.isFormField() && item.getName() != null
+            && item.getName().endsWith(".wgt")) {
+          return write(item, serverPath);
+        }
+      }
+    }
+
+    return null;
+  }
+
+  /**
+   * Write a FileItem to a file prefixed with the given path
+   * 
+   * @param item
+   * @param serverPath
+   * @return
+   * @throws Exception
+   */
+  private static File write(FileItem item, String path) throws Exception {
+    File file = new File(WidgetPackageUtils.convertPathToPlatform(item
+        .getName()));
+    String archiveFileName = file.getName();
+    File uFile = new File(path + File.separator + archiveFileName);
+    item.write(uFile);
+    _logger.debug("Upload completed successfully" + "[" //$NON-NLS-1$ 
//$NON-NLS-2$
+        + archiveFileName + "]-" //$NON-NLS-1$
+        + (item.isInMemory() ? "M" : "D")); //$NON-NLS-1$ //$NON-NLS-2$
+
+    return uFile;
+  }
+
+  /**
+   * Moves a file to the specified path
+   * 
+   * @param uploadPath
+   * @param file
+   * @return
+   * @throws IOException
+   */
+  public static File dealWithDroppedFile(String uploadPath, File file)
+      throws IOException {
+    String serverPath = WidgetPackageUtils.convertPathToPlatform(uploadPath);
+    File uFile = new File(serverPath + File.separator + file.getName());
+    FileUtils.copyFile(file, uFile);
+    file.delete();
+    return uFile;
+  }
+
+  /**
+   * Delete a widget and its resources
+   * 
+   * @param WIDGETFOLDER
+   * @param widgetGuid
+   * @return
+   */
+  public static boolean removeWidgetResources(String WIDGETFOLDER,
+      String widgetGuid) {
+    String folder = WidgetPackageUtils.convertIdToFolderName(widgetGuid);
+    String serverPath = WIDGETFOLDER + File.separator + folder;
+    File pFolder = new File(
+        WidgetPackageUtils.convertPathToPlatform(serverPath));
+    try {
+      _logger.debug("Deleting folder:" + 
pFolder.getCanonicalFile().toString()); //$NON-NLS-1$
+      if (pFolder.getParent() != null) // never call on a root folder
+        FileUtils.deleteDirectory(pFolder);
+    } catch (Exception ex) {
+      _logger.error(ex);
+    }
+    return true;
+  }
 
 }


Reply via email to