View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.modules.rest.rs;
35  
36  import java.io.InputStream;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.ws.rs.Consumes;
42  import javax.ws.rs.FormParam;
43  import javax.ws.rs.GET;
44  import javax.ws.rs.POST;
45  import javax.ws.rs.Path;
46  import javax.ws.rs.PathParam;
47  import javax.ws.rs.Produces;
48  import javax.ws.rs.core.Context;
49  import javax.ws.rs.core.MediaType;
50  
51  import org.apache.commons.io.IOUtils;
52  import org.apache.commons.lang.StringUtils;
53  import org.springframework.beans.factory.BeanDefinitionStoreException;
54  import org.springframework.beans.factory.CannotLoadBeanClassException;
55  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
56  
57  import com.sun.jersey.core.header.FormDataContentDisposition;
58  
59  import fr.paris.lutece.plugins.blobstore.modules.rest.util.constants.BlobStoreRestConstants;
60  import fr.paris.lutece.plugins.blobstore.service.BlobStoreFileItem;
61  import fr.paris.lutece.plugins.blobstore.service.BlobStorePlugin;
62  import fr.paris.lutece.plugins.blobstore.service.IBlobStoreService;
63  import fr.paris.lutece.plugins.blobstore.service.NoSuchBlobException;
64  import fr.paris.lutece.plugins.rest.service.RestConstants;
65  import fr.paris.lutece.portal.service.spring.SpringContextService;
66  import fr.paris.lutece.portal.service.template.AppTemplateService;
67  import fr.paris.lutece.portal.service.util.AppLogService;
68  import fr.paris.lutece.portal.service.util.AppPathService;
69  import fr.paris.lutece.util.html.HtmlTemplate;
70  
71  /**
72   * 
73   * BlobStoreRest
74   * 
75   */
76  @Path( RestConstants.BASE_PATH + BlobStorePlugin.PLUGIN_NAME )
77  public class BlobStoreRest
78  {
79      /**
80       * Get the wadl.xml content
81       * 
82       * @param request
83       *            {@link HttpServletRequest}
84       * @return the content of wadl.xml
85       */
86      @GET
87      @Path( BlobStoreRestConstants.PATH_WADL )
88      @Produces( MediaType.APPLICATION_XML )
89      public String getWADL( @Context HttpServletRequest request )
90      {
91          StringBuilder sbBase = new StringBuilder( AppPathService.getBaseUrl( request ) );
92  
93          if ( sbBase.toString( ).endsWith( BlobStoreRestConstants.SLASH ) )
94          {
95              sbBase.deleteCharAt( sbBase.length( ) - 1 );
96          }
97  
98          sbBase.append( RestConstants.BASE_PATH + BlobStorePlugin.PLUGIN_NAME );
99  
100         Map<String, Object> model = new HashMap<String, Object>( );
101         model.put( BlobStoreRestConstants.MARK_BASE_URL, sbBase.toString( ) );
102 
103         HtmlTemplate t = AppTemplateService.getTemplate( BlobStoreRestConstants.TEMPLATE_WADL, request.getLocale( ), model );
104 
105         return t.getHtml( );
106     }
107 
108     /**
109      * Get the file url
110      * 
111      * @param strBlobStore
112      *            the blobstore
113      * @param strBlobKey
114      *            the blob key
115      * @return the file url
116      */
117     @GET
118     @Path( BlobStoreRestConstants.PATH_FILE_URL )
119     @Produces( MediaType.TEXT_PLAIN )
120     public String getFileUrl( @PathParam( BlobStoreRestConstants.PARAMETER_BLOBSTORE ) String strBlobStore,
121             @PathParam( BlobStoreRestConstants.PARAMETER_BLOB_KEY ) String strBlobKey )
122     {
123         String strDownloadUrl = StringUtils.EMPTY;
124 
125         if ( StringUtils.isNotBlank( strBlobStore ) && StringUtils.isNotBlank( strBlobKey ) )
126         {
127             IBlobStoreService blobStoreService;
128 
129             try
130             {
131                 blobStoreService = (IBlobStoreService) SpringContextService.getPluginBean( BlobStorePlugin.PLUGIN_NAME, strBlobStore );
132                 strDownloadUrl = blobStoreService.getFileUrl( strBlobKey );
133             }
134             catch( BeanDefinitionStoreException e )
135             {
136                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
137             }
138             catch( NoSuchBeanDefinitionException e )
139             {
140                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
141             }
142             catch( CannotLoadBeanClassException e )
143             {
144                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
145             }
146         }
147         else
148         {
149             AppLogService.error( BlobStoreRestConstants.MESSAGE_MANDATORY_FIELDS );
150         }
151 
152         return strDownloadUrl;
153     }
154 
155     /**
156      * Delete a blob
157      * 
158      * @param strBlobKey
159      *            the blob key
160      * @param strBlobStore
161      *            the blobstore
162      * @return the blob key
163      */
164     @POST
165     @Path( BlobStoreRestConstants.PATH_DELETE_BLOBSTORE )
166     @Produces( MediaType.TEXT_HTML )
167     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
168     public String doDeleteBlobStore( @FormParam( BlobStoreRestConstants.PARAMETER_BLOB_KEY ) String strBlobKey,
169             @FormParam( BlobStoreRestConstants.PARAMETER_BLOBSTORE ) String strBlobStore )
170     {
171         String strResponse = StringUtils.EMPTY;
172 
173         if ( StringUtils.isNotBlank( strBlobKey ) && StringUtils.isNotBlank( strBlobStore ) )
174         {
175             strResponse = strBlobKey;
176 
177             IBlobStoreService blobStoreService;
178 
179             try
180             {
181                 blobStoreService = (IBlobStoreService) SpringContextService.getPluginBean( BlobStorePlugin.PLUGIN_NAME, strBlobStore );
182 
183                 BlobStoreFileItem fileItem = new BlobStoreFileItem( strBlobKey, blobStoreService );
184                 fileItem.delete( );
185             }
186             catch( BeanDefinitionStoreException e )
187             {
188                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
189             }
190             catch( NoSuchBeanDefinitionException e )
191             {
192                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
193             }
194             catch( CannotLoadBeanClassException e )
195             {
196                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
197             }
198             catch( NoSuchBlobException e )
199             {
200                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
201             }
202         }
203         else
204         {
205             AppLogService.error( BlobStoreRestConstants.MESSAGE_MANDATORY_FIELDS );
206         }
207 
208         return strResponse;
209     }
210 
211     /**
212      * Create a blob
213      * 
214      * @param strBlobStore
215      *            the the blobstore service
216      * @param blob
217      *            the blob to create
218      * @param blobDetail
219      *            the blob detail
220      * @return the id of the newly created blob
221      */
222     @POST
223     @Path( BlobStoreRestConstants.PATH_CREATE_BLOBSTORE )
224     @Consumes( MediaType.MULTIPART_FORM_DATA )
225     public String doCreateBlobStore( @FormParam( BlobStoreRestConstants.PARAMETER_BLOBSTORE ) String strBlobStore,
226             @FormParam( BlobStoreRestConstants.PARAMETER_BLOB ) InputStream blob,
227             @FormParam( BlobStoreRestConstants.PARAMETER_BLOB ) FormDataContentDisposition blobDetail )
228     {
229         String strBlobKey = StringUtils.EMPTY;
230 
231         if ( StringUtils.isNotBlank( strBlobStore ) && ( blob != null ) )
232         {
233             IBlobStoreService blobStoreService;
234 
235             try
236             {
237                 blobStoreService = (IBlobStoreService) SpringContextService.getPluginBean( BlobStorePlugin.PLUGIN_NAME, strBlobStore );
238                 strBlobKey = blobStoreService.storeInputStream( blob );
239 
240                 String strJSON = BlobStoreFileItem.buildFileMetadata( blobDetail.getFileName( ), blobDetail.getSize( ), strBlobKey, blobDetail.getType( ) );
241 
242                 if ( AppLogService.isDebugEnabled( ) )
243                 {
244                     AppLogService.debug( "Storing " + blobDetail.getName( ) + " with : " + strJSON );
245                 }
246 
247                 strBlobKey = blobStoreService.store( strJSON.getBytes( ) );
248             }
249             catch( BeanDefinitionStoreException e )
250             {
251                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
252             }
253             catch( NoSuchBeanDefinitionException e )
254             {
255                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
256             }
257             catch( CannotLoadBeanClassException e )
258             {
259                 AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE );
260             }
261             finally
262             {
263                 IOUtils.closeQuietly( blob );
264             }
265         }
266         else
267         {
268             AppLogService.error( BlobStoreRestConstants.MESSAGE_MANDATORY_FIELDS );
269         }
270 
271         return strBlobKey;
272     }
273 }