You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2010/04/17 22:16:38 UTC

svn commit: r935245 - in /incubator/wookie/trunk: parser/java/ parser/java/src-test/org/apache/wookie/w3c/test/ parser/java/src/org/apache/wookie/w3c/util/ src/org/apache/wookie/ src/org/apache/wookie/server/ src/org/apache/wookie/util/

Author: scottbw
Date: Sat Apr 17 20:16:38 2010
New Revision: 935245

URL: http://svn.apache.org/viewvc?rev=935245&view=rev
Log:
I refactored several of the methods in WidgetPackageUtils in the Parser sub-project into a new WidgetFileUtils class in the main project. The reason for this is that these methods are not used internally in the parser at all, and are utility methods used by the Wookie server for uploading, moving and deleting widget files. 

Moving these methods also removes two dependencies from the parser, which is good as it should be as portable as we can make it. 

I also updated the file upload to use the latest methods in the FileUpload package as the one we were using had been deprecated; I've tested this out and it looks like it works OK, but please do try it out and see if there are any gotchas. Finally I added more tests for the remaining WidgetPackageUtils methods.

Added:
    incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java
    incubator/wookie/trunk/src/org/apache/wookie/util/WidgetFileUtils.java
Modified:
    incubator/wookie/trunk/parser/java/ivy.xml
    incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
    incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java
    incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java

Modified: incubator/wookie/trunk/parser/java/ivy.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/ivy.xml?rev=935245&r1=935244&r2=935245&view=diff
==============================================================================
--- incubator/wookie/trunk/parser/java/ivy.xml (original)
+++ incubator/wookie/trunk/parser/java/ivy.xml Sat Apr 17 20:16:38 2010
@@ -20,13 +20,9 @@
 		</dependency>
 		<dependency org="commons-io" name="commons-io" rev="1.4" conf="deploy->default">
         </dependency>
-        <dependency org="commons-fileupload" name="commons-fileupload" rev="1.0" conf="deploy->default">
-        </dependency>
         <dependency org="commons-httpclient" name="commons-httpclient" rev="3.0.1" conf="deploy->default">
         </dependency>
         <dependency org="org.jdom" name="jdom" rev="1.1" conf="deploy->default">
         </dependency>
-        <dependency org="org.mortbay.jetty" name="servlet-api-2.5" rev="6.1.3" conf="dev->default">
-        </dependency>
     </dependencies>
 </ivy-module>

