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 fr.paris.lutece.portal.service.admin.AccessDeniedException;
37  import fr.paris.lutece.portal.service.cache.CacheService;
38  import fr.paris.lutece.portal.service.cache.CacheableService;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.security.SecurityTokenService;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.util.ArrayList;
49  import java.util.Collection;
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Map;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  /**
57   * This class provides the user interface to manage system features ( manage logs, view system files, ... ).
58   */
59  public class CacheJspBean extends AdminFeaturesPageJspBean
60  {
61      // Right
62      public static final String RIGHT_CACHE_MANAGEMENT = "CORE_CACHE_MANAGEMENT";
63  
64      // Jsp definition
65      public static final String JSP_MANAGE_CACHES = "ManageCaches.jsp";
66      private static final String JSP_PATH = "jsp/admin/system/";
67  
68      private static final String JSP_TOGGLE_CACHE = "jsp/admin/system/DoToggleCache.jsp";
69      private static final String PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE = "portal.system.message.confirmToggleCache";
70      private static final String PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE_TITLE = "portal.system.message.confirmToggleCacheTitle";
71      private static final String PROPERTY_MESSAGE_INVALID_CACHE_ID = "portal.system.message.invalidCacheId";
72  
73      private static final long serialVersionUID = 7010476999488231065L;
74  
75      // Markers
76      private static final String MARK_SERVICES_LIST = "services_list";
77  
78      // Template Files path
79      private static final String TEMPLATE_MANAGE_CACHES = "admin/system/manage_caches.html";
80      private static final String TEMPLATE_CACHE_INFOS = "admin/system/cache_infos.html";
81      private static final String PARAMETER_ID_CACHE = "id_cache";
82  
83      /**
84       * Returns the page to manage caches
85       * 
86       * @param request
87       *            The HttpServletRequest
88       * @return The HTML code.
89       */
90      public String getManageCaches( HttpServletRequest request )
91      {
92          HashMap<String, Object> model = new HashMap<>( );
93          model.put( MARK_SERVICES_LIST, CacheService.getCacheableServicesList( ) );
94          model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_CACHES ) );
95  
96          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CACHES, getLocale( ), model );
97  
98          return getAdminPage( template.getHtml( ) );
99      }
100 
101     /**
102      * Process cache resetting
103      *
104      * @param request
105      *            The HTTP request
106      * @return The URL to display when the process is done.
107      * @throws AccessDeniedException
108      *             if the security token is invalid
109      */
110     public static String doResetCaches( HttpServletRequest request ) throws AccessDeniedException
111     {
112         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_CACHES ) )
113         {
114             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
115         }
116         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
117 
118         if ( strCacheIndex != null )
119         {
120             int nCacheIndex = Integer.parseInt( strCacheIndex );
121             CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
122             cs.resetCache( );
123         }
124         else
125         {
126             CacheService.resetCaches( );
127             AppTemplateService.resetCache( );
128             I18nService.resetCache( );
129         }
130 
131         return JSP_MANAGE_CACHES;
132     }
133 
134     /**
135      * Reload all properties files of the application
136      *
137      * @param request
138      *            The HTTP request
139      * @return The URL to display when the process is done.
140      * @throws AccessDeniedException
141      */
142     public String doReloadProperties( HttpServletRequest request ) throws AccessDeniedException
143     {
144         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_CACHES ) )
145         {
146             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
147         }
148         AppPropertiesService.reloadAll( );
149 
150         return JSP_MANAGE_CACHES;
151     }
152 
153     /**
154      * Gets cache infos for all caches
155      * 
156      * @param request
157      *            The HTTP request
158      * @return HTML formated cache infos
159      */
160     public String getCacheInfos( HttpServletRequest request )
161     {
162         List<CacheableService> list;
163         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
164 
165         if ( strCacheIndex != null )
166         {
167             int nCacheIndex = Integer.parseInt( strCacheIndex );
168             CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
169             list = new ArrayList<>( );
170             list.add( cs );
171         }
172         else
173         {
174             list = CacheService.getCacheableServicesList( );
175         }
176 
177         HashMap<String, Collection<CacheableService>> model = new HashMap<>( );
178         model.put( MARK_SERVICES_LIST, list );
179 
180         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CACHE_INFOS, getLocale( ), model );
181 
182         return getAdminPage( template.getHtml( ) );
183     }
184 
185     /**
186      * Returns the page of confirmation for changing the cache activation
187      *
188      * @param request
189      *            The Http Request
190      * @return the HTML page
191      */
192     public String getConfirmToggleCache( HttpServletRequest request )
193     {
194         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
195         if ( strCacheIndex != null )
196         {
197             int nCacheIndex = Integer.parseInt( strCacheIndex );
198             CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
199             if ( cs != null )
200             {
201                 Object [ ] messageArgs = {
202                         cs.getName( )
203                 };
204 
205                 Map<String, Object> parameters = new HashMap<>( );
206                 parameters.put( PARAMETER_ID_CACHE, strCacheIndex );
207                 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_TOGGLE_CACHE ) );
208                 return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE, messageArgs,
209                         PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE_TITLE, JSP_TOGGLE_CACHE, "", AdminMessage.TYPE_CONFIRMATION, parameters );
210             }
211         }
212         return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_INVALID_CACHE_ID, JSP_PATH+JSP_MANAGE_CACHES, AdminMessage.TYPE_ERROR );
213     }
214 
215     /**
216      * Process cache toggle on/off
217      *
218      * @param request
219      *            The HTTP request
220      * @return The URL to display when the process is done.
221      * @throws AccessDeniedException
222      *             if the security token is invalid
223      */
224     public static String doToggleCache( HttpServletRequest request ) throws AccessDeniedException
225     {
226         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_TOGGLE_CACHE ) )
227         {
228             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
229         }
230         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
231 
232         if ( strCacheIndex != null )
233         {
234             int nCacheIndex = Integer.parseInt( strCacheIndex );
235             CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
236             cs.enableCache( !cs.isCacheEnable( ) );
237         }
238 
239         return JSP_MANAGE_CACHES;
240     }
241 
242 }