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.directory.service.upload;
35
36 import fr.paris.lutece.plugins.blobstore.service.BlobStoreClientException;
37 import fr.paris.lutece.plugins.blobstore.service.IBlobStoreClientService;
38 import fr.paris.lutece.plugins.directory.business.EntryHome;
39 import fr.paris.lutece.plugins.directory.business.EntryTypeDownloadUrl;
40 import fr.paris.lutece.plugins.directory.business.File;
41 import fr.paris.lutece.plugins.directory.business.IEntry;
42 import fr.paris.lutece.plugins.directory.business.PhysicalFile;
43 import fr.paris.lutece.plugins.directory.business.PhysicalFileHome;
44 import fr.paris.lutece.plugins.directory.business.Record;
45 import fr.paris.lutece.plugins.directory.business.RecordField;
46 import fr.paris.lutece.plugins.directory.business.RecordFieldFilter;
47 import fr.paris.lutece.plugins.directory.business.RecordFieldHome;
48 import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
49 import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
50 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
51 import fr.paris.lutece.plugins.directory.utils.JSONUtils;
52 import fr.paris.lutece.plugins.directory.utils.UrlUtils;
53 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
54 import fr.paris.lutece.portal.service.i18n.I18nService;
55 import fr.paris.lutece.portal.service.plugin.Plugin;
56 import fr.paris.lutece.portal.service.plugin.PluginService;
57 import fr.paris.lutece.portal.service.spring.SpringContextService;
58 import fr.paris.lutece.portal.service.util.AppException;
59 import fr.paris.lutece.portal.service.util.AppLogService;
60 import fr.paris.lutece.portal.web.upload.IAsynchronousUploadHandler;
61 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
62 import fr.paris.lutece.util.filesystem.FileSystemUtil;
63 import fr.paris.lutece.util.filesystem.UploadUtil;
64
65 import net.sf.json.JSONObject;
66
67 import org.apache.commons.fileupload.FileItem;
68 import org.apache.commons.fileupload.FileItemHeaders;
69 import org.apache.commons.lang.StringUtils;
70
71 import java.io.ByteArrayInputStream;
72 import java.io.IOException;
73 import java.io.InputStream;
74 import java.io.OutputStream;
75 import java.io.UnsupportedEncodingException;
76
77 import java.util.ArrayList;
78 import java.util.Collections;
79 import java.util.Enumeration;
80 import java.util.Iterator;
81 import java.util.List;
82 import java.util.Locale;
83 import java.util.Map;
84 import java.util.concurrent.ConcurrentHashMap;
85
86 import javax.servlet.http.HttpServletRequest;
87 import javax.servlet.http.HttpServletResponse;
88 import javax.servlet.http.HttpSession;
89
90
91
92
93
94
95 public class DirectoryAsynchronousUploadHandler implements IAsynchronousUploadHandler
96 {
97
98
99
100
101 public static Map<String, Map<String, List<FileItem>>> _mapAsynchronousUpload = new ConcurrentHashMap<String, Map<String, List<FileItem>>>( );
102 private static final String BEAN_DIRECTORY_ASYNCHRONOUS_UPLOAD_HANDLER = "directory.asynchronousUploadHandler";
103 private static final String PREFIX_ENTRY_ID = "directory_";
104
105
106 private static final String UPLOAD_SUBMIT_PREFIX = "_directory_upload_submit_directory_";
107 private static final String UPLOAD_DELETE_PREFIX = "_directory_upload_delete_directory_";
108 private static final String UPLOAD_CHECKBOX_PREFIX = "_directory_upload_checkbox_directory_";
109
110
111 private static final String PARAMETER_BLOB_KEY = "blob_key";
112 private static final String PARAMETER_BLOBSTORE = "blobstore";
113 private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
114 private static final String PARAMETER_FIELD_NAME = "field_name";
115 private static final String PARAMETER_JSESSION_ID = "jsessionid";
116
117
118 private static final String PROPERTY_MESSAGE_ERROR_UPLOADING_FILE_SESSION_LOST = "directory.message.error.uploading_file.session_lost";
119 private IBlobStoreClientService _blobStoreClientService;
120
121
122
123
124 private DirectoryAsynchronousUploadHandler( )
125 {
126 }
127
128
129
130
131
132
133 public static DirectoryAsynchronousUploadHandler getHandler( )
134 {
135 return SpringContextService.getBean( BEAN_DIRECTORY_ASYNCHRONOUS_UPLOAD_HANDLER );
136 }
137
138
139
140
141
142
143
144 public void setBlobStoreClientService( IBlobStoreClientService blobStoreClientService )
145 {
146 _blobStoreClientService = blobStoreClientService;
147 }
148
149
150
151
152 @Override
153 public boolean isInvoked( HttpServletRequest request )
154 {
155 return DirectoryPlugin.PLUGIN_NAME.equals( request.getParameter( PARAMETER_PLUGIN_NAME ) );
156 }
157
158
159
160
161
162
163 public boolean isBlobStoreClientServiceAvailable( )
164 {
165 return _blobStoreClientService != null;
166 }
167
168
169
170
171
172
173 @Override
174 public void process( HttpServletRequest request, HttpServletResponse response, JSONObject mainObject, List<FileItem> listFileItemsToUpload )
175 {
176
177 if ( ( listFileItemsToUpload == null ) || listFileItemsToUpload.isEmpty( ) )
178 {
179 throw new AppException( "No file uploaded" );
180 }
181
182 String strIdSession = request.getParameter( PARAMETER_JSESSION_ID );
183
184 if ( StringUtils.isNotBlank( strIdSession ) )
185 {
186 String strFieldName = request.getParameter( PARAMETER_FIELD_NAME );
187
188 if ( StringUtils.isBlank( strFieldName ) )
189 {
190 throw new AppException( "id entry is not provided for the current file upload" );
191 }
192
193 initMap( strIdSession, strFieldName );
194
195
196 Map<String, List<FileItem>> mapFileItemsSession = _mapAsynchronousUpload.get( strIdSession );
197
198 List<FileItem> fileItemsSession = mapFileItemsSession.get( strFieldName );
199
200 if ( canUploadFiles( strFieldName, fileItemsSession, listFileItemsToUpload, mainObject, request.getLocale( ) ) )
201 {
202 fileItemsSession.addAll( listFileItemsToUpload );
203
204 JSONObject jsonListFileItems = JSONUtils.getUploadedFileJSON( fileItemsSession );
205 mainObject.accumulateAll( jsonListFileItems );
206
207 JSONUtils.buildJsonSuccess( strFieldName, mainObject );
208 }
209 }
210 else
211 {
212 AppLogService.error( DirectoryAsynchronousUploadHandler.class.getName( ) + " : Session does not exists" );
213
214 String strMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_UPLOADING_FILE_SESSION_LOST, request.getLocale( ) );
215 JSONUtils.buildJsonError( mainObject, strMessage );
216 }
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 public String doUploadFile( String strBaseUrl, FileItem fileItem, String strBlobStore ) throws BlobStoreClientException
233 {
234 if ( isBlobStoreClientServiceAvailable( ) )
235 {
236 return _blobStoreClientService.doUploadFile( strBaseUrl, fileItem, strBlobStore );
237 }
238
239 return StringUtils.EMPTY;
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public void doRemoveFile( Record record, IEntry entry, String strWSRestUrl ) throws BlobStoreClientException
255 {
256 Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
257 RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
258 recordFieldFilter.setIdDirectory( record.getDirectory( ).getIdDirectory( ) );
259 recordFieldFilter.setIdEntry( entry.getIdEntry( ) );
260 recordFieldFilter.setIdRecord( record.getIdRecord( ) );
261
262 List<RecordField> listRecordFields = RecordFieldHome.getRecordFieldList( recordFieldFilter, pluginDirectory );
263
264 if ( ( listRecordFields != null ) && !listRecordFields.isEmpty( ) )
265 {
266 for ( RecordField recordField : listRecordFields )
267 {
268 doRemoveFile( recordField, entry, strWSRestUrl );
269 }
270 }
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public void doRemoveFile( RecordField recordField, IEntry entry, String strWSRestUrl ) throws BlobStoreClientException
286 {
287 if ( isBlobStoreClientServiceAvailable( ) && ( recordField != null ) )
288 {
289
290 String strDownloadFileUrl = entry.convertRecordFieldTitleToString( recordField, null, false );
291
292 if ( StringUtils.isNotBlank( strDownloadFileUrl ) )
293 {
294
295 Map<String, List<String>> mapParameters = UrlUtils.getMapParametersFromUrl( strDownloadFileUrl );
296 List<String> parameterBlobKey = mapParameters.get( PARAMETER_BLOB_KEY );
297 List<String> parameterBlobStore = mapParameters.get( PARAMETER_BLOBSTORE );
298
299 if ( ( parameterBlobKey != null ) && !parameterBlobKey.isEmpty( ) && ( parameterBlobStore != null ) && !parameterBlobStore.isEmpty( ) )
300 {
301 String strBlobKey = parameterBlobKey.get( 0 );
302 String strBlobStore = parameterBlobStore.get( 0 );
303 _blobStoreClientService.doDeleteFile( strWSRestUrl, strBlobStore, strBlobKey );
304 }
305 }
306 }
307 }
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322 public String getFileUrl( String strBaseUrl, String strBlobKey, String strBlobStore ) throws BlobStoreClientException
323 {
324 if ( isBlobStoreClientServiceAvailable( ) )
325 {
326 return _blobStoreClientService.getFileUrl( strBaseUrl, strBlobKey, strBlobStore );
327 }
328
329 return StringUtils.EMPTY;
330 }
331
332
333
334
335
336
337
338
339
340
341 public String getFileName( String strUrl ) throws BlobStoreClientException
342 {
343 if ( isBlobStoreClientServiceAvailable( ) )
344 {
345 return _blobStoreClientService.getFileName( strUrl );
346 }
347
348 return StringUtils.EMPTY;
349 }
350
351
352
353
354
355
356
357
358
359
360
361 public void doDownloadFile( String strUrl, String strFilePath ) throws BlobStoreClientException
362 {
363 if ( isBlobStoreClientServiceAvailable( ) )
364 {
365 _blobStoreClientService.doDownloadFile( strUrl, strFilePath );
366 }
367 }
368
369
370
371
372
373
374
375
376
377
378 public FileItem doDownloadFile( String strUrl ) throws BlobStoreClientException
379 {
380 if ( isBlobStoreClientServiceAvailable( ) )
381 {
382 return _blobStoreClientService.doDownloadFile( strUrl );
383 }
384
385 return null;
386 }
387
388
389
390
391
392
393
394
395
396
397 public List<FileItem> getFileItems( String strIdEntry, String strSessionId )
398 {
399 initMap( strSessionId, buildFieldName( strIdEntry ) );
400
401 if ( StringUtils.isBlank( strIdEntry ) )
402 {
403 throw new AppException( "id entry is not provided for the current file upload" );
404 }
405
406
407 Map<String, List<FileItem>> mapFileItemsSession = _mapAsynchronousUpload.get( strSessionId );
408
409 return mapFileItemsSession.get( buildFieldName( strIdEntry ) );
410 }
411
412
413
414
415
416
417
418
419
420
421
422 public synchronized void removeFileItem( String strIdEntry, String strSessionId, int nIndex )
423 {
424
425 List<FileItem> uploadedFiles = getFileItems( strIdEntry, strSessionId );
426
427 if ( ( uploadedFiles != null ) && !uploadedFiles.isEmpty( ) && ( uploadedFiles.size( ) > nIndex ) )
428 {
429
430 FileItem fileItem = uploadedFiles.remove( nIndex );
431 fileItem.delete( );
432 }
433 }
434
435
436
437
438
439
440
441 public synchronized void removeSessionFiles( String strSessionId )
442 {
443 _mapAsynchronousUpload.remove( strSessionId );
444 }
445
446
447
448
449
450
451
452
453
454
455
456 public void addFileItemToUploadedFile( FileItem fileItem, String strIdEntry, HttpSession session )
457 {
458
459
460 String strFileName = UploadUtil.cleanFileName( FileUploadService.getFileNameOnly( fileItem ) );
461
462
463 List<FileItem> uploadedFiles = getFileItems( strIdEntry, session.getId( ) );
464
465 if ( ( uploadedFiles != null ) && !uploadedFiles.isEmpty( ) )
466 {
467 Iterator<FileItem> iterUploadedFiles = uploadedFiles.iterator( );
468 boolean bNew = true;
469
470 while ( bNew && iterUploadedFiles.hasNext( ) )
471 {
472 FileItem uploadedFile = iterUploadedFiles.next( );
473 String strUploadedFileName = UploadUtil.cleanFileName( FileUploadService.getFileNameOnly( uploadedFile ) );
474
475
476
477 bNew = !( strUploadedFileName.equals( strFileName ) && ( uploadedFile.getSize( ) == fileItem.getSize( ) ) );
478 }
479
480 if ( !bNew )
481 {
482
483
484
485
486 }
487 }
488
489 if ( uploadedFiles != null )
490 {
491 uploadedFiles.add( fileItem );
492 }
493 }
494
495
496
497
498
499
500
501
502 public String buildFieldName( String strIdEntry )
503 {
504 return PREFIX_ENTRY_ID + strIdEntry;
505 }
506
507
508
509
510
511
512
513
514 public String getUploadAction( HttpServletRequest request )
515 {
516 Enumeration<String> enumParamNames = request.getParameterNames( );
517
518 while ( enumParamNames.hasMoreElements( ) )
519 {
520 String paramName = enumParamNames.nextElement( );
521
522 if ( paramName.startsWith( UPLOAD_SUBMIT_PREFIX ) || paramName.startsWith( UPLOAD_DELETE_PREFIX ) )
523 {
524 return paramName;
525 }
526 }
527
528 return null;
529 }
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547 public void doUploadAction( HttpServletRequest request, String strUploadAction, Map<String, List<RecordField>> map, Record record, Plugin plugin )
548 throws DirectoryErrorException
549 {
550 String strIdEntry = findIdEntryFromAction( strUploadAction );
551
552 if ( strUploadAction.startsWith( UPLOAD_SUBMIT_PREFIX ) )
553 {
554 doUploadFile( request, strIdEntry, map, record );
555 }
556 else
557 if ( strUploadAction.startsWith( UPLOAD_DELETE_PREFIX ) )
558 {
559 doDeleteFile( request, strIdEntry, map );
560 }
561 }
562
563
564
565
566
567
568
569
570 private String findIdEntryFromAction( String strAction )
571 {
572 return strAction.startsWith( UPLOAD_SUBMIT_PREFIX ) ? strAction.substring( UPLOAD_SUBMIT_PREFIX.length( ) ) : strAction.substring( UPLOAD_DELETE_PREFIX
573 .length( ) );
574 }
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590 public void doUploadFile( HttpServletRequest request, String strIdEntry, Map<String, List<RecordField>> map, Record record ) throws DirectoryErrorException
591 {
592 FileItem fileUploaded = findUploadedFileFromRequest( request, strIdEntry );
593
594 if ( fileUploaded != null && StringUtils.isNotBlank( fileUploaded.getName( ) ) )
595 {
596 canUploadFile( request, fileUploaded, strIdEntry );
597
598 List<RecordField> listRecordFields = findRecordFields( map, strIdEntry );
599 RecordField recordFieldForUploadedFile = createRecordFieldForUploadedFile( fileUploaded, strIdEntry );
600 addRecordFieldForUploadedFile( recordFieldForUploadedFile, record, listRecordFields );
601 addFileItemToUploadedFile( fileUploaded, strIdEntry, request.getSession( ) );
602 }
603 }
604
605
606
607
608
609
610
611
612
613
614 private FileItem findUploadedFileFromRequest( HttpServletRequest request, String strIdEntry )
615 {
616 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
617 String strFileParameter = buildFieldName( strIdEntry );
618
619 return multipartRequest.getFile( strFileParameter );
620 }
621
622
623
624
625
626
627
628
629
630
631
632
633
634 private void canUploadFile( HttpServletRequest request, FileItem fileItem, String strIdEntry ) throws DirectoryErrorException
635 {
636 HttpSession session = request.getSession( );
637 String strFieldName = buildFieldName( strIdEntry );
638 List<FileItem> listFileItemsToUpload = new ArrayList<FileItem>( );
639 listFileItemsToUpload.add( fileItem );
640
641 canUploadFiles( strFieldName, getFileItems( strIdEntry, session.getId( ) ), listFileItemsToUpload, request.getLocale( ) );
642 }
643
644
645
646
647
648
649
650
651
652
653 private List<RecordField> findRecordFields( Map<String, List<RecordField>> map, String strIdEntry )
654 {
655 List<RecordField> listRecordFields = map.get( strIdEntry );
656
657 if ( listRecordFields == null )
658 {
659 listRecordFields = new ArrayList<RecordField>( );
660 map.put( strIdEntry, listRecordFields );
661 }
662
663 return listRecordFields;
664 }
665
666
667
668
669
670
671
672
673
674
675 private RecordField createRecordFieldForUploadedFile( FileItem fileItem, String strIdEntry )
676 {
677 RecordField recordFieldForUploadedFile = null;
678 Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
679
680
681 IEntry entry = EntryHome.findByPrimaryKey( DirectoryUtils.convertStringToInt( strIdEntry ), plugin );
682
683 if ( entry != null )
684 {
685 recordFieldForUploadedFile = new RecordField( );
686 recordFieldForUploadedFile.setEntry( entry );
687
688 String strFilename = FileUploadService.getFileNameOnly( fileItem );
689
690 if ( ( fileItem.get( ) != null ) && ( fileItem.getSize( ) < Integer.MAX_VALUE ) )
691 {
692 if ( entry instanceof EntryTypeDownloadUrl )
693 {
694 recordFieldForUploadedFile.setFileName( strFilename );
695 recordFieldForUploadedFile.setFileExtension( FileSystemUtil.getMIMEType( strFilename ) );
696 }
697 else
698 {
699 PhysicalFile physicalFile = new PhysicalFile( );
700 physicalFile.setValue( fileItem.get( ) );
701
702 File file = new File( );
703 file.setPhysicalFile( physicalFile );
704 file.setTitle( strFilename );
705 file.setSize( (int) fileItem.getSize( ) );
706 file.setMimeType( FileSystemUtil.getMIMEType( strFilename ) );
707
708 recordFieldForUploadedFile.setFile( file );
709 }
710 }
711 }
712
713 return recordFieldForUploadedFile;
714 }
715
716
717
718
719
720
721
722
723
724
725
726 private void addRecordFieldForUploadedFile( RecordField recordFieldForUploadedFile, Record record, List<RecordField> listRecordFields )
727 {
728 if ( recordFieldForUploadedFile != null )
729 {
730 recordFieldForUploadedFile.setRecord( record );
731 listRecordFields.add( recordFieldForUploadedFile );
732 }
733 }
734
735
736
737
738
739
740
741
742
743
744
745 public void doDeleteFile( HttpServletRequest request, String strIdEntry, Map<String, List<RecordField>> map )
746 {
747 HttpSession session = request.getSession( false );
748
749 if ( session != null )
750 {
751
752
753 String strPrefix = UPLOAD_CHECKBOX_PREFIX + strIdEntry;
754
755
756 Enumeration<String> enumParamNames = request.getParameterNames( );
757 List<Integer> listIndexes = new ArrayList<Integer>( );
758
759 while ( enumParamNames.hasMoreElements( ) )
760 {
761 String strParamName = enumParamNames.nextElement( );
762
763 if ( strParamName.startsWith( strPrefix ) )
764 {
765
766 listIndexes.add( Integer.parseInt( strParamName.substring( strPrefix.length( ) ) ) );
767 }
768 }
769
770 Collections.sort( listIndexes );
771 Collections.reverse( listIndexes );
772
773 for ( int nIndex : listIndexes )
774 {
775
776 List<RecordField> listRecordFields = map.get( strIdEntry );
777
778 if ( listRecordFields != null )
779 {
780 listRecordFields.remove( nIndex );
781 }
782
783
784 removeFileItem( strIdEntry, session.getId( ), nIndex );
785 }
786 }
787 }
788
789
790
791
792
793
794
795
796
797
798
799
800
801 public void doUploadAction( HttpServletRequest request, String strUploadAction, Map<String, List<RecordField>> map ) throws DirectoryErrorException
802 {
803 String strIdEntry = findIdEntryFromAction( strUploadAction );
804
805 if ( strUploadAction.startsWith( UPLOAD_SUBMIT_PREFIX ) )
806 {
807 doUploadFile( request, strIdEntry, map );
808 }
809 else
810 if ( strUploadAction.startsWith( UPLOAD_DELETE_PREFIX ) )
811 {
812 doDeleteFile( request, strIdEntry, map );
813 }
814 }
815
816
817
818
819
820
821
822
823
824
825
826
827
828 public void doUploadFile( HttpServletRequest request, String strIdEntry, Map<String, List<RecordField>> map ) throws DirectoryErrorException
829 {
830 FileItem fileUploaded = findUploadedFileFromRequest( request, strIdEntry );
831
832 if ( fileUploaded != null && StringUtils.isNotBlank( fileUploaded.getName( ) ) )
833 {
834 canUploadFile( request, fileUploaded, strIdEntry );
835
836 addFileItemToUploadedFile( fileUploaded, strIdEntry, request.getSession( ) );
837 }
838 }
839
840
841
842
843
844
845
846
847
848
849
850 public void reinitMap( HttpServletRequest request, Map<String, List<RecordField>> map, Plugin plugin )
851 {
852 HttpSession session = request.getSession( );
853 removeSessionFiles( session.getId( ) );
854
855 if ( ( map != null ) && !map.isEmpty( ) )
856 {
857 for ( java.util.Map.Entry<String, List<RecordField>> param : map.entrySet( ) )
858 {
859 for ( RecordField recordField : param.getValue( ) )
860 {
861 if ( recordField != null )
862 {
863 IEntry entry = recordField.getEntry( );
864
865 if ( ( recordField.getFile( ) != null ) && ( recordField.getFile( ).getPhysicalFile( ) != null ) && !recordField.isLittleThumbnail( )
866 && !recordField.isBigThumbnail( ) )
867 {
868
869 File file = recordField.getFile( );
870 PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ), plugin );
871 FileItem fileItem = new DirectoryFileItem( physicalFile.getValue( ), file.getTitle( ) );
872
873 addFileItemToUploadedFile( fileItem, Integer.toString( entry.getIdEntry( ) ), session );
874 }
875 else
876 if ( recordField.getEntry( ) instanceof EntryTypeDownloadUrl && isBlobStoreClientServiceAvailable( ) )
877 {
878
879 FileItem fileItem;
880
881 try
882 {
883 fileItem = doDownloadFile( recordField.getValue( ) );
884
885 FileItem directoryFileItem = new DirectoryFileItem( fileItem.get( ), fileItem.getName( ) );
886
887 addFileItemToUploadedFile( directoryFileItem, Integer.toString( entry.getIdEntry( ) ), session );
888 }
889 catch( BlobStoreClientException e )
890 {
891 AppLogService.error( DirectoryAsynchronousUploadHandler.class.getName( ) + " - Error when reinit map. Cause : "
892 + e.getMessage( ) );
893 }
894 }
895 }
896 }
897 }
898 }
899 }
900
901
902
903
904
905
906
907
908
909 private void initMap( String strSessionId, String strFieldName )
910 {
911
912 Map<String, List<FileItem>> mapFileItemsSession = _mapAsynchronousUpload.get( strSessionId );
913
914
915 if ( mapFileItemsSession == null )
916 {
917 synchronized( _mapAsynchronousUpload )
918 {
919 if ( _mapAsynchronousUpload.get( strSessionId ) == null )
920 {
921 mapFileItemsSession = new ConcurrentHashMap<String, List<FileItem>>( );
922 _mapAsynchronousUpload.put( strSessionId, mapFileItemsSession );
923 }
924 }
925 }
926
927 if ( mapFileItemsSession != null )
928 {
929 List<FileItem> listFileItems = mapFileItemsSession.get( strFieldName );
930
931 if ( listFileItems == null )
932 {
933 listFileItems = new ArrayList<FileItem>( );
934 mapFileItemsSession.put( strFieldName, listFileItems );
935 }
936 }
937 }
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955 private boolean canUploadFiles( String strFieldName, List<FileItem> listUploadedFileItems, List<FileItem> listFileItemsToUpload, JSONObject mainObject,
956 Locale locale )
957 {
958 if ( StringUtils.isNotBlank( strFieldName ) )
959 {
960 String strIdEntry = strFieldName.substring( PREFIX_ENTRY_ID.length( ) );
961 int nIdEntry = DirectoryUtils.convertStringToInt( strIdEntry );
962 IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, DirectoryUtils.getPlugin( ) );
963
964 if ( entry != null )
965 {
966 try
967 {
968 entry.canUploadFiles( listUploadedFileItems, listFileItemsToUpload, locale );
969 }
970 catch( DirectoryErrorException e )
971 {
972 JSONUtils.buildJsonError( mainObject, e.getErrorMessage( ) );
973
974 return false;
975 }
976
977 return true;
978 }
979 }
980
981 return false;
982 }
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998 private void canUploadFiles( String strFieldName, List<FileItem> listUploadedFileItems, List<FileItem> listFileItemsToUpload, Locale locale )
999 throws DirectoryErrorException
1000 {
1001 if ( StringUtils.isNotBlank( strFieldName ) )
1002 {
1003 String strIdEntry = strFieldName.substring( PREFIX_ENTRY_ID.length( ) );
1004 int nIdEntry = DirectoryUtils.convertStringToInt( strIdEntry );
1005 IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, DirectoryUtils.getPlugin( ) );
1006
1007 if ( entry != null )
1008 {
1009 entry.canUploadFiles( listUploadedFileItems, listFileItemsToUpload, locale );
1010 }
1011 }
1012 }
1013
1014
1015
1016
1017
1018
1019 private static class DirectoryFileItem implements FileItem
1020 {
1021 private static final long serialVersionUID = 1L;
1022 private byte [ ] _bValue;
1023 private final String _strFileName;
1024 private FileItemHeaders _fileItemHeaders;
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034 public DirectoryFileItem( byte [ ] bValue, String strFileName )
1035 {
1036 _bValue = bValue;
1037 _strFileName = strFileName;
1038 }
1039
1040
1041
1042
1043 @Override
1044 public void delete( )
1045 {
1046 _bValue = null;
1047 }
1048
1049
1050
1051
1052 @Override
1053 public byte [ ] get( )
1054 {
1055 return _bValue;
1056 }
1057
1058
1059
1060
1061 @Override
1062 public String getContentType( )
1063 {
1064 return FileSystemUtil.getMIMEType( _strFileName );
1065 }
1066
1067
1068
1069
1070 @Override
1071 public String getFieldName( )
1072 {
1073 return null;
1074 }
1075
1076
1077
1078
1079 @Override
1080 public InputStream getInputStream( ) throws IOException
1081 {
1082 return new ByteArrayInputStream( _bValue );
1083 }
1084
1085
1086
1087
1088 @Override
1089 public String getName( )
1090 {
1091 return _strFileName;
1092 }
1093
1094
1095
1096
1097 @Override
1098 public OutputStream getOutputStream( ) throws IOException
1099 {
1100 return null;
1101 }
1102
1103
1104
1105
1106 @Override
1107 public long getSize( )
1108 {
1109 return _bValue.length;
1110 }
1111
1112
1113
1114
1115 @Override
1116 public String getString( )
1117 {
1118 return new String( _bValue );
1119 }
1120
1121
1122
1123
1124 @Override
1125 public String getString( String encoding ) throws UnsupportedEncodingException
1126 {
1127 return new String( _bValue, encoding );
1128 }
1129
1130
1131
1132
1133 @Override
1134 public boolean isFormField( )
1135 {
1136 return false;
1137 }
1138
1139
1140
1141
1142 @Override
1143 public boolean isInMemory( )
1144 {
1145 return true;
1146 }
1147
1148
1149
1150
1151 @Override
1152 public void setFieldName( String strName )
1153 {
1154
1155 }
1156
1157
1158
1159
1160 @Override
1161 public void setFormField( boolean bState )
1162 {
1163
1164 }
1165
1166
1167
1168
1169 @Override
1170 public void write( java.io.File file ) throws Exception
1171 {
1172
1173 }
1174
1175
1176
1177
1178 @Override
1179 public FileItemHeaders getHeaders( )
1180 {
1181 return _fileItemHeaders;
1182 }
1183
1184
1185
1186
1187 @Override
1188 public void setHeaders( FileItemHeaders headers )
1189 {
1190 _fileItemHeaders = headers;
1191 }
1192 }
1193 }