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 (https://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: https://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: https://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 org.apache.commons.lang3.SystemUtils;
047
048import jakarta.servlet.ServletException;
049import jakarta.servlet.http.HttpServletRequest;
050import jakarta.servlet.http.HttpServletResponse;
051import jakarta.servlet.jsp.PageContext;
052
053/**
054 * A dialog that allows to write the sites configured in OpenCms
055 * into a web server configuration file, using a template.<p>
056 *
057 * @since 9.0.0
058 */
059public class CmsSitesWebserverDialog extends CmsWidgetDialog {
060
061    /** Linux script name. */
062    public static final String DEFAULT_NAME_LINUX_SCRIPT = "script.sh";
063
064    /** Default web server configuration template file. */
065    public static final String DEFAULT_NAME_WEBSERVER_CONFIG = "vhost.template";
066
067    /** Windows script name. */
068    public static final String DEFAULT_NAME_WINDOWS_SCRIPT = "script.bat";
069
070    /** The module name constant. */
071    public static final String MODULE_NAME = "org.opencms.workplace.tools.sites";
072
073    /** Module path. */
074    public static final String MODULE_PATH = "/system/modules/" + MODULE_NAME + "/";
075
076    /** Defines which pages are valid for this dialog. */
077    public static final String[] PAGES = {"page1"};
078
079    /** Module parameter constant for the web server configuration template file. */
080    public static final String PARAM_CONFIG_TEMPLATE = "configtemplate";
081
082    /** A module parameter name for the prefix used for web server configuration files. */
083    public static final String PARAM_FILENAME_PREFIX = "filenameprefix";
084
085    /** The parameter name for the logging directory. */
086    public static final String PARAM_LOGGING_DIR = "loggingdir";
087
088    /** The parameter name of the template file for secure sites. */
089    public static final String PARAM_SECURE_TEMPLATE = "securetemplate";
090
091    /** Module parameter constant for the target path. */
092    public static final String PARAM_TARGET_PATH = "targetpath";
093
094    /** Module parameter constant for the web server script. */
095    public static final String PARAM_WEBSERVER_SCRIPT = "webserverscript";
096
097    /** The working directory for this tool. */
098    public static final String PATH_WEBSERVER_EXPORT = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
099        "server-scripts/");
100
101    /** Sample files folder name. */
102    public static final String TEMPLATE_FILES = "webserver-templates/";
103
104    /** The default file name of the secure server template configuration file. */
105    private static final String DEFAULT_NAME_WEBSERVER_SECURE = "vhost-secure.template";
106
107    /** The default parameter value. */
108    private static final String DEFAULT_PARAM_CONFIG_TEMPLATE = "/path/to/webserver/config.template";
109
110    /** The default prefix used for created web server configuration files, created by this tool. */
111    private static final String DEFAULT_PARAM_FILENAME_PREFIX = "opencms";
112
113    /** The default value for the logging directory parameter. */
114    private static final String DEFAULT_PARAM_LOGGING_DIR = "/path/to/logging/folder/";
115
116    /** The default parameter value. */
117    private static final String DEFAULT_PARAM_SECURE_TEMPLATE = "/path/to/webserver/secure-config.template";
118
119    /** The default parameter value. */
120    private static final String DEFAULT_PARAM_TARGET_PATH = "/path/to/config/target/";
121
122    /** The default parameter value. */
123    private static final String DEFAULT_PARAM_WEBSERVER_SCRIPT = "/path/to/webserver/script.sh";
124
125    /** The default path for apache2 log files on a Unix system. */
126    private static final String DEFAULT_PATH_LOG_LINUX = "/var/log/apache2/";
127
128    /** The default export point URI of the web server script (LINUX). */
129    private static final String DEFAULT_PATH_SCRIPT_LINUX = MODULE_PATH + TEMPLATE_FILES + DEFAULT_NAME_LINUX_SCRIPT;
130
131    /** The default export point URI of the web server script (LINUX). */
132    private static final String DEFAULT_PATH_SCRIPT_WIDNOWS = MODULE_PATH
133        + TEMPLATE_FILES
134        + DEFAULT_NAME_WINDOWS_SCRIPT;
135
136    /** The default export point URI of the web server template. */
137    private static final String DEFAULT_PATH_SECURE_TEMPLATE = MODULE_PATH
138        + TEMPLATE_FILES
139        + DEFAULT_NAME_WEBSERVER_SECURE;
140
141    /** The default export point URI of the web server template. */
142    private static final String DEFAULT_PATH_TEMPLATE = MODULE_PATH + TEMPLATE_FILES + DEFAULT_NAME_WEBSERVER_CONFIG;
143
144    /** The default target path for generated web server configuration files. */
145    private static final String PATH_WEBSERVER_CONFIG = PATH_WEBSERVER_EXPORT + "configs";
146
147    /** The source file used as template for creating a web server configuration files. */
148    private String m_configtemplate;
149
150    /** The prefix used for created web server configuration files, created by this tool. */
151    private String m_filenameprefix;
152
153    /** The logging directory. */
154    private String m_loggingdir;
155
156    /** The template file for secure sites. */
157    private String m_securetemplate;
158
159    /** The target path to store the web server files. */
160    private String m_targetpath;
161
162    /** The script to be executed after updating the web server configurations. */
163    private String m_webserverscript;
164
165    /**
166     * Public constructor with JSP action element.<p>
167     *
168     * @param jsp an initialized JSP action element
169     */
170    public CmsSitesWebserverDialog(CmsJspActionElement jsp) {
171
172        super(jsp);
173    }
174
175    /**
176     * Public constructor with JSP variables.<p>
177     *
178     * @param context the JSP page context
179     * @param req the JSP request
180     * @param res the JSP response
181     */
182    public CmsSitesWebserverDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
183
184        this(new CmsJspActionElement(context, req, res));
185    }
186
187    /**
188     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
189     */
190    @Override
191    public void actionCommit() throws IOException, ServletException {
192
193        Map<String, String[]> params = new HashMap<String, String[]>();
194        params.put(PARAM_WEBSERVER_SCRIPT, new String[] {m_webserverscript});
195        params.put(PARAM_TARGET_PATH, new String[] {m_targetpath});
196        params.put(PARAM_FILENAME_PREFIX, new String[] {m_filenameprefix});
197        params.put(PARAM_CONFIG_TEMPLATE, new String[] {m_configtemplate});
198        params.put(PARAM_LOGGING_DIR, new String[] {m_loggingdir});
199        params.put(PARAM_SECURE_TEMPLATE, new String[] {m_securetemplate});
200        params.put(PARAM_ACTION, new String[] {DIALOG_INITIAL});
201        params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
202        getToolManager().jspForwardPage(this, CmsSitesOverviewList.PATH_REPORTS + "webserver.jsp", params);
203    }
204
205    /**
206     * Returns the configuration file source.<p>
207     *
208     * @return the configuration file source
209     */
210    public String getConfigtemplate() {
211
212        return m_configtemplate;
213    }
214
215    /**
216     * Returns the file name prefix.<p>
217     *
218     * @return the file name prefix
219     */
220    public String getFilenameprefix() {
221
222        return m_filenameprefix;
223    }
224
225    /**
226     * Returns the loggingdir.<p>
227     *
228     * @return the loggingdir
229     */
230    public String getLoggingdir() {
231
232        return m_loggingdir;
233    }
234
235    /**
236     * Returns the securetemplate.<p>
237     *
238     * @return the securetemplate
239     */
240    public String getSecuretemplate() {
241
242        return m_securetemplate;
243    }
244
245    /**
246     * Returns the target path.<p>
247     *
248     * @return the target path
249     */
250    public String getTargetpath() {
251
252        return m_targetpath;
253    }
254
255    /**
256     * Returns the web server script.<p>
257     *
258     * @return the web server script
259     */
260    public String getWebserverscript() {
261
262        return m_webserverscript;
263    }
264
265    /**
266     * Sets the configuration template.<p>
267     *
268     * @param configtemplate the configuration file source to set
269     */
270    public void setConfigtemplate(String configtemplate) {
271
272        m_configtemplate = configtemplate;
273    }
274
275    /**
276     * Sets the file name prefix.<p>
277     *
278     * @param filenameprefix the file name prefix to set
279     */
280    public void setFilenameprefix(String filenameprefix) {
281
282        m_filenameprefix = filenameprefix;
283    }
284
285    /**
286     * Sets the loggingdir.<p>
287     *
288     * @param loggingdir the loggingdir to set
289     */
290    public void setLoggingdir(String loggingdir) {
291
292        m_loggingdir = loggingdir;
293    }
294
295    /**
296     * Sets the securetemplate.<p>
297     *
298     * @param securetemplate the securetemplate to set
299     */
300    public void setSecuretemplate(String securetemplate) {
301
302        m_securetemplate = securetemplate;
303    }
304
305    /**
306     * Sets the target path.<p>
307     *
308     * @param targetpath the target path to set
309     */
310    public void setTargetpath(String targetpath) {
311
312        m_targetpath = targetpath;
313    }
314
315    /**
316     * Sets the web server script.<p>
317     *
318     * @param webserverscript the web server script to set
319     */
320    public void setWebserverscript(String webserverscript) {
321
322        m_webserverscript = webserverscript;
323    }
324
325    /**
326     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
327     */
328    @Override
329    protected String createDialogHtml(String dialog) {
330
331        StringBuffer result = new StringBuffer(1024);
332        result.append(createWidgetTableStart());
333        result.append(createWidgetErrorHeader());
334        result.append(
335            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}