Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.objecteffects.clublist.web.stripes;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.HashMap;
- import java.util.Hashtable;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import net.sourceforge.stripes.action.FileBean;
- import net.sourceforge.stripes.controller.FileUploadLimitExceededException;
- import net.sourceforge.stripes.controller.multipart.MultipartWrapper;
- import org.apache.commons.fileupload.FileItemIterator;
- import org.apache.commons.fileupload.FileItemStream;
- import org.apache.commons.fileupload.FileUploadBase;
- import org.apache.commons.fileupload.FileUploadException;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import org.apache.commons.fileupload.util.Streams;
- import org.apache.commons.io.IOUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * A Stripes MultipartWrapper for Google App Engine (GAE). On GAE the
- * standard MultipartWrapper can't be used because it wants to save
- * the incoming file on the local filesystem and writing to the local
- * filesystem isn't allowed on GAE.
- *
- * @see <a href="http://blog.cloudme.org/2009/11/">cloudme</a>
- * @author Moritz Petersen
- */
- public final class GaeMultipartWrapper implements MultipartWrapper {
- private final transient Logger log = LoggerFactory.getLogger(this
- .getClass());
- /**
- * Ensure this class will not load unless Commons FileUpload is on
- * the classpath.
- */
- static {
- FileUploadException.class.getName();
- }
- private final Hashtable<String, FileBean> files =
- new Hashtable<String, FileBean>();
- private final Hashtable<String, String[]> parameters =
- new Hashtable<String, String[]>();
- @Override
- public void build(final HttpServletRequest request, final File tempDir,
- final long maxPostSize) throws IOException,
- FileUploadLimitExceededException {
- try {
- final ServletFileUpload upload = new ServletFileUpload();
- upload.setSizeMax(maxPostSize);
- final Map<String, List<String>> params =
- new HashMap<String, List<String>>();
- for (final FileItemIterator it = upload.getItemIterator(request); it
- .hasNext();) {
- final FileItemStream item = it.next();
- final String name = item.getFieldName();
- // If it's a form field, add the string value to the
- // list
- final InputStream stream = item.openStream();
- if (item.isFormField()) {
- this.log.debug("Form field {} detected.", name);
- List<String> values = params.get(item.getFieldName());
- if (values == null) {
- values = new ArrayList<String>();
- params.put(item.getFieldName(), values);
- }
- final String charset = request.getCharacterEncoding();
- values.add(charset == null ? Streams.asString(stream)
- : Streams.asString(stream, charset));
- }
- else {
- // Else store the file param
- this.log.debug("File field {} with file name {} detected.",
- name, item.getName());
- this.files.put(
- item.getFieldName(),
- new FileBean(null, item.getContentType(), item
- .getName()) {
- final byte[] buffer = IOUtils
- .toByteArray(stream);
- @Override
- public long getSize() {
- return (this.buffer.length);
- }
- @Override
- public InputStream getInputStream()
- throws IOException {
- return (new ByteArrayInputStream(
- this.buffer));
- }
- @Override
- public void save(final File toFile)
- throws IOException {
- throw (new UnsupportedOperationException());
- }
- @Override
- public void delete() throws IOException {
- throw (new UnsupportedOperationException());
- }
- });
- }
- stream.close();
- }
- // Now convert them down into the usual map of
- // String->String[]
- for (final Map.Entry<String, List<String>> entry : params
- .entrySet()) {
- final List<String> values = entry.getValue();
- this.parameters.put(entry.getKey(),
- values.toArray(new String[values.size()]));
- }
- }
- catch (final FileUploadBase.SizeLimitExceededException slee) {
- throw (new FileUploadLimitExceededException(maxPostSize,
- slee.getActualSize()));
- }
- catch (final FileUploadException fue) {
- final IOException ioe =
- new IOException(
- "Could not parse and cache file upload data.");
- ioe.initCause(fue);
- throw (ioe);
- }
- }
- @Override
- public Enumeration<String> getParameterNames() {
- return (this.parameters.keys());
- }
- @Override
- public String[] getParameterValues(final String name) {
- return (this.parameters.get(name));
- }
- @Override
- public Enumeration<String> getFileParameterNames() {
- return (this.files.keys());
- }
- @Override
- public FileBean getFileParameterValue(final String name) {
- return (this.files.get(name));
- }
- }
Add Comment
Please, Sign In to add comment