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.cache.CacheService;
38 import fr.paris.lutece.portal.service.cache.CacheableService;
39 import fr.paris.lutece.portal.service.i18n.I18nService;
40 import fr.paris.lutece.portal.service.message.AdminMessage;
41 import fr.paris.lutece.portal.service.message.AdminMessageService;
42 import fr.paris.lutece.portal.service.security.SecurityTokenService;
43 import fr.paris.lutece.portal.service.template.AppTemplateService;
44 import fr.paris.lutece.portal.service.util.AppPropertiesService;
45 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
46 import fr.paris.lutece.util.html.HtmlTemplate;
47
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53
54 import javax.servlet.http.HttpServletRequest;
55
56
57
58
59 public class CacheJspBean extends AdminFeaturesPageJspBean
60 {
61
62 public static final String RIGHT_CACHE_MANAGEMENT = "CORE_CACHE_MANAGEMENT";
63
64
65 public static final String JSP_MANAGE_CACHES = "ManageCaches.jsp";
66
67 private static final String JSP_TOGGLE_CACHE = "jsp/admin/system/DoToggleCache.jsp";
68 private static final String PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE = "portal.system.message.confirmToggleCache";
69 private static final String PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE_TITLE = "portal.system.message.confirmToggleCacheTitle";
70 private static final String PROPERTY_MESSAGE_INVALID_CACHE_ID = "portal.system.message.invalidCacheId";
71
72 private static final long serialVersionUID = 7010476999488231065L;
73
74
75 private static final String MARK_SERVICES_LIST = "services_list";
76
77
78 private static final String TEMPLATE_MANAGE_CACHES = "admin/system/manage_caches.html";
79 private static final String TEMPLATE_CACHE_INFOS = "admin/system/cache_infos.html";
80 private static final String PARAMETER_ID_CACHE = "id_cache";
81
82
83
84
85
86
87
88
89 public String getManageCaches( HttpServletRequest request )
90 {
91 HashMap<String, Object> model = new HashMap<>( );
92 model.put( MARK_SERVICES_LIST, CacheService.getCacheableServicesList( ) );
93 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_CACHES ) );
94
95 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CACHES, getLocale( ), model );
96
97 return getAdminPage( template.getHtml( ) );
98 }
99
100
101
102
103
104
105
106
107
108
109 public static String doResetCaches( HttpServletRequest request ) throws AccessDeniedException
110 {
111 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_CACHES ) )
112 {
113 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
114 }
115 String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
116
117 if ( strCacheIndex != null )
118 {
119 int nCacheIndex = Integer.parseInt( strCacheIndex );
120 CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
121 cs.resetCache( );
122 }
123 else
124 {
125 CacheService.resetCaches( );
126 AppTemplateService.resetCache( );
127 I18nService.resetCache( );
128 }
129
130 return JSP_MANAGE_CACHES;
131 }
132
133
134
135
136
137
138
139
140
141 public String doReloadProperties( HttpServletRequest request ) throws AccessDeniedException
142 {
143 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_CACHES ) )
144 {
145 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
146 }
147 AppPropertiesService.reloadAll( );
148
149 return JSP_MANAGE_CACHES;
150 }
151
152
153
154
155
156
157
158
159 public String getCacheInfos( HttpServletRequest request )
160 {
161 List<CacheableService> list;
162 String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
163
164 if ( strCacheIndex != null )
165 {
166 int nCacheIndex = Integer.parseInt( strCacheIndex );
167 CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
168 list = new ArrayList<>( );
169 list.add( cs );
170 }
171 else
172 {
173 list = CacheService.getCacheableServicesList( );
174 }
175
176 HashMap<String, Collection<CacheableService>> model = new HashMap<>( );
177 model.put( MARK_SERVICES_LIST, list );
178
179 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CACHE_INFOS, getLocale( ), model );
180
181 return getAdminPage( template.getHtml( ) );
182 }
183
184
185
186
187
188
189
190
191 public String getConfirmToggleCache( HttpServletRequest request )
192 {
193 String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
194 if ( strCacheIndex != null )
195 {
196 int nCacheIndex = Integer.parseInt( strCacheIndex );
197 CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
198 if ( cs != null )
199 {
200 Object [ ] messageArgs = {
201 cs.getName( )
202 };
203
204 Map<String, Object> parameters = new HashMap<>( );
205 parameters.put( PARAMETER_ID_CACHE, strCacheIndex );
206 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_TOGGLE_CACHE ) );
207 return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE, messageArgs,
208 PROPERTY_MESSAGE_CONFIRM_TOOGLE_CACHE_TITLE, JSP_TOGGLE_CACHE, "", AdminMessage.TYPE_CONFIRMATION, parameters );
209 }
210 }
211 return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_INVALID_CACHE_ID, JSP_MANAGE_CACHES, AdminMessage.TYPE_ERROR );
212 }
213
214
215
216
217
218
219
220
221
222
223 public static String doToggleCache( HttpServletRequest request ) throws AccessDeniedException
224 {
225 if ( !SecurityTokenService.getInstance( ).validate( request, JSP_TOGGLE_CACHE ) )
226 {
227 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
228 }
229 String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
230
231 if ( strCacheIndex != null )
232 {
233 int nCacheIndex = Integer.parseInt( strCacheIndex );
234 CacheableService cs = CacheService.getCacheableServicesList( ).get( nCacheIndex );
235 cs.enableCache( !cs.isCacheEnable( ) );
236 }
237
238 return JSP_MANAGE_CACHES;
239 }
240
241 }