de.egore911.opengate.FileuploadServletRequest.java Source code

Java tutorial

Introduction

Here is the source code for de.egore911.opengate.FileuploadServletRequest.java

Source

/*
 * Copyright (c) 2013 Christoph Brill
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package de.egore911.opengate;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

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.io.IOUtils;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.users.User;

import de.egore911.opengate.model.AbstractEntity;
import de.egore911.opengate.model.BinaryData;

public class FileuploadServletRequest {

    private Map<String, Object> parameters;
    private HttpServletRequest request;
    private Map<String, List<String>> parameterValues;

    public FileuploadServletRequest(Map<String, Object> parameters, Map<String, List<String>> parameterValues) {
        this.parameters = parameters;
        this.parameterValues = parameterValues;
        this.request = null;
    }

    public FileuploadServletRequest(HttpServletRequest request) {
        this.parameters = null;
        this.parameterValues = null;
        this.request = request;
    }

    public static FileuploadServletRequest wrap(HttpServletRequest req, User user)
            throws ServletException, IOException {
        if (req.getContentType().toLowerCase(Locale.ENGLISH).startsWith(FileUploadBase.MULTIPART)) {
            Map<String, Object> parameters = new HashMap<String, Object>();
            Map<String, List<String>> parameterValues = new HashMap<String, List<String>>();
            ServletFileUpload upload = new ServletFileUpload();
            try {
                FileItemIterator iter = upload.getItemIterator(req);
                while (iter.hasNext()) {
                    FileItemStream stream = iter.next();
                    InputStream s = stream.openStream();
                    try {
                        String fieldName = stream.getFieldName();
                        if (stream.isFormField()) {
                            if (parameters.containsKey(fieldName)) {
                                if (!parameterValues.containsKey(fieldName)) {
                                    parameterValues.put(fieldName, new ArrayList<String>());
                                    parameterValues.get(fieldName).add((String) parameters.get(fieldName));
                                }
                                parameterValues.get(fieldName).add(IOUtils.toString(s));
                            } else {
                                parameters.put(fieldName, IOUtils.toString(s));
                            }
                        } else {
                            byte[] byteArray = IOUtils.toByteArray(s);
                            if (byteArray.length > 0) {
                                BinaryData binaryData = new BinaryData();
                                binaryData.setData(new Blob(byteArray));
                                binaryData.setMimetype(stream.getContentType());
                                binaryData.setFilename(stream.getName());
                                binaryData.setSize(byteArray.length);
                                AbstractEntity.setCreated(binaryData, user);
                                parameters.put(fieldName, binaryData);
                            }
                        }
                    } finally {
                        s.close();
                    }
                }
            } catch (FileUploadException e) {
                throw new ServletException(e);
            }
            return new FileuploadServletRequest(parameters, parameterValues);
        } else {
            return new FileuploadServletRequest(req);
        }
    }

    public String getParameter(String string) {
        if (request == null) {
            Object o = parameters.get(string);
            if (o == null || o instanceof String) {
                return (String) o;
            } else {
                throw new IllegalArgumentException(string + " is a " + o.getClass());
            }
        } else {
            return request.getParameter(string);
        }
    }

    public String[] getParameterValues(String string) {
        if (request == null) {
            List<String> list = parameterValues.get(string);
            if (list == null) {
                String object = (String) parameters.get(string);
                if (object == null) {
                    return null;
                }
                return new String[] { object };
            }
            return list.toArray(new String[list.size()]);
        } else {
            return request.getParameterValues(string);
        }
    }

    public BinaryData getFile(String string) {
        if (request == null) {
            Object o = parameters.get(string);
            if (o == null || o instanceof BinaryData) {
                return (BinaryData) o;
            } else {
                throw new IllegalArgumentException(string + " is a " + o.getClass());
            }
        } else {
            throw new IllegalArgumentException("getFile is not allowed on non-multipart requests");
        }
    }
}