001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ugc;
029
030import org.opencms.main.CmsLog;
031import org.opencms.ugc.shared.CmsUgcConstants;
032import org.opencms.util.CmsRequestUtil;
033import org.opencms.util.CmsStringUtil;
034
035import java.util.List;
036import java.util.Map;
037import java.util.concurrent.ConcurrentHashMap;
038
039import javax.servlet.http.HttpServletRequest;
040
041import org.apache.commons.fileupload.FileItem;
042import org.apache.commons.logging.Log;
043
044import com.google.common.collect.Maps;
045
046/**
047 * A helper class which processes and stores uploaded form data belonging to a single form edit session.<p>
048 */
049public class CmsUgcUploadHelper {
050
051    /** Log instance for this class. */
052    private static final Log LOG = CmsLog.getLog(CmsUgcUploadHelper.class);
053
054    /** The stored form data. */
055    private ConcurrentHashMap<String, List<FileItem>> m_storedFormData = new ConcurrentHashMap<String, List<FileItem>>();
056
057    /**
058     * Passes the form data with the given ID to the handler object, then removes it and deletes its stored data.<p>
059     *
060     * The form data is removed even if an exception is thrown while calling the form data handler.
061     *
062     * @param formDataId the id of the form data to process
063     * @param handler the handler to which the form data should be passed
064     * @throws Exception if something goes wrong
065     */
066    public void consumeFormData(String formDataId, I_CmsFormDataHandler handler) throws Exception {
067
068        List<FileItem> items = m_storedFormData.get(formDataId);
069
070        if (items != null) {
071            Map<String, I_CmsFormDataItem> itemMap = Maps.newHashMap();
072            LOG.debug(formDataId + ": Processing file items");
073            for (FileItem item : items) {
074                LOG.debug(formDataId + ": " + item.toString());
075                if (!item.isFormField() && CmsStringUtil.isEmptyOrWhitespaceOnly(item.getName())) {
076                    LOG.debug(formDataId + ": skipping previous file field because it is empty");
077                } else {
078                    itemMap.put(item.getFieldName(), new CmsUgcDataItem(item));
079                }
080            }
081            Exception storedException = null;
082            try {
083                handler.handleFormData(itemMap);
084            } catch (Exception e) {
085                storedException = e;
086            }
087            for (FileItem item : items) {
088                item.delete();
089            }
090            m_storedFormData.remove(formDataId);
091            if (storedException != null) {
092                throw storedException;
093            }
094        }
095    }
096
097    /**
098     * Processes a request caused by a form submit.<p>
099     *
100     * @param request the request to process
101     */
102    void processFormSubmitRequest(HttpServletRequest request) {
103
104        String formDataId = getFormDataId(request);
105        List<FileItem> items = CmsRequestUtil.readMultipartFileItems(request);
106        m_storedFormData.put(formDataId, items);
107    }
108
109    /**
110     * Gets the id to be used for storing form data from the request.<p>
111     *
112     * @param request the request containing the form data
113     *
114     * @return the id to use for storing the form data
115     */
116    private String getFormDataId(HttpServletRequest request) {
117
118        String result = request.getParameter(CmsUgcConstants.PARAM_FORM_DATA_ID);
119        return result;
120    }
121
122}