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.util.Collections;
37  import java.util.Enumeration;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletRequestWrapper;
43  
44  import org.apache.commons.collections.CollectionUtils;
45  import org.apache.commons.fileupload.FileItem;
46  
47  /**
48   * This class provides a Wrapper of an HTTP request that handle multipart content
49   */
50  public class MultipartHttpServletRequest extends HttpServletRequestWrapper
51  {
52      private final Map<String, List<FileItem>> _multipartFiles;
53      private final Map<String, String [ ]> _stringParameters;
54  
55      /**
56       * Constructor
57       * 
58       * @param request
59       *            The HTTP request
60       * @param multipartFiles
61       *            Files
62       * @param parameters
63       *            Request parameters
64       */
65      public MultipartHttpServletRequest( HttpServletRequest request, Map<String, List<FileItem>> multipartFiles, Map<String, String [ ]> parameters )
66      {
67          super( request );
68          _multipartFiles = Collections.unmodifiableMap( multipartFiles );
69          _stringParameters = Collections.unmodifiableMap( parameters );
70      }
71  
72      /**
73       * Gets parameters names
74       * 
75       * @return An enumeration of parameters names
76       */
77      @Override
78      public Enumeration getParameterNames( )
79      {
80          return Collections.enumeration( _stringParameters.keySet( ) );
81      }
82  
83      /**
84       * Gets a parameter value
85       * 
86       * @param strName
87       *            The parameter name
88       * @return The value
89       */
90      @Override
91      public String getParameter( String strName )
92      {
93          String [ ] values = getParameterValues( strName );
94  
95          return ( ( ( values != null ) && ( values.length > 0 ) ) ? values [0] : null );
96      }
97  
98      /**
99       * Gets parameter values
100      * 
101      * @param strName
102      *            The parameter name
103      * @return An array of values
104      */
105     @Override
106     public String [ ] getParameterValues( String strName )
107     {
108         return _stringParameters.get( strName );
109     }
110 
111     /**
112      * Gets the parameter map
113      * 
114      * @return A map containing all request parameters
115      */
116     @Override
117     public Map getParameterMap( )
118     {
119         return _stringParameters;
120     }
121 
122     /**
123      * Gets the list of filenames attached to the request
124      * 
125      * @return The list as an enumeration
126      */
127     public Enumeration<String> getFileNames( )
128     {
129         return Collections.enumeration( _multipartFiles.keySet( ) );
130     }
131 
132     /**
133      * Gets a map of all files attached to the request
134      * 
135      * @return The map
136      */
137     public Map<String, List<FileItem>> getFileListMap( )
138     {
139         return _multipartFiles;
140     }
141 
142     /**
143      * Gets a file. If several files are available for a given name, then only the first one is returned
144      * 
145      * @param strName
146      *            The file name
147      * @return The file as a FileItem
148      */
149     public FileItem getFile( String strName )
150     {
151         List<FileItem> listFileItem = _multipartFiles.get( strName );
152 
153         return CollectionUtils.isNotEmpty( listFileItem ) ? listFileItem.get( 0 ) : null;
154     }
155 
156     /**
157      * Gets a file
158      * 
159      * @param strName
160      *            The file name
161      * @return The file as a FileItem
162      */
163     public List<FileItem> getFileList( String strName )
164     {
165         return _multipartFiles.get( strName );
166     }
167 }