Added: incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java?rev=935245&view=auto
==============================================================================
--- incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java (added)
+++ incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java Sat Apr 17 20:16:38 2010
@@ -0,0 +1,80 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.w3c.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import org.apache.wookie.w3c.util.WidgetPackageUtils;
+import org.junit.Test;
+
+/**
+ * 
+ * Tests for generic utility methods not covered already in conformance tests etc
+ *
+ */
+public class WidgetPackageUtilsTest {
+	
+	@Test
+	public void testConvertPath(){
+		String path = WidgetPackageUtils.convertPathToPlatform("/x/");
+		assertEquals("/x", path);
+	}
+	
+	@Test
+	public void testInvalidLocalePaths(){
+		String[] path;
+		try {
+			path = WidgetPackageUtils.locateFilePaths("locales/", new String[]{"bogus!"}, null);
+			assertNull(path);
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	@Test
+	public void testInvalidLocalePaths2(){
+		String[] path;
+		try {
+			path = WidgetPackageUtils.locateFilePaths("locales/izabogustag", new String[]{"bogus!"}, null);
+			assertNull(path);
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	@Test
+	public void testInvalidLocalePaths3(){
+		String[] path;
+		try {
+			path = WidgetPackageUtils.locateFilePaths("/locales/izabogustag", new String[]{"bogus!"}, null);
+			assertNull(path);
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	@Test
+	public void getLanguageTagForPath(){
+		String locale = WidgetPackageUtils.languageTagForPath("locales/en");
+		assertEquals("en", locale);
+	}
+	
+	@Test
+	public void getLanguageTagForPath2(){
+		String locale = WidgetPackageUtils.languageTagForPath("locales/");
+		assertNull(locale);
+	}
+}

Modified: incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java?rev=935245&r1=935244&r2=935245&view=diff
==============================================================================
--- incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java (original)
+++ incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java Sat Apr 17 20:16:38 2010
@@ -21,17 +21,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.List;
-
-import javax.servlet.http.HttpServletRequest;
 
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipFile;
-import org.apache.commons.fileupload.DiskFileUpload;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadBase;
-import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.log4j.Logger;
 import org.apache.wookie.w3c.util.LocalizationUtils;
@@ -149,49 +141,6 @@ public class WidgetPackageUtils {
 		folder.replaceAll(" ", ""); //$NON-NLS-1$ //$NON-NLS-2$
 		return folder;
 	}
-	
-	public static File dealWithDroppedFile(String uploadPath, File file) throws IOException{	
-		String serverPath = convertPathToPlatform(uploadPath);
-		File uFile = new File(serverPath + File.separator + file.getName());
-		FileUtils.copyFile(file, uFile);
-		file.delete();
-		return uFile;
-	}
-
-	public static File dealWithUploadFile(String uploadPath, HttpServletRequest request) throws Exception {
-		File uFile = null;
-		String serverPath = convertPathToPlatform(uploadPath);
-		_logger.debug(serverPath);
-		String archiveFileName = null;
-		if (FileUploadBase.isMultipartContent(request)) {
-			_logger.debug("uploading file..."); //$NON-NLS-1$
-			DiskFileUpload fu = new DiskFileUpload();
-			// maximum size before a FileUploadException will be thrown
-			fu.setSizeMax(1024 * 1024 * 1024);
-			// maximum size that will be stored in memory
-			fu.setSizeThreshold(1024 * 1024);
-			// the location for saving data that is larger than
-			// getSizeThreshold()
-			fu.setRepositoryPath(uploadPath);
-
-			List<?> fileItems = fu.parseRequest(request);
-			if (!fileItems.isEmpty()) {
-				Iterator<?> i = fileItems.iterator();
-				FileItem fi = (FileItem) i.next();
-				File file = new File(convertPathToPlatform(fi.getName()));
-				archiveFileName = file.getName();
-
-				uFile = new File(serverPath + File.separator + archiveFileName);
-
-				fi.write(uFile);
-				_logger.debug("Upload completed successfully" +  "[" //$NON-NLS-1$ //$NON-NLS-2$
-						+ archiveFileName + "]-" //$NON-NLS-1$
-						+ (fi.isInMemory() ? "M" : "D")); //$NON-NLS-1$ //$NON-NLS-2$
-			}
-		}
-		return uFile;
-	}
-
 
 	public static String convertPathToRelativeUri(String path){
 		return path.replace('\\', '/');
@@ -206,20 +155,6 @@ public class WidgetPackageUtils {
 		return result;
 	}
 
-	public static boolean removeWidgetResources(String WIDGETFOLDER, String widgetGuid){
-		String folder = convertIdToFolderName(widgetGuid);
-		String serverPath = WIDGETFOLDER + File.separator + folder;
-		File pFolder = new File(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;
-	}
 	
 	/**
 	 * Checks for the existence of the Manifest.

Modified: incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java?rev=935245&r1=935244&r2=935245&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java Sat Apr 17 20:16:38 2010
@@ -43,6 +43,7 @@ import org.apache.wookie.helpers.WidgetK
 import org.apache.wookie.manager.IWidgetAdminManager;
 import org.apache.wookie.manager.impl.WidgetAdminManager;
 import org.apache.wookie.server.LocaleHandler;
+import org.apache.wookie.util.WidgetFileUtils;
 import org.apache.wookie.util.gadgets.GadgetUtils;
 import org.apache.wookie.util.html.StartPageProcessor;
 import org.apache.wookie.w3c.W3CWidget;
@@ -51,7 +52,6 @@ import org.apache.wookie.w3c.exceptions.
 import org.apache.wookie.w3c.exceptions.BadWidgetZipFileException;
 import org.apache.wookie.w3c.exceptions.InvalidContentTypeException;
 import org.apache.wookie.w3c.exceptions.InvalidStartFileException;
-import org.apache.wookie.w3c.util.WidgetPackageUtils;
 
 /**
  * Servlet implementation class for Servlet: WidgetAdminServlet
@@ -375,7 +375,7 @@ public class WidgetAdminServlet extends 
 		Widget widget = Widget.findById(Integer.parseInt(widgetId));
 		String guid = widget.getGuid();
 		if(WidgetFactory.destroy(Integer.parseInt(widgetId))){
-			if(WidgetPackageUtils.removeWidgetResources(WIDGETFOLDER, guid)){			
+			if(WidgetFileUtils.removeWidgetResources(WIDGETFOLDER, guid)){			
 				session.setAttribute("message_value", localizedMessages.getString("WidgetAdminServlet.12"));			 //$NON-NLS-1$ //$NON-NLS-2$ 
 			}
 			else{
@@ -465,7 +465,7 @@ public class WidgetAdminServlet extends 
 		session.setAttribute("closeWindow", Boolean.valueOf(true)); //$NON-NLS-1$
 		File zipFile;
 		try {
-			zipFile = WidgetPackageUtils.dealWithUploadFile(UPLOADFOLDER, request);
+			zipFile = WidgetFileUtils.upload(UPLOADFOLDER, request);
 		} 
 		catch (Exception ex) {
 			session.setAttribute("error_value", localizedMessages.getString("WidgetAdminServlet.28") + "\n" + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

Modified: incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java?rev=935245&r1=935244&r2=935245&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java Sat Apr 17 20:16:38 2010
@@ -30,6 +30,7 @@ import org.apache.wookie.feature.Feature
 import org.apache.wookie.helpers.FlashMessage;
 import org.apache.wookie.helpers.WidgetFactory;
 import org.apache.wookie.util.WgtWatcher;
+import org.apache.wookie.util.WidgetFileUtils;
 import org.apache.wookie.util.hibernate.DBManagerFactory;
 import org.apache.wookie.util.hibernate.HibernateUtil;
 import org.apache.wookie.util.hibernate.IDBManager;
@@ -169,7 +170,7 @@ public class ContextListener implements 
 	 				public void fileModified(File f) {
 	 					try{
 	 						dbManager.beginTransaction();
-	 						File upload = WidgetPackageUtils.dealWithDroppedFile(UPLOADFOLDER, f);
+	 						File upload = WidgetFileUtils.dealWithDroppedFile(UPLOADFOLDER, f);
 	 						W3CWidgetFactory fac = new W3CWidgetFactory();
 	 						fac.setLocales(locales);
 	 						fac.setLocalPath(contextPath+localWidgetFolderPath);

Added: 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=935245&view=auto
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/util/WidgetFileUtils.java (added)
+++ incubator/wookie/trunk/src/org/apache/wookie/util/WidgetFileUtils.java Sat Apr 17 20:16:38 2010
@@ -0,0 +1,127 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.apache.commons.io.FileUtils;
+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
+ */
+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;
+	}
+
+
+}