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 package fr.paris.lutece.portal.web.system;
35
36 import fr.paris.lutece.portal.service.admin.AdminUserService;
37 import fr.paris.lutece.portal.service.datastore.DatastoreService;
38 import fr.paris.lutece.portal.service.datastore.LocalizedData;
39 import fr.paris.lutece.portal.service.datastore.LocalizedDataGroup;
40 import fr.paris.lutece.portal.service.i18n.I18nService;
41 import fr.paris.lutece.portal.service.site.properties.SitePropertiesService;
42 import fr.paris.lutece.portal.service.template.AppTemplateService;
43 import fr.paris.lutece.portal.service.util.AppLogService;
44 import fr.paris.lutece.portal.service.util.AppPathService;
45 import fr.paris.lutece.portal.service.util.AppPropertiesService;
46 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
47 import fr.paris.lutece.util.html.HtmlTemplate;
48
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.FileNotFoundException;
52 import java.io.IOException;
53 import java.io.Serializable;
54
55 import java.util.ArrayList;
56 import java.util.Collection;
57 import java.util.Date;
58 import java.util.HashMap;
59 import java.util.List;
60 import java.util.Map;
61 import java.util.StringTokenizer;
62
63 import javax.servlet.ServletContext;
64 import javax.servlet.http.HttpServletRequest;
65
66
67
68
69
70
71 public class SystemJspBean extends AdminFeaturesPageJspBean
72 {
73
74 public static final String RIGHT_PROPERTIES_MANAGEMENT = "CORE_PROPERTIES_MANAGEMENT";
75 public static final String RIGHT_LOGS_VISUALISATION = "CORE_LOGS_VISUALISATION";
76
77
78 public static final String JSP_MANAGE_PROPERTIES = "ManageProperties.jsp";
79
80
81 private static final long serialVersionUID = 3770485521087669430L;
82
83
84 private static final String MARK_FILES_LIST = "files_list";
85 private static final String MARK_FILES_SYSTEM_DIRECTORY = "files_system_directory";
86 private static final String MARK_FILES_SYSTEM_NAME = "file_system_name";
87 private static final String MARK_FILE_SYSTEM_DATA = "file_system_data";
88
89
90 private static final String MARK_PROPERTIES_GROUPS_LIST = "groups_list";
91
92
93 private static final String TEMPLATE_MANAGE_FILES_SYSTEM = "admin/system/manage_files_system.html";
94 private static final String TEMPLATE_VIEW_FILES_SYSTEM = "admin/system/view_files_system.html";
95 private static final String TEMPLATE_VIEW_FILE = "admin/system/view_file.html";
96
97
98 private static final String TEMPLATE_MODIFY_PROPERTIES = "admin/system/modify_properties.html";
99
100
101 private static final String PARAMETER_FILE = "file";
102 private static final String PARAMETER_DIRECTORY = "directory";
103 private static final String PARAMETER_DIR = "dir";
104
105
106 private static final String PROPERTY_FILES_SYSTEM_LIST = "system.list";
107 private static final String PROPERTY_TITLE_MANAGE_FILES_SYSTEM = "portal.system.manage_files_system.pageTitle";
108 private static final String PROPERTY_FILE_DESCRIPTION = "portal.system.manage_files_system.description.";
109 private static final String PROPERTY_FILE_NAME = "portal.system.manage_files_system.name.";
110 private static final String PROPERTY_TITLE_VIEW_FILES_SYSTEM = "portal.system.view_files_system.pageTitle";
111 private static final String PROPERTY_TITLE_VIEW_FILE = "portal.system.view_file.pageTitle";
112 private static final String MARK_WEBAPP_URL = "webapp_url";
113 private static final String MARK_LOCALE = "locale";
114
115
116
117
118
119
120
121 public String getManageFilesSystem( HttpServletRequest request )
122 {
123 setPageTitleProperty( PROPERTY_TITLE_MANAGE_FILES_SYSTEM );
124
125 StringTokenizer st = new StringTokenizer( AppPropertiesService.getProperty( PROPERTY_FILES_SYSTEM_LIST ), "," );
126 ArrayList<SystemFile> list = new ArrayList<SystemFile>( );
127
128 while ( st.hasMoreElements( ) )
129 {
130 String strFileSystemName = st.nextToken( ).trim( );
131 SystemFile file = new SystemFile( );
132 file.setName( I18nService.getLocalizedString( PROPERTY_FILE_NAME + strFileSystemName, request.getLocale( ) ) );
133 file.setDescription( I18nService.getLocalizedString( PROPERTY_FILE_DESCRIPTION + strFileSystemName,
134 request.getLocale( ) ) );
135 file.setDirectory( AppPropertiesService.getProperty( "system." + strFileSystemName + ".directory" ) );
136 list.add( file );
137 }
138
139 HashMap<String, Collection<SystemFile>> model = new HashMap<String, Collection<SystemFile>>( );
140 model.put( MARK_FILES_LIST, list );
141
142 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_FILES_SYSTEM, getLocale( ), model );
143
144 return getAdminPage( template.getHtml( ) );
145 }
146
147
148
149
150
151
152
153 public String getManageFilesSystemDir( HttpServletRequest request )
154 {
155 setPageTitleProperty( PROPERTY_TITLE_VIEW_FILES_SYSTEM );
156
157 String strDir = request.getParameter( PARAMETER_DIR );
158
159
160 StringTokenizer st = new StringTokenizer( AppPropertiesService.getProperty( PROPERTY_FILES_SYSTEM_LIST ), "," );
161
162 boolean bDirIsValid = false;
163
164 while ( st.hasMoreElements( ) )
165 {
166
167 String strFileSystemName = st.nextToken( ).trim( );
168 String strDirectory = AppPropertiesService.getProperty( "system." + strFileSystemName + ".directory" );
169
170 if ( strDirectory.equals( strDir ) )
171 {
172 bDirIsValid = true;
173
174 break;
175 }
176 }
177
178 if ( !bDirIsValid )
179 {
180 return getManageFilesSystem( request );
181 }
182
183 String strDirectory = AppPathService.getWebAppPath( ) + strDir;
184 File directoryLog = new File( strDirectory );
185 File[] fileLog = directoryLog.listFiles( );
186
187
188 int nLogNumber = fileLog.length;
189 String[] arrayLog = new String[nLogNumber];
190 ArrayList<SystemFile> list = new ArrayList<SystemFile>( );
191
192 for ( int i = 0; i < nLogNumber; i++ )
193 {
194 arrayLog[i] = fileLog[i].getName( );
195 }
196
197
198 File[] screenArrayLog = new File[nLogNumber];
199 String name;
200
201 for ( int i = 0; i < nLogNumber; i++ )
202 {
203 int numero = i;
204 name = arrayLog[i];
205
206 for ( int a = 0; a < nLogNumber; a++ )
207 {
208 String strNameTemp = arrayLog[a];
209
210 if ( name.toLowerCase( ).trim( ).compareTo( strNameTemp.toLowerCase( ).trim( ) ) > 0 )
211 {
212 name = strNameTemp;
213 numero = a;
214 }
215 }
216
217 screenArrayLog[i] = fileLog[numero];
218 arrayLog[numero] = "{";
219 }
220
221 for ( int i = 0; i < nLogNumber; i++ )
222 {
223 File file = screenArrayLog[i];
224 SystemFile f = new SystemFile( );
225 f.setName( file.getName( ) );
226 f.setDirectory( strDir );
227 f.setSize( (int) ( file.length( ) / 1000 ) + 1 );
228 f.setDate( new Date( file.lastModified( ) ) );
229 list.add( f );
230 }
231
232 Map<String, Serializable> model = new HashMap<String, Serializable>( );
233 model.put( MARK_FILES_LIST, list );
234 model.put( MARK_FILES_SYSTEM_DIRECTORY, strDir );
235
236 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_FILES_SYSTEM, getLocale( ), model );
237
238 return getAdminPage( template.getHtml( ) );
239 }
240
241
242
243
244
245
246
247
248
249 public String getFileView( HttpServletRequest request )
250 {
251 HashMap<String, Object> model = new HashMap<String, Object>( );
252 setPageTitleProperty( PROPERTY_TITLE_VIEW_FILE );
253
254 String strFilePath;
255 String strFileData;
256 String strFile = request.getParameter( PARAMETER_FILE );
257 String strDirectory = request.getParameter( PARAMETER_DIRECTORY );
258
259 if ( strFile == null )
260 {
261 strFilePath = "ERROR";
262 strFileData = "No file selected !";
263 }
264 else
265 {
266 strFilePath = AppPathService.getWebAppPath( );
267
268 if ( strFilePath == null )
269 {
270 strFilePath = "ERROR";
271 strFileData = strFile + " not found !";
272 }
273 else
274 {
275 strFileData = getFileData( strFilePath + strDirectory + strFile );
276 }
277 }
278
279 model.put( MARK_FILES_SYSTEM_NAME, strFilePath + strDirectory + strFile );
280 model.put( MARK_FILE_SYSTEM_DATA, strFileData );
281 model.put( MARK_FILES_SYSTEM_DIRECTORY, strDirectory );
282
283 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_FILE, getLocale( ), model );
284
285 return getAdminPage( template.getHtml( ) );
286 }
287
288
289
290
291
292
293
294 public String getManageProperties( HttpServletRequest request )
295 {
296 HashMap<String, Object> model = new HashMap<String, Object>( );
297
298 model.put( MARK_PROPERTIES_GROUPS_LIST, SitePropertiesService.getGroups( getLocale( ) ) );
299 model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
300 model.put( MARK_LOCALE, getLocale( ).getLanguage( ) );
301
302 HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MODIFY_PROPERTIES, getLocale( ), model );
303
304 return getAdminPage( templateList.getHtml( ) );
305 }
306
307
308
309
310
311
312
313
314 public static String doModifyProperties( HttpServletRequest request, ServletContext context )
315 {
316 List<LocalizedDataGroup> groups = SitePropertiesService.getGroups( AdminUserService.getAdminUser( request )
317 .getLocale( ) );
318
319 for ( LocalizedDataGroup group : groups )
320 {
321 List<LocalizedData> datas = group.getLocalizedDataList( );
322
323 for ( LocalizedData data : datas )
324 {
325 String strValue = request.getParameter( data.getKey( ) );
326
327 if ( ( strValue != null ) && !data.getValue( ).equals( strValue ) )
328 {
329 DatastoreService.setDataValue( data.getKey( ), strValue );
330 }
331 }
332 }
333
334
335 return JSP_MANAGE_PROPERTIES;
336 }
337
338
339
340
341
342
343
344
345
346 private static String getFileData( String strFilePath )
347 {
348 StringBuilder sbData = new StringBuilder( );
349
350 FileInputStream is = null;
351
352 try
353 {
354 is = new FileInputStream( strFilePath );
355
356 int chr = 0;
357
358 while ( chr != -1 )
359 {
360 chr = is.read( );
361 sbData.append( (char) chr );
362 }
363
364
365 sbData.setLength( sbData.length( ) - 1 );
366 }
367 catch ( FileNotFoundException e )
368 {
369 sbData.append( "File " ).append( strFilePath ).append( " not found" );
370 }
371 catch ( IOException e )
372 {
373 sbData.append( "Error reading the file : " );
374 sbData.append( strFilePath );
375 }
376 finally
377 {
378 if ( is != null )
379 {
380 try
381 {
382 is.close( );
383 }
384 catch ( IOException e )
385 {
386 AppLogService.error( e.getMessage( ), e );
387 }
388 }
389 }
390
391 return sbData.toString( );
392 }
393 }