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