View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * This class provides the user interface to manage system features ( manage
69   * logs, view system files, ... ).
70   */
71  public class SystemJspBean extends AdminFeaturesPageJspBean
72  {
73      // Right
74      public static final String RIGHT_PROPERTIES_MANAGEMENT = "CORE_PROPERTIES_MANAGEMENT";
75      public static final String RIGHT_LOGS_VISUALISATION = "CORE_LOGS_VISUALISATION";
76  
77      // Jsp definition
78      public static final String JSP_MANAGE_PROPERTIES = "ManageProperties.jsp";
79  
80      /** serial id */
81      private static final long serialVersionUID = 3770485521087669430L;
82  
83      // Markers
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      //    private static final String MARK_WEBMASTER_PROPERTIES = "webmaster_properties";
90      private static final String MARK_PROPERTIES_GROUPS_LIST = "groups_list";
91  
92      // Template Files path
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      //    private static final String TEMPLATE_MANAGE_PROPERTIES = "admin/system/manage_properties.html";
98      private static final String TEMPLATE_MODIFY_PROPERTIES = "admin/system/modify_properties.html";
99  
100     // Parameters
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     // Properties file definition
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      * Returns ViewLogs page
117      *
118      * @param request The HTTP request.
119      * @return The HTML code
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      * Get directory list
149      *
150      * @param request The request
151      * @return The html code
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         // Check if dir is valid
160         StringTokenizer st = new StringTokenizer( AppPropertiesService.getProperty( PROPERTY_FILES_SYSTEM_LIST ), "," );
161 
162         boolean bDirIsValid = false;
163 
164         while ( st.hasMoreElements(  ) )
165         {
166             // The dir param is valid, if and only if it's equal to a property value
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         // Creating names array
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         //defines the order of the logs
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     // Methods to display a system file
243     /**
244      * Returns a FileView page
245      *
246      * @param request The HTTP request.
247      * @return The HTML code
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      * Returns the form to update info about the webmaster.properties
290      *
291      * @param request The Http request
292      * @return The HTML form to update info
293      */
294     public String getManageProperties( HttpServletRequest request )
295     {
296         HashMap<String, Object> model = new HashMap<String, Object>(  );
297         // Insert the rows in the list
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      * Process the change form of the webmaster.properties
309      *
310      * @param request The Http request
311      * @param context The context
312      * @return The Jsp URL of the process result
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         // if the operation occurred well, redirects towards the view of the Properties
335         return JSP_MANAGE_PROPERTIES;
336     }
337 
338     ///////////////////////////////////////////////////////////////////////////////
339     // Private Implementation
340     /**
341      * Returns the content of a file.
342      *
343      * @param strFilePath The file Path
344      * @return The content of the file.
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             //we delete the end of file character
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 }