View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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 _defaulFileStoreServiceProvider;
63      private List<IFileStoreServiceProvider> _fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );
64  
65      private static FileService _instance;
66  
67      /**
68       * init
69       */
70      private FileService( )
71      {
72          _defaulFileStoreServiceProvider = getDefaultServiceProvider( );
73      }
74  
75      /**
76       * getter
77       * 
78       * @return the instance
79       */
80      public static FileService getInstance( )
81      {
82          if ( _instance == null )
83          {
84              _instance = new FileService( );
85          }
86          return _instance;
87      }
88  
89      /**
90       * get the current FileStoreService provider
91       * 
92       * @return the current FileStoreService provider
93       */
94      public IFileStoreServiceProvider getFileStoreServiceProvider( )
95      {
96          return getFileStoreServiceProvider( null) ;
97      }
98  
99      /**
100      * get the FileStoreService provider
101      * 
102      * @param strFileStoreServiceProviderName
103      * @return the current FileStoreService provider
104      */
105     public IFileStoreServiceProvider getFileStoreServiceProvider( String strFileStoreServiceProviderName )
106     {
107         _fileStoreServiceProviderList = SpringContextService.getBeansOfType( IFileStoreServiceProvider.class );
108 
109     	if ( strFileStoreServiceProviderName == null && _defaulFileStoreServiceProvider != null )
110     	{
111     		return _defaulFileStoreServiceProvider;
112     	}
113     	
114         // search file service
115         if ( !_fileStoreServiceProviderList.isEmpty( ) && strFileStoreServiceProviderName != null )
116         {
117             for ( IFileStoreServiceProvider fss : _fileStoreServiceProviderList )
118             {
119                 if ( strFileStoreServiceProviderName.equals( fss.getName( ) ) )
120                 {
121                       return fss;
122                 }
123             }
124         }
125 
126         // otherwise
127         throw new AppException( MSG_NO_FILE_SERVICE );
128     }
129 
130     /**
131      * get default File Store Service Provider
132      * 
133      * @return the provider
134      */
135     private IFileStoreServiceProvider getDefaultServiceProvider( )
136     {
137         // search default file service
138         if ( !_fileStoreServiceProviderList.isEmpty( ) )
139         {
140             for ( IFileStoreServiceProvider fss : _fileStoreServiceProviderList )
141             {
142                 if ( fss.isDefault( ) )
143                 {
144                     return fss;
145                 }
146             }
147 
148             // return the first one otherwise
149             return _fileStoreServiceProviderList.get( 0 ) ;
150         }
151 
152         // otherwise
153         throw new AppException( MSG_NO_FILE_SERVICE );
154     }
155 }