NormalizeFileItem.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 fr.paris.lutece.util.filesystem.UploadUtil;

  36. import org.apache.commons.fileupload.FileItem;
  37. import org.apache.commons.fileupload.FileItemHeaders;
  38. import org.apache.commons.io.FilenameUtils;

  39. import java.io.File;
  40. import java.io.IOException;
  41. import java.io.InputStream;
  42. import java.io.OutputStream;
  43. import java.io.UnsupportedEncodingException;

  44. /**
  45.  * This class is used to normalize the file names. This class override the method getName () of FileItem
  46.  */
  47. public class NormalizeFileItem implements FileItem
  48. {
  49.     private static final long serialVersionUID = 8696893066570050604L;
  50.     private FileItem _item;

  51.     /**
  52.      * Instantiates a new normalize file item.
  53.      *
  54.      * @param item
  55.      *            the item
  56.      */
  57.     public NormalizeFileItem( FileItem item )
  58.     {
  59.         _item = item;
  60.     }

  61.     /**
  62.      * {@inheritDoc}
  63.      */
  64.     @Override
  65.     public void delete( )
  66.     {
  67.         _item.delete( );
  68.     }

  69.     /**
  70.      * {@inheritDoc}
  71.      */
  72.     @Override
  73.     public byte [ ] get( )
  74.     {
  75.         return _item.get( );
  76.     }

  77.     /**
  78.      * {@inheritDoc}
  79.      */
  80.     @Override
  81.     public String getContentType( )
  82.     {
  83.         return _item.getContentType( );
  84.     }

  85.     /**
  86.      * {@inheritDoc}
  87.      */
  88.     @Override
  89.     public String getFieldName( )
  90.     {
  91.         return _item.getFieldName( );
  92.     }

  93.     /**
  94.      * {@inheritDoc}
  95.      */
  96.     @Override
  97.     public InputStream getInputStream( ) throws IOException
  98.     {
  99.         return _item.getInputStream( );
  100.     }

  101.     /**
  102.      * {@inheritDoc}
  103.      */
  104.     @Override
  105.     public String getName( )
  106.     {
  107.         return UploadUtil.cleanFileName( FilenameUtils.getName( _item.getName( ) ) );
  108.     }

  109.     /**
  110.      * {@inheritDoc}
  111.      */
  112.     @Override
  113.     public OutputStream getOutputStream( ) throws IOException
  114.     {
  115.         return _item.getOutputStream( );
  116.     }

  117.     /**
  118.      * {@inheritDoc}
  119.      */
  120.     @Override
  121.     public long getSize( )
  122.     {
  123.         return _item.getSize( );
  124.     }

  125.     /**
  126.      * {@inheritDoc}
  127.      */
  128.     @Override
  129.     public String getString( )
  130.     {
  131.         return _item.getString( );
  132.     }

  133.     /**
  134.      * {@inheritDoc}
  135.      */
  136.     @Override
  137.     public String getString( String encoding ) throws UnsupportedEncodingException
  138.     {
  139.         return _item.getString( encoding );
  140.     }

  141.     /**
  142.      * {@inheritDoc}
  143.      */
  144.     @Override
  145.     public boolean isFormField( )
  146.     {
  147.         return _item.isFormField( );
  148.     }

  149.     /**
  150.      * {@inheritDoc}
  151.      */
  152.     @Override
  153.     public boolean isInMemory( )
  154.     {
  155.         return _item.isInMemory( );
  156.     }

  157.     /**
  158.      * {@inheritDoc}
  159.      */
  160.     @Override
  161.     public void setFieldName( String name )
  162.     {
  163.         _item.setFieldName( name );
  164.     }

  165.     /**
  166.      * {@inheritDoc}
  167.      */
  168.     @Override
  169.     public void setFormField( boolean state )
  170.     {
  171.         _item.setFormField( state );
  172.     }

  173.     /**
  174.      * {@inheritDoc}
  175.      */
  176.     @Override
  177.     public void write( File file ) throws Exception
  178.     {
  179.         _item.write( file );
  180.     }

  181.     /**
  182.      * {@inheritDoc}
  183.      */
  184.     @Override
  185.     public FileItemHeaders getHeaders( )
  186.     {
  187.         return _item.getHeaders( );
  188.     }

  189.     /**
  190.      * {@inheritDoc}
  191.      */
  192.     @Override
  193.     public void setHeaders( FileItemHeaders headers )
  194.     {
  195.         _item.setHeaders( headers );
  196.     }
  197. }