View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.blobstore.service.database;
35  
36  import java.io.InputStream;
37  
38  import org.apache.commons.lang3.StringUtils;
39  
40  import fr.paris.lutece.plugins.blobstore.business.BytesBlobStore;
41  import fr.paris.lutece.plugins.blobstore.business.InputStreamBlobStore;
42  import fr.paris.lutece.plugins.blobstore.business.database.DatabaseBlobStoreHome;
43  import fr.paris.lutece.plugins.blobstore.business.database.IDatabaseBlobStoreHome;
44  import fr.paris.lutece.plugins.blobstore.service.BlobStoreFileItem;
45  import fr.paris.lutece.plugins.blobstore.service.IBlobStoreService;
46  import fr.paris.lutece.plugins.blobstore.service.download.IBlobStoreDownloadUrlService;
47  import fr.paris.lutece.plugins.blobstore.service.download.JSPBlobStoreDownloadUrlService;
48  import fr.paris.lutece.plugins.blobstore.util.BlobStoreLibUtils;
49  import fr.paris.lutece.portal.service.spring.SpringContextService;
50  import fr.paris.lutece.portal.service.util.AppException;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  import java.io.IOException;
53  import org.apache.commons.fileupload.FileItem;
54  
55  /**
56   * 
57   * DatabaseBlobStoreService.
58   * 
59   */
60  public class DatabaseBlobStoreService implements IBlobStoreService
61  {
62      private static final long serialVersionUID = 1L;
63      private static final String MESSAGE_COULD_NOT_CREATE_BLOB = "BlobStore Error when generating a new id blob";
64  
65      /**
66       * name defaulted to databaseBlobstore - only one can be supported by webapp
67       */
68      private String _strName = "databaseBlobstore";
69  
70      /** Uses {@link JSPBlobStoreDownloadUrlService} as default one */
71      private IBlobStoreDownloadUrlService _downloadUrlService = new JSPBlobStoreDownloadUrlService( );
72  
73      /**
74       * Gets the downloadService
75       * 
76       * @return the downloadService
77       */
78      public IBlobStoreDownloadUrlService getDownloadUrlService( )
79      {
80          return _downloadUrlService;
81      }
82  
83      /**
84       * Sets the downloadService
85       * 
86       * @param downloadUrlService
87       *            downloadService
88       */
89      public void setDownloadUrlService( IBlobStoreDownloadUrlService downloadUrlService )
90      {
91          _downloadUrlService = downloadUrlService;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String getName( )
99      {
100         return _strName;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public void setName( String strName )
108     {
109         _strName = strName;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public void delete( String strKey )
117     {
118         IDatabaseBlobStoreHome databaseBlobStoreHome = SpringContextService.getBean( DatabaseBlobStoreHome.BEAN_SERVICE );
119         databaseBlobStoreHome.remove( strKey );
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public byte [ ] getBlob( String strKey )
127     {
128         byte [ ] blob = null;
129 
130         if ( StringUtils.isNotBlank( strKey ) )
131         {
132             IDatabaseBlobStoreHome databaseBlobStoreHome = SpringContextService.getBean( DatabaseBlobStoreHome.BEAN_SERVICE );
133             BytesBlobStore blobStore = databaseBlobStoreHome.findByPrimaryKey( strKey );
134 
135             if ( blobStore != null )
136             {
137                 blob = blobStore.getValue( );
138             }
139         }
140 
141         return blob;
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public String store( byte [ ] blob )
149     {
150         String strKey = BlobStoreLibUtils.generateNewIdBlob( );
151 
152         if ( StringUtils.isNotBlank( strKey ) )
153         {
154             BytesBlobStore blobStore = new BytesBlobStore( );
155             blobStore.setId( strKey );
156             blobStore.setValue( blob );
157 
158             IDatabaseBlobStoreHome databaseBlobStoreHome = SpringContextService.getBean( DatabaseBlobStoreHome.BEAN_SERVICE );
159             databaseBlobStoreHome.create( blobStore );
160         }
161         else
162         {
163             AppLogService.error( MESSAGE_COULD_NOT_CREATE_BLOB );
164         }
165 
166         return strKey;
167     }
168 
169     /**
170      * {@inheritDoc}
171      */
172     @Override
173     public void update( String strKey, byte [ ] blob )
174     {
175         if ( StringUtils.isNotBlank( strKey ) )
176         {
177             IDatabaseBlobStoreHome databaseBlobStoreHome = SpringContextService.getBean( DatabaseBlobStoreHome.BEAN_SERVICE );
178             BytesBlobStore blobStore = databaseBlobStoreHome.findByPrimaryKey( strKey );
179 
180             if ( blobStore != null )
181             {
182                 blobStore.setValue( blob );
183                 databaseBlobStoreHome.update( blobStore );
184             }
185         }
186     }
187 
188     /**
189      * {@inheritDoc}
190      */
191     @Override
192     public String storeInputStream( InputStream inputStream )
193     {
194         String strKey = BlobStoreLibUtils.generateNewIdBlob( );
195 
196         if ( StringUtils.isNotBlank( strKey ) )
197         {
198             InputStreamBlobStore blobStore = new InputStreamBlobStore( );
199             blobStore.setInputStream( inputStream );
200             blobStore.setId( strKey );
201 
202             IDatabaseBlobStoreHome databaseBlobStoreHome = SpringContextService.getBean( DatabaseBlobStoreHome.BEAN_SERVICE );
203             databaseBlobStoreHome.createInputStream( blobStore );
204         }
205         else
206         {
207             AppLogService.error( MESSAGE_COULD_NOT_CREATE_BLOB );
208         }
209 
210         return strKey;
211     }
212 
213     /*
214      * (non-Javadoc)
215      * 
216      * @see fr.paris.lutece.portal.service.blobstore.BlobStoreService#storeFileItem (java.io.InputStream)
217      */
218     @Override
219     public String storeFileItem( FileItem fileItem )
220     {
221         try
222         {
223             // store the file content
224             String strBlobContentKey = storeInputStream( fileItem.getInputStream( ) );
225 
226             // build metadata as json file and store it
227             String strMetadata = BlobStoreFileItem.buildFileMetadata( fileItem.getName( ), fileItem.getSize( ), strBlobContentKey, fileItem.getContentType( ) );
228             String strMetadataKey = store( strMetadata.getBytes( ) );
229 
230             return strMetadataKey;
231         }
232         catch( final IOException e )
233         {
234             throw new AppException( e.getMessage( ), e );
235         }
236     }
237 
238     /**
239      * {@inheritDoc}
240      */
241     @Override
242     public void updateInputStream( String strKey, InputStream inputStream )
243     {
244         InputStreamBlobStore blobStore = new InputStreamBlobStore( );
245         blobStore.setInputStream( inputStream );
246         blobStore.setId( strKey );
247 
248         IDatabaseBlobStoreHome databaseBlobStoreHome = SpringContextService.getBean( DatabaseBlobStoreHome.BEAN_SERVICE );
249         databaseBlobStoreHome.updateInputStream( blobStore );
250     }
251 
252     /**
253      * {@inheritDoc}
254      */
255     @Override
256     public InputStream getBlobInputStream( String strKey )
257     {
258         IDatabaseBlobStoreHome databaseBlobStoreHome = SpringContextService.getBean( DatabaseBlobStoreHome.BEAN_SERVICE );
259 
260         return databaseBlobStoreHome.findByPrimaryKeyInputStream( strKey );
261     }
262 
263     /**
264      * {@inheritDoc}
265      */
266     @Override
267     public String getBlobUrl( String strKey )
268     {
269         return _downloadUrlService.getDownloadUrl( getName( ), strKey );
270     }
271 
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     public String getFileUrl( String strKey )
277     {
278         return _downloadUrlService.getFileUrl( getName( ), strKey );
279     }
280 }