View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * Handles asynchronous uploads.
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       * {@inheritDoc}
68       * 
69       * @throws IOException
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                  // add to existing array
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      * Gets the handler implementing the new version IAsynchronousUploadHandler2
123      *
124      * @param request
125      *            the request
126      * @return the handler found, <code>null</code> otherwise.
127      * @see IAsynchronousUploadHandler2#isInvoked(HttpServletRequest)
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 }