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.business.portlet.PortletType;
37 import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
38 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
39 import fr.paris.lutece.portal.service.admin.AdminUserService;
40 import fr.paris.lutece.portal.service.database.AppConnectionService;
41 import fr.paris.lutece.portal.service.i18n.I18nService;
42 import fr.paris.lutece.portal.service.init.AppInfo;
43 import fr.paris.lutece.portal.service.message.AdminMessage;
44 import fr.paris.lutece.portal.service.message.AdminMessageService;
45 import fr.paris.lutece.portal.service.plugin.Plugin;
46 import fr.paris.lutece.portal.service.plugin.PluginService;
47 import fr.paris.lutece.portal.service.security.SecurityTokenService;
48 import fr.paris.lutece.portal.service.template.AppTemplateService;
49 import fr.paris.lutece.portal.service.util.AppLogService;
50 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
51 import fr.paris.lutece.util.ReferenceList;
52 import fr.paris.lutece.util.html.HtmlTemplate;
53
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.Locale;
58 import java.util.Map;
59
60 import javax.servlet.ServletContext;
61 import javax.servlet.http.HttpServletRequest;
62
63
64
65
66 public class PluginJspBean extends AdminFeaturesPageJspBean
67 {
68
69
70 public static final String RIGHT_MANAGE_PLUGINS = "CORE_PLUGINS_MANAGEMENT";
71 private static final long serialVersionUID = -9058113426951331118L;
72 private static final String TEMPLATE_MANAGE_PLUGINS = "admin/system/manage_plugins.html";
73 private static final String MARK_PLUGINS_LIST = "plugins_list";
74 private static final String MARK_CORE = "core";
75 private static final String MARK_POOLS_LIST = "pools_list";
76 private static final String MARK_FILTER_LIST = "filter_list";
77 private static final String MARK_CURRENT_FILTER = "current_filter";
78 private static final String PROPERTY_PLUGIN_MESSAGE = "portal.system.message.confirmDisable";
79 private static final String PROPERTY_PLUGIN_PORTLET_EXIST_MESSAGE = "portal.system.message.portletExist";
80 private static final String PROPERTY_PLUGIN_NO_CORE_COMPATIBILITY_MESSAGE = "portal.system.message.noCoreCompatibility";
81 private static final String PROPERTY_PLUGIN_INSTALL_ERROR = "portal.system.message.installError";
82 private static final String PARAM_PLUGIN_NAME = "plugin_name";
83 private static final String PARAM_PLUGIN_TYPE = "plugin_type";
84 private static final String PARAM_DB_POOL_NAME = "db_pool_name";
85 private static final String PARAM_PLUGIN_TYPE_ALL = "all";
86 private static final String PARAM_PLUGIN_TYPE_PORTLET = "portlet";
87 private static final String PARAM_PLUGIN_TYPE_APPLICATION = "application";
88 private static final String PARAM_PLUGIN_TYPE_FEATURE = "feature";
89 private static final String PARAM_PLUGIN_TYPE_INSERTSERVICE = "insertservice";
90 private static final String PARAM_PLUGIN_TYPE_CONTENTSERVICE = "contentservice";
91 private static final String PROPERTY_PLUGIN_TYPE_NAME_ALL = "portal.system.pluginType.name.all";
92 private static final String PROPERTY_PLUGIN_TYPE_NAME_APPLICATION = "portal.system.pluginType.name.application";
93 private static final String PROPERTY_PLUGIN_TYPE_NAME_PORTLET = "portal.system.pluginType.name.portlet";
94 private static final String PROPERTY_PLUGIN_TYPE_NAME_FEATURE = "portal.system.pluginType.name.feature";
95 private static final String PROPERTY_PLUGIN_TYPE_NAME_INSERTSERVICE = "portal.system.pluginType.name.insertService";
96 private static final String PROPERTY_PLUGIN_TYPE_NAME_CONTENTSERVICE = "portal.system.pluginType.name.contentService";
97 private static final String TEMPLATE_PLUGIN_DETAILS = "/admin/system/view_plugin.html";
98 private static final String JSP_UNINSTALL_PLUGIN = "jsp/admin/system/DoUninstallPlugin.jsp";
99
100
101
102
103
104
105
106
107 public String getManagePlugins( HttpServletRequest request )
108 {
109 Locale locale = AdminUserService.getLocale( request );
110 String strPluginTypeFilter = request.getParameter( PARAM_PLUGIN_TYPE );
111 Collection<Plugin> listPlugins = PluginService.getPluginList( );
112 HashMap<String, Object> model = new HashMap<>( );
113 model.put( MARK_PLUGINS_LIST, filterPluginsList( listPlugins, strPluginTypeFilter ) );
114 model.put( MARK_CORE, PluginService.getCore( ) );
115 model.put( MARK_POOLS_LIST, getPoolsList( ) );
116 model.put( MARK_FILTER_LIST, getPluginTypeFilterList( locale ) );
117 model.put( MARK_CURRENT_FILTER, ( strPluginTypeFilter != null ) ? strPluginTypeFilter : "" );
118 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_PLUGINS ) );
119
120 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_PLUGINS, locale, model );
121
122 return getAdminPage( template.getHtml( ) );
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136 public String doInstallPlugin( HttpServletRequest request, ServletContext context ) throws AccessDeniedException
137 {
138 String strPluginName = request.getParameter( PARAM_PLUGIN_NAME );
139 Plugin plugin = PluginService.getPlugin( strPluginName );
140
141 if ( !verifyCoreCompatibility( plugin ) )
142 {
143 Object [ ] args = {
144 plugin.getMinCoreVersion( ), plugin.getMaxCoreVersion( )
145 };
146
147 return AdminMessageService.getMessageUrl( request, PROPERTY_PLUGIN_NO_CORE_COMPATIBILITY_MESSAGE, args, AdminMessage.TYPE_STOP );
148
149 }
150 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_PLUGINS ) )
151 {
152 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
153 }
154 try
155 {
156 plugin.install( );
157 }
158 catch( Exception e )
159 {
160 AppLogService.error( e.getMessage( ), e );
161
162 return AdminMessageService.getMessageUrl( request, PROPERTY_PLUGIN_INSTALL_ERROR, AdminMessage.TYPE_STOP );
163 }
164
165 return getHomeUrl( request );
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179 public String doUninstallPlugin( HttpServletRequest request, ServletContext context ) throws AccessDeniedException
180 {
181 if ( !SecurityTokenService.getInstance( ).validate( request, JSP_UNINSTALL_PLUGIN ) )
182 {
183 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
184 }
185 try
186 {
187 String strPluginName = request.getParameter( PARAM_PLUGIN_NAME );
188 Plugin plugin = PluginService.getPlugin( strPluginName );
189 plugin.uninstall( );
190 }
191 catch( Exception e )
192 {
193 AppLogService.error( e.getMessage( ), e );
194 }
195
196 return getHomeUrl( request );
197 }
198
199
200
201
202
203
204
205
206 public String getConfirmUninstallPlugin( HttpServletRequest request )
207 {
208 String strPluginName = request.getParameter( PARAM_PLUGIN_NAME );
209 Plugin plugin = PluginService.getPlugin( strPluginName );
210 Collection<PortletType> listPortletTypes = plugin.getPortletTypes( );
211 String strMessageKey = PROPERTY_PLUGIN_MESSAGE;
212 Map<String, String> parameters = new HashMap<>( );
213 parameters.put( PARAM_PLUGIN_NAME, strPluginName );
214 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_UNINSTALL_PLUGIN ) );
215 String strAdminMessageUrl = AdminMessageService.getMessageUrl( request, strMessageKey, JSP_UNINSTALL_PLUGIN, AdminMessage.TYPE_CONFIRMATION,
216 parameters );
217
218 for ( PortletType portletType : listPortletTypes )
219 {
220 String strPluginHomeClass = portletType.getHomeClass( );
221
222 if ( ( plugin.getType( ) & Plugin.PLUGIN_TYPE_PORTLET ) != 0 && isPortletExists( strPluginHomeClass ) )
223 {
224 strMessageKey = PROPERTY_PLUGIN_PORTLET_EXIST_MESSAGE;
225 strAdminMessageUrl = AdminMessageService.getMessageUrl( request, strMessageKey, AdminMessage.TYPE_CONFIRMATION );
226 }
227 }
228
229 return strAdminMessageUrl;
230 }
231
232
233
234
235
236
237
238
239
240
241 public String doModifyPluginPool( HttpServletRequest request ) throws AccessDeniedException
242 {
243 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_PLUGINS ) )
244 {
245 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
246 }
247 String strPluginName = request.getParameter( PARAM_PLUGIN_NAME );
248 String strDbPoolName = request.getParameter( PARAM_DB_POOL_NAME );
249
250 try
251 {
252 Plugin plugin = PluginService.getPlugin( strPluginName );
253 plugin.updatePoolName( strDbPoolName );
254 }
255 catch( Exception e )
256 {
257 AppLogService.error( e.getMessage( ), e );
258 }
259
260 return getHomeUrl( request );
261 }
262
263
264
265
266
267
268
269
270 public String getPluginDescription( HttpServletRequest request )
271 {
272 String strPluginName = request.getParameter( PARAM_PLUGIN_NAME );
273 Plugin plugin;
274 if ( PluginService.getCore( ).getName( ).equals( strPluginName ) )
275 {
276 plugin = PluginService.getCore( );
277 }
278 else
279 {
280 plugin = PluginService.getPlugin( strPluginName );
281 }
282
283
284 I18nService.localizeCollection( plugin.getRights( ), getLocale( ) );
285
286 I18nService.localizeCollection( plugin.getPortletTypes( ), getLocale( ) );
287
288 I18nService.localizeCollection( plugin.getInsertServices( ), getLocale( ) );
289
290 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PLUGIN_DETAILS, getLocale( ), plugin );
291
292 return getAdminPage( template.getHtml( ) );
293 }
294
295
296
297
298
299
300
301
302
303
304
305
306
307 private Collection<Plugin> filterPluginsList( Collection<Plugin> listPlugins, String strPluginTypeFilter )
308 {
309 Collection<Plugin> list = new ArrayList<>( );
310 if ( strPluginTypeFilter == null )
311 {
312 return listPlugins;
313 }
314
315 for ( Plugin plugin : listPlugins )
316 {
317 boolean filter = false;
318 if ( strPluginTypeFilter.equals( PARAM_PLUGIN_TYPE_APPLICATION ) )
319 {
320
321 filter = ( plugin.getType( ) & Plugin.PLUGIN_TYPE_APPLICATION ) == 0;
322 }
323 else
324 if ( strPluginTypeFilter.equals( PARAM_PLUGIN_TYPE_PORTLET ) )
325 {
326
327 filter = ( plugin.getType( ) & Plugin.PLUGIN_TYPE_PORTLET ) == 0;
328 }
329 else
330 if ( strPluginTypeFilter.equals( PARAM_PLUGIN_TYPE_FEATURE ) )
331 {
332
333 filter = ( plugin.getType( ) & Plugin.PLUGIN_TYPE_FEATURE ) == 0;
334 }
335 else
336 if ( strPluginTypeFilter.equals( PARAM_PLUGIN_TYPE_INSERTSERVICE ) )
337 {
338
339 filter = ( plugin.getType( ) & Plugin.PLUGIN_TYPE_INSERTSERVICE ) == 0;
340 }
341 else
342 if ( strPluginTypeFilter.equals( PARAM_PLUGIN_TYPE_CONTENTSERVICE ) )
343 {
344
345 filter = ( plugin.getType( ) & Plugin.PLUGIN_TYPE_CONTENTSERVICE ) == 0;
346 }
347
348 if ( !filter )
349 {
350 list.add( plugin );
351 }
352 }
353 return list;
354 }
355
356
357
358
359
360
361
362
363 private ReferenceList getPluginTypeFilterList( Locale locale )
364 {
365 ReferenceListnceList.html#ReferenceList">ReferenceList list = new ReferenceList( );
366 list.addItem( PARAM_PLUGIN_TYPE_ALL, I18nService.getLocalizedString( PROPERTY_PLUGIN_TYPE_NAME_ALL, locale ) );
367 list.addItem( PARAM_PLUGIN_TYPE_APPLICATION, I18nService.getLocalizedString( PROPERTY_PLUGIN_TYPE_NAME_APPLICATION, locale ) );
368 list.addItem( PARAM_PLUGIN_TYPE_PORTLET, I18nService.getLocalizedString( PROPERTY_PLUGIN_TYPE_NAME_PORTLET, locale ) );
369 list.addItem( PARAM_PLUGIN_TYPE_FEATURE, I18nService.getLocalizedString( PROPERTY_PLUGIN_TYPE_NAME_FEATURE, locale ) );
370 list.addItem( PARAM_PLUGIN_TYPE_INSERTSERVICE, I18nService.getLocalizedString( PROPERTY_PLUGIN_TYPE_NAME_INSERTSERVICE, locale ) );
371 list.addItem( PARAM_PLUGIN_TYPE_CONTENTSERVICE, I18nService.getLocalizedString( PROPERTY_PLUGIN_TYPE_NAME_CONTENTSERVICE, locale ) );
372
373 return list;
374 }
375
376
377
378
379
380
381 private ReferenceList getPoolsList( )
382 {
383 ReferenceListst.html#ReferenceList">ReferenceList listPools = new ReferenceList( );
384 listPools.addItem( AppConnectionService.NO_POOL_DEFINED, " " );
385 AppConnectionService.getPoolList( listPools );
386
387 return listPools;
388 }
389
390
391
392
393
394
395
396
397 private boolean isPortletExists( String strPluginHomeClass )
398 {
399 String strPortletTypeId = PortletTypeHome.getPortletTypeId( strPluginHomeClass );
400
401 return ( PortletTypeHome.getNbPortletTypeByPortlet( strPortletTypeId ) != 0 );
402 }
403
404
405
406
407
408
409
410
411 private boolean verifyCoreCompatibility( Plugin plugin )
412 {
413 String strCoreVersion = AppInfo.getVersion( );
414
415
416 int nPos = strCoreVersion.indexOf( '-' );
417
418 if ( nPos > 0 )
419 {
420 strCoreVersion = strCoreVersion.substring( 0, nPos );
421 }
422
423 String [ ] coreVersion = strCoreVersion.split( "\\." );
424
425 String strMinCoreVersion = ( plugin.getMinCoreVersion( ) == null ) ? "" : plugin.getMinCoreVersion( );
426 String strMaxCoreVersion = ( plugin.getMaxCoreVersion( ) == null ) ? "" : plugin.getMaxCoreVersion( );
427
428
429 boolean bMin = ( strMinCoreVersion == null ) || strMinCoreVersion.trim( ).equals( "" );
430
431 if ( ( strMinCoreVersion != null ) && !strMinCoreVersion.trim( ).equals( "" ) )
432 {
433 String [ ] minCoreVersion = strMinCoreVersion.split( "\\." );
434
435 if ( checkCoreMinCompatibility( minCoreVersion, coreVersion ) )
436 {
437 AppLogService.debug( "Min core version ok : {}", plugin.getMinCoreVersion( ) );
438 bMin = true;
439 }
440 }
441
442
443 boolean bMax = ( strMaxCoreVersion == null ) || strMaxCoreVersion.trim( ).equals( "" );
444
445 if ( ( strMaxCoreVersion != null ) && !strMaxCoreVersion.trim( ).equals( "" ) )
446 {
447 String [ ] maxCoreVersion = strMaxCoreVersion.split( "\\." );
448
449 if ( checkCoreMaxCompatibility( maxCoreVersion, coreVersion ) )
450 {
451 AppLogService.debug( "Max core version ok : {}", plugin.getMaxCoreVersion( ) );
452 bMax = true;
453 }
454 }
455
456 return bMin && bMax;
457 }
458
459
460
461
462
463
464
465
466
467
468 private boolean checkCoreMinCompatibility( String [ ] minCoreVersion, String [ ] coreVersion )
469 {
470 for ( int i = 0; i < Math.min( minCoreVersion.length, coreVersion.length ); ++i )
471 {
472 if ( ( Integer.parseInt( minCoreVersion [i] ) ) < ( Integer.parseInt( coreVersion [i] ) ) )
473 {
474 return true;
475 }
476
477 if ( ( Integer.parseInt( minCoreVersion [i] ) ) > ( Integer.parseInt( coreVersion [i] ) ) )
478 {
479 return false;
480 }
481 }
482
483 return true;
484 }
485
486
487
488
489
490
491
492
493
494
495 private boolean checkCoreMaxCompatibility( String [ ] maxCoreVersion, String [ ] coreVersion )
496 {
497 for ( int i = 0; i < Math.min( maxCoreVersion.length, coreVersion.length ); ++i )
498 {
499 if ( ( Integer.parseInt( maxCoreVersion [i] ) ) > ( Integer.parseInt( coreVersion [i] ) ) )
500 {
501 return true;
502 }
503
504 if ( ( Integer.parseInt( maxCoreVersion [i] ) ) < ( Integer.parseInt( coreVersion [i] ) ) )
505 {
506 return false;
507 }
508 }
509
510 return false;
511 }
512 }