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