webwork/src/main/webwork/multipart WebworkMultiPartRequest.java,NONE,1.1 ProgressMonitor.java,NONE,1.1

[email protected] Sat, 27 Dec 2003 15:15:06 -0800
Newsgroups gmane.comp.java.open-symphony.cvs
Message-ID <[email protected]>
Update of /cvsroot/opensymphony/webwork/src/main/webwork/multipart
In directory sc8-pr-cvs1:/tmp/cvs-serv9111/src/main/webwork/multipart

Added Files:
	WebworkMultiPartRequest.java ProgressMonitor.java 
Log Message:
Progress bean
Wrapper for internal webwork multipart parsing

--- NEW FILE: WebworkMultiPartRequest.java ---
/*
 * WebWork, Web Application Framework
 *
 * Distributable under Apache license.
 * See terms of license at opensource.org
 */
package webwork.multipart;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.LogFactory;
import webwork.config.Configuration;
import webwork.multipart.parser.MultipartListener;
import webwork.multipart.parser.MultipartRequest;

public class WebworkMultiPartRequest extends MultiPartRequest implements MultipartListener
{
  private ProgressMonitor monitor;

  private MultipartRequest multi;

  /**
   * The encoding is looked up from the configuration setting 'webwork.i18n.encoding'.  This is usually set in
   * default.properties & webwork.properties.
   */
  private String getEncoding()
  {
    try
    {
      String encoding = Configuration.getString("webwork.i18n.encoding");
      if(encoding != null)
      {
        return encoding;
      }
    }
    catch(IllegalArgumentException e)
    {
      LogFactory.getLog(WebworkMultiPartRequest.class).info(
        "Could not get encoding property 'webwork.i18n.encoding' for file upload.  Using system default");
    }
    return null;
  }

  /**
   * @param maxSize maximum size post allowed
   * @param saveDir the directory to save off the file
   * @param req     the request containing the multipart
   */
  public WebworkMultiPartRequest(HttpServletRequest req, String saveDir, int maxSize) throws IOException
  {
    boolean memoryUpload = false;
    try
    {
      memoryUpload = "true".equalsIgnoreCase(Configuration.getString("webwork.multipart.uploadToMemory"));
    }
    catch(IllegalArgumentException e)
    {
      LogFactory.getLog(WebworkMultiPartRequest.class).info(
        "Could not read webwork.multipart.uploadToMemory property for file upload. Defaulting to false");
    }

    monitor = new ProgressMonitor();
    req.getSession().setAttribute("progress", monitor);

    if(memoryUpload)
    {
      multi = new MultipartRequest(req.getContentType(),
                                   req.getContentLength(),
                                   req.getInputStream(),
                                   maxSize,
                                   MultipartRequest.IGNORE_FILES_IF_MAX_BYES_EXCEEDED,
                                   getEncoding(),
                                   this);
    }
    else
    {
      multi = new MultipartRequest(req.getContentType(),
                                   req.getContentLength(),
                                   req.getInputStream(),
                                   saveDir,
                                   maxSize,
                                   MultipartRequest.IGNORE_FILES_IF_MAX_BYES_EXCEEDED,
                                   getEncoding(),
                                   this);
    }
    req.getSession().removeAttribute("progress");
  }

  public Enumeration getParameterNames()
  {
    return multi.getParameterNames();
  }

  public String getParameter(String name)
  {
    return multi.getURLParameter(name);
  }

  public String[] getParameterValues(String name)
  {
    Enumeration enum = multi.getURLParameters(name);
    if(!enum.hasMoreElements())
    {
      return null;
    }

    List values = new ArrayList();
    while(enum.hasMoreElements())
    {
      values.add(enum.nextElement());
    }
    return (String[])values.toArray(new String[values.size()]);
  }

  public Enumeration getFileNames()
  {
    return multi.getFileParameterNames();
  }

  public String getFilesystemName(String name)
  {
    return multi.getBaseFilename(name);
  }

  public String getContentType(String name)
  {
    return multi.getContentType(name);
  }

  public File getFile(String name)
  {
    return multi.getFile(name);
  }

  public InputStream getMemoryFileContents(String name)
  {
    return multi.getFileContents(name);
  }

  public void fileCompleted(String fileName)
  {
    monitor.setCompleted(true);
  }

  public void fileStarted(String fileName, long size)
  {
    monitor.setFileName(fileName);
    monitor.setSize(size);
  }

  public void dataRead(String fileName, int amount)
  {
    monitor.setRead(monitor.getRead() + amount);
  }

}

--- NEW FILE: ProgressMonitor.java ---
package webwork.multipart;

/**
 * @author Hani Suleiman ([email protected])
 *         Date: Dec 20, 2003
 *         Time: 11:06:59 PM
 */
class ProgressMonitor
{
  private String fileName;
  private int read;
  private long size;
  private boolean completed;

  public String getFileName()
  {
    return fileName;
  }

  public void setFileName(String fileName)
  {
    this.fileName = fileName;
  }

  public int getRead()
  {
    return read;
  }

  public void setRead(int read)
  {
    this.read = read;
  }

  public long getRemaining()
  {
    return size - read;
  }

  public long getSize()
  {
    return size;
  }

  public void setSize(long size)
  {
    this.size = size;
  }

  public boolean isCompleted()
  {
    return completed;
  }

  public void setCompleted(boolean completed)
  {
    this.completed = completed;
  }
}




-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click