Login | Register
My pages Projects Community openCollabNet

Discussions > cvs > CVS update: joist/java/org/joist/util FileUploadServlet.java FileUpload.java RenameableFileUpload.java

Project highlights: Architectural Overview

joist
Discussion topic

Back to topic list

CVS update: joist/java/org/joist/util FileUploadServlet.java FileUpload.java RenameableFileUpload.java

Author dlr
Full name Daniel Rall
Date 2000-11-13 14:02:05 PST
Message User: dlr
  Date: 00/11/13 14:02:05

  Modified: java/org/joist/util FileUpload.java
                        RenameableFileUpload.java
  Added: java/org/joist/util FileUploadServlet.java
  Log:
  Refactored FileUpload and RenameableFileUpload to be utility classes (rather than servlets). Created an abstract file upload servlet as a starting point for and example of using these utility classes.
  
  Revision Changes Path
  1.8 +57 -44 joist/java/org/joist​/util/FileUpload.jav​a
  
  Index: FileUpload.java
  ====================​====================​====================​=======
  RCS file: /cvs/joist/java/org/​joist/util/FileUploa​d.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FileUpload.java 2000/11/08 06:39:16 1.7
  +++ FileUpload.java 2000/11/13 22:02:04 1.8
  @@ -1,5 +1,5 @@
   /* ====================​====================​====================​====
  - * Copyright (c) 2000 Collab.Net. All rights reserved.
  + * Copyright (c) 2000 CollabNet. All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are
  @@ -14,7 +14,7 @@
    *
    * 3. The end-user documentation included with the redistribution, if
    * any, must include the following acknowlegement: "This product includes
  - * software developed by Collab.Net (http://www.Collab.Net/)."
  + * software developed by CollabNet (http://www.CollabNet/)."
    * Alternately, this acknowlegement may appear in the software itself, if
    * and wherever such third-party acknowlegements normally appear.
    *
  @@ -24,7 +24,7 @@
    *
    * 5. Products derived from this software may not use the "Tigris" name
    * nor may "Tigris" appear in their names without prior written
  - * permission of Collab.Net.
  + * permission of CollabNet.
    *
    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  @@ -41,7 +41,7 @@
    * ====================​====================​====================​========
    *
    * This software consists of voluntary contributions made by many
  - * individuals on behalf of Collab.Net.
  + * individuals on behalf of CollabNet.
    */
   
   package org.joist.util;
  @@ -60,7 +60,7 @@
   import com.oreilly.servlet.*;
   
   /**
  - * Decendants of this servlet are responsible for receiving file uploads.
  + * Decendants of this class are responsible for handling file uploads.
    * Note that the form submitting data to this servlet must have an encoding
    * type of <tt>multipart/​form-data</tt>​ (so that the post data will have a
    * content type of the same). Here is an example form:<br>
  @@ -70,13 +70,14 @@
    * Your file: <input type="file" name="file"> <input type="submit">
    * </form>
    * </pre>
  + * This class is <i>not</i> thread-safe.
    *
    * @author <a href="mailto:dlr@col​lab.net">Daniel L. Rall</a>
  - * @version $Id: FileUpload.java,v 1.7 2000/11/08 06:39:16 dlr Exp $
  + * @version $Id: FileUpload.java,v 1.8 2000/11/13 22:02:04 dlr Exp $
    *
  - * Copyright (c) 2000 Collab.Net, Inc.
  + * Copyright (c) 2000 CollabNet, Inc.
    */
  -public class FileUpload extends UnsecureServlet
  +public class FileUpload
   {
       /**
        * The log to write to.
  @@ -94,25 +95,33 @@
       private static final String REQ_CONTENT_TYPE = "multipart/form-data";
   
       /**
  - * Display the UI. If the content type of the HTTP request is in
  + * The misc. properties used by this file upload.
  + */
  + protected Properties myProperties;
  +
  + /**
  + * The uploaded <code>File</code> object(s).
  + */
  + private List files;
  +
  + /**
  + * If the content type of the HTTP request is in
        * <code>REQ_CONT​ENT_TYPE</code​>, attempt to processes it as a file
        * upload.
        *
  - * @param connection JDBC connection--not used by this method. Only here
  - * to implement the interface of UnsecureServlet.
  - * @param context The WebContext used by this request.
  - * @return The name of the template to use.
  + * @param conn JDBC connection--not used by this method. Only here to
  + * implement the interface of UnsecureServlet.
  + * @param ctx The WebContext used by this request.
  + * @param props The misc. information used by this file upload.
  + * @return The name of the template to use.
        *
        * @exception HandlerException
        * @exception RedirectException
        */
  - protected String processRequest (Connection connection, WebContext context)
  - throws HandlerException, RedirectException
  + public FileUpload (Connection conn, WebContext context, Properties props)
  + throws Exception
       {
  - if (debug) log.debug("start");
  -
  - // Let the superclass do it's part.
  - String templateName = super.processRequest​(connection, context);
  + this.myProperties = props;
           String contentType = context.getRequest()​.getContentType();
   
           // If the content type is not equal to REQ_CONTENT_TYPE, just display
  @@ -125,10 +134,10 @@
                   MultipartRequest mreq = getMultipartRequest(context);
   
                   // Get file handles and do any extra processing.
  - Vector files = processUploadedFiles​(connection, context, mreq);
  + this.files = processUploadedFiles(conn, context, mreq);
   
                   // Set permissions appropriately.
  - setFilesystemPermiss​ions(files);
  + setFilesystemPermissions();
   
                   // Add references to the saved files to the WebContext.
                   context.put("fileNames", files);
  @@ -144,15 +153,20 @@
           }
           else
           {
  - if (debug)
  - {
  - log.debug("FileUpload: Content type not specified or invalid: "
  - + contentType);
  - }
  + log.warning("Content type not specified or invalid: " +
  + contentType);
           }
  + }
   
  - if (debug) log.debug("done");
  - return templateName;
  + /**
  + * Gets the list of uploaded files, providing the <code>File</code>
  + * objects denoting the final save location of the uploads.
  + *
  + * @return The list of <code>File</code> objects from this upload.
  + */
  + public List getFiles ()
  + {
  + return files;
       }
   
       /**
  @@ -194,18 +208,18 @@
        *
        * @exception JoistException
        */
  - protected void setFilesystemPermissions (Vector files)
  + protected void setFilesystemPermissions ()
       throws JoistException
       {
       String perms = getFilesystemPermissions();
   
  - Enumeration enum = files.elements();
       String path;
       StringBuffer cmd = new StringBuffer();
  - while (enum.hasMoreElements())
  + int nbrFiles = files.size();
  + for (int i = 0; i < nbrFiles; i++)
       {
           // Build the command.
  - path = ((File)enum.nextElem​ent()).getPath();
  + path = ((File)files.get(i)).getPath();
           cmd.append("chmod ").append(perms).append(' ');
           cmd.append(path);
   
  @@ -240,25 +254,24 @@
   
       /**
        * Performs any additional processing that may be desired, then returns
  - * the names that the uploaded files were saved as.
  + * handles to the files that the uploaded files were saved as.
        *
  - * @param connection The JDBC connection to use.
  - * @param mreq The MultipartRequest to use.
  - * @param context The WebContext to use.
  - * @return The <code>File</code> objects the uploaded files were
  - * saved as.
  - */
  - protected Vector processUploadedFiles (Connection connection,
  - WebContext context,
  - MultipartRequest mreq)
  + * @param conn The JDBC database connection to use.
  + * @param mreq The MultipartRequest to use.
  + * @param ctx The WebContext to use.
  + * @return The <code>File</code> objects the uploaded files were
  + * saved as.
  + */
  + protected List processUploadedFiles (Connection conn, WebContext ctx,
  + MultipartRequest mreq)
       {
  - Vector files = new Vector();
  + List files = new ArrayList();
   
       // Iterate files, adding corresponding File objects for each.
       Enumeration fileNames = mreq.getFileNames();
       while (fileNames.hasMoreElements())
       {
  - files.addElement(mre​q.getFile((String)fi​leNames.nextElement(​)));
  + files.add(mreq.getFi​le((String)fileNames​.nextElement()));
       }
   
       return files;
  
  
  
  1.6 +15 -9 joist/java/org/joist​/util/RenameableFile​Upload.java
  
  Index: RenameableFileUpload.java
  ====================​====================​====================​=======
  RCS file: /cvs/joist/java/org/​joist/util/Renameabl​eFileUpload.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- RenameableFileUpload.java 2000/11/08 06:44:06 1.5
  +++ RenameableFileUpload.java 2000/11/13 22:02:04 1.6
  @@ -68,7 +68,7 @@
    * the file is on a different partition or physical disk.
    *
    * @author <a href="mailto:dlr@col​lab.net">Daniel L. Rall</a>
  - * @version $Id: RenameableFileUpload.java,v 1.5 2000/11/08 06:44:06 dlr Exp $
  + * @version $Id: RenameableFileUpload.java,v 1.6 2000/11/13 22:02:04 dlr Exp $
    *
    * Copyright (c) 2000 Collab.Net, Inc.
    */
  @@ -84,6 +84,13 @@
        */
       private static final boolean debug = false;
   
  + public RenameableFileUpload (Connection conn, WebContext ctx,
  + Properties props)
  + throws Exception
  + {
  + super(conn, ctx, props);
  + }
  +
       /**
        * Performs any additional processing that may be desired, then returns
        * the names that the uploaded files were saved as.
  @@ -93,11 +100,10 @@
        * @param context The context of user interaction.
        * @return The File objects the uploaded files were saved as.
        */
  - protected Vector processUploadedFiles (Connection conn,
  - WebContext context,
  - MultipartRequest mreq)
  + protected List processUploadedFiles (Connection conn, WebContext ctx,
  + MultipartRequest mreq)
       {
  - Vector files = new Vector();
  + List files = new ArrayList();
   
       // Iterate files, adding File objects corresponding to the destination
           // path for each.
  @@ -106,9 +112,9 @@
       {
               src = mreq.getFile((String​)fileNames.nextEleme​nt());
           dest = new File(getDestinationPath() + File.separator +
  - getSaveAsFileName(conn, context, src.getName()));
  + getSaveAsFileName(conn, ctx, src.getName()));
           src.renameTo(dest);
  - files.addElement(dest);
  + files.add(dest);
       }
   
       return files;
  @@ -119,12 +125,12 @@
        * implemented by subclasses.
        *
        * @param conn The JDBC connection to use.
  - * @param context The context of user interaction.
  + * @param ctx The context of user interaction.
        * @param clntFname The client supplied file name.
        * @return The name to save the file as.
        */
       protected abstract String getSaveAsFileName (Connection conn,
  - WebContext context,
  + WebContext ctx,
                            String clntFname);
   
       /**
  
  
  
  1.1 joist/java/org/joist​/util/FileUploadServ​let.java
  
  Index: FileUploadServlet.java
  ====================​====================​====================​=======
  /* ====================​====================​====================​====
   * Copyright (c) 2000 CollabNet. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement: "This product includes
   * software developed by CollabNet (http://www.CollabNet/)."
   * Alternately, this acknowlegement may appear in the software itself, if
   * and wherever such third-party acknowlegements normally appear.
   *
   * 4. The hosted project names must not be used to endorse or promote
   * products derived from this software without prior written
   * permission. For written permission, please contact info at collab dot net.
   *
   * 5. Products derived from this software may not use the "Tigris" name
   * nor may "Tigris" appear in their names without prior written
   * permission of CollabNet.
   *
   * 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 COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   * DAMAGES (INCLUDING, 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 CollabNet.
   */
  
  package org.joist.util;
  
  import java.io.*;
  import java.util.*;
  import java.sql.*;
  import javax.servlet.http.*;
  
  import org.webmacro.util.*;
  import org.webmacro.servlet.*;
  
  import org.joist.*;
  import org.joist.security.*;
  
  import com.oreilly.servlet.*;
  
  /**
   * Decendants of this servlet are responsible for receiving file uploads.
   * Note that the form submitting data to this servlet must have an encoding
   * type of <tt>multipart/​form-data</tt>​ (so that the post data will have a
   * content type of the same). Here is an example form:<br>
   * <pre>
   * <form action="/servlet/MyFileUpload" enctype="multipart/form-data"
   * method="post">
   * Your file: <input type="file" name="file"> <input type="submit">
   * </form>
   * </pre>
   *
   * @author <a href="mailto:dlr@col​lab.net">Daniel L. Rall</a>
   * @version $Id: FileUploadServlet.java,v 1.1 2000/11/13 22:02:04 dlr Exp $
   *
   * Copyright (c) 2000 CollabNet, Inc.
   */
  public abstract class FileUploadServlet extends UnsecureServlet
  {
      /**
       * The log to write to.
       */
      private static Log log = new Log("fUpSrv", "FileUploadServlet");
  
      /**
       * Whether to log debugging information.
       */
      private static final boolean debug = true;
     
      /**
       * Display the UI. If the content type of the HTTP request is
       * appropriate, attempt to processes it as a file upload.
       *
       * @param conn JDBC connection--not used by this method. Only here to
       * implement the interface of UnsecureServlet.
       * @param ctx The WebContext used by this request.
       * @return The name of the template to use.
       *
       * @exception HandlerException
       * @exception RedirectException
       */
      protected String processRequest (Connection conn, WebContext ctx)
      throws HandlerException, RedirectException
      {
      if (debug) log.debug("start");
  
      // Let the superclass do it's part.
      String templateName = super.processRequest(conn, ctx);
          String contentType = ctx.getRequest().get​ContentType();
  
          try
          {
              // Parse and save the uploaded files.
              FileUpload upload = new FileUpload(conn, ctx, myProperties);
  
              // Get a list of file handles.
              List files = upload.getFiles();
  
              // Add references to the saved files to the WebContext.
              ctx.put("fileNames", files);
          }
          catch (Exception e)
          {
              log.exception(e);
              throw new HandlerException(e.g​etMessage());
          }
  
      if (debug) log.debug("done");
      return templateName;
      }
  }

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

CVS update: joist/java/org/joist/util FileUploadServlet.java FileUpload.java RenameableFileUpload.java dlr Daniel Rall 2000-11-13 14:02:05 PST
Messages per page: