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 String path = file.getAbsolutePath(); 079 if (!path.endsWith(".zip") && !path.endsWith(".gz")) { 080 result.add(file.getAbsolutePath()); 081 } 082 } 083 return result; 084 } 085 086 /** 087 * @see org.opencms.ui.apps.logfile.I_CmsLogDownloadProvider#readAllLogs() 088 */ 089 public InputStream readAllLogs() { 090 091 FileOutputStream fos = null; 092 ZipGenerator zipGen = null; 093 try { 094 fos = new FileOutputStream(ZIP_PATH); 095 zipGen = new ZipGenerator(fos); 096 for (File file : CmsLogFileOptionProvider.getLogFiles()) { 097 if (!file.isDirectory() & !ZIP_PATH.equals(file.getAbsolutePath())) { 098 zipGen.addToZip(new File(CmsLogFileApp.LOG_FOLDER), file); 099 } 100 } 101 zipGen.close(); 102 fos.close(); 103 return new FileInputStream(ZIP_PATH); 104 105 } catch (IOException e) { 106 LOG.error("unable to build zip file", e); 107 return null; 108 } finally { 109 if (zipGen != null) { 110 try { 111 zipGen.close(); 112 } catch (Exception e) { 113 LOG.info(e.getLocalizedMessage(), e); 114 } 115 } 116 if (fos != null) { 117 try { 118 fos.close(); 119 } catch (Exception e) { 120 LOG.info(e.getLocalizedMessage(), e); 121 } 122 } 123 } 124 } 125 126 /** 127 * @see org.opencms.ui.apps.logfile.I_CmsLogDownloadProvider#readLog(java.lang.String) 128 */ 129 public InputStream readLog(String path) { 130 131 Set<String> files = getLogFiles(); 132 if (files.contains(path)) { 133 try { 134 return new FileInputStream(path); 135 } catch (FileNotFoundException e) { 136 return null; 137 } 138 } else { 139 return null; 140 } 141 } 142 143}