FileService.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;

  35. import fr.paris.lutece.portal.service.spring.SpringContextService;
  36. import fr.paris.lutece.portal.service.util.AppException;
  37. import java.util.List;

  38. /**
  39.  *
  40.  * @author SLE
  41.  */
  42. public class FileService
  43. {

  44.     // parameters
  45.     public static final String PARAMETER_FILE_ID = "file_id";
  46.     public static final String PARAMETER_RESOURCE_ID = "resource_id";
  47.     public static final String PARAMETER_RESOURCE_TYPE = "resource_type";
  48.     public static final String PARAMETER_VALIDITY_TIME = "validity_time";
  49.     public static final String PARAMETER_DATA = "data";
  50.     public static final String PARAMETER_BO = "is_bo";
  51.     public static final String PARAMETER_PROVIDER = "provider";

  52.     // constants
  53.     public static final String PERMISSION_VIEW = "VIEW";

  54.     // messages
  55.     private static final String MSG_NO_FILE_SERVICE = "No file service Available";

  56.     private IFileStoreServiceProvider _currentFileStoreServiceProvider;
  57.     private static FileService _instance = new FileService( );

  58.     /**
  59.      * init
  60.      */
  61.     private FileService( )
  62.     {
  63.         _currentFileStoreServiceProvider = getDefaultServiceProvider( );
  64.     }

  65.     /**
  66.      * getter
  67.      *
  68.      * @return the instance
  69.      */
  70.     public static FileService getInstance( )
  71.     {
  72.         return _instance;
  73.     }
  74.    

  75.     /**
  76.      * health check
  77.      *
  78.      * @return true if available
  79.      */
  80.     public boolean healthCheck( )
  81.     {
  82.         return _currentFileStoreServiceProvider.healthCheck( );
  83.     }


  84.     /**
  85.      * get the current FileStoreService provider
  86.      *
  87.      * @return the current FileStoreService provider
  88.      */
  89.     public IFileStoreServiceProvider getFileStoreServiceProvider( )
  90.     {
  91.         return _currentFileStoreServiceProvider;
  92.     }

  93.     /**
  94.      * get the FileStoreService provider
  95.      *
  96.      * @param strFileStoreServiceProviderName
  97.      * @return the current FileStoreService provider
  98.      */
  99.     public IFileStoreServiceProvider getFileStoreServiceProvider( String strFileStoreServiceProviderName )
  100.     {
  101.         List<IFileStoreServiceProvider> fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );

  102.         // search file service
  103.         if ( !fileStoreServiceProviderList.isEmpty( ) )
  104.         {
  105.             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
  106.             {
  107.                 if ( strFileStoreServiceProviderName.equals( fss.getName( ) ) )
  108.                 {
  109.                       return fss;
  110.                 }
  111.             }
  112.         }

  113.         // otherwise
  114.         throw new AppException( MSG_NO_FILE_SERVICE );
  115.     }

  116.     /**
  117.      * get the current FileStoreService provider
  118.      *
  119.      * @param strFileStoreServiceProviderName
  120.      */
  121.     public void setFileStoreServiceProvider( String strFileStoreServiceProviderName )
  122.     {
  123.         List<IFileStoreServiceProvider> fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );

  124.         // search file service
  125.         if ( !fileStoreServiceProviderList.isEmpty( ) )
  126.         {
  127.             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
  128.             {
  129.                 if ( strFileStoreServiceProviderName.equals( fss.getName( ) ) )
  130.                 {
  131.                     _currentFileStoreServiceProvider = fss;
  132.                     return;
  133.                 }
  134.             }
  135.         }

  136.         // otherwise
  137.         throw new AppException( MSG_NO_FILE_SERVICE );
  138.     }

  139.     /**
  140.      * get default File Store Service Provider
  141.      *
  142.      * @return the provider
  143.      */
  144.     private IFileStoreServiceProvider getDefaultServiceProvider( )
  145.     {
  146.         List<IFileStoreServiceProvider> fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );

  147.         // search default file service
  148.         if ( !fileStoreServiceProviderList.isEmpty( ) )
  149.         {
  150.             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
  151.             {
  152.                 if ( fss.isDefault( ) )
  153.                 {
  154.                     return fss;
  155.                 }
  156.             }

  157.             // return the first one otherwise
  158.             return fileStoreServiceProviderList.get( 0 ) ;
  159.         }

  160.         // otherwise
  161.         throw new AppException( MSG_NO_FILE_SERVICE );
  162.     }
  163. }