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.daemon.AppDaemonService;
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.AppLogService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.web.admin.AdminPageJspBean;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import org.apache.commons.lang3.StringUtils;
49  
50  import java.util.HashMap;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   *
56   */
57  public class DaemonsJspBean extends AdminPageJspBean
58  {
59      public static final String RIGHT_DAEMONS_MANAGEMENT = "CORE_DAEMONS_MANAGEMENT";
60      private static final long serialVersionUID = 3636973660388119199L;
61      private static final String TEMPLATE_MANAGE_DAEMONS = "admin/system/manage_daemons.html";
62      private static final String MARK_DAEMONS_LIST = "daemons_list";
63      private static final String MARK_LOG_MAX_SIZE = "log_max_size";
64      private static final String PARAMETER_DAEMON = "daemon";
65      private static final String PARAMETER_ACTION = "action";
66      private static final String PARAMETER_INTERVAL = "interval";
67      private static final String ACTION_START = "START";
68      private static final String ACTION_STOP = "STOP";
69      private static final String ACTION_RUN = "RUN";
70      private static final String ACTION_UPDATE_INTERVAL = "UPDATE_INTERVAL";
71      private static final String PROPERTY_FIELD_INTERVAL = "portal.system.manage_daemons.columnTitleInterval";
72      private static final String PROPERTY_DAEMON_LASTRUNLOG_MAX_SIZE = "portal.system.manage_daemons.lastrunlog.maxsize";
73      private static final String MESSAGE_MANDATORY_FIELD = "portal.util.message.mandatoryField";
74      private static final String MESSAGE_NUMERIC_FIELD = "portal.util.message.errorNumericField";
75  
76      /**
77       * Build the manage daemon page
78       * 
79       * @param request
80       *            The HTTP request
81       * @return The Manage daemon page
82       */
83      public String getManageDaemons( HttpServletRequest request )
84      {
85          HashMap<String, Object> model = new HashMap<>( );
86          
87          model.put( MARK_LOG_MAX_SIZE, AppPropertiesService.getPropertyInt( PROPERTY_DAEMON_LASTRUNLOG_MAX_SIZE, 30000 ) );
88          model.put( MARK_DAEMONS_LIST, AppDaemonService.getDaemonEntries( ) );
89          model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_DAEMONS ) );
90  
91          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DAEMONS, getLocale( ), model );
92  
93          return getAdminPage( template.getHtml( ) );
94      }
95  
96      /**
97       * Process the daemon action
98       * 
99       * @param request
100      *            The HTTP request
101      * @return The forward URL
102      * @throws AccessDeniedException
103      *             if the security token is invalid
104      */
105     public String doDaemonAction( HttpServletRequest request ) throws AccessDeniedException
106     {
107         String strAction = request.getParameter( PARAMETER_ACTION );
108         String strDaemonKey = request.getParameter( PARAMETER_DAEMON );
109 
110         switch( strAction )
111         {
112             case ACTION_START:
113                 assertSecurityToken( request );
114                 AppDaemonService.startDaemon( strDaemonKey );
115                 break;
116             case ACTION_STOP:
117                 assertSecurityToken( request );
118                 AppDaemonService.stopDaemon( strDaemonKey );
119                 break;
120             case ACTION_RUN:
121                 assertSecurityToken( request );
122                 AppDaemonService.signalDaemon( strDaemonKey );
123                 break;
124             case ACTION_UPDATE_INTERVAL:
125                 String strErrorMessage = null;
126                 String strDaemonInterval = request.getParameter( PARAMETER_INTERVAL );
127 
128                 Object [ ] tabFieldInterval = {
129                         I18nService.getLocalizedString( PROPERTY_FIELD_INTERVAL, getLocale( ) )
130                 };
131 
132                 if ( StringUtils.isEmpty( strDaemonInterval ) )
133                 {
134                     strErrorMessage = MESSAGE_MANDATORY_FIELD;
135                 }
136                 else
137                     if ( !StringUtils.isNumeric( strDaemonInterval ) )
138                     {
139                         strErrorMessage = MESSAGE_NUMERIC_FIELD;
140                     }
141 
142                 if ( strErrorMessage != null )
143                 {
144                     return AdminMessageService.getMessageUrl( request, strErrorMessage, tabFieldInterval, AdminMessage.TYPE_STOP );
145                 }
146 
147                 assertSecurityToken( request );
148                 AppDaemonService.modifyDaemonInterval( strDaemonKey, strDaemonInterval );
149                 break;
150             default:
151                 AppLogService.error( "Unknown daemon action : {}", strAction );
152         }
153 
154         return getHomeUrl( request );
155     }
156 
157     /**
158      * Asserts that the security token is valid
159      * 
160      * @param request
161      *            the request
162      * @throws AccessDeniedException
163      *             if the security token is invalid
164      */
165     private void assertSecurityToken( HttpServletRequest request ) throws AccessDeniedException
166     {
167         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_DAEMONS ) )
168         {
169             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
170         }
171     }
172 }