1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package fr.paris.lutece.plugins.managelogs.web;
36
37 import fr.paris.lutece.plugins.managelogs.util.ManageLogsUtil;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.portal.service.util.AppPropertiesService;
40 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
41 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
42 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
43 import fr.paris.lutece.util.ReferenceList;
44
45 import javax.servlet.http.HttpServletRequest;
46 import java.io.IOException;
47 import java.nio.charset.Charset;
48 import java.nio.file.DirectoryStream;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.*;
53
54 import static fr.paris.lutece.plugins.managelogs.web.AbstractManageLogsPropertiesJspBean.RIGHT_MANAGELOGSPROPERTIES;
55
56
57
58
59 @Controller( controllerJsp = "DownloadLog.jsp", controllerPath = "jsp/admin/plugins/managelogs/", right = RIGHT_MANAGELOGSPROPERTIES )
60 public class DownloadLogJspBean extends AbstractManageLogsPropertiesJspBean
61 {
62 static final long serialVersionUID = -1;
63
64
65 private static final String TEMPLATE_MANAGE_LOGPROPERTIESS = "/admin/plugins/managelogs/download_logs.html";
66
67
68 private static final String PARAMETER_ID_LOG = "idLog";
69
70
71 private static final String PROPERTY_PAGE_TITLE_MANAGE_LOGPROPERTIESS = "managelogs.manage_logdownload.pageTitle";
72
73
74 private static final String MARK_LIST_LOGS = "logs_list";
75
76
77
78
79
80 private static final String VIEW_DEFAULT = "listLogs";
81
82
83 private static final String ACTION_DOWNLOAD_LOG = "download";
84
85
86
87
88
89
90
91
92 private static final String CONTENT_TYPE = "application/octet-stream";
93
94
95
96
97
98
99
100 @View( value = VIEW_DEFAULT, defaultView = true )
101 public String getLogs( HttpServletRequest request )
102 {
103 Map<String, Object> model = new HashMap<>( );
104
105 ReferenceList rf = getLogFilesReferenceList();
106
107 if ( rf.isEmpty() )
108 {
109
110 AppLogService.error( "No log file found" );
111 }
112
113 model.put( MARK_LIST_LOGS, rf );
114
115 return getPage( PROPERTY_PAGE_TITLE_MANAGE_LOGPROPERTIESS, TEMPLATE_MANAGE_LOGPROPERTIESS, model );
116 }
117
118 private static ReferenceList getLogFilesReferenceList()
119 {
120 ReferenceList rf = new ReferenceList();
121
122 Set<String> setLogFiles = getLogFiles();
123
124 if ( setLogFiles.isEmpty() )
125 {
126
127 AppLogService.error( "No log file found" );
128 }
129 else
130 {
131
132 List<String> listFilesSorted = new ArrayList<>( setLogFiles );
133 Collections.sort( listFilesSorted );
134
135 int i=0;
136 for ( String file : listFilesSorted )
137 {
138 rf.addItem( i, file );
139 i++;
140 }
141 }
142
143 return rf;
144 }
145
146
147
148
149
150 private static Set<String> getLogFiles()
151 {
152
153 Set<String> setLogs = new HashSet<>( );
154
155
156 String log4jConfigFile = null;
157
158
159 if ( !APP_SERVER_MULTI_WEBAPP )
160 {
161 log4jConfigFile = System.getProperty( "log4j.configuration" );
162 if ( ManageLogsUtil.isFileReadable( log4jConfigFile ) )
163 {
164 setLogs.addAll( readLogConf( log4jConfigFile ) );
165 }
166 }
167
168
169 setLogs.addAll( getLogsFromFile( TMP_LOG_ABSOLUTE, log4jConfigFile ) );
170
171
172 setLogs.addAll( getLogsFromFile( ALTERNATE_LOG_CONF_FILE_ABSOLUTE, log4jConfigFile ) );
173
174
175 setLogs.addAll( getLogsFromFile( LUTECE_CONF_FILE_ABSOLUTE, log4jConfigFile ) );
176
177
178 String strListAdditionalLogDir = AppPropertiesService.getProperty( "managelogs.addlog.folder" );
179 if ( strListAdditionalLogDir != null )
180 {
181 for ( String logDir : strListAdditionalLogDir.split( ";" ) )
182 {
183 if ( !ManageLogsUtil.isNullOrEmptyWithTrim( logDir ) )
184 {
185 try
186 {
187 setLogs.addAll ( listFilesInDirectory( logDir.trim() ) );
188 }
189 catch ( IOException e )
190 {
191 AppLogService.error( "Error getting additionnal files from " + logDir, e );
192 }
193 }
194 }
195 }
196
197 return setLogs;
198 }
199
200 private static Set<String> getLogsFromFile( String file, String log4jConfigFile )
201 {
202 Set<String> setLogs = new HashSet<>( );
203 if ( !file.equalsIgnoreCase( log4jConfigFile ) && ManageLogsUtil.isFileReadable( file ) )
204 {
205 setLogs.addAll( readLogConf( file ) );
206 }
207 return setLogs;
208 }
209
210
211
212
213
214
215 private static Set<String> readLogConf( String fileName )
216 {
217 Set<String> logFile = new HashSet<>( );
218 try
219 {
220 List<String> lines = Files.readAllLines( Paths.get( fileName ), Charset.defaultCharset( ) );
221
222 List<String> listFilesFromConfiguration = getFilesFromConfiguration( lines, false );
223
224 for ( String fileFromConfiguration : listFilesFromConfiguration )
225 {
226 String absoluteDirectory = Paths.get( fileFromConfiguration ).getParent().toString();
227
228
229 Set<String> filesInDirectory = listFilesInDirectory( absoluteDirectory );
230 for ( String strFileInDirectory : filesInDirectory )
231 {
232
233 if ( Paths.get( strFileInDirectory ).getFileName().toString().startsWith( Paths.get( fileFromConfiguration ).getFileName().toString() ) )
234 {
235 logFile.add( Paths.get( strFileInDirectory ).toString() );
236 }
237 }
238 }
239 }
240 catch ( IOException e )
241 {
242 AppLogService.error( "Error reading file " + fileName, e );
243 }
244
245 return logFile;
246 }
247
248
249
250
251
252
253
254
255
256 private static Set<String> listFilesInDirectory( String dir ) throws IOException
257 {
258 Set<String> fileList = new HashSet<>();
259 if ( ManageLogsUtil.isFileReadable( dir ) )
260 {
261 try ( DirectoryStream<Path> stream = Files.newDirectoryStream( Paths.get( dir ) ) )
262 {
263 for ( Path path : stream )
264 {
265 if ( !Files.isDirectory( path ) && isLogFileAccessible( path.toString( ) ) )
266 {
267 fileList.add( path.toString( ) );
268 }
269 }
270 }
271 }
272 return fileList;
273 }
274
275
276
277
278
279
280
281
282 @Action( ACTION_DOWNLOAD_LOG )
283 public String exportCategories( HttpServletRequest request )
284 {
285 String strId =request.getParameter( PARAMETER_ID_LOG );
286
287
288 ReferenceList listRF = DownloadLogJspBean.getLogFilesReferenceList();
289
290 if ( strId == null || Integer.parseInt( strId ) > listRF.size() )
291 {
292 AppLogService.error( "Error, log number null or invalid" );
293 }
294 else
295 {
296
297 Path path = Paths.get( listRF.get( Integer.parseInt( strId ) ).getName() );
298
299 try
300 {
301 download( Files.readAllBytes( path ), path.getFileName().toString(), CONTENT_TYPE );
302 }
303 catch ( IOException e )
304 {
305 AppLogService.error( "Error downloading file", e );
306 }
307 }
308
309 return getLogs( request );
310 }
311
312 }