View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of 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 java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.IOException;
39  import java.io.Serializable;
40  import java.sql.Timestamp;
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.Collections;
44  import java.util.Date;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.servlet.ServletContext;
50  import javax.servlet.http.HttpServletRequest;
51  
52  import org.apache.logging.log4j.core.LoggerContext;
53  
54  import fr.paris.lutece.portal.business.file.File;
55  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
56  import fr.paris.lutece.portal.service.admin.AdminUserService;
57  import fr.paris.lutece.portal.service.datastore.DatastoreService;
58  import fr.paris.lutece.portal.service.datastore.LocalizedData;
59  import fr.paris.lutece.portal.service.datastore.LocalizedDataGroup;
60  import fr.paris.lutece.portal.service.file.FileService;
61  import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
62  import fr.paris.lutece.portal.service.i18n.I18nService;
63  import fr.paris.lutece.portal.service.security.SecurityTokenService;
64  import fr.paris.lutece.portal.service.site.properties.SitePropertiesService;
65  import fr.paris.lutece.portal.service.template.AppTemplateService;
66  import fr.paris.lutece.portal.service.util.AppLogService;
67  import fr.paris.lutece.portal.service.util.AppPathService;
68  import fr.paris.lutece.portal.service.util.AppPropertiesService;
69  import fr.paris.lutece.portal.service.util.LoggerInfo;
70  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
71  import fr.paris.lutece.util.html.HtmlTemplate;
72  import fr.paris.lutece.util.http.SecurityUtil;
73  
74  /**
75   * This class provides the user interface to manage system features ( manage logs, view system files, ... ).
76   */
77  public class SystemJspBean extends AdminFeaturesPageJspBean
78  {
79      // Right
80      public static final String RIGHT_PROPERTIES_MANAGEMENT = "CORE_PROPERTIES_MANAGEMENT";
81  
82      // Jsp definition
83      public static final String JSP_MANAGE_PROPERTIES = "ManageProperties.jsp";
84  
85      /** serial id */
86      private static final long serialVersionUID = 3770485521087669430L;
87  
88      // Markers
89      private static final String MARK_PROPERTIES_GROUPS_LIST = "groups_list";
90  
91      // Template 
92      private static final String TEMPLATE_MODIFY_PROPERTIES = "admin/system/modify_properties.html";
93  
94      // Properties file definition
95      private static final String MARK_WEBAPP_URL = "webapp_url";
96      private static final String MARK_LOCALE = "locale";
97  
98      
99      /**
100      * Returns the form to update site properties in DataStore
101      *
102      * @param request
103      *            The Http request
104      * @return The HTML form to update info
105      */
106     public String getManageProperties( HttpServletRequest request )
107     {
108         Map<String, Object> model = new HashMap<>( );
109         model.put( MARK_PROPERTIES_GROUPS_LIST, SitePropertiesService.getGroups( getLocale( ) ) );
110         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
111         model.put( MARK_LOCALE, getLocale( ).getLanguage( ) );
112         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_PROPERTIES ) );
113 
114         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MODIFY_PROPERTIES, getLocale( ), model );
115 
116         return getAdminPage( templateList.getHtml( ) );
117     }
118 
119     /**
120      * Process the update of site properties in DataStore
121      *
122      * @param request
123      *            The Http request
124      * @param context
125      *            The context
126      * @return The Jsp URL of the process result
127      * @throws AccessDeniedException
128      *             if the security token is invalid
129      */
130     public static String doModifyProperties( HttpServletRequest request, ServletContext context ) throws AccessDeniedException
131     {
132         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_PROPERTIES ) )
133         {
134             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
135         }
136         List<LocalizedDataGroup> groups = SitePropertiesService.getGroups( AdminUserService.getAdminUser( request ).getLocale( ) );
137 
138         for ( LocalizedDataGroup group : groups )
139         {
140             List<LocalizedData> datas = group.getLocalizedDataList( );
141 
142             for ( LocalizedData data : datas )
143             {
144                 String strValue = request.getParameter( data.getKey( ) );
145 
146                 if ( ( strValue != null ) && !data.getValue( ).equals( strValue ) )
147                 {
148                     DatastoreService.setDataValue( data.getKey( ), strValue );
149                 }
150             }
151         }
152 
153         // if the operation occurred well, redirects towards the view of the Properties
154         return JSP_MANAGE_PROPERTIES;
155     }
156 }