1 /*
2 * Copyright (c) 2002-2025, 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
36 import fr.paris.lutece.util.filesystem.UploadUtil;
37
38 import org.apache.commons.fileupload.FileItem;
39 import org.apache.commons.fileupload.FileItemHeaders;
40 import org.apache.commons.io.FilenameUtils;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.OutputStream;
46 import java.io.UnsupportedEncodingException;
47
48 /**
49 * This class is used to normalize the file names. This class override the method getName () of FileItem
50 */
51 public class NormalizeFileItem implements FileItem
52 {
53 private static final long serialVersionUID = 8696893066570050604L;
54 private FileItem _item;
55
56 /**
57 * Instantiates a new normalize file item.
58 *
59 * @param item
60 * the item
61 */
62 public NormalizeFileItem( FileItem item )
63 {
64 _item = item;
65 }
66
67 /**
68 * {@inheritDoc}
69 */
70 @Override
71 public void delete( )
72 {
73 _item.delete( );
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public byte [ ] get( )
81 {
82 return _item.get( );
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public String getContentType( )
90 {
91 return _item.getContentType( );
92 }
93
94 /**
95 * {@inheritDoc}
96 */
97 @Override
98 public String getFieldName( )
99 {
100 return _item.getFieldName( );
101 }
102
103 /**
104 * {@inheritDoc}
105 */
106 @Override
107 public InputStream getInputStream( ) throws IOException
108 {
109 return _item.getInputStream( );
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 @Override
116 public String getName( )
117 {
118 return UploadUtil.cleanFileName( FilenameUtils.getName( _item.getName( ) ) );
119 }
120
121 /**
122 * {@inheritDoc}
123 */
124 @Override
125 public OutputStream getOutputStream( ) throws IOException
126 {
127 return _item.getOutputStream( );
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 @Override
134 public long getSize( )
135 {
136 return _item.getSize( );
137 }
138
139 /**
140 * {@inheritDoc}
141 */
142 @Override
143 public String getString( )
144 {
145 return _item.getString( );
146 }
147
148 /**
149 * {@inheritDoc}
150 */
151 @Override
152 public String getString( String encoding ) throws UnsupportedEncodingException
153 {
154 return _item.getString( encoding );
155 }
156
157 /**
158 * {@inheritDoc}
159 */
160 @Override
161 public boolean isFormField( )
162 {
163 return _item.isFormField( );
164 }
165
166 /**
167 * {@inheritDoc}
168 */
169 @Override
170 public boolean isInMemory( )
171 {
172 return _item.isInMemory( );
173 }
174
175 /**
176 * {@inheritDoc}
177 */
178 @Override
179 public void setFieldName( String name )
180 {
181 _item.setFieldName( name );
182 }
183
184 /**
185 * {@inheritDoc}
186 */
187 @Override
188 public void setFormField( boolean state )
189 {
190 _item.setFormField( state );
191 }
192
193 /**
194 * {@inheritDoc}
195 */
196 @Override
197 public void write( File file ) throws Exception
198 {
199 _item.write( file );
200 }
201
202 /**
203 * {@inheritDoc}
204 */
205 @Override
206 public FileItemHeaders getHeaders( )
207 {
208 return _item.getHeaders( );
209 }
210
211 /**
212 * {@inheritDoc}
213 */
214 @Override
215 public void setHeaders( FileItemHeaders headers )
216 {
217 _item.setHeaders( headers );
218 }
219 }