- Revision
- 451
- Author
- mauro
- Date
- 2007-12-07 11:13:26 -0600 (Fri, 07 Dec 2007)
Log Message
WAFFLE-44: Added support for file uploads. Simple example shows it in action.
Modified Paths
- trunk/examples/simple-example/pom.xml
- trunk/examples/simple-example/resources/scripts/myscript.js
- trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/SimpleRegistrar.java
- trunk/pom.xml
- trunk/waffle-core/pom.xml
Added Paths
- trunk/examples/simple-example/resources/upload.jspx
- trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/UploadController.java
- trunk/waffle-core/src/main/java/org/codehaus/waffle/io/
- trunk/waffle-core/src/main/java/org/codehaus/waffle/io/FileUploader.java
- trunk/waffle-core/src/main/java/org/codehaus/waffle/io/RequestFileUploader.java
Diff
Modified: trunk/examples/simple-example/pom.xml (450 => 451)
--- trunk/examples/simple-example/pom.xml 2007-12-06 14:15:44 UTC (rev 450) +++ trunk/examples/simple-example/pom.xml 2007-12-07 17:13:26 UTC (rev 451) @@ -1,4 +1,5 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> @@ -17,6 +18,16 @@ <scope>runtime</scope> </dependency> <dependency> + <groupId>commons-fileupload</groupId> + <artifactId>commons-fileupload</artifactId> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <scope>runtime</scope>
Modified: trunk/examples/simple-example/resources/scripts/myscript.js (450 => 451)
--- trunk/examples/simple-example/resources/scripts/myscript.js 2007-12-06 14:15:44 UTC (rev 450) +++ trunk/examples/simple-example/resources/scripts/myscript.js 2007-12-07 17:13:26 UTC (rev 451) @@ -2,6 +2,13 @@ createElementSubmitForm(document.forms[0], methodName); } +function fireMultipartMethod(methodName) { + var form = document.forms[0]; + form.method="post"; + form.encoding="multipart/form-data"; + createElementSubmitForm(form, methodName); +} + function createElementSubmitForm(form, methodName) { var method = document.createElement("input"); method.setAttribute("type", "hidden");
Added: trunk/examples/simple-example/resources/upload.jspx (0 => 451)
--- trunk/examples/simple-example/resources/upload.jspx (rev 0) +++ trunk/examples/simple-example/resources/upload.jspx 2007-12-07 17:13:26 UTC (rev 451) @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8"?> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:jsp="http://java.sun.com/JSP/Page" + xmlns:c="http://java.sun.com/jsp/jstl/core" + xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"> + +<jsp:output doctype-root-element="html" + doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" + doctype-system="http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/> +<jsp:directive.page contentType="text/html;charset=UTF-8"/> + +<head> + <title>Select a file to upload</title> + <style type="text/css" title="currentStyle" media="screen"> + @import "stylesheets/style.css"; + </style> + <script src="" type="text/_javascript_"> + // keep this space + </script> + <script src="" type="text/_javascript_"> + // keep this space + </script> +</head> + +<body> +<form action="" + + <h3>Waffle file upload</h3> + + <input type="file" name="importFile"/> + + <a href="" File</a> + + <br/> + +</form> +</body> + +</html> \ No newline at end of file
Modified: trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/SimpleRegistrar.java (450 => 451)
--- trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/SimpleRegistrar.java 2007-12-06 14:15:44 UTC (rev 450) +++ trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/SimpleRegistrar.java 2007-12-07 17:13:26 UTC (rev 451) @@ -1,11 +1,13 @@ package org.codehaus.waffle.example.simple; -import org.codehaus.waffle.registrar.AbstractRegistrar; -import org.codehaus.waffle.registrar.Registrar; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.codehaus.waffle.example.simple.action.CalculatorController; import org.codehaus.waffle.example.simple.action.HelloWorldController; import org.codehaus.waffle.example.simple.action.PersonController; import org.codehaus.waffle.example.simple.dao.SimplePersonDAO; +import org.codehaus.waffle.io.RequestFileUploader; +import org.codehaus.waffle.registrar.AbstractRegistrar; +import org.codehaus.waffle.registrar.Registrar; public class SimpleRegistrar extends AbstractRegistrar { @@ -29,4 +31,11 @@ //register("automobileValidator", AutomobileControllerValidator.class); register("person", PersonController.class); } + + @Override + public void request() { + register("fileItemFactory", DiskFileItemFactory.class); + register("uploader", RequestFileUploader.class); + register("upload", UploadController.class); + } }
Added: trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/UploadController.java (0 => 451)
--- trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/UploadController.java (rev 0) +++ trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/UploadController.java 2007-12-07 17:13:26 UTC (rev 451) @@ -0,0 +1,29 @@ +package org.codehaus.waffle.example.simple; + +import java.util.List; + +import org.apache.commons.fileupload.FileItem; +import org.codehaus.waffle.action.annotation.DefaultActionMethod; +import org.codehaus.waffle.io.FileUploader; + +public class UploadController { + + private FileUploader uploader; + + public UploadController(FileUploader uploader) { + this.uploader = uploader; + } + + @DefaultActionMethod + public void upload(){ + List<FileItem> fileItems = uploader.getFileItems(); + for ( FileItem file : fileItems ){ + System.out.println(file.getName()); + System.out.println(new String(file.get())); + } + if ( uploader.hasErrors() ){ + System.out.println(uploader.getErrors()); + } + } + +}
Modified: trunk/pom.xml (450 => 451)
--- trunk/pom.xml 2007-12-06 14:15:44 UTC (rev 450) +++ trunk/pom.xml 2007-12-07 17:13:26 UTC (rev 451) @@ -80,6 +80,16 @@ <scope>provided</scope> </dependency> <dependency> + <groupId>commons-fileupload</groupId> + <artifactId>commons-fileupload</artifactId> + <version>1.2</version> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>1.3</version> + </dependency> + <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1</version> @@ -117,7 +127,7 @@ </exclusions> <scope>test</scope> </dependency> - <dependency> + <dependency> <!-- junit:junit is required by surefire until 2.3.1 or 2.4 is released and it can be configured to use junit:junit-dep --> <groupId>junit</groupId> <artifactId>junit</artifactId> @@ -132,10 +142,10 @@ <groupId>org.jmock</groupId> <artifactId>jmock-junit4</artifactId> </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> </dependencies> <build> @@ -165,7 +175,7 @@ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> - <version>2.3</version> + <version>2.3</version> <configuration> <includes> <include>**/*Test.java</include> @@ -454,6 +464,11 @@ <timezone>-3</timezone> </developer> </developers> + <contributors> + <contributor> + <name>Gleb Mazursky</name> + </contributor> + </contributors> <scm> <developerConnection>
Modified: trunk/waffle-core/pom.xml (450 => 451)
--- trunk/waffle-core/pom.xml 2007-12-06 14:15:44 UTC (rev 450) +++ trunk/waffle-core/pom.xml 2007-12-07 17:13:26 UTC (rev 451) @@ -37,6 +37,10 @@ <artifactId>xstream</artifactId> </dependency> <dependency> + <groupId>commons-fileupload</groupId> + <artifactId>commons-fileupload</artifactId> + </dependency> + <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </dependency>
Added: trunk/waffle-core/src/main/java/org/codehaus/waffle/io/FileUploader.java (0 => 451)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/io/FileUploader.java (rev 0) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/io/FileUploader.java 2007-12-07 17:13:26 UTC (rev 451) @@ -0,0 +1,45 @@ +/***************************************************************************** + * Copyright (C) 2005,2006 Michael Ward * + * All rights reserved. * + * ------------------------------------------------------------------------- * + * The software in this package is published under the terms of the BSD * + * style license a copy of which has been included with this distribution in * + * the LICENSE.txt file. * + * * + *****************************************************************************/ +package org.codehaus.waffle.io; + +import java.util.Collection; +import java.util.List; +import org.apache.commons.fileupload.FileItem; + +/** + * Allows to upload files from a multipart request + * + * @author Gleb Mazursky + * @author Mauro Talevi + */ +public interface FileUploader { + + /** + * Returns the FileItems for uploaded files + * + * @return A List of FileItems + */ + List<FileItem> getFileItems(); + + /** + * Returns errors generated when parsing the multipart request. + * + * @return the error Collection. + */ + Collection<String> getErrors(); + + /** + * Determines if any errors occured when parsing the multipart request + * + * @return <tt>true</tt> if any errors occured + */ + boolean hasErrors(); + +}
Added: trunk/waffle-core/src/main/java/org/codehaus/waffle/io/RequestFileUploader.java (0 => 451)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/io/RequestFileUploader.java (rev 0) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/io/RequestFileUploader.java 2007-12-07 17:13:26 UTC (rev 451) @@ -0,0 +1,105 @@ +/***************************************************************************** + * Copyright (C) 2005,2006 Michael Ward * + * All rights reserved. * + * ------------------------------------------------------------------------- * + * The software in this package is published under the terms of the BSD * + * style license a copy of which has been included with this distribution in * + * the LICENSE.txt file. * + * * + *****************************************************************************/ +package org.codehaus.waffle.io; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItem; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.codehaus.waffle.Startable; + +/** + * Request-based FileUploader. The files are uploaded when the object is started. If there are any errors they are + * logged and retrievable via the [EMAIL PROTECTED] #getErrors()} method. An action handling a multipart form should first check + * [EMAIL PROTECTED] #hasErrors()} before doing any other processing. + * + * @author Gleb Mazursky + * @author Mauro Talevi + */ +public class RequestFileUploader implements Startable, FileUploader { + private final HttpServletRequest request; + private final FileItemFactory itemFactory; + private Collection<String> errors = new ArrayList<String>(); + private List<FileItem> fileItems = new ArrayList<FileItem>(); + + /** + * Creates RequestFileUploader + * + * @param request the HttpServletRequest + * @param itemFactory the FileItemFactory + */ + public RequestFileUploader(HttpServletRequest request, FileItemFactory itemFactory) { + this.request = request; + this.itemFactory = itemFactory; + } + + public List<FileItem> getFileItems() { + return fileItems; + } + + public Collection<String> getErrors() { + return errors; + } + + public boolean hasErrors() { + return !errors.isEmpty(); + } + + /** + * Upload files on request init + */ + public void start() { + uploadFiles(request, itemFactory); + } + + /** + * Performs files cleanup on request destroy + */ + public void stop() { + for (FileItem file : getFileItems()) { + if (file instanceof DiskFileItem) { + File diskFile = ((DiskFileItem) file).getStoreLocation(); + if (diskFile.exists()) { + diskFile.delete(); + } + } + } + } + + /** + * Processes request to retrieve file uploads and records any errors. + * + * @param request the HttpServletRequest + * @param itemFactory the FileItemFactory + */ + @SuppressWarnings("unchecked") + private void uploadFiles(HttpServletRequest request, FileItemFactory itemFactory) { + if (ServletFileUpload.isMultipartContent(request)) { + try { + ServletFileUpload fileUpload = new ServletFileUpload(itemFactory); + errors.clear(); + fileItems.clear(); + fileItems.addAll(fileUpload.parseRequest(request)); + } catch (FileUploadException e) { + errors.add("Failed to upload files: " + e.getMessage()); + } + } + } + + +}
To unsubscribe from this list please visit:
