sylvain 2003/11/13 06:56:12
Modified: src/java/org/apache/cocoon/servlet/multipart MultipartHttpServletRequest.java Part.java PartInMemory.java PartOnDisk.java RequestFactory.java Log: Allow parts to be detached from the request and thus survive the end of its processing. This is needed for the woody upload widget. Revision Changes Path 1.5 +6 -4 cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/MultipartHttpServletRequest.java Index: MultipartHttpServletRequest.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/MultipartHttpServletRequest.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- MultipartHttpServletRequest.java 30 Jul 2003 02:21:31 -0000 1.4 +++ MultipartHttpServletRequest.java 13 Nov 2003 14:56:12 -0000 1.5 @@ -98,9 +98,11 @@ Enumeration e = getParameterNames(); while (e.hasMoreElements()) { Object o = get( (String)e.nextElement() ); - if (o instanceof PartOnDisk) { - File file = ((PartOnDisk) o).getFile(); - file.delete(); + if (o instanceof Part) { + Part part = (Part)o; + if (part.disposeWithRequest()) { + part.dispose(); + } } } } 1.3 +26 -3 cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/Part.java Index: Part.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/Part.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Part.java 17 Apr 2003 20:11:24 -0000 1.2 +++ Part.java 13 Nov 2003 14:56:12 -0000 1.3 @@ -53,6 +53,8 @@ import java.io.InputStream; import java.util.Map; +import org.apache.avalon.framework.activity.Disposable; + /** * This (abstract) class represents a file part parsed from a http post stream. @@ -60,7 +62,9 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Jeroen ter Voorde</a> * @version CVS $Id$ */ -public abstract class Part { +public abstract class Part implements Disposable { + + private boolean disposeWithRequest = true; /** Field headers */ protected Map headers; @@ -99,7 +103,26 @@ public String getMimeType() { return (String) headers.get("content-type"); } - + + /** + * Do we want any temporary resource held by this part to be cleaned up when processing of + * the request that created it is finished? Default is <code>true</code>. + * + * @return <code>true</code> if the part should be disposed with the request. + */ + public boolean disposeWithRequest() { + return this.disposeWithRequest; + } + + /** + * Set the value of the <code>disposeWithRequest</code> flag (default is <code>true</code>). + * + * @param dispose <code>true</code> if the part should be disposed after request processing + */ + public void setDisposeWithRequest(boolean dispose) { + this.disposeWithRequest = dispose; + } + /** * Returns an InputStream containing the file data * @throws Exception 1.4 +13 -2 cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/PartInMemory.java Index: PartInMemory.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/PartInMemory.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- PartInMemory.java 19 Aug 2003 06:01:26 -0000 1.3 +++ PartInMemory.java 13 Nov 2003 14:56:12 -0000 1.4 @@ -98,6 +98,17 @@ * @throws Exception */ public InputStream getInputStream() throws Exception { - return in; + if (this.in != null) { + return this.in; + } else { + throw new IllegalStateException("This part has already been disposed."); + } + } + + /** + * Clean the byte array content buffer holding part data + */ + public void dispose() { + this.in = null; } } 1.3 +24 -2 cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/PartOnDisk.java Index: PartOnDisk.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/PartOnDisk.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- PartOnDisk.java 18 Aug 2003 21:55:40 -0000 1.2 +++ PartOnDisk.java 13 Nov 2003 14:56:12 -0000 1.3 @@ -76,6 +76,10 @@ protected PartOnDisk(Map headers, File file) { super(headers); this.file = file; + + // Ensure the file will be deleted when we exit the JVM + this.file.deleteOnExit(); + this.size = (int) file.length(); } @@ -106,7 +110,11 @@ * @throws Exception */ public InputStream getInputStream() throws Exception { - return new FileInputStream(file); + if (this.file != null) { + return new FileInputStream(file); + } else { + throw new IllegalStateException("This part has already been disposed."); + } } /** @@ -114,5 +122,19 @@ */ public String toString() { return file.getPath(); + } + + public void dispose() { + if (this.file != null) { + this.file.delete(); + this.file = null; + } + } + + public void finalize() throws Throwable { + // Ensure the file has been deleted + dispose(); + + super.finalize(); } } 1.2 +10 -2 cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/RequestFactory.java Index: RequestFactory.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/servlet/multipart/RequestFactory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- RequestFactory.java 4 Apr 2003 13:19:05 -0000 1.1 +++ RequestFactory.java 13 Nov 2003 14:56:12 -0000 1.2 @@ -88,7 +88,15 @@ this.allowOverwrite = allowOverwrite; this.silentlyRename = silentlyRename; this.maxUploadSize = maxUploadSize; - this.defaultCharEncoding = defaultCharEncoding; + this.defaultCharEncoding = defaultCharEncoding; + + if (saveUploadedFilesToDisk) { + // Empty the contents of the upload directory + File[] files = uploadDirectory.listFiles(); + for (int i = 0; i < files.length; i++) { + files[i].delete(); + } + } } /**