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.gwt.client.ui.input.upload.impl;
029
030import org.opencms.gwt.client.ui.input.upload.CmsFileInfo;
031import org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog;
032import org.opencms.gwt.client.ui.input.upload.I_CmsUploader;
033import org.opencms.gwt.shared.I_CmsUploadConstants;
034
035import java.util.List;
036
037import com.google.gwt.dom.client.InputElement;
038import com.google.gwt.dom.client.Style.Display;
039import com.google.gwt.http.client.URL;
040import com.google.gwt.user.client.ui.FlowPanel;
041import com.google.gwt.user.client.ui.FormPanel;
042import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
043import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
044import com.google.gwt.user.client.ui.Hidden;
045import com.google.gwt.user.client.ui.Panel;
046import com.google.gwt.user.client.ui.RootPanel;
047
048/**
049 * The default uploader implementation. Use if the file API is not available.<p>
050 */
051public class CmsUploaderDefault implements I_CmsUploader {
052
053    /**
054     * Implements the submit handler (Used for browsers that don't support file api).<p>
055     */
056    private class CmsUploadHandler implements SubmitCompleteHandler {
057
058        /** The upload dialog instance. */
059        private I_CmsUploadDialog m_dialog;
060
061        /** The submitted form. */
062        private FormPanel m_form;
063
064        /**
065         * The default constructor.<p>
066         *
067         * @param dialog the upload dialog instance
068         * @param form the submitted form
069         */
070        public CmsUploadHandler(I_CmsUploadDialog dialog, FormPanel form) {
071
072            m_dialog = dialog;
073            m_form = form;
074        }
075
076        /**
077         * @see com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler#onSubmitComplete(com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent)
078         */
079        public void onSubmitComplete(SubmitCompleteEvent event) {
080
081            m_dialog.parseResponse(event.getResults());
082            m_form.removeFromParent();
083        }
084    }
085
086    /**
087     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploader#uploadFiles(java.lang.String, java.lang.String, boolean, java.lang.String, java.util.List, java.util.List, org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog)
088     */
089    public void uploadFiles(
090        String uploadUri,
091        String targetFolder,
092        boolean isRootPath,
093        String postCreateHandler,
094        List<CmsFileInfo> filesToUpload,
095        List<String> filesToUnzip,
096        boolean keepFileNames,
097        I_CmsUploadDialog dialog) {
098
099        FormPanel form = createForm(
100            uploadUri,
101            targetFolder,
102            isRootPath,
103            postCreateHandler,
104            filesToUpload,
105            filesToUnzip);
106        form.addSubmitCompleteHandler(new CmsUploadHandler(dialog, form));
107        form.getElement().getStyle().setDisplay(Display.NONE);
108        RootPanel.get().add(form);
109        form.submit();
110    }
111
112    /**
113     * Creates a hidden input field with the given name and value and adds it to the form panel.<p>
114     *
115     * @param form the form panel
116     * @param fieldName the field name
117     * @param fieldValue the field value
118     */
119    private void addHiddenField(Panel form, String fieldName, String fieldValue) {
120
121        Hidden inputField = new Hidden();
122        inputField.setName(fieldName);
123        inputField.setValue(fieldValue);
124        form.add(inputField);
125    }
126
127    /**
128     * Creates a form to submit the upload files.<p>
129     *
130     * @param uploadUri the upload URI
131     * @param targetFolder the target folder
132     * @param isRootPath if the target folder is given as a root path
133     * @param postCreateHandler the post-create handler
134     * @param filesToUpload the files to upload
135     * @param filesToUnzip the files to unzip
136     * @return the created form panel
137     */
138    private FormPanel createForm(
139        String uploadUri,
140        String targetFolder,
141        boolean isRootPath,
142        String postCreateHandler,
143        List<CmsFileInfo> filesToUpload,
144        List<String> filesToUnzip) {
145
146        // create a form using the POST method and multipart MIME encoding
147        FormPanel form = new FormPanel();
148        form.setAction(uploadUri);
149        form.setEncoding(FormPanel.ENCODING_MULTIPART);
150        form.setMethod(FormPanel.METHOD_POST);
151        // create a panel that contains the file input fields and the target folder
152        FlowPanel inputFieldsPanel = new FlowPanel();
153        int count = 0;
154        for (CmsFileInfo info : filesToUpload) {
155            InputElement input = info.getInputElement();
156            String fieldName = "file_" + count++;
157            input.setName(fieldName);
158            inputFieldsPanel.getElement().appendChild(input);
159            addHiddenField(
160                inputFieldsPanel,
161                fieldName + I_CmsUploadConstants.UPLOAD_FILENAME_ENCODED_SUFFIX,
162                URL.encode(info.getOverrideFileName()));
163        }
164        for (String filename : filesToUnzip) {
165            addHiddenField(inputFieldsPanel, I_CmsUploadConstants.UPLOAD_UNZIP_FILES_FIELD_NAME, URL.encode(filename));
166        }
167        addHiddenField(inputFieldsPanel, I_CmsUploadConstants.UPLOAD_TARGET_FOLDER_FIELD_NAME, targetFolder);
168        if (postCreateHandler != null) {
169            addHiddenField(inputFieldsPanel, I_CmsUploadConstants.POST_CREATE_HANDLER, postCreateHandler);
170        }
171        addHiddenField(inputFieldsPanel, I_CmsUploadConstants.UPLOAD_IS_ROOT_PATH_FIELD_NAME, "" + isRootPath);
172        form.setWidget(inputFieldsPanel);
173        return form;
174    }
175
176}