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