Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  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.util.List;
 
 
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

Parameters:
uploadPath the path to upload files to
request the servlet request
Returns:
the widget file that was uploaded
Throws:
java.lang.Exception if the file could not be uploaded
 
 	public static File upload(String uploadPathHttpServletRequest requestthrows 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

Parameters:
item
serverPath
Returns:
Throws:
java.lang.Exception
 
 	private static File write(FileItem itemString paththrows 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

Parameters:
uploadPath
file
Returns:
Throws:
java.io.IOException
 
 	public static File dealWithDroppedFile(String uploadPathFile filethrows 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

Parameters:
WIDGETFOLDER
widgetGuid
Returns:
	public static boolean removeWidgetResources(String WIDGETFOLDERString 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;
	}
New to GrepCode? Check out our FAQ X