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.asynchronousupload.service;
35
36 import java.util.ArrayList;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.Map;
41 import java.util.concurrent.ConcurrentHashMap;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpSession;
45
46 import org.apache.commons.fileupload.FileItem;
47 import org.apache.commons.lang3.StringUtils;
48
49 import fr.paris.lutece.portal.service.i18n.I18nService;
50 import fr.paris.lutece.portal.service.spring.SpringContextService;
51 import fr.paris.lutece.portal.service.util.AppException;
52 import fr.paris.lutece.util.filesystem.UploadUtil;
53
54 public class AsynchronousUploadHandler extends AbstractAsynchronousUploadHandler
55 {
56
57 private static final String BEAN_NAME = "asynchronous-upload.asynchronousUploadHandler";
58 private static final String HANDLER_NAME = "asynchronousUploadHandler";
59
60
61 private static final String ERROR_MESSAGE_UNKNOWN_ERROR = "asynchronousupload.message.unknownError";
62
63
64 private static Map<String, Map<String, List<FileItem>>> _mapAsynchronousUpload = new ConcurrentHashMap<>( );
65
66
67
68
69
70
71 public static AsynchronousUploadHandler getHandler( )
72 {
73 return SpringContextService.getBean( BEAN_NAME );
74 }
75
76 @Override
77 public String canUploadFiles( HttpServletRequest request, String strFieldName, List<FileItem> listFileItemsToUpload, Locale locale )
78 {
79 if ( StringUtils.isNotBlank( strFieldName ) )
80 {
81 String sessionId = getCustomSessionId( request.getSession( ) );
82 initMap( sessionId, strFieldName );
83
84 return null;
85 }
86 return I18nService.getLocalizedString( ERROR_MESSAGE_UNKNOWN_ERROR, locale );
87 }
88
89 @Override
90 public List<FileItem> getListUploadedFiles( String strFieldName, HttpSession session )
91 {
92 if ( StringUtils.isBlank( strFieldName ) )
93 {
94 throw new AppException( "id field name is not provided for the current file upload" );
95 }
96
97 String sessionId = getCustomSessionId( session );
98
99 initMap( sessionId, strFieldName );
100
101
102 Map<String, List<FileItem>> mapFileItemsSession = _mapAsynchronousUpload.get( sessionId );
103
104 return mapFileItemsSession.get( strFieldName );
105 }
106
107 @Override
108 public void addFileItemToUploadedFilesList( FileItem fileItem, String strFieldName, HttpServletRequest request )
109 {
110
111
112 String strFileName = UploadUtil.cleanFileName( fileItem.getName( ).trim( ) );
113
114 String sessionId = getCustomSessionId( request.getSession( ) );
115 initMap( sessionId, strFieldName );
116
117
118 List<FileItem> uploadedFiles = getListUploadedFiles( strFieldName, request.getSession( ) );
119
120 if ( uploadedFiles != null )
121 {
122 boolean bNew = true;
123
124 if ( !uploadedFiles.isEmpty( ) )
125 {
126 Iterator<FileItem> iterUploadedFiles = uploadedFiles.iterator( );
127
128 while ( bNew && iterUploadedFiles.hasNext( ) )
129 {
130 FileItem uploadedFile = iterUploadedFiles.next( );
131 String strUploadedFileName = UploadUtil.cleanFileName( uploadedFile.getName( ).trim( ) );
132
133
134
135 bNew = !( StringUtils.equals( strUploadedFileName, strFileName ) && ( uploadedFile.getSize( ) == fileItem.getSize( ) ) );
136 }
137 }
138
139 if ( bNew )
140 {
141 uploadedFiles.add( fileItem );
142 }
143 }
144 }
145
146 @Override
147 public String getHandlerName( )
148 {
149 return HANDLER_NAME;
150 }
151
152 private void initMap( String strSessionId, String strFieldName )
153 {
154
155 Map<String, List<FileItem>> mapFileItemsSession = _mapAsynchronousUpload.get( strSessionId );
156
157
158 if ( mapFileItemsSession == null )
159 {
160 synchronized( this )
161 {
162
163 mapFileItemsSession = _mapAsynchronousUpload.computeIfAbsent( strSessionId, s -> new ConcurrentHashMap<>( ) );
164 }
165 }
166
167 mapFileItemsSession.computeIfAbsent( strFieldName, s -> new ArrayList<>( ) );
168 }
169
170 @Override
171 public void removeSessionFiles( HttpSession session )
172 {
173 String sessionId = (String) session.getAttribute( PARAM_CUSTOM_SESSION_ID );
174 if ( sessionId != null )
175 {
176 _mapAsynchronousUpload.remove( sessionId );
177 }
178
179 }
180 }