lumpynose

google app engine multipartwrapper

Mar 1st, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.38 KB | None | 0 0
  1. package com.objecteffects.clublist.web.stripes;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.Enumeration;
  9. import java.util.HashMap;
  10. import java.util.Hashtable;
  11. import java.util.List;
  12. import java.util.Map;
  13.  
  14. import javax.servlet.http.HttpServletRequest;
  15.  
  16. import net.sourceforge.stripes.action.FileBean;
  17. import net.sourceforge.stripes.controller.FileUploadLimitExceededException;
  18. import net.sourceforge.stripes.controller.multipart.MultipartWrapper;
  19.  
  20. import org.apache.commons.fileupload.FileItemIterator;
  21. import org.apache.commons.fileupload.FileItemStream;
  22. import org.apache.commons.fileupload.FileUploadBase;
  23. import org.apache.commons.fileupload.FileUploadException;
  24. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  25. import org.apache.commons.fileupload.util.Streams;
  26. import org.apache.commons.io.IOUtils;
  27.  
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30.  
  31. /**
  32.  * A Stripes MultipartWrapper for Google App Engine (GAE). On GAE the
  33.  * standard MultipartWrapper can't be used because it wants to save
  34.  * the incoming file on the local filesystem and writing to the local
  35.  * filesystem isn't allowed on GAE.
  36.  *
  37.  * @see <a href="http://blog.cloudme.org/2009/11/">cloudme</a>
  38.  * @author Moritz Petersen
  39.  */
  40. public final class GaeMultipartWrapper implements MultipartWrapper {
  41.     private final transient Logger log = LoggerFactory.getLogger(this
  42.             .getClass());
  43.  
  44.     /**
  45.      * Ensure this class will not load unless Commons FileUpload is on
  46.      * the classpath.
  47.      */
  48.     static {
  49.         FileUploadException.class.getName();
  50.     }
  51.  
  52.     private final Hashtable<String, FileBean> files =
  53.             new Hashtable<String, FileBean>();
  54.     private final Hashtable<String, String[]> parameters =
  55.             new Hashtable<String, String[]>();
  56.  
  57.     @Override
  58.     public void build(final HttpServletRequest request, final File tempDir,
  59.             final long maxPostSize) throws IOException,
  60.             FileUploadLimitExceededException {
  61.         try {
  62.             final ServletFileUpload upload = new ServletFileUpload();
  63.  
  64.             upload.setSizeMax(maxPostSize);
  65.  
  66.             final Map<String, List<String>> params =
  67.                     new HashMap<String, List<String>>();
  68.  
  69.             for (final FileItemIterator it = upload.getItemIterator(request); it
  70.                     .hasNext();) {
  71.                 final FileItemStream item = it.next();
  72.  
  73.                 final String name = item.getFieldName();
  74.  
  75.                 // If it's a form field, add the string value to the
  76.                 // list
  77.                 final InputStream stream = item.openStream();
  78.  
  79.                 if (item.isFormField()) {
  80.                     this.log.debug("Form field {} detected.", name);
  81.  
  82.                     List<String> values = params.get(item.getFieldName());
  83.  
  84.                     if (values == null) {
  85.                         values = new ArrayList<String>();
  86.                         params.put(item.getFieldName(), values);
  87.                     }
  88.  
  89.                     final String charset = request.getCharacterEncoding();
  90.  
  91.                     values.add(charset == null ? Streams.asString(stream)
  92.                             : Streams.asString(stream, charset));
  93.                 }
  94.                 else {
  95.                     // Else store the file param
  96.                     this.log.debug("File field {} with file name {} detected.",
  97.                             name, item.getName());
  98.  
  99.                     this.files.put(
  100.                             item.getFieldName(),
  101.                             new FileBean(null, item.getContentType(), item
  102.                                     .getName()) {
  103.                                 final byte[] buffer = IOUtils
  104.                                         .toByteArray(stream);
  105.  
  106.                                 @Override
  107.                                 public long getSize() {
  108.                                     return (this.buffer.length);
  109.                                 }
  110.  
  111.                                 @Override
  112.                                 public InputStream getInputStream()
  113.                                         throws IOException {
  114.                                     return (new ByteArrayInputStream(
  115.                                             this.buffer));
  116.                                 }
  117.  
  118.                                 @Override
  119.                                 public void save(final File toFile)
  120.                                         throws IOException {
  121.                                     throw (new UnsupportedOperationException());
  122.                                 }
  123.  
  124.                                 @Override
  125.                                 public void delete() throws IOException {
  126.                                     throw (new UnsupportedOperationException());
  127.                                 }
  128.                             });
  129.                 }
  130.  
  131.                 stream.close();
  132.             }
  133.  
  134.             // Now convert them down into the usual map of
  135.             // String->String[]
  136.             for (final Map.Entry<String, List<String>> entry : params
  137.                     .entrySet()) {
  138.                 final List<String> values = entry.getValue();
  139.                 this.parameters.put(entry.getKey(),
  140.                         values.toArray(new String[values.size()]));
  141.             }
  142.         }
  143.         catch (final FileUploadBase.SizeLimitExceededException slee) {
  144.             throw (new FileUploadLimitExceededException(maxPostSize,
  145.                     slee.getActualSize()));
  146.         }
  147.         catch (final FileUploadException fue) {
  148.             final IOException ioe =
  149.                     new IOException(
  150.                             "Could not parse and cache file upload data.");
  151.             ioe.initCause(fue);
  152.  
  153.             throw (ioe);
  154.         }
  155.  
  156.     }
  157.  
  158.     @Override
  159.     public Enumeration<String> getParameterNames() {
  160.         return (this.parameters.keys());
  161.     }
  162.  
  163.     @Override
  164.     public String[] getParameterValues(final String name) {
  165.         return (this.parameters.get(name));
  166.     }
  167.  
  168.     @Override
  169.     public Enumeration<String> getFileParameterNames() {
  170.         return (this.files.keys());
  171.     }
  172.  
  173.     @Override
  174.     public FileBean getFileParameterValue(final String name) {
  175.         return (this.files.get(name));
  176.     }
  177. }
Add Comment
Please, Sign In to add comment