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 GmbH & Co. KG, 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.workplace.tools.modules;
029
030import org.opencms.configuration.CmsConfigurationException;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.main.CmsSystemInfo;
036import org.opencms.main.OpenCms;
037import org.opencms.module.CmsModule;
038import org.opencms.module.CmsModuleDependency;
039import org.opencms.module.CmsModuleImportExportHandler;
040import org.opencms.module.CmsModuleManager;
041import org.opencms.workplace.administration.A_CmsImportFromHttp;
042import org.opencms.workplace.tools.CmsToolDialog;
043import org.opencms.workplace.tools.CmsToolManager;
044
045import java.io.File;
046import java.io.IOException;
047import java.util.HashMap;
048import java.util.List;
049import java.util.Map;
050
051import javax.servlet.ServletException;
052import javax.servlet.http.HttpServletRequest;
053import javax.servlet.http.HttpServletResponse;
054import javax.servlet.jsp.PageContext;
055
056import org.apache.commons.logging.Log;
057
058/**
059 * Class to upload a module with HTTP upload.<p>
060 *
061 * @since 6.0.0
062 */
063public class CmsModulesUploadFromHttp extends A_CmsImportFromHttp {
064
065    /** The dialog URI. */
066    public static final String DIALOG_URI = PATH_WORKPLACE + "admin/modules/modules_import.jsp";
067
068    /** Modulename parameter. */
069    public static final String PARAM_MODULE = "module";
070
071    /** The log object for this class. */
072    private static final Log LOG = CmsLog.getLog(CmsModulesUploadFromHttp.class);
073
074    /**
075     * Public constructor with JSP action element.<p>
076     *
077     * @param jsp an initialized JSP action element
078     */
079    public CmsModulesUploadFromHttp(CmsJspActionElement jsp) {
080
081        super(jsp);
082    }
083
084    /**
085     * Public constructor with JSP variables.<p>
086     *
087     * @param context the JSP page context
088     * @param req the JSP request
089     * @param res the JSP response
090     */
091    public CmsModulesUploadFromHttp(PageContext context, HttpServletRequest req, HttpServletResponse res) {
092
093        this(new CmsJspActionElement(context, req, res));
094    }
095
096    /**
097     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#actionCommit()
098     */
099    @Override
100    public void actionCommit() throws IOException, ServletException {
101
102        try {
103            copyFileToServer(
104                OpenCms.getSystemInfo().getPackagesRfsPath() + File.separator + CmsSystemInfo.FOLDER_MODULES);
105        } catch (CmsException e) {
106            // error copying the file to the OpenCms server
107            if (LOG.isErrorEnabled()) {
108                LOG.error(e.getLocalizedMessage(getLocale()), e);
109            }
110            setException(e);
111            return;
112        }
113        /// copied
114        CmsConfigurationException exception = null;
115        CmsModule module = null;
116        try {
117            String importpath = OpenCms.getSystemInfo().getPackagesRfsPath();
118            importpath = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
119                importpath + "modules/" + getParamImportfile());
120            module = CmsModuleImportExportHandler.readModuleFromImport(importpath);
121
122            // check if all dependencies are fulfilled
123            List dependencies = OpenCms.getModuleManager().checkDependencies(
124                module,
125                CmsModuleManager.DEPENDENCY_MODE_IMPORT);
126            if (!dependencies.isEmpty()) {
127                StringBuffer dep = new StringBuffer(32);
128                for (int i = 0; i < dependencies.size(); i++) {
129                    CmsModuleDependency dependency = (CmsModuleDependency)dependencies.get(i);
130                    dep.append("\n - ");
131                    dep.append(dependency.getName());
132                    dep.append(" (Version: ");
133                    dep.append(dependency.getVersion());
134                    dep.append(")");
135                }
136                exception = new CmsConfigurationException(
137                    Messages.get().container(
138                        Messages.ERR_ACTION_MODULE_DEPENDENCY_2,
139                        getParamImportfile(),
140                        new String(dep)));
141            }
142        } catch (CmsConfigurationException e) {
143            exception = e;
144        }
145
146        if ((module != null) && (exception == null)) {
147
148            // refresh the list
149            Map objects = (Map)getSettings().getListObject();
150            if (objects != null) {
151                objects.remove(CmsModulesList.class.getName());
152            }
153
154            // redirect
155            Map param = new HashMap();
156            param.put(CmsModulesList.PARAM_MODULE, getParamImportfile());
157            param.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
158            param.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/modules"));
159            if (OpenCms.getModuleManager().hasModule(module.getName())) {
160                param.put(CmsModulesUploadFromServer.PARAM_MODULENAME, module.getName());
161                getToolManager().jspForwardPage(this, CmsModulesUploadFromServer.REPLACE_ACTION_REPORT, param);
162            } else {
163                getToolManager().jspForwardPage(this, CmsModulesUploadFromServer.IMPORT_ACTION_REPORT, param);
164            }
165        } else {
166            if (exception != null) {
167                // log it
168                if (LOG.isErrorEnabled()) {
169                    LOG.error(exception.getLocalizedMessage(getLocale()), exception);
170                }
171                // then throw to avoid blank page telling nothing due to missing forward
172                throw new CmsRuntimeException(exception.getMessageContainer(), exception);
173            }
174        }
175    }
176
177    /**
178     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#getDialogReturnUri()
179     */
180    @Override
181    public String getDialogReturnUri() {
182
183        return DIALOG_URI;
184    }
185
186    /**
187     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#getImportMessage()
188     */
189    @Override
190    public String getImportMessage() {
191
192        return key(Messages.GUI_MODULES_IMPORT_FILE_0);
193    }
194
195    /**
196     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#getStarttext()
197     */
198    @Override
199    public String getStarttext() {
200
201        return key(Messages.GUI_MODULES_IMPORT_BLOCK_0);
202    }
203
204    /**
205     * @see org.opencms.workplace.CmsWorkplace#initMessages()
206     */
207    @Override
208    protected void initMessages() {
209
210        // add specific dialog resource bundle
211        addMessages(Messages.get().getBundleName());
212        // add default resource bundles
213        addMessages(org.opencms.workplace.Messages.get().getBundleName());
214        addMessages(org.opencms.workplace.tools.Messages.get().getBundleName());
215    }
216}