View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.asynchronousupload.service;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.io.OutputStream;
40  import java.io.SequenceInputStream;
41  import java.io.UnsupportedEncodingException;
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.List;
45  import java.util.stream.Collectors;
46  
47  import org.apache.commons.fileupload.FileItem;
48  import org.apache.commons.fileupload.FileItemHeaders;
49  import org.apache.commons.io.FilenameUtils;
50  import org.apache.commons.io.IOUtils;
51  
52  import fr.paris.lutece.portal.service.util.AppLogService;
53  import fr.paris.lutece.util.filesystem.UploadUtil;
54  
55  /**
56   * File item witch contains a list of partial file item
57   */
58  public class PartialFileItemGroup implements FileItem
59  {
60      private static final long serialVersionUID = 8696893066570050604L;
61      private List<FileItem> _items;
62      private SequenceInputStream _sequenceInputStream;
63  
64      /**
65       * Instantiates a new normalize file item.
66       *
67       * @param item
68       *            the item
69       */
70      public PartialFileItemGroup( List<FileItem> items )
71      {
72          _items = items;
73          List<InputStream> vOut = new ArrayList<>( );
74  
75          try
76          {
77  
78              for ( FileItem fileItem : items )
79              {
80                  vOut.add( fileItem.getInputStream( ) );
81  
82              }
83          }
84          catch( IOException e )
85          {
86              AppLogService.error( "error creating Partial File item sequence inputstream", e );
87          }
88          _sequenceInputStream = new SequenceInputStream( Collections.enumeration( vOut ) );
89  
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public void delete( )
97      {
98          for ( FileItem item : _items )
99          {
100             item.delete( );
101         }
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public byte [ ] get( )
109     {
110 
111         byte [ ] bReturn = null;
112         try
113         {
114             bReturn = IOUtils.toByteArray( _sequenceInputStream );
115         }
116         catch( IOException e )
117         {
118             AppLogService.error( "error getting Partial File item  inputstream", e );
119 
120         }
121         return bReturn;
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public String getContentType( )
129     {
130         return _items.get( 0 ).getContentType( );
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public String getFieldName( )
138     {
139         return _items.get( 0 ).getFieldName( );
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public InputStream getInputStream( ) throws IOException
147     {
148         return _sequenceInputStream;
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public String getName( )
156     {
157         return UploadUtil.cleanFileName( FilenameUtils.getName( _items.get( 0 ).getName( ) ) );
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public OutputStream getOutputStream( ) throws IOException
165     {
166         return null;
167     }
168 
169     /**
170      * {@inheritDoc}
171      */
172     @Override
173     public long getSize( )
174     {
175         return _items.stream( ).collect( Collectors.summingLong( FileItem::getSize ) );
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public String getString( )
183     {
184         return _items.get( 0 ).getString( );
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public String getString( String encoding ) throws UnsupportedEncodingException
192     {
193         return _items.get( 0 ).getString( encoding );
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public boolean isFormField( )
201     {
202         return _items.get( 0 ).isFormField( );
203     }
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public boolean isInMemory( )
210     {
211         return _items.get( 0 ).isInMemory( );
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public void setFieldName( String name )
219     {
220         _items.get( 0 ).setFieldName( name );
221     }
222 
223     /**
224      * {@inheritDoc}
225      */
226     @Override
227     public void setFormField( boolean state )
228     {
229         _items.get( 0 ).setFormField( state );
230     }
231 
232     /**
233      * {@inheritDoc}
234      */
235     @Override
236     public void write( File file ) throws Exception
237     {
238         // Nothing
239     }
240 
241     /**
242      * {@inheritDoc}
243      */
244     @Override
245     public FileItemHeaders getHeaders( )
246     {
247         return _items.get( 0 ).getHeaders( );
248     }
249 
250     /**
251      * {@inheritDoc}
252      */
253     @Override
254     public void setHeaders( FileItemHeaders headers )
255     {
256         // No Default Headers
257     }
258 }