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, 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.ui.apps.logfile;
029
030import org.opencms.main.CmsLog;
031import org.opencms.ui.apps.logfile.CmsLogDownloadDialog.ZipGenerator;
032
033import java.io.File;
034import java.io.FileInputStream;
035import java.io.FileNotFoundException;
036import java.io.FileOutputStream;
037import java.io.IOException;
038import java.io.InputStream;
039import java.util.HashSet;
040import java.util.Set;
041
042import org.apache.commons.logging.Log;
043
044/**
045 * Default implementation for the log download functionality.
046 */
047public class CmsDefaultLogDownloadProvider implements I_CmsLogDownloadProvider {
048
049    /** Logger instance for the class. */
050    private static final Log LOG = CmsLog.getLog(CmsDefaultLogDownloadProvider.class);
051
052    /** Path to zip file.*/
053    private static final String ZIP_PATH = CmsLogFileApp.LOG_FOLDER + "logs.zip";
054
055    /**
056     * @see org.opencms.ui.apps.logfile.I_CmsLogDownloadProvider#canDownloadAllLogs()
057     */
058    public boolean canDownloadAllLogs() {
059
060        return true;
061    }
062
063    /**
064     * @see org.opencms.ui.apps.logfile.I_CmsLogDownloadProvider#getDownloadPrefix()
065     */
066    public String getDownloadPrefix() {
067
068        return "";
069    }
070
071    /**
072     * @see org.opencms.ui.apps.logfile.I_CmsLogDownloadProvider#getLogFiles()
073     */
074    public Set<String> getLogFiles() {
075
076        Set<String> result = new HashSet<>();
077        for (File file : CmsLogFileOptionProvider.getLogFiles()) {
078            if (file.isDirectory()) {
079                continue;
080            }
081            String path = file.getAbsolutePath();
082            if (!path.endsWith(".zip") && !path.endsWith(".gz")) {
083                result.add(file.getAbsolutePath());
084            }
085        }
086        return result;
087    }
088
089    /**
090     * @see org.opencms.ui.apps.logfile.I_CmsLogDownloadProvider#readAllLogs()
091     */
092    public InputStream readAllLogs() {
093
094        FileOutputStream fos = null;
095        ZipGenerator zipGen = null;
096        try {
097            fos = new FileOutputStream(ZIP_PATH);
098            zipGen = new ZipGenerator(fos);
099            for (File file : CmsLogFileOptionProvider.getLogFiles()) {
100                if (!file.isDirectory() & !ZIP_PATH.equals(file.getAbsolutePath())) {
101                    zipGen.addToZip(new File(CmsLogFileApp.LOG_FOLDER), file);
102                }
103            }
104            zipGen.close();
105            fos.close();
106            return new FileInputStream(ZIP_PATH);
107
108        } catch (IOException e) {
109            LOG.error("unable to build zip file", e);
110            return null;
111        } finally {
112            if (zipGen != null) {
113                try {
114                    zipGen.close();
115                } catch (Exception e) {
116                    LOG.info(e.getLocalizedMessage(), e);
117                }
118            }
119            if (fos != null) {
120                try {
121                    fos.close();
122                } catch (Exception e) {
123                    LOG.info(e.getLocalizedMessage(), e);
124                }
125            }
126        }
127    }
128
129    /**
130     * @see org.opencms.ui.apps.logfile.I_CmsLogDownloadProvider#readLog(java.lang.String)
131     */
132    public InputStream readLog(String path) {
133
134        Set<String> files = getLogFiles();
135        if (files.contains(path)) {
136            try {
137                return new FileInputStream(path);
138            } catch (FileNotFoundException e) {
139                return null;
140            }
141        } else {
142            return null;
143        }
144    }
145
146}