View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.filegenerator.web.admin;
35  
36  import java.io.IOException;
37  import java.io.OutputStream;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import com.rometools.utils.Strings;
46  
47  import fr.paris.lutece.plugins.filegenerator.business.TemporaryFile;
48  import fr.paris.lutece.plugins.filegenerator.business.TemporaryFileHome;
49  import fr.paris.lutece.plugins.filegenerator.service.TemporaryFileService;
50  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
51  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
52  import fr.paris.lutece.portal.service.i18n.I18nService;
53  import fr.paris.lutece.portal.service.template.AppTemplateService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
56  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
57  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
58  import fr.paris.lutece.util.html.HtmlTemplate;
59  
60  /**
61   * This class provides the user interface to list temporary files
62   */
63  @Controller( controllerJsp = "ManageMyFiles.jsp", controllerPath = "jsp/admin/plugins/filegenerator/", right = "VIEW_TEMP_FILES" )
64  public class TemporaryFilesJspBean extends MVCAdminJspBean
65  {
66      private static final long serialVersionUID = 2823024035296798126L;
67  
68      // Rights
69      public static final String VIEW_TEMP_FILES = "VIEW_TEMP_FILES";
70  
71      // Parameter
72      public static final String PARAMETER_FILE_ID = "file_id";
73  
74      // View
75      private static final String VIEW_MY_FILES = "view_myFiles";
76  
77      // Templates
78      private static final String TEMPLATE_TEMPORARY_FILES = "admin/plugins/filegenerator/manage_temporary_files.html";
79      private static final String PROPERTY_TITLE_MANAGE_FILES_SYSTEM = "filegenerator.manage_temporary_files.pageTitle";
80      private static final String PROPERTY_MSG_DAYS_DELETE = "filegenerator.manage_temporary_files.help";
81  
82      // Marks
83      private static final String MARK_FILES = "files_list";
84      private static final String MARK_DAYS_DELETE = "msg_days_before_delete";
85  
86      // Messages
87      private static final String MESSAGE_FILE_ACCESS_DENIED = "Access Denied to this file";
88  
89      @View( value = VIEW_MY_FILES, defaultView = true )
90      public String getTemporaryFiles( HttpServletRequest request )
91      {
92          setPageTitleProperty( PROPERTY_TITLE_MANAGE_FILES_SYSTEM );
93          List<TemporaryFile> listFiles = TemporaryFileHome.findByUser( getUser( ) );
94          Map<String, Object> model = new HashMap<>( );
95          model.put( MARK_FILES, listFiles );
96  
97          String daysBeforeDelete = AppPropertiesService.getProperty( "daemon.temporaryfilesDaemon.days.defore.delete", "30" );
98          String message = I18nService.getLocalizedString( PROPERTY_MSG_DAYS_DELETE, new String [ ] {
99                  daysBeforeDelete
100         }, getLocale( ) );
101         model.put( MARK_DAYS_DELETE, message );
102 
103         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TEMPORARY_FILES, getLocale( ), model );
104 
105         return getAdminPage( template.getHtml( ) );
106     }
107 
108     public void doDownloadFile( HttpServletRequest request, HttpServletResponse response ) throws AccessDeniedException, IOException
109     {
110         String strId = request.getParameter( PARAMETER_FILE_ID );
111         if ( Strings.isNotEmpty( strId ) )
112         {
113             TemporaryFile file = TemporaryFileHome.findByPrimaryKey( Integer.valueOf( strId ) );
114 
115             if ( file.getUser( ).getUserId( ) != getUser( ).getUserId( ) )
116             {
117                 throw new AccessDeniedException( MESSAGE_FILE_ACCESS_DENIED );
118             }
119             if ( file.getIdPhysicalFile( ) == null )
120             {
121                 throw new AccessDeniedException( "File not yet generated" );
122             }
123             PhysicalFile physicalFile = TemporaryFileService.getInstance( ).loadPhysicalFile( file.getIdPhysicalFile( ) );
124             if ( physicalFile != null )
125             {
126                 response.setContentType( file.getTitle( ) );
127                 response.setHeader( "Content-Disposition", "attachment; filename=\"" + file.getTitle( ) + "\";" );
128                 OutputStream out = response.getOutputStream( );
129                 out.write( physicalFile.getValue() != null ? physicalFile.getValue() : new byte[0]);
130                 out.flush( );
131                 out.close( );
132             }
133         }
134     }
135 
136     public void doDeleteFile( HttpServletRequest request, HttpServletResponse response ) throws AccessDeniedException, IOException
137     {
138         String strId = request.getParameter( PARAMETER_FILE_ID );
139         if ( Strings.isNotEmpty( strId ) )
140         {
141             TemporaryFile file = TemporaryFileHome.findByPrimaryKey( Integer.valueOf( strId ) );
142             if ( file.getUser( ).getUserId( ) != getUser( ).getUserId( ) )
143             {
144                 throw new AccessDeniedException( MESSAGE_FILE_ACCESS_DENIED );
145             }
146             TemporaryFileService.getInstance( ).removeTemporaryFile( file );
147         }
148         redirectView( request, VIEW_MY_FILES );
149     }
150 }