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.service;
35
36 import net.sf.json.JSONException;
37 import net.sf.json.JSONObject;
38
39 import org.apache.commons.fileupload.FileItem;
40
41 import org.apache.logging.log4j.LogManager;
42 import org.apache.logging.log4j.Logger;
43
44 import java.io.File;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.OutputStream;
48 import java.io.UnsupportedEncodingException;
49 import org.apache.commons.fileupload.FileItemHeaders;
50
51
52
53
54
55
56
57
58
59
60 public class BlobStoreFileItem implements FileItem
61 {
62 public static final String JSON_KEY_FILE_SIZE = "fileSize";
63 public static final String JSON_KEY_FILE_NAME = "fileName";
64 public static final String JSON_KEY_FILE_CONTENT_TYPE = "fileContentType";
65 public static final String JSON_KEY_FILE_BLOB_ID = "fileBlobId";
66 public static final String JSON_KEY_FILE_METADATA_BLOB_ID = "fileMetadata";
67 private static final long serialVersionUID = 1L;
68 private static Logger _logger = LogManager.getLogger( "lutece.blobstore" );
69 private final IBlobStoreService _blobstoreService;
70 private final String _strBlobId;
71 private String _strFileName;
72 private long _lFileSize;
73 private String _strFileBlobId;
74 private String _strContentType;
75
76
77
78
79
80
81
82
83
84
85
86
87 public BlobStoreFileItem( String strBlobId, IBlobStoreService blobstoreService ) throws NoSuchBlobException
88 {
89 _strBlobId = strBlobId;
90 _blobstoreService = blobstoreService;
91
92
93 byte [ ] blob = _blobstoreService.getBlob( _strBlobId );
94
95 if ( blob == null )
96 {
97 throw new NoSuchBlobException( "No blob found for id " + strBlobId );
98 }
99
100 JSONObject jsonObject = parseBlob( blob );
101
102 if ( jsonObject != null )
103 {
104 String strSize = (String) jsonObject.get( JSON_KEY_FILE_SIZE );
105 _lFileSize = Long.parseLong( strSize );
106 _strFileName = (String) jsonObject.get( JSON_KEY_FILE_NAME );
107
108 _strFileBlobId = (String) jsonObject.get( JSON_KEY_FILE_BLOB_ID );
109 _strContentType = jsonObject.getString( JSON_KEY_FILE_CONTENT_TYPE );
110 }
111 else
112 {
113 throw new NoSuchBlobException( strBlobId );
114 }
115 }
116
117
118
119
120
121
122 public String getBlobId( )
123 {
124 return _strBlobId;
125 }
126
127
128
129
130
131
132 public String getFileBlobId( )
133 {
134 return _strFileBlobId;
135 }
136
137
138
139
140 @Override
141 public void delete( )
142 {
143 _blobstoreService.delete( _strFileBlobId );
144 _blobstoreService.delete( _strBlobId );
145 }
146
147
148
149
150 @Override
151 public byte [ ] get( )
152 {
153 return _blobstoreService.getBlob( _strFileBlobId );
154 }
155
156
157
158
159 @Override
160 public String getContentType( )
161 {
162 return _strContentType;
163 }
164
165
166
167
168
169
170 @Override
171 public String getFieldName( )
172 {
173 return null;
174 }
175
176
177
178
179
180
181
182 @Override
183 public InputStream getInputStream( ) throws IOException
184 {
185 return _blobstoreService.getBlobInputStream( _strFileBlobId );
186 }
187
188
189
190
191 @Override
192 public String getName( )
193 {
194 return _strFileName;
195 }
196
197
198
199
200
201
202
203
204 @Override
205 public OutputStream getOutputStream( ) throws IOException
206 {
207 throw new UnsupportedOperationException( );
208 }
209
210
211
212
213 @Override
214 public long getSize( )
215 {
216 return _lFileSize;
217 }
218
219
220
221
222 @Override
223 public String getString( )
224 {
225 return new String( get( ) );
226 }
227
228
229
230
231 @Override
232 public String getString( String encoding ) throws UnsupportedEncodingException
233 {
234 return new String( get( ), encoding );
235 }
236
237
238
239
240
241
242 @Override
243 public boolean isFormField( )
244 {
245 return false;
246 }
247
248
249
250
251
252
253 @Override
254 public boolean isInMemory( )
255 {
256 return false;
257 }
258
259
260
261
262
263
264
265 @Override
266 public void setFieldName( String name )
267 {
268
269 }
270
271
272
273
274
275
276
277 @Override
278 public void setFormField( boolean state )
279 {
280
281 }
282
283
284
285
286
287
288
289
290
291 @Override
292 public void write( File file ) throws Exception
293 {
294 throw new UnsupportedOperationException( );
295 }
296
297
298
299
300 @Override
301 public String toString( )
302 {
303 return "BlobId:" + _strBlobId + " FileBlobId:" + _strFileBlobId + " FileName:" + _strFileName;
304 }
305
306
307
308
309
310
311
312
313 private static JSONObject parseBlob( byte [ ] blob )
314 {
315 if ( blob == null )
316 {
317 return null;
318 }
319
320 try
321 {
322 return JSONObject.fromObject( new String( blob ) );
323 }
324 catch( JSONException je )
325 {
326 _logger.error( je.getMessage( ), je );
327 }
328
329 return null;
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 public static final String buildFileMetadata( String strFileName, long lSize, String strFileBlobId, String strContentType )
346 {
347 JSONObject json = new JSONObject( );
348 json.accumulate( BlobStoreFileItem.JSON_KEY_FILE_SIZE, Long.toString( lSize ) );
349 json.accumulate( BlobStoreFileItem.JSON_KEY_FILE_NAME, strFileName );
350 json.accumulate( BlobStoreFileItem.JSON_KEY_FILE_BLOB_ID, strFileBlobId );
351 json.accumulate( BlobStoreFileItem.JSON_KEY_FILE_CONTENT_TYPE, strContentType );
352
353 return json.toString( );
354 }
355
356 @Override
357 public FileItemHeaders getHeaders() {
358 throw new UnsupportedOperationException("Not supported yet.");
359 }
360
361 @Override
362 public void setHeaders(FileItemHeaders fih) {
363 throw new UnsupportedOperationException("Not supported yet.");
364 }
365 }