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.plugins.genericattributes.business;
35  
36  import fr.paris.lutece.util.filesystem.FileSystemUtil;
37  
38  import org.apache.commons.fileupload.FileItem;
39  import org.apache.commons.fileupload.FileItemHeaders;
40  
41  import java.io.ByteArrayInputStream;
42  import java.io.IOException;
43  import java.io.InputStream;
44  import java.io.OutputStream;
45  import java.io.UnsupportedEncodingException;
46  
47  /**
48   * GenAttFileItem : builds a new fileItem
49   */
50  public class GenAttFileItem implements FileItem
51  {
52      private static final long serialVersionUID = -8540841906551362771L;
53      private byte [ ] _bValue;
54      private String _strFileName;
55      private String _strFieldName;
56      private int _nIdResponse;
57      private FileItemHeaders _fileItemHeaders;
58  
59      /**
60       * Creates a new file item
61       * 
62       * @param bValue
63       *            the byte value
64       * @param strFileName
65       *            the file name
66       */
67      public GenAttFileItem( byte [ ] bValue, String strFileName )
68      {
69          _bValue = bValue;
70          _strFileName = strFileName;
71      }
72  
73      /**
74       * Creates a new file item
75       * 
76       * @param bValue
77       *            the byte value
78       * @param strFileName
79       *            the file name
80       * @param nIdResponse
81       *            The id of the response associated with this file item if any
82       */
83      public GenAttFileItem( byte [ ] bValue, String strFileName, int nIdResponse )
84      {
85          _bValue = bValue;
86          _strFileName = strFileName;
87          _nIdResponse = nIdResponse;
88      }
89  
90      /**
91       * Creates a new file item
92       * 
93       * @param bValue
94       *            the byte value
95       * @param strFileName
96       *            the file name
97       * @param strFieldName
98       *            The name of the HTML field associated with this file, if any
99       * @param nIdResponse
100      *            The id of the response associated with this file item if any
101      */
102     public GenAttFileItem( byte [ ] bValue, String strFileName, String strFieldName, int nIdResponse )
103     {
104         _bValue = bValue;
105         _strFileName = strFileName;
106         _strFieldName = strFieldName;
107         _nIdResponse = nIdResponse;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public void delete( )
115     {
116         _bValue = null;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public byte [ ] get( )
124     {
125         return _bValue;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public String getContentType( )
133     {
134         return FileSystemUtil.getMIMEType( _strFileName );
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public String getFieldName( )
142     {
143         return _strFieldName;
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public InputStream getInputStream( ) throws IOException
151     {
152         return new ByteArrayInputStream( _bValue );
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public String getName( )
160     {
161         return _strFileName;
162     }
163 
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public OutputStream getOutputStream( ) throws IOException
169     {
170         return null;
171     }
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public long getSize( )
178     {
179         return _bValue.length;
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public String getString( )
187     {
188         return new String( _bValue );
189     }
190 
191     /**
192      * {@inheritDoc}
193      */
194     @Override
195     public String getString( String encoding ) throws UnsupportedEncodingException
196     {
197         return new String( _bValue, encoding );
198     }
199 
200     /**
201      * Get the id of the associated response, if any
202      * 
203      * @return The id of the associated response, if any
204      */
205     public int getIdResponse( )
206     {
207         return _nIdResponse;
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public boolean isFormField( )
215     {
216         return false;
217     }
218 
219     /**
220      * {@inheritDoc}
221      */
222     @Override
223     public boolean isInMemory( )
224     {
225         return true;
226     }
227 
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     public void setFieldName( String strName )
233     {
234         _strFieldName = strName;
235     }
236 
237     /**
238      * {@inheritDoc}
239      */
240     @Override
241     public void setFormField( boolean bState )
242     {
243         // nothing
244     }
245 
246     /**
247      * Set the id of the associated response, if any
248      * 
249      * @param nIdResponse
250      *            The id of the associated response, if any
251      */
252     public void setIdResponse( int nIdResponse )
253     {
254         _nIdResponse = nIdResponse;
255     }
256 
257     /**
258      * {@inheritDoc}
259      */
260     @Override
261     public void write( java.io.File file ) throws Exception
262     {
263         // nothing
264     }
265 
266     /**
267      * {@inheritDoc}
268      */
269     @Override
270     public FileItemHeaders getHeaders( )
271     {
272         return _fileItemHeaders;
273     }
274 
275     /**
276      * {@inheritDoc}
277      */
278     @Override
279     public void setHeaders( FileItemHeaders headers )
280     {
281         _fileItemHeaders = headers;
282     }
283 }