View Javadoc

1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.digglike.web;
35  
36  import fr.paris.lutece.plugins.digglike.business.ExportFormat;
37  import fr.paris.lutece.plugins.digglike.business.ExportFormatHome;
38  import fr.paris.lutece.plugins.digglike.service.ExportFormatResourceIdService;
39  import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
40  import fr.paris.lutece.portal.business.rbac.RBAC;
41  import fr.paris.lutece.portal.service.fileupload.FileUploadService;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
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.rbac.RBACService;
47  import fr.paris.lutece.portal.service.template.AppTemplateService;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.portal.service.util.AppPathService;
50  import fr.paris.lutece.portal.service.util.AppPropertiesService;
51  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
52  import fr.paris.lutece.portal.web.constants.Parameters;
53  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
54  import fr.paris.lutece.util.html.HtmlTemplate;
55  import fr.paris.lutece.util.html.Paginator;
56  import fr.paris.lutece.util.url.UrlItem;
57  
58  import org.apache.commons.fileupload.FileItem;
59  
60  import org.xml.sax.InputSource;
61  
62  import java.io.ByteArrayInputStream;
63  import java.io.IOException;
64  import java.io.OutputStream;
65  
66  import java.util.HashMap;
67  import java.util.List;
68  import java.util.Locale;
69  import java.util.Map;
70  
71  import javax.servlet.http.HttpServletRequest;
72  import javax.servlet.http.HttpServletResponse;
73  
74  import javax.xml.parsers.SAXParser;
75  import javax.xml.parsers.SAXParserFactory;
76  
77  
78  /**
79   *
80   * class ExportFormatJspBean
81   *
82   */
83  public class ExportFormatJspBean extends PluginAdminPageJspBean
84  {
85      private static final long serialVersionUID = 2892557628133467782L;
86  
87      //	templates
88      private static final String TEMPLATE_MANAGE_EXPORT = "admin/plugins/digglike/manage_export_format.html";
89      private static final String TEMPLATE_CREATE_EXPORT = "admin/plugins/digglike/create_export_format.html";
90      private static final String TEMPLATE_MODIFY_EXPORT = "admin/plugins/digglike/modify_export_format.html";
91  
92      //	Markers
93      private static final String MARK_EXPORT_LIST = "export_list";
94      private static final String MARK_EXPORT = "export";
95      private static final String MARK_PAGINATOR = "paginator";
96      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
97  
98      //	parameters form
99      private static final String PARAMETER_ID_EXPORT = "id_export";
100     private static final String PARAMETER_TITLE = "title";
101     private static final String PARAMETER_DESCRIPTION = "description";
102     private static final String PARAMETER_EXTENSION = "extension";
103     private static final String PARAMETER_PAGE_INDEX = "page_index";
104 
105     //	 other constants
106     private static final String EMPTY_STRING = "";
107     private static final String CONSTANT_MIME_TYPE_OCTETSTREAM = "application/octet-stream";
108 
109     //	message
110     private static final String MESSAGE_CONFIRM_REMOVE_EXPORT = "digglike.message.confirmRemoveExportFormat";
111     private static final String MESSAGE_MANDATORY_FIELD = "digglike.message.mandatory.field";
112     private static final String MESSAGE_ERROR_DURING_DOWNLOAD_FILE = "digglike.message.errorDuringDownloadFile";
113     private static final String MESSAGE_YOU_ARE_NOT_ALLOWED_TO_DOWLOAD_THIS_FILE = "digglike.message.youAreNotAllowedToDownloadFile";
114     private static final String FIELD_TITLE = "digglike.createExportFormat.labelTitle";
115     private static final String FIELD_DESCRIPTION = "digglike.createExportFormat.labelDescription";
116     private static final String FIELD_EXTENSION = "digglike.createExportFormat.labelExtension";
117     private static final String FIELD_XSL = "digglike.createExportFormat.labelXsl";
118     private static final String MESSAGE_STYLESHEET_NOT_VALID = "digglike.message.stylesheetNotValid";
119 
120     //	properties
121     private static final String PROPERTY_ITEM_PER_PAGE = "digglike.itemsPerPage";
122     private static final String PROPERTY_MANAGE_EXPORT_FORMAT_TITLE = "digglike.manageExportFormat.pageTitle";
123     private static final String PROPERTY_MODIFY_EXPORT_FORMAT_TITLE = "digglike.modifyExportFormat.title";
124     private static final String PROPERTY_CREATE_EXPORT_FORMAT_TITLE = "digglike.createExportFormat.title";
125 
126     //Jsp Definition
127     private static final String JSP_MANAGE_EXPORT_FORMAT = "jsp/admin/plugins/digglike/ManageExportFormat.jsp";
128     private static final String JSP_DO_REMOVE_EXPORT_FORMAT = "jsp/admin/plugins/digglike/DoRemoveExportFormat.jsp";
129 
130     //	session fields
131     private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_ITEM_PER_PAGE, 15 );
132     private String _strCurrentPageIndexExport;
133     private int _nItemsPerPageExportFormat;
134 
135     /**
136      * Return management export format ( list of export format )
137      * @param request The Http request
138      * @return Html ExportFormat
139      */
140     public String getManageExportFormat( HttpServletRequest request )
141     {
142         Plugin plugin = getPlugin(  );
143         Locale locale = getLocale(  );
144         Map<String, Object> model = new HashMap<String, Object>(  );
145         List<ExportFormat> listExportFormat = ExportFormatHome.getList( plugin );
146         listExportFormat = (List<ExportFormat>) RBACService.getAuthorizedCollection( listExportFormat,
147                 ExportFormatResourceIdService.PERMISSION_MANAGE, getUser(  ) );
148         _strCurrentPageIndexExport = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX,
149                 _strCurrentPageIndexExport );
150         _nItemsPerPageExportFormat = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE,
151                 _nItemsPerPageExportFormat, _nDefaultItemsPerPage );
152 
153         Paginator<ExportFormat> paginator = new Paginator<ExportFormat>( listExportFormat, _nItemsPerPageExportFormat,
154                 getJspManageExportFormat( request ), PARAMETER_PAGE_INDEX, _strCurrentPageIndexExport );
155 
156         model.put( MARK_PAGINATOR, paginator );
157         model.put( MARK_NB_ITEMS_PER_PAGE, EMPTY_STRING + _nItemsPerPageExportFormat );
158         model.put( MARK_EXPORT_LIST, paginator.getPageItems(  ) );
159         setPageTitleProperty( PROPERTY_MANAGE_EXPORT_FORMAT_TITLE );
160 
161         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_EXPORT, locale, model );
162 
163         //ReferenceList refMailingList;
164         //refMailingList=AdminMailingListService.getMailingLists(adminUser);
165         return getAdminPage( templateList.getHtml(  ) );
166     }
167 
168     /**
169      * Gets the export format creation page
170      * @param request The HTTP request
171      * @return The export format creation page
172      */
173     public String getCreateExportFormat( HttpServletRequest request )
174     {
175         if ( !RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
176                     ExportFormatResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
177         {
178             return getManageExportFormat( request );
179         }
180 
181         Locale locale = getLocale(  );
182         Map<String, Object> model = new HashMap<String, Object>(  );
183         setPageTitleProperty( PROPERTY_CREATE_EXPORT_FORMAT_TITLE );
184 
185         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_EXPORT, locale, model );
186 
187         return getAdminPage( template.getHtml(  ) );
188     }
189 
190     /**
191      * Perform the export format creation
192      * @param request The HTTP request
193      * @return The URL to go after performing the action
194      */
195     public String doCreateExportFormat( HttpServletRequest request )
196     {
197         if ( !RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
198                     ExportFormatResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
199         {
200             return getJspManageExportFormat( request );
201         }
202 
203         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
204         ExportFormat exportFormat = new ExportFormat(  );
205         String strError = getExportFormatData( multipartRequest, exportFormat );
206 
207         if ( strError != null )
208         {
209             return strError;
210         }
211 
212         ExportFormatHome.create( exportFormat, getPlugin(  ) );
213 
214         return getJspManageExportFormat( request );
215     }
216 
217     /**
218      * Gets the export format modification page
219      * @param request The HTTP request
220      * @return The export format creation page
221      */
222     public String getModifyExportFormat( HttpServletRequest request )
223     {
224         Plugin plugin = getPlugin(  );
225         Locale locale = getLocale(  );
226         ExportFormat exportFormat;
227         String strIdExport = request.getParameter( PARAMETER_ID_EXPORT );
228         Map<String, Object> model = new HashMap<String, Object>(  );
229         int nIdExport = -1;
230 
231         if ( ( strIdExport != null ) && !strIdExport.equals( EMPTY_STRING ) &&
232                 RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, strIdExport,
233                     ExportFormatResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
234         {
235             try
236             {
237                 nIdExport = Integer.parseInt( strIdExport );
238             }
239             catch ( NumberFormatException ne )
240             {
241                 AppLogService.error( ne );
242 
243                 return getManageExportFormat( request );
244             }
245         }
246         else
247         {
248             return getManageExportFormat( request );
249         }
250 
251         exportFormat = ExportFormatHome.findByPrimaryKey( nIdExport, plugin );
252         model.put( MARK_EXPORT, exportFormat );
253         setPageTitleProperty( PROPERTY_MODIFY_EXPORT_FORMAT_TITLE );
254 
255         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_EXPORT, locale, model );
256 
257         return getAdminPage( template.getHtml(  ) );
258     }
259 
260     /**
261      * Perform the export format modification
262      * @param request The HTTP request
263      * @return The URL to go after performing the action
264      */
265     public String doModifyExportFormat( HttpServletRequest request )
266     {
267         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
268         Plugin plugin = getPlugin(  );
269         ExportFormat exportFormat;
270         String strIdExport = multipartRequest.getParameter( PARAMETER_ID_EXPORT );
271         int nIdExport = -1;
272 
273         if ( ( strIdExport != null ) && !strIdExport.equals( EMPTY_STRING ) &&
274                 RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, strIdExport,
275                     ExportFormatResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
276         {
277             try
278             {
279                 nIdExport = Integer.parseInt( strIdExport );
280             }
281             catch ( NumberFormatException ne )
282             {
283                 AppLogService.error( ne );
284 
285                 return getHomeUrl( request );
286             }
287         }
288         else
289         {
290             return getHomeUrl( request );
291         }
292 
293         exportFormat = ExportFormatHome.findByPrimaryKey( nIdExport, plugin );
294 
295         String strError = getExportFormatData( multipartRequest, exportFormat );
296 
297         if ( strError != null )
298         {
299             return strError;
300         }
301 
302         ExportFormatHome.update( exportFormat, getPlugin(  ) );
303 
304         return getJspManageExportFormat( request );
305     }
306 
307     /**
308      * Gets the confirmation page of delete export format
309      * @param request The HTTP request
310      * @return the confirmation page of delete export format
311      */
312     public String getConfirmRemoveExportFormat( HttpServletRequest request )
313     {
314         if ( request.getParameter( PARAMETER_ID_EXPORT ) == null )
315         {
316             return getHomeUrl( request );
317         }
318 
319         String strIdExport = request.getParameter( PARAMETER_ID_EXPORT );
320         UrlItem url = new UrlItem( JSP_DO_REMOVE_EXPORT_FORMAT );
321         url.addParameter( PARAMETER_ID_EXPORT, strIdExport );
322 
323         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_EXPORT, url.getUrl(  ),
324             AdminMessage.TYPE_CONFIRMATION );
325     }
326 
327     /**
328      * Perform the export format supression
329      * @param request The HTTP request
330      * @return The URL to go after performing the action
331      */
332     public String doRemoveExportFormat( HttpServletRequest request )
333     {
334         String strIdExport = request.getParameter( PARAMETER_ID_EXPORT );
335         int nIdExport = -1;
336 
337         if ( ( request.getParameter( PARAMETER_ID_EXPORT ) == null ) ||
338                 !RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, strIdExport,
339                     ExportFormatResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
340         {
341             return getJspManageExportFormat( request );
342         }
343 
344         try
345         {
346             nIdExport = Integer.parseInt( strIdExport );
347         }
348         catch ( NumberFormatException ne )
349         {
350             AppLogService.error( ne );
351 
352             return getHomeUrl( request );
353         }
354 
355         if ( nIdExport != -1 )
356         {
357             ExportFormatHome.remove( nIdExport, getPlugin(  ) );
358         }
359 
360         return getJspManageExportFormat( request );
361     }
362 
363     /**
364      * Get the request data and if there is no error insert the data in the
365      * exportFormat object specified in parameter.
366      * return null if there is no error or else return the error page url
367      * @param multipartRequest the request
368      * @param exportFormat the exportFormat Object
369      * @return null if there is no error or else return the error page url
370      */
371     private String getExportFormatData( MultipartHttpServletRequest multipartRequest, ExportFormat exportFormat )
372     {
373         String strTitle = multipartRequest.getParameter( PARAMETER_TITLE );
374         String strDescription = multipartRequest.getParameter( PARAMETER_DESCRIPTION );
375         String strExtension = multipartRequest.getParameter( PARAMETER_EXTENSION );
376 
377         String strFieldError = EMPTY_STRING;
378         FileItem fileSource = multipartRequest.getFile( Parameters.STYLESHEET_SOURCE );
379         String strFilename = FileUploadService.getFileNameOnly( fileSource );
380 
381         if ( ( strTitle == null ) || strTitle.trim(  ).equals( EMPTY_STRING ) )
382         {
383             strFieldError = FIELD_TITLE;
384         }
385 
386         else if ( ( strDescription == null ) || strDescription.trim(  ).equals( EMPTY_STRING ) )
387         {
388             strFieldError = FIELD_DESCRIPTION;
389         }
390         else if ( ( strExtension == null ) || strExtension.trim(  ).equals( EMPTY_STRING ) )
391         {
392             strFieldError = FIELD_EXTENSION;
393         }
394 
395         else if ( ( strFilename == null ) || ( strFilename.equals( "" ) && ( exportFormat.getXsl(  ) == null ) ) )
396         {
397             strFieldError = FIELD_XSL;
398         }
399 
400         //Mandatory fields
401         if ( !strFieldError.equals( EMPTY_STRING ) )
402         {
403             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, getLocale(  ) ) };
404 
405             return AdminMessageService.getMessageUrl( multipartRequest, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
406                 AdminMessage.TYPE_STOP );
407         }
408 
409         byte[] baXslSource = fileSource.get(  );
410 
411         //Check the XML validity of the XSL stylesheet
412         if ( ( strFilename != null ) && !strFilename.equals( "" ) && ( isValid( baXslSource ) != null ) )
413         {
414             Object[] args = { isValid( baXslSource ) };
415 
416             return AdminMessageService.getMessageUrl( multipartRequest, MESSAGE_STYLESHEET_NOT_VALID, args,
417                 AdminMessage.TYPE_STOP );
418         }
419 
420         exportFormat.setTitle( strTitle );
421         exportFormat.setDescription( strDescription );
422         exportFormat.setExtension( strExtension );
423 
424         if ( ( strFilename != null ) && !strFilename.equals( "" ) )
425         {
426             exportFormat.setXsl( baXslSource );
427         }
428 
429         return null;
430     }
431 
432     /**
433      * write in the http response the value of the response whose identifier is
434      * specified in the request
435      * if there is no response return a error
436      * @param request the http request
437      * @param response The http response
438      * @return The URL to go after performing the action
439      */
440     public String doDownloadXslFile( HttpServletRequest request, HttpServletResponse response )
441     {
442         Plugin plugin = getPlugin(  );
443         ExportFormat exportFormat;
444         String strIdExport = request.getParameter( PARAMETER_ID_EXPORT );
445         int nIdExport = -1;
446 
447         if ( ( strIdExport != null ) && !strIdExport.equals( EMPTY_STRING ) &&
448                 RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, strIdExport,
449                     ExportFormatResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
450         {
451             try
452             {
453                 nIdExport = Integer.parseInt( strIdExport );
454             }
455             catch ( NumberFormatException ne )
456             {
457                 AppLogService.error( ne );
458 
459                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DURING_DOWNLOAD_FILE,
460                     AdminMessage.TYPE_STOP );
461             }
462         }
463         else
464         {
465             return AdminMessageService.getMessageUrl( request, MESSAGE_YOU_ARE_NOT_ALLOWED_TO_DOWLOAD_THIS_FILE,
466                 AdminMessage.TYPE_STOP );
467         }
468 
469         exportFormat = ExportFormatHome.findByPrimaryKey( nIdExport, plugin );
470 
471         if ( exportFormat != null )
472         {
473             try
474             {
475                 byte[] byteFileOutPut = exportFormat.getXsl(  );
476                 DiggUtils.addHeaderResponse( request, response, exportFormat.getTitle(  ) + ".xsl" );
477                 response.setContentType( CONSTANT_MIME_TYPE_OCTETSTREAM );
478                 response.setContentLength( byteFileOutPut.length );
479 
480                 OutputStream os = response.getOutputStream(  );
481                 os.write( byteFileOutPut );
482                 os.close(  );
483             }
484             catch ( IOException e )
485             {
486                 AppLogService.error( e );
487 
488                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DURING_DOWNLOAD_FILE,
489                     AdminMessage.TYPE_STOP );
490             }
491         }
492 
493         return getJspManageExportFormat( request );
494     }
495 
496     /**
497      * Use parsing for validate the modify xsl file
498      * @param baXslSource the xsl source
499      * @return the message exception when the validation is false
500      */
501     private String isValid( byte[] baXslSource )
502     {
503         String strError = null;
504 
505         try
506         {
507             SAXParserFactory factory = SAXParserFactory.newInstance(  );
508             SAXParser analyzer = factory.newSAXParser(  );
509             InputSource is = new InputSource( new ByteArrayInputStream( baXslSource ) );
510             analyzer.getXMLReader(  ).parse( is );
511         }
512         catch ( Exception e )
513         {
514             strError = e.getMessage(  );
515         }
516 
517         return strError;
518     }
519 
520     /**
521      * return the url of manage export format
522      * @param request the request
523      * @return the url of manage export format
524      */
525     private String getJspManageExportFormat( HttpServletRequest request )
526     {
527         return AppPathService.getBaseUrl( request ) + JSP_MANAGE_EXPORT_FORMAT;
528     }
529 }