DaemonsJspBean.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.daemon.AppDaemonService;
  37. import fr.paris.lutece.portal.service.i18n.I18nService;
  38. import fr.paris.lutece.portal.service.message.AdminMessage;
  39. import fr.paris.lutece.portal.service.message.AdminMessageService;
  40. import fr.paris.lutece.portal.service.security.SecurityTokenService;
  41. import fr.paris.lutece.portal.service.template.AppTemplateService;
  42. import fr.paris.lutece.portal.service.util.AppLogService;
  43. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  44. import fr.paris.lutece.portal.web.admin.AdminPageJspBean;
  45. import fr.paris.lutece.util.html.HtmlTemplate;

  46. import org.apache.commons.lang3.StringUtils;

  47. import java.util.HashMap;

  48. import javax.servlet.http.HttpServletRequest;

  49. /**
  50.  *
  51.  */
  52. public class DaemonsJspBean extends AdminPageJspBean
  53. {
  54.     public static final String RIGHT_DAEMONS_MANAGEMENT = "CORE_DAEMONS_MANAGEMENT";
  55.     private static final long serialVersionUID = 3636973660388119199L;
  56.     private static final String TEMPLATE_MANAGE_DAEMONS = "admin/system/manage_daemons.html";
  57.     private static final String MARK_DAEMONS_LIST = "daemons_list";
  58.     private static final String MARK_LOG_MAX_SIZE = "log_max_size";
  59.     private static final String PARAMETER_DAEMON = "daemon";
  60.     private static final String PARAMETER_ACTION = "action";
  61.     private static final String PARAMETER_INTERVAL = "interval";
  62.     private static final String ACTION_START = "START";
  63.     private static final String ACTION_STOP = "STOP";
  64.     private static final String ACTION_RUN = "RUN";
  65.     private static final String ACTION_UPDATE_INTERVAL = "UPDATE_INTERVAL";
  66.     private static final String PROPERTY_FIELD_INTERVAL = "portal.system.manage_daemons.columnTitleInterval";
  67.     private static final String PROPERTY_DAEMON_LASTRUNLOG_MAX_SIZE = "portal.system.manage_daemons.lastrunlog.maxsize";
  68.     private static final String MESSAGE_MANDATORY_FIELD = "portal.util.message.mandatoryField";
  69.     private static final String MESSAGE_NUMERIC_FIELD = "portal.util.message.errorNumericField";

  70.     /**
  71.      * Build the manage daemon page
  72.      *
  73.      * @param request
  74.      *            The HTTP request
  75.      * @return The Manage daemon page
  76.      */
  77.     public String getManageDaemons( HttpServletRequest request )
  78.     {
  79.         HashMap<String, Object> model = new HashMap<>( );
  80.        
  81.         model.put( MARK_LOG_MAX_SIZE, AppPropertiesService.getPropertyInt( PROPERTY_DAEMON_LASTRUNLOG_MAX_SIZE, 30000 ) );
  82.         model.put( MARK_DAEMONS_LIST, AppDaemonService.getDaemonEntries( ) );
  83.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_DAEMONS ) );

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

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

  87.     /**
  88.      * Process the daemon action
  89.      *
  90.      * @param request
  91.      *            The HTTP request
  92.      * @return The forward URL
  93.      * @throws AccessDeniedException
  94.      *             if the security token is invalid
  95.      */
  96.     public String doDaemonAction( HttpServletRequest request ) throws AccessDeniedException
  97.     {
  98.         String strAction = request.getParameter( PARAMETER_ACTION );
  99.         String strDaemonKey = request.getParameter( PARAMETER_DAEMON );

  100.         switch( strAction )
  101.         {
  102.             case ACTION_START:
  103.                 assertSecurityToken( request );
  104.                 AppDaemonService.startDaemon( strDaemonKey );
  105.                 break;
  106.             case ACTION_STOP:
  107.                 assertSecurityToken( request );
  108.                 AppDaemonService.stopDaemon( strDaemonKey );
  109.                 break;
  110.             case ACTION_RUN:
  111.                 assertSecurityToken( request );
  112.                 AppDaemonService.signalDaemon( strDaemonKey );
  113.                 break;
  114.             case ACTION_UPDATE_INTERVAL:
  115.                 String strErrorMessage = null;
  116.                 String strDaemonInterval = request.getParameter( PARAMETER_INTERVAL );

  117.                 Object [ ] tabFieldInterval = {
  118.                         I18nService.getLocalizedString( PROPERTY_FIELD_INTERVAL, getLocale( ) )
  119.                 };

  120.                 if ( StringUtils.isEmpty( strDaemonInterval ) )
  121.                 {
  122.                     strErrorMessage = MESSAGE_MANDATORY_FIELD;
  123.                 }
  124.                 else
  125.                     if ( !StringUtils.isNumeric( strDaemonInterval ) )
  126.                     {
  127.                         strErrorMessage = MESSAGE_NUMERIC_FIELD;
  128.                     }

  129.                 if ( strErrorMessage != null )
  130.                 {
  131.                     return AdminMessageService.getMessageUrl( request, strErrorMessage, tabFieldInterval, AdminMessage.TYPE_STOP );
  132.                 }

  133.                 assertSecurityToken( request );
  134.                 AppDaemonService.modifyDaemonInterval( strDaemonKey, strDaemonInterval );
  135.                 break;
  136.             default:
  137.                 AppLogService.error( "Unknown daemon action : {}", strAction );
  138.         }

  139.         return getHomeUrl( request );
  140.     }

  141.     /**
  142.      * Asserts that the security token is valid
  143.      *
  144.      * @param request
  145.      *            the request
  146.      * @throws AccessDeniedException
  147.      *             if the security token is invalid
  148.      */
  149.     private void assertSecurityToken( HttpServletRequest request ) throws AccessDeniedException
  150.     {
  151.         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_DAEMONS ) )
  152.         {
  153.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  154.         }
  155.     }
  156. }