1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
78
79
80
81
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
98
99
100
101
102
103
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
159
160
161
162
163
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 }