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.portal.service.file.implementation;
35  
36  import java.io.ByteArrayInputStream;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.fileupload.FileItem;
44  import org.apache.commons.io.IOUtils;
45  import org.apache.commons.lang3.StringUtils;
46  
47  import fr.paris.lutece.api.user.User;
48  import fr.paris.lutece.portal.business.file.File;
49  import fr.paris.lutece.portal.business.file.FileHome;
50  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
51  import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
52  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
53  import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
54  import fr.paris.lutece.portal.service.file.ExpiredLinkException;
55  import fr.paris.lutece.portal.service.file.FileService;
56  import fr.paris.lutece.portal.service.file.IFileDownloadUrlService;
57  import fr.paris.lutece.portal.service.file.IFileRBACService;
58  import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
59  import fr.paris.lutece.portal.service.security.SecurityService;
60  import fr.paris.lutece.portal.service.security.UserNotSignedException;
61  import fr.paris.lutece.portal.service.util.AppException;
62  
63  /**
64   * 
65   * DatabaseBlobStoreService.
66   * 
67   */
68  public class LocalDatabaseFileService implements IFileStoreServiceProvider
69  {
70      private static final long serialVersionUID = 1L;
71  
72      private IFileDownloadUrlService _fileDownloadUrlService;
73      private IFileRBACService _fileRBACService;
74      private String _strName;
75      private boolean _bDefault;
76  
77      /**
78       * init
79       * 
80       * @param _fileDownloadUrlService
81       * @param _fileRBACService
82       */
83      public LocalDatabaseFileService( IFileDownloadUrlService _fileDownloadUrlService, IFileRBACService _fileRBACService )
84      {
85          this._fileDownloadUrlService = _fileDownloadUrlService;
86          this._fileRBACService = _fileRBACService;
87      }
88  
89      /**
90       * get the FileRBACService
91       * 
92       * @return the FileRBACService
93       */
94      public IFileRBACService getFileRBACService( )
95      {
96          return _fileRBACService;
97      }
98  
99      /**
100      * set the FileRBACService
101      * 
102      * @param fileRBACService
103      */
104     public void setFileRBACService( IFileRBACService fileRBACService )
105     {
106         this._fileRBACService = fileRBACService;
107     }
108 
109     /**
110      * Get the downloadService
111      * 
112      * @return the downloadService
113      */
114     public IFileDownloadUrlService getDownloadUrlService( )
115     {
116         return _fileDownloadUrlService;
117     }
118 
119     /**
120      * Sets the downloadService
121      * 
122      * @param downloadUrlService
123      *            downloadService
124      */
125     public void setDownloadUrlService( IFileDownloadUrlService downloadUrlService )
126     {
127         _fileDownloadUrlService = downloadUrlService;
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public String getName( )
135     {
136         return _strName;
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public void delete( String strKey )
144     {
145         int nfileId = Integer.parseInt( strKey );
146         FileHome.remove( nfileId );
147 
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public File getFile( String strKey )
155     {
156         return getFile( strKey, true );
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     public File getFileMetaData( String strKey )
164     {
165         return getFile( strKey, false );
166     }
167 
168     /**
169      * get file from database
170      * 
171      * @param strKey
172      * @param withPhysicalFile
173      * 
174      * @return the file with the physical file content if withPhysicalFile is true, null otherwise
175      */
176     public File getFile( String strKey, boolean withPhysicalFile )
177     {
178         if ( StringUtils.isNotBlank( strKey ) )
179         {
180             int nfileId = Integer.parseInt( strKey );
181             
182             // get meta data
183             File file = FileHome.findByPrimaryKey( nfileId );
184 
185             // check if the file exists and was inserted with this provider
186             if ( file == null 
187             		|| ( file.getOrigin( ) == null && getName( ) != null ) 
188             		|| ( file.getOrigin( ) != null && !file.getOrigin( ).equals( getName( ) ) ) )
189             {
190             	return null;
191             }
192             
193             if ( withPhysicalFile )
194             {
195                 // get file content
196                 file.setPhysicalFile( PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ) ) );
197             }
198             
199             return file;
200         }
201 
202         return null;
203     }
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public String storeBytes( byte [ ] blob )
210     {
211         File/portal/business/file/File.html#File">File file = new File( );
212         PhysicalFile/physicalfile/PhysicalFile.html#PhysicalFile">PhysicalFile physicalFile = new PhysicalFile( );
213         physicalFile.setValue( blob );
214         file.setPhysicalFile( physicalFile );
215         file.setOrigin( getName( ) );
216         
217         int nFileId = FileHome.create( file );
218 
219         return String.valueOf( nFileId );
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public String storeInputStream( InputStream inputStream )
227     {
228         File/portal/business/file/File.html#File">File file = new File( );
229         PhysicalFile/physicalfile/PhysicalFile.html#PhysicalFile">PhysicalFile physicalFile = new PhysicalFile( );
230 
231         byte [ ] buffer;
232 
233         try
234         {
235             buffer = new byte [ inputStream.available( )];
236         }
237         catch( IOException ex )
238         {
239             throw new AppException( ex.getMessage( ), ex );
240         }
241 
242         physicalFile.setValue( buffer );
243         file.setPhysicalFile( physicalFile );
244 
245         file.setOrigin( getName( ) );
246 
247         int nFileId = FileHome.create( file );
248 
249         return String.valueOf( nFileId );
250     }
251 
252     /**
253      * {@inheritDoc}
254      */
255     @Override
256     public String storeFileItem( FileItem fileItem )
257     {
258 
259         File/portal/business/file/File.html#File">File file = new File( );
260         file.setTitle( fileItem.getName( ) );
261         file.setSize( (int) fileItem.getSize( ) );
262         file.setMimeType( fileItem.getContentType( ) );
263 
264         file.setOrigin( getName( ) );
265         
266         PhysicalFile/physicalfile/PhysicalFile.html#PhysicalFile">PhysicalFile physicalFile = new PhysicalFile( );
267 
268         byte [ ] byteArray;
269 
270         try
271         {
272             byteArray = IOUtils.toByteArray( fileItem.getInputStream( ) );
273         }
274         catch( IOException ex )
275         {
276             throw new AppException( ex.getMessage( ), ex );
277         }
278 
279         physicalFile.setValue( byteArray );
280         file.setPhysicalFile( physicalFile );
281 
282         int nFileId = FileHome.create( file );
283 
284         return String.valueOf( nFileId );
285     }
286 
287     /**
288      * {@inheritDoc}
289      */
290     @Override
291     public String storeFile( File file )
292     {
293         file.setOrigin( getName( ) );
294         
295         int nFileId = FileHome.create( file );
296 
297         return String.valueOf( nFileId );
298     }
299 
300     public void setDefault( boolean bDefault )
301     {
302         this._bDefault = bDefault;
303     }
304 
305     public void setName( String strName )
306     {
307         _strName = strName;
308     }
309 
310     /**
311      * {@inheritDoc}
312      */
313     @Override
314     public boolean isDefault( )
315     {
316         return _bDefault;
317     }
318 
319     /**
320      * {@inheritDoc}
321      */
322     @Override
323     public InputStream getInputStream( String strKey )
324     {
325 
326         File file = getFile( strKey );
327 
328         return new ByteArrayInputStream( file.getPhysicalFile( ).getValue( ) );
329     }
330 
331     /**
332      * {@inheritDoc}
333      */
334     @Override
335     public String getFileDownloadUrlFO( String strKey )
336     {
337         return _fileDownloadUrlService.getFileDownloadUrlFO( strKey, getName( ) );
338     }
339 
340     /**
341      * {@inheritDoc}
342      */
343     @Override
344     public String getFileDownloadUrlFO( String strKey, Map<String, String> additionnalData )
345     {
346         return _fileDownloadUrlService.getFileDownloadUrlFO( strKey, additionnalData, getName( ) );
347     }
348 
349     /**
350      * {@inheritDoc}
351      */
352     @Override
353     public String getFileDownloadUrlBO( String strKey )
354     {
355         return _fileDownloadUrlService.getFileDownloadUrlBO( strKey, getName( ) );
356     }
357 
358     /**
359      * {@inheritDoc}
360      */
361     @Override
362     public String getFileDownloadUrlBO( String strKey, Map<String, String> additionnalData )
363     {
364         return _fileDownloadUrlService.getFileDownloadUrlBO( strKey, additionnalData, getName( ) );
365     }
366 
367     /**
368      * {@inheritDoc}
369      */
370     @Override
371     public void checkAccessRights( Map<String, String> fileData, User user ) throws AccessDeniedException, UserNotSignedException
372     {
373         if ( _fileRBACService != null )
374         {
375             _fileRBACService.checkAccessRights( fileData, user );
376         }
377     }
378 
379     /**
380      * {@inheritDoc}
381      */
382     @Override
383     public void checkLinkValidity( Map<String, String> fileData ) throws ExpiredLinkException
384     {
385         _fileDownloadUrlService.checkLinkValidity( fileData );
386     }
387 
388     /**
389      * {@inheritDoc}
390      */
391     @Override
392     public File getFileFromRequestBO( HttpServletRequest request ) throws AccessDeniedException, ExpiredLinkException, UserNotSignedException
393     {
394         Map<String, String> fileData = _fileDownloadUrlService.getRequestDataBO( request );
395 
396         // check access rights
397         checkAccessRights( fileData, AdminAuthenticationService.getInstance( ).getRegisteredUser( request ) );
398 
399         // check validity
400         checkLinkValidity( fileData );
401 
402         String strFileId = fileData.get( FileService.PARAMETER_FILE_ID );
403 
404         return getFile( strFileId );
405     }
406 
407     /**
408      * {@inheritDoc}
409      */
410     @Override
411     public File getFileFromRequestFO( HttpServletRequest request ) throws AccessDeniedException, ExpiredLinkException, UserNotSignedException
412     {
413 
414         Map<String, String> fileData = _fileDownloadUrlService.getRequestDataFO( request );
415 
416         // check access rights
417         checkAccessRights( fileData, SecurityService.getInstance( ).getRegisteredUser( request ) );
418 
419         // check validity
420         checkLinkValidity( fileData );
421 
422         String strFileId = fileData.get( FileService.PARAMETER_FILE_ID );
423 
424         return getFile( strFileId );
425     }
426 }