LocalDatabaseFileService.java

  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. import java.io.ByteArrayInputStream;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.util.Map;

  39. import javax.servlet.http.HttpServletRequest;

  40. import org.apache.commons.fileupload.FileItem;
  41. import org.apache.commons.io.IOUtils;
  42. import org.apache.commons.lang3.StringUtils;

  43. import fr.paris.lutece.api.user.User;
  44. import fr.paris.lutece.portal.business.file.File;
  45. import fr.paris.lutece.portal.business.file.FileHome;
  46. import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
  47. import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
  48. import fr.paris.lutece.portal.service.admin.AccessDeniedException;
  49. import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
  50. import fr.paris.lutece.portal.service.file.ExpiredLinkException;
  51. import fr.paris.lutece.portal.service.file.FileService;
  52. import fr.paris.lutece.portal.service.file.IFileDownloadUrlService;
  53. import fr.paris.lutece.portal.service.file.IFileRBACService;
  54. import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
  55. import fr.paris.lutece.portal.service.security.SecurityService;
  56. import fr.paris.lutece.portal.service.security.UserNotSignedException;
  57. import fr.paris.lutece.portal.service.util.AppException;

  58. /**
  59.  *
  60.  * DatabaseBlobStoreService.
  61.  *
  62.  */
  63. public class LocalDatabaseFileService implements IFileStoreServiceProvider
  64. {
  65.     private static final long serialVersionUID = 1L;

  66.     private IFileDownloadUrlService _fileDownloadUrlService;
  67.     private IFileRBACService _fileRBACService;
  68.     private String _strName;
  69.     private boolean _bDefault;

  70.     /**
  71.      * init
  72.      *
  73.      * @param _fileDownloadUrlService
  74.      * @param _fileRBACService
  75.      */
  76.     public LocalDatabaseFileService( IFileDownloadUrlService _fileDownloadUrlService, IFileRBACService _fileRBACService )
  77.     {
  78.         this._fileDownloadUrlService = _fileDownloadUrlService;
  79.         this._fileRBACService = _fileRBACService;
  80.     }

  81.     /**
  82.      * get the FileRBACService
  83.      *
  84.      * @return the FileRBACService
  85.      */
  86.     public IFileRBACService getFileRBACService( )
  87.     {
  88.         return _fileRBACService;
  89.     }

  90.     /**
  91.      * set the FileRBACService
  92.      *
  93.      * @param fileRBACService
  94.      */
  95.     public void setFileRBACService( IFileRBACService fileRBACService )
  96.     {
  97.         this._fileRBACService = fileRBACService;
  98.     }

  99.     /**
  100.      * Get the downloadService
  101.      *
  102.      * @return the downloadService
  103.      */
  104.     public IFileDownloadUrlService getDownloadUrlService( )
  105.     {
  106.         return _fileDownloadUrlService;
  107.     }

  108.     /**
  109.      * Sets the downloadService
  110.      *
  111.      * @param downloadUrlService
  112.      *            downloadService
  113.      */
  114.     public void setDownloadUrlService( IFileDownloadUrlService downloadUrlService )
  115.     {
  116.         _fileDownloadUrlService = downloadUrlService;
  117.     }

  118.     /**
  119.      * {@inheritDoc}
  120.      */
  121.     @Override
  122.     public String getName( )
  123.     {
  124.         return _strName;
  125.     }

  126.     /**
  127.      * {@inheritDoc}
  128.      */
  129.     @Override
  130.     public void delete( String strKey )
  131.     {
  132.         int nfileId = Integer.parseInt( strKey );
  133.         FileHome.remove( nfileId );

  134.     }

  135.     /**
  136.      * {@inheritDoc}
  137.      */
  138.     @Override
  139.     public File getFile( String strKey )
  140.     {
  141.         return getFile( strKey, true );
  142.     }

  143.     /**
  144.      * {@inheritDoc}
  145.      */
  146.     @Override
  147.     public File getFileMetaData( String strKey )
  148.     {
  149.         return getFile( strKey, false );
  150.     }

  151.     /**
  152.      * get file from database
  153.      *
  154.      * @param strKey
  155.      * @param withPhysicalFile
  156.      *
  157.      * @return the file with the physical file content if withPhysicalFile is true, null otherwise
  158.      */
  159.     public File getFile( String strKey, boolean withPhysicalFile )
  160.     {
  161.         if ( StringUtils.isNotBlank( strKey ) )
  162.         {
  163.             int nfileId = Integer.parseInt( strKey );
  164.            
  165.             // get meta data
  166.             File file = FileHome.findByPrimaryKey( nfileId );

  167.             // check if the file exists and was inserted with this provider
  168.             if ( file == null
  169.                     || ( file.getOrigin( ) == null && getName( ) != null )
  170.                     || ( file.getOrigin( ) != null && !file.getOrigin( ).equals( getName( ) ) ) )
  171.             {
  172.                 return null;
  173.             }
  174.            
  175.             if ( withPhysicalFile )
  176.             {
  177.                 // get file content
  178.                 file.setPhysicalFile( PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ) ) );
  179.             }
  180.            
  181.             return file;
  182.         }

  183.         return null;
  184.     }

  185.     /**
  186.      * {@inheritDoc}
  187.      */
  188.     @Override
  189.     public String storeBytes( byte [ ] blob )
  190.     {
  191.         File file = new File( );
  192.         PhysicalFile physicalFile = new PhysicalFile( );
  193.         physicalFile.setValue( blob );
  194.         file.setPhysicalFile( physicalFile );
  195.         file.setOrigin( getName( ) );
  196.        
  197.         int nFileId = FileHome.create( file );

  198.         return String.valueOf( nFileId );
  199.     }

  200.     /**
  201.      * {@inheritDoc}
  202.      */
  203.     @Override
  204.     public String storeInputStream( InputStream inputStream )
  205.     {
  206.         File file = new File( );
  207.         PhysicalFile physicalFile = new PhysicalFile( );

  208.         byte [ ] buffer;

  209.         try
  210.         {
  211.             buffer = new byte [ inputStream.available( )];
  212.         }
  213.         catch( IOException ex )
  214.         {
  215.             throw new AppException( ex.getMessage( ), ex );
  216.         }

  217.         physicalFile.setValue( buffer );
  218.         file.setPhysicalFile( physicalFile );

  219.         file.setOrigin( getName( ) );

  220.         int nFileId = FileHome.create( file );

  221.         return String.valueOf( nFileId );
  222.     }

  223.     /**
  224.      * {@inheritDoc}
  225.      */
  226.     @Override
  227.     public String storeFileItem( FileItem fileItem )
  228.     {

  229.         File file = new File( );
  230.         file.setTitle( fileItem.getName( ) );
  231.         file.setSize( (int) fileItem.getSize( ) );
  232.         file.setMimeType( fileItem.getContentType( ) );

  233.         file.setOrigin( getName( ) );
  234.        
  235.         PhysicalFile physicalFile = new PhysicalFile( );

  236.         byte [ ] byteArray;

  237.         try
  238.         {
  239.             byteArray = IOUtils.toByteArray( fileItem.getInputStream( ) );
  240.         }
  241.         catch( IOException ex )
  242.         {
  243.             throw new AppException( ex.getMessage( ), ex );
  244.         }

  245.         physicalFile.setValue( byteArray );
  246.         file.setPhysicalFile( physicalFile );

  247.         int nFileId = FileHome.create( file );

  248.         return String.valueOf( nFileId );
  249.     }

  250.     /**
  251.      * {@inheritDoc}
  252.      */
  253.     @Override
  254.     public String storeFile( File file )
  255.     {
  256.         file.setOrigin( getName( ) );
  257.        
  258.         int nFileId = FileHome.create( file );

  259.         return String.valueOf( nFileId );
  260.     }

  261.     public void setDefault( boolean bDefault )
  262.     {
  263.         this._bDefault = bDefault;
  264.     }

  265.     public void setName( String strName )
  266.     {
  267.         _strName = strName;
  268.     }

  269.     /**
  270.      * {@inheritDoc}
  271.      */
  272.     @Override
  273.     public boolean isDefault( )
  274.     {
  275.         return _bDefault;
  276.     }

  277.     /**
  278.      * {@inheritDoc}
  279.      */
  280.     @Override
  281.     public InputStream getInputStream( String strKey )
  282.     {

  283.         File file = getFile( strKey );

  284.         return new ByteArrayInputStream( file.getPhysicalFile( ).getValue( ) );
  285.     }

  286.     /**
  287.      * {@inheritDoc}
  288.      */
  289.     @Override
  290.     public String getFileDownloadUrlFO( String strKey )
  291.     {
  292.         return _fileDownloadUrlService.getFileDownloadUrlFO( strKey, getName( ) );
  293.     }

  294.     /**
  295.      * {@inheritDoc}
  296.      */
  297.     @Override
  298.     public String getFileDownloadUrlFO( String strKey, Map<String, String> additionnalData )
  299.     {
  300.         return _fileDownloadUrlService.getFileDownloadUrlFO( strKey, additionnalData, getName( ) );
  301.     }

  302.     /**
  303.      * {@inheritDoc}
  304.      */
  305.     @Override
  306.     public String getFileDownloadUrlBO( String strKey )
  307.     {
  308.         return _fileDownloadUrlService.getFileDownloadUrlBO( strKey, getName( ) );
  309.     }

  310.     /**
  311.      * {@inheritDoc}
  312.      */
  313.     @Override
  314.     public String getFileDownloadUrlBO( String strKey, Map<String, String> additionnalData )
  315.     {
  316.         return _fileDownloadUrlService.getFileDownloadUrlBO( strKey, additionnalData, getName( ) );
  317.     }

  318.     /**
  319.      * {@inheritDoc}
  320.      */
  321.     @Override
  322.     public void checkAccessRights( Map<String, String> fileData, User user ) throws AccessDeniedException, UserNotSignedException
  323.     {
  324.         if ( _fileRBACService != null )
  325.         {
  326.             _fileRBACService.checkAccessRights( fileData, user );
  327.         }
  328.     }

  329.     /**
  330.      * {@inheritDoc}
  331.      */
  332.     @Override
  333.     public void checkLinkValidity( Map<String, String> fileData ) throws ExpiredLinkException
  334.     {
  335.         _fileDownloadUrlService.checkLinkValidity( fileData );
  336.     }

  337.     /**
  338.      * {@inheritDoc}
  339.      */
  340.     @Override
  341.     public File getFileFromRequestBO( HttpServletRequest request ) throws AccessDeniedException, ExpiredLinkException, UserNotSignedException
  342.     {
  343.         Map<String, String> fileData = _fileDownloadUrlService.getRequestDataBO( request );

  344.         // check access rights
  345.         checkAccessRights( fileData, AdminAuthenticationService.getInstance( ).getRegisteredUser( request ) );

  346.         // check validity
  347.         checkLinkValidity( fileData );

  348.         String strFileId = fileData.get( FileService.PARAMETER_FILE_ID );

  349.         return getFile( strFileId );
  350.     }

  351.     /**
  352.      * {@inheritDoc}
  353.      */
  354.     @Override
  355.     public File getFileFromRequestFO( HttpServletRequest request ) throws AccessDeniedException, ExpiredLinkException, UserNotSignedException
  356.     {

  357.         Map<String, String> fileData = _fileDownloadUrlService.getRequestDataFO( request );

  358.         // check access rights
  359.         checkAccessRights( fileData, SecurityService.getInstance( ).getRegisteredUser( request ) );

  360.         // check validity
  361.         checkLinkValidity( fileData );

  362.         String strFileId = fileData.get( FileService.PARAMETER_FILE_ID );

  363.         return getFile( strFileId );
  364.     }
  365. }