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       * get the current FileStoreService provider
85       * 
86       * @return the current FileStoreService provider
87       */
88      public IFileStoreServiceProvider getFileStoreServiceProvider( )
89      {
90          return _currentFileStoreServiceProvider;
91      }
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 
103         // search file service
104         if ( !fileStoreServiceProviderList.isEmpty( ) )
105         {
106             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
107             {
108                 if ( strFileStoreServiceProviderName.equals( fss.getName( ) ) )
109                 {
110                       return fss;
111                 }
112             }
113         }
114 
115         // otherwise
116         throw new AppException( MSG_NO_FILE_SERVICE );
117     }
118 
119     /**
120      * get the current FileStoreService provider
121      * 
122      * @param strFileStoreServiceProviderName
123      */
124     public void setFileStoreServiceProvider( String strFileStoreServiceProviderName )
125     {
126         List<IFileStoreServiceProvider> fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );
127 
128         // search file service
129         if ( !fileStoreServiceProviderList.isEmpty( ) )
130         {
131             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
132             {
133                 if ( strFileStoreServiceProviderName.equals( fss.getName( ) ) )
134                 {
135                     _currentFileStoreServiceProvider = fss;
136                     return;
137                 }
138             }
139         }
140 
141         // otherwise
142         throw new AppException( MSG_NO_FILE_SERVICE );
143     }
144 
145     /**
146      * get default File Store Service Provider
147      * 
148      * @return the provider
149      */
150     private IFileStoreServiceProvider getDefaultServiceProvider( )
151     {
152         List<IFileStoreServiceProvider> fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );
153 
154         // search default file service
155         if ( !fileStoreServiceProviderList.isEmpty( ) )
156         {
157             for ( IFileStoreServiceProvider fss : fileStoreServiceProviderList )
158             {
159                 if ( fss.isDefault( ) )
160                 {
161                     return fss;
162                 }
163             }
164 
165             // return the first one otherwise
166             return fileStoreServiceProviderList.get( 0 ) ;
167         }
168 
169         // otherwise
170         throw new AppException( MSG_NO_FILE_SERVICE );
171     }
172 }