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;
35  
36  import fr.paris.lutece.portal.service.spring.SpringContextService;
37  import fr.paris.lutece.portal.service.util.AppException;
38  import java.util.List;
39  
40  /**
41   *
42   * @author SLE
43   */
44  public class FileService
45  {
46  
47      // parameters
48      public static final String PARAMETER_FILE_ID = "file_id";
49      public static final String PARAMETER_RESOURCE_ID = "resource_id";
50      public static final String PARAMETER_RESOURCE_TYPE = "resource_type";
51      public static final String PARAMETER_VALIDITY_TIME = "validity_time";
52      public static final String PARAMETER_DATA = "data";
53      public static final String PARAMETER_BO = "is_bo";
54      public static final String PARAMETER_PROVIDER = "provider";
55  
56      // constants
57      public static final String PERMISSION_VIEW = "VIEW";
58  
59      // messages
60      private static final String MSG_NO_FILE_SERVICE = "No file service Available";
61  
62      private IFileStoreServiceProvider _currentFileStoreServiceProvider;
63      private static FileService/file/FileService.html#FileService">FileService _instance = new FileService( );
64  
65      /**
66       * init
67       */
68      private FileService( )
69      {
70          _currentFileStoreServiceProvider = getDefaultServiceProvider( );
71      }
72  
73      /**
74       * getter
75       * 
76       * @return the instance
77       */
78      public static FileService getInstance( )
79      {
80          return _instance;
81      }
82      
83  
84      /**
85       * health check 
86       * 
87       * @return true if available
88       */
89      public boolean healthCheck( )
90      {
91          return _currentFileStoreServiceProvider.healthCheck( );
92      }
93  
94  
95      /**
96       * get the current FileStoreService provider
97       * 
98       * @return the current FileStoreService provider
99       */
100     public IFileStoreServiceProvider getFileStoreServiceProvider( )
101     {
102         return _currentFileStoreServiceProvider;
103     }
104 
105     /**
106      * get the FileStoreService provider
107      * 
108      * @param strFileStoreServiceProviderName
109      * @return the current FileStoreService provider
110      */
111     public IFileStoreServiceProvider getFileStoreServiceProvider( String strFileStoreServiceProviderName )
112     {
113     	List<IFileStoreServiceProvider> fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );
114 
115         // search file service
116         if ( !fileStoreServiceProviderList.isEmpty( ) )
117         {
118             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
119             {
120                 if ( strFileStoreServiceProviderName.equals( fss.getName( ) ) )
121                 {
122                       return fss;
123                 }
124             }
125         }
126 
127         // otherwise
128         throw new AppException( MSG_NO_FILE_SERVICE );
129     }
130 
131     /**
132      * get the current FileStoreService provider
133      * 
134      * @param strFileStoreServiceProviderName
135      */
136     public void setFileStoreServiceProvider( String strFileStoreServiceProviderName )
137     {
138         List<IFileStoreServiceProvider> fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );
139 
140         // search file service
141         if ( !fileStoreServiceProviderList.isEmpty( ) )
142         {
143             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
144             {
145                 if ( strFileStoreServiceProviderName.equals( fss.getName( ) ) )
146                 {
147                     _currentFileStoreServiceProvider = fss;
148                     return;
149                 }
150             }
151         }
152 
153         // otherwise
154         throw new AppException( MSG_NO_FILE_SERVICE );
155     }
156 
157     /**
158      * get default File Store Service Provider
159      * 
160      * @return the provider
161      */
162     private IFileStoreServiceProvider getDefaultServiceProvider( )
163     {
164         List<IFileStoreServiceProvider> fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );
165 
166         // search default file service
167         if ( !fileStoreServiceProviderList.isEmpty( ) )
168         {
169             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
170             {
171                 if ( fss.isDefault( ) )
172                 {
173                     return fss;
174                 }
175             }
176 
177             // return the first one otherwise
178             return fileStoreServiceProviderList.get( 0 ) ;
179         }
180 
181         // otherwise
182         throw new AppException( MSG_NO_FILE_SERVICE );
183     }
184 }