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.util.httpaccess;
35  
36  import org.apache.commons.fileupload.FileItem;
37  import org.apache.commons.fileupload.FileItemHeaders;
38  
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  /**
46   *
47   * MemoryFileItem
48   *
49   */
50  public class MemoryFileItem implements FileItem
51  {
52      private static final long serialVersionUID = 922231338093963479L;
53      private byte [ ] _data;
54      private String _strName;
55      private long _lSize;
56      private String _strContentType;
57  
58      /**
59       * Constructor
60       * 
61       * @param data
62       *            the data
63       * @param strName
64       *            the file name
65       * @param lSize
66       *            the size of the file
67       * @param strContentType
68       *            the content type of the file
69       */
70      public MemoryFileItem( byte [ ] data, String strName, long lSize, String strContentType )
71      {
72          _data = data;
73          _strName = strName;
74          _lSize = lSize;
75          _strContentType = strContentType;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      public void delete( )
82      {
83          // Nothing
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      public byte [ ] get( )
90      {
91          return _data;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public String getContentType( )
98      {
99          return _strContentType;
100     }
101 
102     /**
103      * Not supported
104      * 
105      * @return null
106      */
107     public String getFieldName( )
108     {
109         return null;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     public InputStream getInputStream( ) throws IOException
116     {
117         throw new UnsupportedOperationException( );
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     public String getName( )
124     {
125         return _strName;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     public OutputStream getOutputStream( ) throws IOException
132     {
133         throw new UnsupportedOperationException( );
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     public long getSize( )
140     {
141         return _lSize;
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     public String getString( )
148     {
149         return new String( get( ) );
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     public String getString( String strEncoding ) throws UnsupportedEncodingException
156     {
157         return new String( get( ), strEncoding );
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     public boolean isFormField( )
164     {
165         return false;
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     public boolean isInMemory( )
172     {
173         return true;
174     }
175 
176     /**
177      * {@inheritDoc}
178      */
179     public void setFieldName( String strFieldName )
180     {
181         // Nothing
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     public void setFormField( boolean state )
188     {
189         // Nothing
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     public void write( File file ) throws Exception
196     {
197         throw new UnsupportedOperationException( );
198     }
199 
200     public FileItemHeaders getHeaders( )
201     {
202         throw new UnsupportedOperationException( );
203     }
204 
205     public void setHeaders( FileItemHeaders headers )
206     {
207         throw new UnsupportedOperationException( );
208     }
209 }