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.portal.web.upload;
35
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Map.Entry;
42
43 import javax.servlet.http.HttpServlet;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
47 import org.apache.commons.fileupload.FileItem;
48
49 import com.fasterxml.jackson.databind.ObjectMapper;
50
51 import fr.paris.lutece.portal.service.spring.SpringContextService;
52 import fr.paris.lutece.portal.service.util.AppLogService;
53
54
55
56
57 public class UploadServlet extends HttpServlet
58 {
59 private static final long serialVersionUID = 1L;
60 private static final String JSON_FILE_SIZE = "fileSize";
61 private static final String JSON_FILE_NAME = "fileName";
62 private static final String JSON_FILES = "files";
63 private static final String JSON_UTF8_CONTENT_TYPE = "application/json; charset=UTF-8";
64 private static final ObjectMapper objectMapper = new ObjectMapper( );
65
66
67
68
69
70
71 @Override
72 protected void doPost( HttpServletRequest req, HttpServletResponse response ) throws IOException
73 {
74 MultipartHttpServletRequestaris/lutece/portal/web/upload/MultipartHttpServletRequest.html#MultipartHttpServletRequest">MultipartHttpServletRequest request = (MultipartHttpServletRequest) req;
75
76 List<FileItem> listFileItems = new ArrayList<>( );
77 Map<String, Object> mapJson = new HashMap<>( );
78 List<Map<String, Object>> listJsonFileMap = new ArrayList<>( );
79 mapJson.put( JSON_FILES, listJsonFileMap );
80
81 for ( Entry<String, List<FileItem>> entry : ( request.getFileListMap( ) ).entrySet( ) )
82 {
83 for ( FileItem fileItem : entry.getValue( ) )
84 {
85 Map<String, Object> jsonFileMap = new HashMap<>( );
86 jsonFileMap.put( JSON_FILE_NAME, fileItem.getName( ) );
87 jsonFileMap.put( JSON_FILE_SIZE, fileItem.getSize( ) );
88
89
90 listJsonFileMap.add( jsonFileMap );
91
92 listFileItems.add( fileItem );
93 }
94 }
95
96 IAsynchronousUploadHandler2 handler2 = getHandler2( request );
97 if ( handler2 != null )
98 {
99 handler2.process( request, response, mapJson, listFileItems );
100 }
101 else
102 {
103 AppLogService.error( "No handler found, removing temporary files" );
104
105 for ( FileItem fileItem : listFileItems )
106 {
107 fileItem.delete( );
108 }
109 }
110
111 String strResultJson = objectMapper.writeValueAsString( mapJson );
112 if ( AppLogService.isDebugEnabled( ) )
113 {
114 AppLogService.debug( "Aysnchronous upload : " + strResultJson );
115 }
116
117 response.setContentType( JSON_UTF8_CONTENT_TYPE );
118 response.getWriter( ).print( strResultJson );
119 }
120
121
122
123
124
125
126
127
128
129 private IAsynchronousUploadHandler2 getHandler2( HttpServletRequest request )
130 {
131 for ( IAsynchronousUploadHandler2 handler : SpringContextService.getBeansOfType( IAsynchronousUploadHandler2.class ) )
132 {
133 if ( handler.isInvoked( request ) )
134 {
135 return handler;
136 }
137 }
138
139 return null;
140 }
141 }