1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.blobstore.web;
35
36 import fr.paris.lutece.plugins.blobstore.service.BlobStoreFileItem;
37 import fr.paris.lutece.plugins.blobstore.service.IBlobStoreService;
38 import fr.paris.lutece.plugins.blobstore.service.NoSuchBlobException;
39 import fr.paris.lutece.plugins.blobstore.util.BlobStoreLibConstants;
40 import fr.paris.lutece.plugins.blobstore.util.BlobStoreUtils;
41 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
42 import fr.paris.lutece.portal.service.i18n.I18nService;
43 import fr.paris.lutece.portal.service.spring.SpringContextService;
44 import fr.paris.lutece.portal.service.util.AppLogService;
45 import fr.paris.lutece.util.filesystem.UploadUtil;
46
47 import org.apache.commons.io.IOUtils;
48 import org.apache.commons.lang3.StringUtils;
49
50 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
51
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.io.OutputStream;
55
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
58
59
60
61
62
63 public class BlobStoreJspBean
64 {
65 private static final String PROPERTY_MESSAGE_NO_SUCH_BLOB = "blobstore.download.noSuchBlob";
66 private static final String PROPERTY_MESSAGE_ERROR_RETRIEVING_BLOB = "blobstore.download.error";
67 private static final String PROPERTY_MESSAGE_NO_SUCH_BLOBSTORE = "blobstore.download.noSuchBlobStore";
68 private static final String PROPERTY_MESSAGE_ACCESS_DENIED = "blobstore.download.accessDenied";
69
70
71
72
73
74
75
76
77
78
79 public String doDownloadFile( HttpServletRequest request, HttpServletResponse response )
80 {
81 String strErrorMessage = null;
82
83 if ( BlobStoreUtils.getRequestAuthenticator( ).isRequestAuthenticated( request ) )
84 {
85 String strBlobKey = request.getParameter( BlobStoreLibConstants.PARAMETER_BLOB_KEY );
86 String strBlobstore = request.getParameter( BlobStoreLibConstants.PARAMETER_BLOB_STORE );
87
88 IBlobStoreService blobstoreService = (IBlobStoreService) SpringContextService.getBean( strBlobstore );
89
90 if ( blobstoreService != null )
91 {
92 if ( StringUtils.isNotBlank( strBlobKey ) )
93 {
94 try
95 {
96 BlobStoreFileItem fileItem = new BlobStoreFileItem( strBlobKey, blobstoreService );
97 DownloadableFile file = new DownloadableFile( );
98 file.setContentType( fileItem.getContentType( ) );
99 file.setFileName( UploadUtil.cleanFileName( FileUploadService.getFileNameOnly( fileItem ) ) );
100 file.setInputStream( fileItem.getInputStream( ) );
101 file.setSize( (int) fileItem.getSize( ) );
102
103 strErrorMessage = writeFile( request, response, file );
104 }
105 catch( IOException ioe )
106 {
107 AppLogService.error( ioe.getMessage( ), ioe );
108
109 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_RETRIEVING_BLOB, request.getLocale( ) );
110 }
111 catch( NoSuchBlobException nsbe )
112 {
113
114 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_NO_SUCH_BLOB, request.getLocale( ) );
115 }
116 }
117 else
118 {
119
120 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_NO_SUCH_BLOB, request.getLocale( ) );
121 }
122 }
123 else
124 {
125
126 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_NO_SUCH_BLOBSTORE, request.getLocale( ) );
127 }
128 }
129 else
130 {
131 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_ACCESS_DENIED, request.getLocale( ) );
132 }
133
134
135 return strErrorMessage;
136 }
137
138
139
140
141
142
143
144
145
146
147 public String doDownloadBlob( HttpServletRequest request, HttpServletResponse response )
148 {
149 String strBlobKey = request.getParameter( BlobStoreLibConstants.PARAMETER_BLOB_KEY );
150 String strBlobstore = request.getParameter( BlobStoreLibConstants.PARAMETER_BLOB_STORE );
151
152 IBlobStoreService blobstoreService;
153
154 try
155 {
156 blobstoreService = (IBlobStoreService) SpringContextService.getBean( strBlobstore );
157 }
158 catch( NoSuchBeanDefinitionException ex )
159 {
160 blobstoreService = null;
161 }
162
163 String strErrorMessage = null;
164
165 if ( blobstoreService != null )
166 {
167 if ( StringUtils.isNotBlank( strBlobKey ) )
168 {
169
170
171
172 InputStream is = blobstoreService.getBlobInputStream( strBlobKey );
173
174 if ( is != null )
175 {
176 DownloadableFile file = new DownloadableFile( );
177 file.setFileName( strBlobKey );
178 file.setInputStream( is );
179
180 strErrorMessage = writeFile( request, response, file );
181 }
182 else
183 {
184
185 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_RETRIEVING_BLOB, request.getLocale( ) );
186 }
187 }
188 else
189 {
190
191 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_NO_SUCH_BLOB, request.getLocale( ) );
192 }
193 }
194 else
195 {
196
197 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_NO_SUCH_BLOBSTORE, request.getLocale( ) );
198 }
199
200
201 return strErrorMessage;
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215 private String writeFile( HttpServletRequest request, HttpServletResponse response, DownloadableFile file )
216 {
217 String strErrorMessage = null;
218 OutputStream os = null;
219 InputStream is = file.getInputStream( );
220
221 try
222 {
223 response.setHeader( "Content-Disposition", "attachment ;filename=\"" + file.getFileName( ) );
224 response.setHeader( "Pragma", "public" );
225 response.setHeader( "Expires", "0" );
226 response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
227
228 if ( file.getContentType( ) != null )
229 {
230 response.setContentType( file.getContentType( ) );
231 }
232 else
233 {
234 response.setContentType( "application/force-download" );
235 }
236
237 if ( file.getSize( ) != 0 )
238 {
239 response.setContentLength( file.getSize( ) );
240 }
241
242 os = response.getOutputStream( );
243 IOUtils.copy( is, os );
244 }
245 catch( IOException ioe )
246 {
247 AppLogService.error( ioe.getMessage( ), ioe );
248
249 strErrorMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_RETRIEVING_BLOB, request.getLocale( ) );
250 }
251 finally
252 {
253 IOUtils.closeQuietly( is );
254 IOUtils.closeQuietly( os );
255 }
256
257 return strErrorMessage;
258 }
259
260
261
262
263
264
265 private static class DownloadableFile
266 {
267 private String _strFileName;
268 private String _strContentType;
269 private int _nSize;
270 private InputStream _inputStream;
271
272
273
274
275
276
277 public InputStream getInputStream( )
278 {
279 return _inputStream;
280 }
281
282
283
284
285
286
287
288 public void setInputStream( InputStream inputStream )
289 {
290 _inputStream = inputStream;
291 }
292
293
294
295
296
297
298 public String getFileName( )
299 {
300 return _strFileName;
301 }
302
303
304
305
306
307
308
309 public void setFileName( String strFileName )
310 {
311 _strFileName = strFileName;
312 }
313
314
315
316
317
318
319 public int getSize( )
320 {
321 return _nSize;
322 }
323
324
325
326
327
328
329
330 public void setSize( int nSize )
331 {
332 _nSize = nSize;
333 }
334
335
336
337
338
339
340 public String getContentType( )
341 {
342 return _strContentType;
343 }
344
345
346
347
348
349
350
351 public void setContentType( String strContentType )
352 {
353 _strContentType = strContentType;
354 }
355 }
356 }