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