001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2009 Alkacon Software (http://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: http://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: http://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.workplace.tools.sites;
033
034import org.opencms.db.CmsExportPoint;
035import org.opencms.jsp.CmsJspActionElement;
036import org.opencms.main.OpenCms;
037import org.opencms.widgets.CmsInputWidget;
038import org.opencms.workplace.CmsWidgetDialog;
039import org.opencms.workplace.CmsWidgetDialogParameter;
040import org.opencms.workplace.tools.CmsToolDialog;
041
042import java.io.IOException;
043import java.util.HashMap;
044import java.util.Map;
045
046import javax.servlet.ServletException;
047import javax.servlet.http.HttpServletRequest;
048import javax.servlet.http.HttpServletResponse;
049import javax.servlet.jsp.PageContext;
050
051import org.apache.commons.lang3.SystemUtils;
052
053
054/**
055 * A dialog that allows to write the sites configured in OpenCms
056 * into a web server configuration file, using a template.<p>
057 *
058 * @since 9.0.0
059 */
060public class CmsSitesWebserverDialog extends CmsWidgetDialog {
061
062    /** Linux script name. */
063    public static final String DEFAULT_NAME_LINUX_SCRIPT = "script.sh";
064
065    /** Default web server configuration template file. */
066    public static final String DEFAULT_NAME_WEBSERVER_CONFIG = "vhost.template";
067
068    /** Windows script name. */
069    public static final String DEFAULT_NAME_WINDOWS_SCRIPT = "script.bat";
070
071    /** The module name constant. */
072    public static final String MODULE_NAME = "org.opencms.workplace.tools.sites";
073
074    /** Module path. */
075    public static final String MODULE_PATH = "/system/modules/" + MODULE_NAME + "/";
076
077    /** Defines which pages are valid for this dialog. */
078    public static final String[] PAGES = {"page1"};
079
080    /** Module parameter constant for the web server configuration template file. */
081    public static final String PARAM_CONFIG_TEMPLATE = "configtemplate";
082
083    /** A module parameter name for the prefix used for web server configuration files. */
084    public static final String PARAM_FILENAME_PREFIX = "filenameprefix";
085
086    /** The parameter name for the logging directory. */
087    public static final String PARAM_LOGGING_DIR = "loggingdir";
088
089    /** The parameter name of the template file for secure sites. */
090    public static final String PARAM_SECURE_TEMPLATE = "securetemplate";
091
092    /** Module parameter constant for the target path. */
093    public static final String PARAM_TARGET_PATH = "targetpath";
094
095    /** Module parameter constant for the web server script. */
096    public static final String PARAM_WEBSERVER_SCRIPT = "webserverscript";
097
098    /** The working directory for this tool. */
099    public static final String PATH_WEBSERVER_EXPORT = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
100        "server-scripts/");
101
102    /** Sample files folder name. */
103    public static final String TEMPLATE_FILES = "webserver-templates/";
104
105    /** The default file name of the secure server template configuration file. */
106    private static final String DEFAULT_NAME_WEBSERVER_SECURE = "vhost-secure.template";
107
108    /** The default parameter value. */
109    private static final String DEFAULT_PARAM_CONFIG_TEMPLATE = "/path/to/webserver/config.template";
110
111    /** The default prefix used for created web server configuration files, created by this tool. */
112    private static final String DEFAULT_PARAM_FILENAME_PREFIX = "opencms";
113
114    /** The default value for the logging directory parameter. */
115    private static final String DEFAULT_PARAM_LOGGING_DIR = "/path/to/logging/folder/";
116
117    /** The default parameter value. */
118    private static final String DEFAULT_PARAM_SECURE_TEMPLATE = "/path/to/webserver/secure-config.template";
119
120    /** The default parameter value. */
121    private static final String DEFAULT_PARAM_TARGET_PATH = "/path/to/config/target/";
122
123    /** The default parameter value. */
124    private static final String DEFAULT_PARAM_WEBSERVER_SCRIPT = "/path/to/webserver/script.sh";
125
126    /** The default path for apache2 log files on a Unix system. */
127    private static final String DEFAULT_PATH_LOG_LINUX = "/var/log/apache2/";
128
129    /** The default export point URI of the web server script (LINUX). */
130    private static final String DEFAULT_PATH_SCRIPT_LINUX = MODULE_PATH + TEMPLATE_FILES + DEFAULT_NAME_LINUX_SCRIPT;
131
132    /** The default export point URI of the web server script (LINUX). */
133    private static final String DEFAULT_PATH_SCRIPT_WIDNOWS = MODULE_PATH
134        + TEMPLATE_FILES
135        + DEFAULT_NAME_WINDOWS_SCRIPT;
136
137    /** The default export point URI of the web server template. */
138    private static final String DEFAULT_PATH_SECURE_TEMPLATE = MODULE_PATH
139        + TEMPLATE_FILES
140        + DEFAULT_NAME_WEBSERVER_SECURE;
141
142    /** The default export point URI of the web server template. */
143    private static final String DEFAULT_PATH_TEMPLATE = MODULE_PATH + TEMPLATE_FILES + DEFAULT_NAME_WEBSERVER_CONFIG;
144
145    /** The default target path for generated web server configuration files. */
146    private static final String PATH_WEBSERVER_CONFIG = PATH_WEBSERVER_EXPORT + "configs";
147
148    /** The source file used as template for creating a web server configuration files. */
149    private String m_configtemplate;
150
151    /** The prefix used for created web server configuration files, created by this tool. */
152    private String m_filenameprefix;
153
154    /** The logging directory. */
155    private String m_loggingdir;
156
157    /** The template file for secure sites. */
158    private String m_securetemplate;
159
160    /** The target path to store the web server files. */
161    private String m_targetpath;
162
163    /** The script to be executed after updating the web server configurations. */
164    private String m_webserverscript;
165
166    /**
167     * Public constructor with JSP action element.<p>
168     *
169     * @param jsp an initialized JSP action element
170     */
171    public CmsSitesWebserverDialog(CmsJspActionElement jsp) {
172
173        super(jsp);
174    }
175
176    /**
177     * Public constructor with JSP variables.<p>
178     *
179     * @param context the JSP page context
180     * @param req the JSP request
181     * @param res the JSP response
182     */
183    public CmsSitesWebserverDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
184
185        this(new CmsJspActionElement(context, req, res));
186    }
187
188    /**
189     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
190     */
191    @Override
192    public void actionCommit() throws IOException, ServletException {
193
194        Map<String, String[]> params = new HashMap<String, String[]>();
195        params.put(PARAM_WEBSERVER_SCRIPT, new String[] {m_webserverscript});
196        params.put(PARAM_TARGET_PATH, new String[] {m_targetpath});
197        params.put(PARAM_FILENAME_PREFIX, new String[] {m_filenameprefix});
198        params.put(PARAM_CONFIG_TEMPLATE, new String[] {m_configtemplate});
199        params.put(PARAM_LOGGING_DIR, new String[] {m_loggingdir});
200        params.put(PARAM_SECURE_TEMPLATE, new String[] {m_securetemplate});
201        params.put(PARAM_ACTION, new String[] {DIALOG_INITIAL});
202        params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
203        getToolManager().jspForwardPage(this, CmsSitesOverviewList.PATH_REPORTS + "webserver.jsp", params);
204    }
205
206    /**
207     * Returns the configuration file source.<p>
208     *
209     * @return the configuration file source
210     */
211    public String getConfigtemplate() {
212
213        return m_configtemplate;
214    }
215
216    /**
217     * Returns the file name prefix.<p>
218     *
219     * @return the file name prefix
220     */
221    public String getFilenameprefix() {
222
223        return m_filenameprefix;
224    }
225
226    /**
227     * Returns the loggingdir.<p>
228     *
229     * @return the loggingdir
230     */
231    public String getLoggingdir() {
232
233        return m_loggingdir;
234    }
235
236    /**
237     * Returns the securetemplate.<p>
238     *
239     * @return the securetemplate
240     */
241    public String getSecuretemplate() {
242
243        return m_securetemplate;
244    }
245
246    /**
247     * Returns the target path.<p>
248     *
249     * @return the target path
250     */
251    public String getTargetpath() {
252
253        return m_targetpath;
254    }
255
256    /**
257     * Returns the web server script.<p>
258     *
259     * @return the web server script
260     */
261    public String getWebserverscript() {
262
263        return m_webserverscript;
264    }
265
266    /**
267     * Sets the configuration template.<p>
268     *
269     * @param configtemplate the configuration file source to set
270     */
271    public void setConfigtemplate(String configtemplate) {
272
273        m_configtemplate = configtemplate;
274    }
275
276    /**
277     * Sets the file name prefix.<p>
278     *
279     * @param filenameprefix the file name prefix to set
280     */
281    public void setFilenameprefix(String filenameprefix) {
282
283        m_filenameprefix = filenameprefix;
284    }
285
286    /**
287     * Sets the loggingdir.<p>
288     *
289     * @param loggingdir the loggingdir to set
290     */
291    public void setLoggingdir(String loggingdir) {
292
293        m_loggingdir = loggingdir;
294    }
295
296    /**
297     * Sets the securetemplate.<p>
298     *
299     * @param securetemplate the securetemplate to set
300     */
301    public void setSecuretemplate(String securetemplate) {
302
303        m_securetemplate = securetemplate;
304    }
305
306    /**
307     * Sets the target path.<p>
308     *
309     * @param targetpath the target path to set
310     */
311    public void setTargetpath(String targetpath) {
312
313        m_targetpath = targetpath;
314    }
315
316    /**
317     * Sets the web server script.<p>
318     *
319     * @param webserverscript the web server script to set
320     */
321    public void setWebserverscript(String webserverscript) {
322
323        m_webserverscript = webserverscript;
324    }
325
326    /**
327     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
328     */
329    @Override
330    protected String createDialogHtml(String dialog) {
331
332        StringBuffer result = new StringBuffer(1024);
333        result.append(createWidgetTableStart());
334        result.append(createWidgetErrorHeader());
335        result.append(dialogBlockStart(
336            Messages.get().getBundle(getCms().getRequestContext().getLocale()).key(
337                Messages.GUI_SITES_WEBSERVER_TITLE_0)));
338        result.append(createWidgetTableStart());
339        result.append(createDialogRowsHtml(0, 5));
340        result.append(createWidgetTableEnd());
341        result.append(dialogBlockEnd());
342        result.append(createWidgetTableEnd());
343        return result.toString();
344    }
345
346    /**
347     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
348     */
349    @Override
350    protected void defineWidgets() {
351
352        initMembers(OpenCms.getModuleManager().getModule(MODULE_NAME).getParameters());
353        setKeyPrefix(CmsSitesOverviewList.KEY_PREFIX_SITES);
354        addWidget(new CmsWidgetDialogParameter(this, PARAM_CONFIG_TEMPLATE, PAGES[0], new CmsInputWidget()));
355        addWidget(new CmsWidgetDialogParameter(this, PARAM_SECURE_TEMPLATE, PAGES[0], new CmsInputWidget()));
356        addWidget(new CmsWidgetDialogParameter(this, PARAM_TARGET_PATH, PAGES[0], new CmsInputWidget()));
357        addWidget(new CmsWidgetDialogParameter(this, PARAM_WEBSERVER_SCRIPT, PAGES[0], new CmsInputWidget()));
358        addWidget(new CmsWidgetDialogParameter(this, PARAM_LOGGING_DIR, PAGES[0], new CmsInputWidget()));
359        addWidget(new CmsWidgetDialogParameter(this, PARAM_FILENAME_PREFIX, PAGES[0], new CmsInputWidget()));
360    }
361
362    /**
363     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
364     */
365    @Override
366    protected String[] getPageArray() {
367
368        return PAGES;
369    }
370
371    /**
372     * Initializes the values of the members.<p>
373     *
374     * @param params the parameter map to get a value from
375     */
376    protected void initMembers(Map<String, String> params) {
377
378        clearDialogObject();
379
380        m_webserverscript = getParameter(params, PARAM_WEBSERVER_SCRIPT, DEFAULT_PARAM_WEBSERVER_SCRIPT);
381        m_targetpath = getParameter(params, PARAM_TARGET_PATH, DEFAULT_PARAM_TARGET_PATH);
382        m_configtemplate = getParameter(params, PARAM_CONFIG_TEMPLATE, DEFAULT_PARAM_CONFIG_TEMPLATE);
383        m_securetemplate = getParameter(params, PARAM_SECURE_TEMPLATE, DEFAULT_PARAM_SECURE_TEMPLATE);
384        m_filenameprefix = getParameter(params, PARAM_FILENAME_PREFIX, DEFAULT_PARAM_FILENAME_PREFIX);
385        m_loggingdir = getParameter(params, PARAM_LOGGING_DIR, DEFAULT_PARAM_LOGGING_DIR);
386
387        if (DEFAULT_PARAM_WEBSERVER_SCRIPT.equals(m_webserverscript)
388            || DEFAULT_PARAM_CONFIG_TEMPLATE.equals(m_configtemplate)
389            || DEFAULT_PARAM_SECURE_TEMPLATE.equals(m_securetemplate)) {
390            for (CmsExportPoint point : OpenCms.getModuleManager().getModule(MODULE_NAME).getExportPoints()) {
391                if (DEFAULT_PATH_TEMPLATE.equals(point.getUri())) {
392                    m_configtemplate = point.getDestinationPath();
393                }
394                if (DEFAULT_PATH_SECURE_TEMPLATE.equals(point.getUri())) {
395                    m_securetemplate = point.getDestinationPath();
396                }
397                if (DEFAULT_PARAM_WEBSERVER_SCRIPT.equals(m_webserverscript)) {
398                    if (DEFAULT_PATH_SCRIPT_WIDNOWS.equals(point.getUri()) && SystemUtils.IS_OS_WINDOWS) {
399                        // only take the windows script if the OS is a windows
400                        m_webserverscript = point.getDestinationPath();
401                    } else if (DEFAULT_PATH_SCRIPT_LINUX.equals(point.getUri())) {
402                        m_webserverscript = point.getDestinationPath();
403                    }
404                }
405            }
406        }
407
408        if (DEFAULT_PARAM_TARGET_PATH.equals(m_targetpath)) {
409            m_targetpath = PATH_WEBSERVER_CONFIG;
410        }
411
412        if (DEFAULT_PARAM_LOGGING_DIR.equals(m_loggingdir)) {
413            m_loggingdir = SystemUtils.IS_OS_WINDOWS ? "" : DEFAULT_PATH_LOG_LINUX;
414        }
415
416        setDialogObject(this);
417    }
418
419    /**
420     * Returns a parameter value from the module parameters,
421     * or a given default value in case the parameter is not set.<p>
422     *
423     * @param params the parameter map to get a value from
424     * @param key the parameter to return the value for
425     * @param defaultValue the default value in case there is no value stored for this key
426     *
427     * @return the parameter value from the module parameters
428     */
429    private String getParameter(Map<String, String> params, String key, String defaultValue) {
430
431        String value = params.get(key);
432        return (value != null) ? value : defaultValue;
433    }
434}