View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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 fr.paris.lutece.portal.service.spring.SpringContextService;
37  import fr.paris.lutece.portal.service.util.AppLogService;
38  
39  import net.sf.json.JSONArray;
40  import net.sf.json.JSONObject;
41  
42  import org.apache.commons.fileupload.FileItem;
43  
44  import java.io.IOException;
45  
46  import java.util.ArrayList;
47  import java.util.List;
48  import java.util.Map.Entry;
49  
50  import javax.servlet.ServletException;
51  import javax.servlet.http.HttpServlet;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  
56  /**
57   * Handles asynchronous uploads.
58   */
59  public class UploadServlet extends HttpServlet
60  {
61      private static final long serialVersionUID = 1L;
62      private static final String JSON_FILE_SIZE = "fileSize";
63      private static final String JSON_FILE_NAME = "fileName";
64      private static final String JSON_FILES = "files";
65      private static final String JSON_UTF8_CONTENT_TYPE = "application/json; charset=UTF-8";
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      protected void doPost( HttpServletRequest req, HttpServletResponse response )
72          throws ServletException, IOException
73      {
74          MultipartHttpServletRequest request = (MultipartHttpServletRequest) req;
75  
76          List<FileItem> listFileItems = new ArrayList<FileItem>(  );
77          JSONObject json = new JSONObject(  );
78          json.element( JSON_FILES, new JSONArray(  ) );
79  
80          for ( Entry<String, List<FileItem>> entry : ( request.getFileListMap(  ) ).entrySet(  ) )
81          {
82              for ( FileItem fileItem : entry.getValue(  ) )
83              {
84                  JSONObject jsonFile = new JSONObject(  );
85                  jsonFile.element( JSON_FILE_NAME, fileItem.getName(  ) );
86                  jsonFile.element( JSON_FILE_SIZE, fileItem.getSize(  ) );
87  
88                  // add to existing array
89                  json.accumulate( JSON_FILES, jsonFile );
90  
91                  listFileItems.add( fileItem );
92              }
93          }
94  
95          IAsynchronousUploadHandler handler = getHandler( request );
96  
97          if ( handler == null )
98          {
99              AppLogService.error( "No handler found, removing temporary files" );
100 
101             for ( FileItem fileItem : listFileItems )
102             {
103                 fileItem.delete(  );
104             }
105         }
106         else
107         {
108             handler.process( request, response, json, listFileItems );
109         }
110 
111         if ( AppLogService.isDebugEnabled(  ) )
112         {
113             AppLogService.debug( "Aysnchronous upload : " + json.toString(  ) );
114         }
115 
116         response.setContentType(JSON_UTF8_CONTENT_TYPE);
117         response.getWriter().print( json.toString(  ) );
118     }
119 
120     /**
121      * Gets the handler
122      * @param request the reques
123      * @return the handler found, <code>null</code> otherwise.
124      * @see IAsynchronousUploadHandler#isInvoked(HttpServletRequest)
125      */
126     private IAsynchronousUploadHandler getHandler( HttpServletRequest request )
127     {
128         for ( IAsynchronousUploadHandler handler : SpringContextService.getBeansOfType( 
129                 IAsynchronousUploadHandler.class ) )
130         {
131             if ( handler.isInvoked( request ) )
132             {
133                 return handler;
134             }
135         }
136 
137         return null;
138     }
139 }