sylvain     2003/11/13 07:02:08

  Modified:    src/java/org/apache/cocoon/servlet/multipart
                        MultipartHttpServletRequest.java Part.java
                        PartInMemory.java PartOnDisk.java
                        RequestFactory.java
  Added:       src/java/org/apache/cocoon/acting CopySourceAction.java
  Log:
  Port changes made to the 2.1 branch
  
  Revision  Changes    Path
  1.1                  
cocoon-2.2/src/java/org/apache/cocoon/acting/CopySourceAction.java
  
  Index: CopySourceAction.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.acting;
  
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.Map;
  
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.thread.ThreadSafe;
  import org.apache.cocoon.environment.Redirector;
  import org.apache.excalibur.source.ModifiableSource;
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceResolver;
  
  /**
   * The CopySourceAction copies the content of it's "src" attribute to its 
"dest" parameter.
   * The destination must of course resolve to a <code>WriteableSource</code>
   * <p>
   * Example :
   * <pre>
   *   &lt;map:act type="copy-source" src="cocoon://pipeline.xml"&gt;
   *     &lt;map:parameter name="dest" 
value="context://WEB-INF/data/file.xml"/&gt;
   *     .../...
   *   &lt;/map:act&gt;
   *</pre>
   *
   * @author <a href="http://www.apache.org/~sylvain/";>Sylvain Wallez</a>
   * @version CVS $Id: CopySourceAction.java,v 1.1 2003/11/13 15:02:07 sylvain 
Exp $
   */
  public class CopySourceAction extends ServiceableAction implements ThreadSafe
  {
      
      private SourceResolver resolver;
  
      public void service(ServiceManager manager) throws ServiceException {
          super.service(manager);
          this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
      }
      
      public Map act(Redirector redirector, 
org.apache.cocoon.environment.SourceResolver oldResolver, Map objectModel, 
String source, Parameters par)
          throws Exception {
          
          // Get source and destination Sources
          Source src = resolver.resolveURI(source);
          Source dest = resolver.resolveURI(par.getParameter("dest"));
          
          // Check that dest is writeable
          if (! (dest instanceof ModifiableSource)) {
              throw new IllegalArgumentException("Non-writeable URI : " + 
dest.getURI());
          }
          
          ModifiableSource wdest = (ModifiableSource)dest;
          
          // Get streams
          InputStream is = src.getInputStream();
          OutputStream os = wdest.getOutputStream();
          
          // And transfer all content.
          try {
              byte[] buffer = new byte[1024];
              int len;
              while ((len = is.read(buffer, 0, buffer.length)) > 0) {
                  os.write(buffer, 0, len);
              }
              os.close();
          } catch(Exception e) {
              if (wdest.canCancel(os)) {
                  wdest.cancel(os);
              }
          } finally {
              is.close();
          }
          // Success !
          return EMPTY_MAP;
      }
  }
  
  
  
  1.5       +6 -4      
cocoon-2.2/src/java/org/apache/cocoon/servlet/multipart/MultipartHttpServletRequest.java
  
  Index: MultipartHttpServletRequest.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.2/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 15:02:07 -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.2/src/java/org/apache/cocoon/servlet/multipart/Part.java
  
  Index: Part.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.2/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 15:02:07 -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.2/src/java/org/apache/cocoon/servlet/multipart/PartInMemory.java
  
  Index: PartInMemory.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.2/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 15:02:07 -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.2/src/java/org/apache/cocoon/servlet/multipart/PartOnDisk.java
  
  Index: PartOnDisk.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.2/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 15:02:07 -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.2/src/java/org/apache/cocoon/servlet/multipart/RequestFactory.java
  
  Index: RequestFactory.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.2/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 15:02:07 -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();
  +           }
  +       }
       }
   
       /**
  
  
  

Reply via email to