CacheJspBean.java

  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. import fr.paris.lutece.portal.service.admin.AccessDeniedException;
  36. import fr.paris.lutece.portal.service.cache.CacheService;
  37. import fr.paris.lutece.portal.service.cache.CacheableService;
  38. import fr.paris.lutece.portal.service.i18n.I18nService;
  39. import fr.paris.lutece.portal.service.message.AdminMessage;
  40. import fr.paris.lutece.portal.service.message.AdminMessageService;
  41. import fr.paris.lutece.portal.service.security.SecurityTokenService;
  42. import fr.paris.lutece.portal.service.template.AppTemplateService;
  43. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  44. import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
  45. import fr.paris.lutece.util.html.HtmlTemplate;

  46. import java.util.ArrayList;
  47. import java.util.Collection;
  48. import java.util.HashMap;
  49. import java.util.List;
  50. import java.util.Map;

  51. import javax.servlet.http.HttpServletRequest;

  52. /**
  53.  * This class provides the user interface to manage system features ( manage logs, view system files, ... ).
  54.  */
  55. public class CacheJspBean extends AdminFeaturesPageJspBean
  56. {
  57.     // Right
  58.     public static final String RIGHT_CACHE_MANAGEMENT = "CORE_CACHE_MANAGEMENT";

  59.     // Jsp definition
  60.     public static final String JSP_MANAGE_CACHES = "ManageCaches.jsp";

  61.     private static final String JSP_TOGGLE_CACHE = "jsp/admin/system/DoToggleCache.jsp";
  62.     private static final String PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE = "portal.system.message.confirmToggleCache";
  63.     private static final String PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE_TITLE = "portal.system.message.confirmToggleCacheTitle";
  64.     private static final String PROPERTY_MESSAGE_INVALID_CACHE_ID = "portal.system.message.invalidCacheId";

  65.     private static final long serialVersionUID = 7010476999488231065L;

  66.     // Markers
  67.     private static final String MARK_SERVICES_LIST = "services_list";

  68.     // Template Files path
  69.     private static final String TEMPLATE_MANAGE_CACHES = "admin/system/manage_caches.html";
  70.     private static final String TEMPLATE_CACHE_INFOS = "admin/system/cache_infos.html";
  71.     private static final String PARAMETER_ID_CACHE = "id_cache";

  72.     /**
  73.      * Returns the page to manage caches
  74.      *
  75.      * @param request
  76.      *            The HttpServletRequest
  77.      * @return The HTML code.
  78.      */
  79.     public String getManageCaches( HttpServletRequest request )
  80.     {
  81.         HashMap<String, Object> model = new HashMap<>( );
  82.         model.put( MARK_SERVICES_LIST, CacheService.getCacheableServicesList( ) );
  83.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_CACHES ) );

  84.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CACHES, getLocale( ), model );

  85.         return getAdminPage( template.getHtml( ) );
  86.     }

  87.     /**
  88.      * Process cache resetting
  89.      *
  90.      * @param request
  91.      *            The HTTP request
  92.      * @return The URL to display when the process is done.
  93.      * @throws AccessDeniedException
  94.      *             if the security token is invalid
  95.      */
  96.     public static String doResetCaches( HttpServletRequest request ) throws AccessDeniedException
  97.     {
  98.         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_CACHES ) )
  99.         {
  100.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  101.         }
  102.         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );

  103.         if ( strCacheIndex != null )
  104.         {
  105.             int nCacheIndex = Integer.parseInt( strCacheIndex );
  106.             CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
  107.             cs.resetCache( );
  108.         }
  109.         else
  110.         {
  111.             CacheService.resetCaches( );
  112.             AppTemplateService.resetCache( );
  113.             I18nService.resetCache( );
  114.         }

  115.         return JSP_MANAGE_CACHES;
  116.     }

  117.     /**
  118.      * Reload all properties files of the application
  119.      *
  120.      * @param request
  121.      *            The HTTP request
  122.      * @return The URL to display when the process is done.
  123.      * @throws AccessDeniedException
  124.      */
  125.     public String doReloadProperties( HttpServletRequest request ) throws AccessDeniedException
  126.     {
  127.         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_CACHES ) )
  128.         {
  129.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  130.         }
  131.         AppPropertiesService.reloadAll( );

  132.         return JSP_MANAGE_CACHES;
  133.     }

  134.     /**
  135.      * Gets cache infos for all caches
  136.      *
  137.      * @param request
  138.      *            The HTTP request
  139.      * @return HTML formated cache infos
  140.      */
  141.     public String getCacheInfos( HttpServletRequest request )
  142.     {
  143.         List<CacheableService> list;
  144.         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );

  145.         if ( strCacheIndex != null )
  146.         {
  147.             int nCacheIndex = Integer.parseInt( strCacheIndex );
  148.             CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
  149.             list = new ArrayList<>( );
  150.             list.add( cs );
  151.         }
  152.         else
  153.         {
  154.             list = CacheService.getCacheableServicesList( );
  155.         }

  156.         HashMap<String, Collection<CacheableService>> model = new HashMap<>( );
  157.         model.put( MARK_SERVICES_LIST, list );

  158.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CACHE_INFOS, getLocale( ), model );

  159.         return getAdminPage( template.getHtml( ) );
  160.     }

  161.     /**
  162.      * Returns the page of confirmation for changing the cache activation
  163.      *
  164.      * @param request
  165.      *            The Http Request
  166.      * @return the HTML page
  167.      */
  168.     public String getConfirmToggleCache( HttpServletRequest request )
  169.     {
  170.         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
  171.         if ( strCacheIndex != null )
  172.         {
  173.             int nCacheIndex = Integer.parseInt( strCacheIndex );
  174.             CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
  175.             if ( cs != null )
  176.             {
  177.                 Object [ ] messageArgs = {
  178.                         cs.getName( )
  179.                 };

  180.                 Map<String, Object> parameters = new HashMap<>( );
  181.                 parameters.put( PARAMETER_ID_CACHE, strCacheIndex );
  182.                 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_TOGGLE_CACHE ) );
  183.                 return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE, messageArgs,
  184.                         PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE_TITLE, JSP_TOGGLE_CACHE, "", AdminMessage.TYPE_CONFIRMATION, parameters );
  185.             }
  186.         }
  187.         return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_INVALID_CACHE_ID, JSP_MANAGE_CACHES, AdminMessage.TYPE_ERROR );
  188.     }

  189.     /**
  190.      * Process cache toggle on/off
  191.      *
  192.      * @param request
  193.      *            The HTTP request
  194.      * @return The URL to display when the process is done.
  195.      * @throws AccessDeniedException
  196.      *             if the security token is invalid
  197.      */
  198.     public static String doToggleCache( HttpServletRequest request ) throws AccessDeniedException
  199.     {
  200.         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_TOGGLE_CACHE ) )
  201.         {
  202.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  203.         }
  204.         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );

  205.         if ( strCacheIndex != null )
  206.         {
  207.             int nCacheIndex = Integer.parseInt( strCacheIndex );
  208.             CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
  209.             cs.enableCache( !cs.isCacheEnable( ) );
  210.         }

  211.         return JSP_MANAGE_CACHES;
  212.     }

  213. }