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.xsl;
35
36 import fr.paris.lutece.portal.business.file.File;
37 import fr.paris.lutece.portal.business.file.FileHome;
38 import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
39 import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
40 import fr.paris.lutece.portal.business.rbac.RBAC;
41 import fr.paris.lutece.portal.business.xsl.XslExport;
42 import fr.paris.lutece.portal.business.xsl.XslExportHome;
43 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
44 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
45 import fr.paris.lutece.portal.service.i18n.I18nService;
46 import fr.paris.lutece.portal.service.message.AdminMessage;
47 import fr.paris.lutece.portal.service.message.AdminMessageService;
48 import fr.paris.lutece.portal.service.plugin.Plugin;
49 import fr.paris.lutece.portal.service.plugin.PluginService;
50 import fr.paris.lutece.portal.service.rbac.RBACService;
51 import fr.paris.lutece.portal.service.template.AppTemplateService;
52 import fr.paris.lutece.portal.service.util.AppPathService;
53 import fr.paris.lutece.portal.service.util.AppPropertiesService;
54 import fr.paris.lutece.portal.service.xsl.XslExportResourceIdService;
55 import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
56 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
57 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
58 import fr.paris.lutece.util.ReferenceItem;
59 import fr.paris.lutece.util.ReferenceList;
60 import fr.paris.lutece.util.filesystem.FileSystemUtil;
61 import fr.paris.lutece.util.html.HtmlTemplate;
62 import fr.paris.lutece.util.html.Paginator;
63 import fr.paris.lutece.util.url.UrlItem;
64
65 import org.apache.commons.fileupload.FileItem;
66 import org.apache.commons.lang.StringUtils;
67
68 import org.xml.sax.InputSource;
69
70 import java.io.ByteArrayInputStream;
71 import java.io.IOException;
72 import java.io.OutputStream;
73
74 import java.util.Collection;
75 import java.util.HashMap;
76 import java.util.List;
77
78 import javax.servlet.http.HttpServletRequest;
79 import javax.servlet.http.HttpServletResponse;
80
81 import javax.xml.parsers.SAXParser;
82 import javax.xml.parsers.SAXParserFactory;
83
84
85
86
87
88
89
90 public class XslExportJspBean extends PluginAdminPageJspBean
91 {
92
93
94
95 public static final String RIGHT_MANAGE_XSL_EXPORT = "CORE_XSL_EXPORT_MANAGEMENT";
96
97
98
99
100 private static final long serialVersionUID = -8697851692630602527L;
101
102
103 private static final String TEMPLATE_MANAGE_XSL_EXPORT = "admin/xsl/manage_xsl_export.html";
104 private static final String TEMPLATE_CREATE_XSL_EXPORT = "admin/xsl/create_xsl_export.html";
105 private static final String TEMPLATE_MODIFY_XSL_EXPORT = "admin/xsl/modify_xsl_export.html";
106
107
108 private static final String MARK_XSL_EXPORT_LIST = "xsl_export_list";
109 private static final String MARK_XSL_EXPORT = "xsl_export";
110 private static final String MARK_PAGINATOR = "paginator";
111 private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
112 private static final String MARK_PERMISSION_CREATE = "right_create";
113 private static final String MARK_PERMISSION_MODIFY = "right_modify";
114 private static final String MARK_PERMISSION_DELETE = "right_delete";
115 private static final String MARK_LIST_PLUGINS = "list_plugins";
116
117
118 private static final String PARAMETER_ID_XSL_EXPORT = "id_xsl_export";
119 private static final String PARAMETER_ID_FILE = "id_file";
120 private static final String PARAMETER_TITLE = "title";
121 private static final String PARAMETER_DESCRIPTION = "description";
122 private static final String PARAMETER_EXTENSION = "extension";
123 private static final String PARAMETER_PAGE_INDEX = "page_index";
124 private static final String PARAMETER_PLUGIN = "plugin";
125
126
127 private static final String EMPTY_STRING = "";
128
129
130 private static final String MESSAGE_CONFIRM_REMOVE_XSL_EXPORT = "portal.xsl.message.confirm_remove_xsl_export";
131 private static final String MESSAGE_MANDATORY_FIELD = "portal.util.message.mandatoryField";
132
133
134 private static final String FIELD_TITLE = "portal.xsl.create_xsl_export.label_title";
135 private static final String FIELD_DESCRIPTION = "portal.xsl.create_xsl_export.label_description";
136 private static final String FIELD_EXTENSION = "portal.xsl.create_xsl_export.label_extension";
137
138
139 private static final String FIELD_FILE = "portal.xsl.create_xsl_export.label_file";
140 private static final String MESSAGE_XML_NOT_VALID = "portal.xsl.message.xml_not_valid";
141 private static final String MESSAGE_PERMISSION_DENIED = "portal.xsl.message.permission_denied";
142
143
144 private static final String PROPERTY_ITEM_PER_PAGE = "paginator.xsl.itemsPerPage";
145 private static final String PROPERTY_MANAGE_XSL_EXPORT_TITLE = "portal.xsl.manage_xsl_export.page_title";
146 private static final String PROPERTY_MODIFY_XSL_EXPORT_TITLE = "portal.xsl.modify_xsl_export.title";
147 private static final String PROPERTY_CREATE_XSL_EXPORT_TITLE = "portal.xsl.create_xsl_export.title";
148
149
150 private static final String JSP_MANAGE_XSL_EXPORT = "jsp/admin/xsl/ManageXslExport.jsp";
151 private static final String JSP_DO_REMOVE_XSL_EXPORT = "jsp/admin/xsl/DoRemoveXslExport.jsp";
152
153
154 private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_ITEM_PER_PAGE, 15 );
155 private String _strCurrentPageIndex;
156 private int _nItemsPerPage;
157
158
159
160
161
162
163 public String getManageXslExport( HttpServletRequest request )
164 {
165 HashMap<String, Object> model = new HashMap<String, Object>( );
166 List<XslExport> listXslExport = XslExportHome.getList( );
167 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
168 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
169 _nDefaultItemsPerPage );
170
171 model.put( MARK_PERMISSION_CREATE,
172 RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
173 XslExportResourceIdService.PERMISSION_CREATE, getUser( ) ) );
174 model.put( MARK_PERMISSION_MODIFY,
175 RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
176 XslExportResourceIdService.PERMISSION_MODIFY, getUser( ) ) );
177 model.put( MARK_PERMISSION_DELETE,
178 RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
179 XslExportResourceIdService.PERMISSION_DELETE, getUser( ) ) );
180
181 LocalizedPaginator<XslExport> paginator = new LocalizedPaginator<XslExport>( listXslExport, _nItemsPerPage,
182 getJspManageXslExport( request ), PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
183
184 model.put( MARK_PAGINATOR, paginator );
185 model.put( MARK_NB_ITEMS_PER_PAGE, EMPTY_STRING + _nItemsPerPage );
186 model.put( MARK_XSL_EXPORT_LIST, paginator.getPageItems( ) );
187 setPageTitleProperty( PROPERTY_MANAGE_XSL_EXPORT_TITLE );
188
189 HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_XSL_EXPORT, getLocale( ), model );
190
191 return getAdminPage( templateList.getHtml( ) );
192 }
193
194
195
196
197
198
199
200 public String getCreateXslExport( HttpServletRequest request )
201 throws AccessDeniedException
202 {
203 HashMap<String, Object> model = new HashMap<String, Object>( );
204
205 Collection<Plugin> listPlugins = PluginService.getPluginList( );
206 ReferenceList refListPlugins = new ReferenceList( );
207 ReferenceItem refItem = new ReferenceItem( );
208 Plugin pluginCore = PluginService.getCore( );
209 refItem.setCode( pluginCore.getName( ) );
210 refItem.setName( pluginCore.getName( ) );
211 refListPlugins.add( refItem );
212
213 for ( Plugin plugin : listPlugins )
214 {
215 if ( plugin != null )
216 {
217 refItem = new ReferenceItem( );
218 refItem.setCode( plugin.getName( ) );
219 refItem.setName( plugin.getName( ) );
220 refListPlugins.add( refItem );
221 }
222 }
223
224 model.put( MARK_LIST_PLUGINS, refListPlugins );
225
226 if ( !RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
227 XslExportResourceIdService.PERMISSION_CREATE, getUser( ) ) )
228 {
229 throw new AccessDeniedException( MESSAGE_PERMISSION_DENIED );
230 }
231
232 setPageTitleProperty( PROPERTY_CREATE_XSL_EXPORT_TITLE );
233
234 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_XSL_EXPORT, getLocale( ), model );
235
236 return getAdminPage( template.getHtml( ) );
237 }
238
239
240
241
242
243
244
245 public String doCreateXslExport( HttpServletRequest request )
246 throws AccessDeniedException
247 {
248 XslExport xslExport = new XslExport( );
249 String strError = getXslExportData( request, xslExport );
250
251 if ( !RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
252 XslExportResourceIdService.PERMISSION_CREATE, getUser( ) ) )
253 {
254 throw new AccessDeniedException( MESSAGE_PERMISSION_DENIED );
255 }
256
257 if ( strError != null )
258 {
259 return strError;
260 }
261
262 if ( xslExport.getFile( ) != null )
263 {
264 xslExport.getFile( ).setIdFile( FileHome.create( xslExport.getFile( ) ) );
265 }
266
267 XslExportHome.create( xslExport );
268
269 return getJspManageXslExport( request );
270 }
271
272
273
274
275
276
277
278 public String getModifyXslExport( HttpServletRequest request )
279 throws AccessDeniedException
280 {
281 if ( !RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
282 XslExportResourceIdService.PERMISSION_MODIFY, getUser( ) ) )
283 {
284 throw new AccessDeniedException( MESSAGE_PERMISSION_DENIED );
285 }
286
287 XslExport xslExport;
288 String strIdXslExport = request.getParameter( PARAMETER_ID_XSL_EXPORT );
289 HashMap<String, Object> model = new HashMap<String, Object>( );
290 int nIdXslExport = Integer.parseInt( strIdXslExport );
291 xslExport = XslExportHome.findByPrimaryKey( nIdXslExport );
292 model.put( MARK_XSL_EXPORT, xslExport );
293
294 Collection<Plugin> listPlugins = PluginService.getPluginList( );
295 ReferenceList refListPlugins = new ReferenceList( );
296 ReferenceItem refItem = new ReferenceItem( );
297 Plugin pluginCore = PluginService.getCore( );
298 refItem.setCode( pluginCore.getName( ) );
299 refItem.setName( pluginCore.getName( ) );
300 refListPlugins.add( refItem );
301
302 for ( Plugin plugin : listPlugins )
303 {
304 if ( plugin != null )
305 {
306 refItem = new ReferenceItem( );
307 refItem.setCode( plugin.getName( ) );
308 refItem.setName( plugin.getName( ) );
309 refListPlugins.add( refItem );
310 }
311 }
312
313 model.put( MARK_LIST_PLUGINS, refListPlugins );
314
315 setPageTitleProperty( PROPERTY_MODIFY_XSL_EXPORT_TITLE );
316
317 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_XSL_EXPORT, getLocale( ), model );
318
319 return getAdminPage( template.getHtml( ) );
320 }
321
322
323
324
325
326
327
328 public String doModifyXslExport( HttpServletRequest request )
329 throws AccessDeniedException
330 {
331 if ( !RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
332 XslExportResourceIdService.PERMISSION_MODIFY, getUser( ) ) )
333 {
334 throw new AccessDeniedException( MESSAGE_PERMISSION_DENIED );
335 }
336
337 XslExport xslExport;
338 String strIdXslExport = request.getParameter( PARAMETER_ID_XSL_EXPORT );
339 int nIdXslExport = Integer.parseInt( strIdXslExport );
340 xslExport = XslExportHome.findByPrimaryKey( nIdXslExport );
341
342 String strError = getXslExportData( request, xslExport );
343
344 if ( strError != null )
345 {
346 return strError;
347 }
348
349
350 File fileStore = XslExportHome.findByPrimaryKey( nIdXslExport ).getFile( );
351
352 if ( xslExport.getFile( ) != null )
353 {
354
355 File fileSource = xslExport.getFile( );
356
357 fileSource.setIdFile( fileStore.getIdFile( ) );
358
359 if ( fileStore.getPhysicalFile( ) != null )
360 {
361 fileSource.getPhysicalFile( ).setIdPhysicalFile( fileStore.getPhysicalFile( ).getIdPhysicalFile( ) );
362 }
363
364 FileHome.update( fileSource );
365 }
366 else
367 {
368 xslExport.setFile( fileStore );
369 }
370
371 XslExportHome.update( xslExport );
372
373 return getJspManageXslExport( request );
374 }
375
376
377
378
379
380
381
382 public String getConfirmRemoveXslExport( HttpServletRequest request )
383 throws AccessDeniedException
384 {
385 if ( !RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
386 XslExportResourceIdService.PERMISSION_DELETE, getUser( ) ) )
387 {
388 throw new AccessDeniedException( MESSAGE_PERMISSION_DENIED );
389 }
390
391 String strIdXslExport = request.getParameter( PARAMETER_ID_XSL_EXPORT );
392
393 UrlItem url = new UrlItem( JSP_DO_REMOVE_XSL_EXPORT );
394 url.addParameter( PARAMETER_ID_XSL_EXPORT, strIdXslExport );
395
396 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_XSL_EXPORT, url.getUrl( ),
397 AdminMessage.TYPE_CONFIRMATION );
398 }
399
400
401
402
403
404
405
406 public String doRemoveXslExport( HttpServletRequest request )
407 throws AccessDeniedException
408 {
409 if ( !RBACService.isAuthorized( XslExport.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
410 XslExportResourceIdService.PERMISSION_DELETE, getUser( ) ) )
411 {
412 throw new AccessDeniedException( MESSAGE_PERMISSION_DENIED );
413 }
414
415
416 String strIdXslExport = request.getParameter( PARAMETER_ID_XSL_EXPORT );
417 int nIdXslExport = Integer.parseInt( strIdXslExport );
418 XslExport xslExport = XslExportHome.findByPrimaryKey( nIdXslExport );
419
420
421
422
423
424
425
426
427
428 XslExportHome.remove( nIdXslExport );
429
430 if ( xslExport.getFile( ) != null )
431 {
432 FileHome.remove( xslExport.getFile( ).getIdFile( ) );
433 }
434
435 return getJspManageXslExport( request );
436 }
437
438
439
440
441
442
443
444 public void doDownloadXslExport( HttpServletRequest request, HttpServletResponse response )
445 throws IOException
446 {
447 String strXslExportId = request.getParameter( PARAMETER_ID_XSL_EXPORT );
448
449 if ( strXslExportId != null )
450 {
451 int nXslExportId = Integer.parseInt( strXslExportId );
452 XslExport xslExport = XslExportHome.findByPrimaryKey( nXslExportId );
453
454 String strMimetype = xslExport.getFile( ).getMimeType( );
455 response.setContentType( ( strMimetype != null ) ? strMimetype : "application/octet-stream" );
456 response.setHeader( "Content-Disposition",
457 "attachement; filename=\"" + xslExport.getFile( ).getTitle( ) + "\"" );
458
459 OutputStream out = response.getOutputStream( );
460 PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( xslExport.getFile( ).getPhysicalFile( )
461 .getIdPhysicalFile( ) );
462 out.write( physicalFile.getValue( ) );
463 out.flush( );
464 out.close( );
465 }
466 }
467
468
469
470
471
472
473
474
475
476 private String getXslExportData( HttpServletRequest request, XslExport xslExport )
477 {
478 String strError = StringUtils.EMPTY;
479 String strTitle = request.getParameter( PARAMETER_TITLE );
480 String strDescription = request.getParameter( PARAMETER_DESCRIPTION );
481 String strExtension = request.getParameter( PARAMETER_EXTENSION );
482 String strPlugin = request.getParameter( PARAMETER_PLUGIN );
483 File fileSource = getFileData( PARAMETER_ID_FILE, request );
484
485 if ( ( strTitle == null ) || strTitle.trim( ).equals( EMPTY_STRING ) )
486 {
487 strError = FIELD_TITLE;
488 }
489
490 else if ( ( strDescription == null ) || strDescription.trim( ).equals( EMPTY_STRING ) )
491 {
492 strError = FIELD_DESCRIPTION;
493 }
494
495 else if ( StringUtils.isBlank( strExtension ) )
496 {
497 strError = FIELD_EXTENSION;
498 }
499
500 else if ( ( xslExport.getFile( ) == null ) && ( fileSource == null ) )
501 {
502 strError = FIELD_FILE;
503 }
504
505 if ( strPlugin == null )
506 {
507 strPlugin = StringUtils.EMPTY;
508 }
509
510
511 if ( !strError.equals( EMPTY_STRING ) )
512 {
513 Object[] tabRequiredFields = { I18nService.getLocalizedString( strError, getLocale( ) ) };
514
515 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
516 AdminMessage.TYPE_STOP );
517 }
518
519
520 if ( fileSource != null )
521 {
522 strError = isValid( fileSource.getPhysicalFile( ).getValue( ) );
523
524 if ( strError != null )
525 {
526 Object[] args = { strError };
527
528 return AdminMessageService.getMessageUrl( request, MESSAGE_XML_NOT_VALID, args, AdminMessage.TYPE_STOP );
529 }
530 }
531
532 xslExport.setTitle( strTitle );
533 xslExport.setDescription( strDescription );
534 xslExport.setExtension( strExtension );
535 xslExport.setPlugin( strPlugin );
536
537 xslExport.setFile( fileSource );
538
539 return null;
540 }
541
542
543
544
545
546
547 private String isValid( byte[] baXslSource )
548 {
549 String strError = null;
550
551 try
552 {
553 SAXParserFactory factory = SAXParserFactory.newInstance( );
554 SAXParser analyzer = factory.newSAXParser( );
555 InputSource is = new InputSource( new ByteArrayInputStream( baXslSource ) );
556 analyzer.getXMLReader( ).parse( is );
557 }
558 catch ( Exception e )
559 {
560 strError = e.getMessage( );
561 }
562
563 return strError;
564 }
565
566
567
568
569
570
571 private String getJspManageXslExport( HttpServletRequest request )
572 {
573 return AppPathService.getBaseUrl( request ) + JSP_MANAGE_XSL_EXPORT;
574 }
575
576
577
578
579
580
581
582
583
584 private static File getFileData( String strFileInputName, HttpServletRequest request )
585 {
586 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
587 FileItem fileItem = multipartRequest.getFile( strFileInputName );
588
589 if ( ( fileItem != null ) && ( fileItem.getName( ) != null ) && !EMPTY_STRING.equals( fileItem.getName( ) ) )
590 {
591 File file = new File( );
592 PhysicalFile physicalFile = new PhysicalFile( );
593 physicalFile.setValue( fileItem.get( ) );
594 file.setTitle( FileUploadService.getFileNameOnly( fileItem ) );
595 file.setSize( (int) fileItem.getSize( ) );
596 file.setPhysicalFile( physicalFile );
597 file.setMimeType( FileSystemUtil.getMIMEType( FileUploadService.getFileNameOnly( fileItem ) ) );
598
599 return file;
600 }
601
602 return null;
603 }
604 }