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