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.workplace.rfsfile;
029
030import org.opencms.configuration.CmsWorkplaceConfiguration;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsSystemInfo;
033import org.opencms.main.OpenCms;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.widgets.CmsCheckboxWidget;
036import org.opencms.widgets.CmsComboWidget;
037import org.opencms.widgets.CmsSelectWidgetOption;
038import org.opencms.workplace.CmsWidgetDialogParameter;
039
040import java.io.ByteArrayOutputStream;
041import java.io.File;
042import java.io.OutputStreamWriter;
043import java.nio.charset.Charset;
044import java.util.ArrayList;
045import java.util.Iterator;
046import java.util.LinkedList;
047import java.util.List;
048import java.util.SortedMap;
049
050import javax.servlet.http.HttpServletRequest;
051import javax.servlet.http.HttpServletResponse;
052import javax.servlet.jsp.PageContext;
053
054/**
055 * A <code>{@link org.opencms.workplace.CmsWidgetDialog}</code> that allows
056 * modification of the properties of the
057 * <code>{@link org.opencms.util.CmsRfsFileViewer}</code> bean.<p>
058 *
059 * @since 6.0.0
060 */
061public class CmsRfsFileViewSettingsDialog extends A_CmsRfsFileWidgetDialog {
062
063    /** localized messages Keys prefix. */
064    public static final String KEY_PREFIX = "logfile";
065
066    /**
067     * @param jsp the CmsJspActionElement.
068     */
069    public CmsRfsFileViewSettingsDialog(CmsJspActionElement jsp) {
070
071        super(jsp);
072    }
073
074    /**
075     * Public constructor with JSP variables.<p>
076     *
077     * @param context the JSP page context
078     * @param req the JSP request
079     * @param res the JSP response
080     */
081    public CmsRfsFileViewSettingsDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
082
083        this(new CmsJspActionElement(context, req, res));
084    }
085
086    /**
087     * Commits the <code>{@link org.opencms.util.CmsRfsFileViewer}</code> to the
088     * <code>{@link org.opencms.workplace.CmsWorkplaceManager}</code>. <p>
089     *
090     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
091     */
092    @Override
093    public void actionCommit() {
094
095        List<Throwable> errors = new ArrayList<Throwable>();
096
097        try {
098            // set the edited settings
099            OpenCms.getWorkplaceManager().setFileViewSettings(getCms(), m_logView);
100            // write the configuration
101            OpenCms.writeConfiguration(CmsWorkplaceConfiguration.class);
102            setDialogObject(null);
103        } catch (Throwable t) {
104            errors.add(t);
105        }
106
107        // set the list of errors to display when saving failed
108        setCommitErrors(errors);
109    }
110
111    /**
112     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
113     *
114     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
115     *
116     * @param dialog the dialog (page) to get the HTML for
117     * @return the dialog HTML for all defined widgets of the named dialog (page)
118     */
119    @Override
120    protected String createDialogHtml(String dialog) {
121
122        StringBuffer result = new StringBuffer(1024);
123
124        // create widget table
125        result.append(createWidgetTableStart());
126
127        // show error header once if there were validation errors
128        result.append(createWidgetErrorHeader());
129
130        // create the widgets for the settings page
131        result.append(dialogBlockStart(key(Messages.GUI_WORKPLACE_LOGVIEW_SETTINGS_NAME_0)));
132        result.append(createWidgetTableStart());
133        result.append(createDialogRowsHtml(0, 4));
134        result.append(createWidgetTableEnd());
135        result.append(dialogBlockEnd());
136
137        result.append(createWidgetTableEnd());
138
139        return result.toString();
140
141    }
142
143    /**
144     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
145     */
146    @Override
147    protected void defineWidgets() {
148
149        setKeyPrefix(KEY_PREFIX);
150        super.defineWidgets();
151        addWidget(
152            new CmsWidgetDialogParameter(m_logView, "isLogfile", "page1", new CmsCheckboxWidget(CmsStringUtil.TRUE)));
153        addWidget(
154            new CmsWidgetDialogParameter(
155                m_logView,
156                "filePath",
157                "page1",
158                new CmsComboWidget(createComboConfigurationFileChoice())));
159
160        // options for windowsize combowidget:
161        List<CmsSelectWidgetOption> comboOptions = new LinkedList<CmsSelectWidgetOption>();
162        comboOptions.add(new CmsSelectWidgetOption("100"));
163        comboOptions.add(new CmsSelectWidgetOption("200"));
164        comboOptions.add(new CmsSelectWidgetOption("400"));
165        comboOptions.add(new CmsSelectWidgetOption("600"));
166        comboOptions.add(new CmsSelectWidgetOption("800"));
167        addWidget(new CmsWidgetDialogParameter(m_logView, "windowSize", "page1", new CmsComboWidget(comboOptions)));
168
169        // file encoding combowidget;
170        addWidget(
171            new CmsWidgetDialogParameter(
172                m_logView,
173                "fileEncoding",
174                "page1",
175                new CmsComboWidget(createComboConfigurationEncodingChoice())));
176
177        addWidget(new CmsWidgetDialogParameter(m_logView, "enabled", "page1", new CmsCheckboxWidget()));
178
179    }
180
181    /**
182     * Returns a list of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> instances for the
183     * <code>{@link CmsComboWidget}</code> with the supported encodings of the
184     * current system and the default encoding set as default combo option.<p>
185     *
186     * @return a list of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> instances for the
187     *         <code>{@link CmsComboWidget}</code> with the supported encodings of the
188     *         current system and the default encoding set as default combo option.<p>
189     */
190    private List<CmsSelectWidgetOption> createComboConfigurationEncodingChoice() {
191
192        List<CmsSelectWidgetOption> result = new LinkedList<CmsSelectWidgetOption>();
193        SortedMap<String, Charset> csMap = Charset.availableCharsets();
194        // default charset: see http://java.sun.com/j2se/corejava/intl/reference/faqs/index.html#default-encoding
195        // before java 1.5 there is no other way (System property "file.encoding" is implementation detail not in vmspec.
196        Charset defaultCs = Charset.forName(new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding());
197        Charset cs;
198        Iterator<Charset> it = csMap.values().iterator();
199        while (it.hasNext()) {
200            cs = it.next();
201            // default? no equals required: safety by design!
202            if (cs == defaultCs) {
203                result.add(
204                    new CmsSelectWidgetOption(
205                        cs.name(),
206                        true,
207                        null,
208                        key(Messages.GUI_WORKPLACE_LOGVIEW_FILE_CHARSET_DEF_HELP_0)));
209            } else {
210                if (!cs.name().startsWith("x")) {
211                    result.add(
212                        new CmsSelectWidgetOption(
213                            cs.name(),
214                            false,
215                            null,
216                            key(Messages.GUI_WORKPLACE_LOGVIEW_FILE_CHARSET_HELP_0)));
217                }
218            }
219        }
220
221        return result;
222
223    }
224
225    /**
226     * Returns a list of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> instances for the
227     *         <code>{@link CmsComboWidget}</code> with default file locations of OpenCms.<p>
228     *
229     * @return a list of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> instances for the
230     *         <code>{@link CmsComboWidget}</code> with default file locations of OpenCms
231     *
232     */
233    private List<CmsSelectWidgetOption> createComboConfigurationFileChoice() {
234
235        List<CmsSelectWidgetOption> result = new LinkedList<CmsSelectWidgetOption>();
236        CmsSystemInfo sysInfo = OpenCms.getSystemInfo();
237        // log file, default
238        result.add(
239            new CmsSelectWidgetOption(
240                sysInfo.getLogFileRfsPath(),
241                true,
242                null,
243                key(Messages.GUI_WORKPLACE_LOGVIEW_FILE_LOG_HELP_0)));
244        // opencms.properties
245        result.add(new CmsSelectWidgetOption(
246            sysInfo.getConfigurationFileRfsPath(),
247            false,
248            null,
249            key(Messages.GUI_WORKPLACE_LOGVIEW_FILE_CONF_HELP_0)));
250        // config xml
251        String configPath = sysInfo.getConfigFolder();
252        if (configPath != null) {
253            File configFolder = new File(configPath);
254            File[] configFiles = configFolder.listFiles();
255            File configFile;
256            for (int i = 0; i < configFiles.length; i++) {
257                configFile = configFiles[i];
258                if (configFile.isFile()) {
259                    if (configFile.getName().endsWith(".xml")) {
260                        result.add(
261                            new CmsSelectWidgetOption(
262                                configFile.getAbsolutePath(),
263                                false,
264                                null,
265                                key(Messages.GUI_WORKPLACE_LOGVIEW_FILE_XMLCONF_HELP_0)));
266                    }
267                }
268            }
269        }
270        return result;
271    }
272}