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.jsp.CmsJspActionElement;
031import org.opencms.main.CmsRuntimeException;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsDateUtil;
034import org.opencms.util.CmsRfsFileViewer;
035import org.opencms.widgets.CmsDisplayWidget;
036import org.opencms.workplace.CmsDialog;
037import org.opencms.workplace.CmsWidgetDialog;
038import org.opencms.workplace.CmsWidgetDialogParameter;
039import org.opencms.workplace.tools.CmsToolDialog;
040
041import java.io.File;
042import java.io.IOException;
043import java.text.DateFormat;
044import java.util.ArrayList;
045import java.util.Date;
046import java.util.HashMap;
047import java.util.List;
048import java.util.Map;
049
050import javax.servlet.ServletException;
051import javax.servlet.http.HttpServletRequest;
052import javax.servlet.http.HttpServletResponse;
053import javax.servlet.jsp.PageContext;
054
055/**
056 * Shows useful information about the current file chosen within the
057 * <code>{@link org.opencms.util.CmsRfsFileViewer}</code> and offers
058 * a direct download link. <p>
059 *
060 * @since 6.0.0
061 */
062public class CmsRfsFileDownloadDialog extends CmsWidgetDialog {
063
064    /** localized messages Keys prefix. */
065    public static final String KEY_PREFIX = "workplace.download";
066
067    /** Defines which pages are valid for this dialog. */
068    public static final String[] PAGES = {"page1"};
069
070    /** The file to download. */
071    private File m_downloadFile;
072
073    /** The file date. */
074    private String m_filedate;
075
076    /** The file name. */
077    private String m_filename;
078
079    /** The file path. */
080    private String m_filepath;
081
082    /** The file size. */
083    private String m_filesize;
084
085    /**
086     * Public constructor with JSP action element.<p>
087     *
088     * @param jsp an initialized JSP action element
089     */
090    public CmsRfsFileDownloadDialog(CmsJspActionElement jsp) {
091
092        super(jsp);
093
094    }
095
096    /**
097     * Public constructor with JSP variables.<p>
098     *
099     * @param context the JSP page context
100     * @param req the JSP request
101     * @param res the JSP response
102     */
103    public CmsRfsFileDownloadDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
104
105        this(new CmsJspActionElement(context, req, res));
106    }
107
108    /**
109     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
110     */
111    @Override
112    public void actionCommit() throws IOException, ServletException {
113
114        List<Throwable> errors = new ArrayList<Throwable>();
115        Map<String, String[]> params = new HashMap<String, String[]>();
116        params.put(CmsDialog.PARAM_CLOSELINK, new String[] {getParamCloseLink()});
117        params.put(CmsToolDialog.PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
118        getToolManager().jspForwardPage(this, "/system/workplace/admin/workplace/logfileview/dodownload.jsp", params);
119
120        setCommitErrors(errors);
121    }
122
123    /**
124     * @see org.opencms.workplace.CmsWidgetDialog#dialogButtonsCustom()
125     */
126    @Override
127    public String dialogButtonsCustom() {
128
129        return dialogButtons(new int[] {BUTTON_OK, BUTTON_CANCEL}, new String[2]);
130    }
131
132    /**
133     * Returns the file date.<p>
134     *
135     * @return the file date
136     */
137    public String getFiledate() {
138
139        return m_filedate;
140    }
141
142    /**
143     * Returns the file name.<p>
144     *
145     * @return the file name
146     */
147    public String getFilename() {
148
149        return m_filename;
150    }
151
152    /**
153     * Returns the file path.<p>
154     *
155     * @return the file path
156     */
157    public String getFilepath() {
158
159        return m_filepath;
160    }
161
162    /**
163     * Returns the file size.<p>
164     *
165     * @return the file size
166     */
167    public String getFilesize() {
168
169        return m_filesize;
170    }
171
172    /**
173     * Sets the file date.<p>
174     *
175     * @param filedate the file date to set
176     */
177    public void setFiledate(String filedate) {
178
179        m_filedate = filedate;
180    }
181
182    /**
183     * Sets the file name.<p>
184     *
185     * @param filename the file name to set
186     */
187    public void setFilename(String filename) {
188
189        m_filename = filename;
190    }
191
192    /**
193     * Sets the file path.<p>
194     *
195     * @param filepath the file path to set
196     */
197    public void setFilepath(String filepath) {
198
199        m_filepath = filepath;
200    }
201
202    /**
203     * Sets the file size.<p>
204     *
205     * @param filesize the file size to set
206     */
207    public void setFilesize(String filesize) {
208
209        m_filesize = filesize;
210    }
211
212    /**
213     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
214     *
215     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
216     *
217     * @param dialog the dialog (page) to get the HTML for
218     * @return the dialog HTML for all defined widgets of the named dialog (page)
219     */
220    @Override
221    protected String createDialogHtml(String dialog) {
222
223        StringBuffer result = new StringBuffer(1024);
224
225        result.append(createWidgetTableStart());
226        // show error header once if there were validation errors
227        result.append(createWidgetErrorHeader());
228
229        if (dialog.equals(PAGES[0])) {
230            // create the widgets for the first dialog page
231            result.append(dialogBlockStart(key(Messages.GUI_WORKPLACE_LOGVIEW_DOWNLOAD_START_MSG_0)));
232            result.append(createWidgetTableStart());
233            result.append(createDialogRowsHtml(0, 3));
234            result.append(createWidgetTableEnd());
235            result.append(dialogBlockEnd());
236        }
237        result.append(createWidgetTableEnd());
238        return result.toString();
239    }
240
241    /**
242     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
243     */
244    @Override
245    protected void defineWidgets() {
246
247        setKeyPrefix(KEY_PREFIX);
248
249        setFilename(getDownloadFile().getName());
250        setFilesize("" + getDownloadFile().length());
251        setFilepath(getDownloadFile().getAbsolutePath());
252        setFiledate(
253            CmsDateUtil.getDateTime(new Date(getDownloadFile().lastModified()), DateFormat.MEDIUM, getLocale()));
254
255        addWidget(new CmsWidgetDialogParameter(this, "filename", PAGES[0], new CmsDisplayWidget()));
256        addWidget(new CmsWidgetDialogParameter(this, "filesize", PAGES[0], new CmsDisplayWidget()));
257        addWidget(new CmsWidgetDialogParameter(this, "filepath", PAGES[0], new CmsDisplayWidget()));
258        addWidget(new CmsWidgetDialogParameter(this, "filedate", PAGES[0], new CmsDisplayWidget()));
259    }
260
261    /**
262     * Returns the file that will be downloaded upon clicking the download button
263     * generated in this form by <code>{@link #dialogButtonsOkCancel()}</code>.<p>
264     *
265     * @return the file that will be downloaded upon clicking the download button
266     *         generated in this form by <code>{@link #dialogButtonsOkCancel()}</code>
267     *
268     * @throws CmsRuntimeException if access to the chosen file to download fails
269     */
270    protected File getDownloadFile() throws CmsRuntimeException {
271
272        if (m_downloadFile == null) {
273            // no clone needed: we just read here.
274            CmsRfsFileViewer fileView = OpenCms.getWorkplaceManager().getFileViewSettings();
275            m_downloadFile = new File(fileView.getFilePath());
276            try {
277                // 2nd check: it is impossible to set an invalid path to that class.
278                m_downloadFile = m_downloadFile.getCanonicalFile();
279            } catch (IOException ioex) {
280                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_FILE_ACCESS_0), ioex);
281            }
282        }
283        return m_downloadFile;
284    }
285
286    /**
287     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
288     */
289    @Override
290    protected String[] getPageArray() {
291
292        return PAGES;
293    }
294
295    /**
296     * @see org.opencms.workplace.CmsWorkplace#initMessages()
297     */
298    @Override
299    protected void initMessages() {
300
301        super.initMessages();
302        addMessages(org.opencms.workplace.tools.workplace.rfsfile.Messages.get().getBundleName());
303    }
304}