MultipartHttpServletRequest.java

  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. import java.util.Collections;
  36. import java.util.Enumeration;
  37. import java.util.List;
  38. import java.util.Map;

  39. import javax.servlet.http.HttpServletRequest;
  40. import javax.servlet.http.HttpServletRequestWrapper;

  41. import org.apache.commons.collections.CollectionUtils;
  42. import org.apache.commons.fileupload.FileItem;

  43. /**
  44.  * This class provides a Wrapper of an HTTP request that handle multipart content
  45.  */
  46. public class MultipartHttpServletRequest extends HttpServletRequestWrapper
  47. {
  48.     private final Map<String, List<FileItem>> _multipartFiles;
  49.     private final Map<String, String [ ]> _stringParameters;

  50.     /**
  51.      * Constructor
  52.      *
  53.      * @param request
  54.      *            The HTTP request
  55.      * @param multipartFiles
  56.      *            Files
  57.      * @param parameters
  58.      *            Request parameters
  59.      */
  60.     public MultipartHttpServletRequest( HttpServletRequest request, Map<String, List<FileItem>> multipartFiles, Map<String, String [ ]> parameters )
  61.     {
  62.         super( request );
  63.         _multipartFiles = Collections.unmodifiableMap( multipartFiles );
  64.         _stringParameters = Collections.unmodifiableMap( parameters );
  65.     }

  66.     /**
  67.      * Gets parameters names
  68.      *
  69.      * @return An enumeration of parameters names
  70.      */
  71.     @Override
  72.     public Enumeration getParameterNames( )
  73.     {
  74.         return Collections.enumeration( _stringParameters.keySet( ) );
  75.     }

  76.     /**
  77.      * Gets a parameter value
  78.      *
  79.      * @param strName
  80.      *            The parameter name
  81.      * @return The value
  82.      */
  83.     @Override
  84.     public String getParameter( String strName )
  85.     {
  86.         String [ ] values = getParameterValues( strName );

  87.         return ( ( ( values != null ) && ( values.length > 0 ) ) ? values [0] : null );
  88.     }

  89.     /**
  90.      * Gets parameter values
  91.      *
  92.      * @param strName
  93.      *            The parameter name
  94.      * @return An array of values
  95.      */
  96.     @Override
  97.     public String [ ] getParameterValues( String strName )
  98.     {
  99.         return _stringParameters.get( strName );
  100.     }

  101.     /**
  102.      * Gets the parameter map
  103.      *
  104.      * @return A map containing all request parameters
  105.      */
  106.     @Override
  107.     public Map getParameterMap( )
  108.     {
  109.         return _stringParameters;
  110.     }

  111.     /**
  112.      * Gets the list of filenames attached to the request
  113.      *
  114.      * @return The list as an enumeration
  115.      */
  116.     public Enumeration<String> getFileNames( )
  117.     {
  118.         return Collections.enumeration( _multipartFiles.keySet( ) );
  119.     }

  120.     /**
  121.      * Gets a map of all files attached to the request
  122.      *
  123.      * @return The map
  124.      */
  125.     public Map<String, List<FileItem>> getFileListMap( )
  126.     {
  127.         return _multipartFiles;
  128.     }

  129.     /**
  130.      * Gets a file. If several files are available for a given name, then only the first one is returned
  131.      *
  132.      * @param strName
  133.      *            The file name
  134.      * @return The file as a FileItem
  135.      */
  136.     public FileItem getFile( String strName )
  137.     {
  138.         List<FileItem> listFileItem = _multipartFiles.get( strName );

  139.         return CollectionUtils.isNotEmpty( listFileItem ) ? listFileItem.get( 0 ) : null;
  140.     }

  141.     /**
  142.      * Gets a file
  143.      *
  144.      * @param strName
  145.      *            The file name
  146.      * @return The file as a FileItem
  147.      */
  148.     public List<FileItem> getFileList( String strName )
  149.     {
  150.         return _multipartFiles.get( strName );
  151.     }
  152. }