1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.server.impl.browser;
20
21 import org.apache.chemistry.opencmis.commons.impl.Constants;
22 import org.apache.chemistry.opencmis.server.shared.HttpUtils;
23 import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStream;
24
25 import org.apache.commons.fileupload.FileItemIterator;
26 import org.apache.commons.fileupload.FileItemStream;
27 import org.apache.commons.fileupload.servlet.ServletFileUpload;
28
29 import java.io.BufferedInputStream;
30 import java.io.File;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33
34 import java.math.BigInteger;
35
36 import java.net.URLDecoder;
37
38 import java.util.Collections;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.Map;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletRequestWrapper;
45
46
47 public class POSTHttpServletRequestWrapper extends HttpServletRequestWrapper
48 {
49 private final boolean isMultipart;
50 private Map<String, String[]> parameters;
51 private String filename;
52 private String contentType;
53 private BigInteger size;
54 private InputStream stream;
55
56 public POSTHttpServletRequestWrapper( HttpServletRequest request, File tempDir, int memoryThreshold )
57 throws Exception
58 {
59 super( request );
60
61 parameters = new HashMap<String, String[]>( );
62
63
64 parseFormData( request.getQueryString( ) );
65
66
67 isMultipart = ServletFileUpload.isMultipartContent( request );
68
69 if ( isMultipart )
70 {
71 ServletFileUpload upload = new ServletFileUpload( );
72 FileItemIterator iter = upload.getItemIterator( request );
73
74 while ( iter.hasNext( ) )
75 {
76 FileItemStream item = iter.next( );
77 String name = item.getFieldName( );
78 InputStream itemStream = new BufferedInputStream( item.openStream( ) );
79
80 if ( item.isFormField( ) )
81 {
82 InputStreamReader reader = new InputStreamReader( itemStream, "UTF-8" );
83
84 try
85 {
86 StringBuilder sb = new StringBuilder( );
87
88 char[] buffer = new char[64 * 1024];
89 int b = 0;
90
91 while ( ( b = reader.read( buffer ) ) > -1 )
92 {
93 sb.append( buffer, 0, b );
94 }
95
96 addParameter( name, sb.toString( ) );
97 }
98 finally
99 {
100 try
101 {
102 reader.close( );
103 }
104 catch ( Exception e )
105 {
106
107 }
108 }
109 }
110 else
111 {
112 filename = item.getName( );
113 contentType = ( ( item.getContentType( ) == null ) ? Constants.MEDIATYPE_OCTETSTREAM
114 : item.getContentType( ) );
115
116 ThresholdOutputStream os = new ThresholdOutputStream( tempDir, memoryThreshold );
117
118 try
119 {
120 byte[] buffer = new byte[64 * 1024];
121 int b = 0;
122
123 while ( ( b = itemStream.read( buffer ) ) > -1 )
124 {
125 os.write( buffer, 0, b );
126 }
127
128 os.close( );
129
130 size = BigInteger.valueOf( os.getSize( ) );
131 stream = os.getInputStream( );
132 }
133 catch ( Exception e )
134 {
135
136
137 os.destroy( );
138 throw e;
139 }
140 finally
141 {
142 try
143 {
144 itemStream.close( );
145 }
146 catch ( Exception e )
147 {
148
149 }
150 }
151 }
152 }
153
154 String filenameControl = HttpUtils.getStringParameter( this, Constants.CONTROL_FILENAME );
155
156 if ( ( ( filenameControl ) != null ) && ( filenameControl.trim( ).length( ) > 0 ) )
157 {
158 filename = filenameControl;
159 }
160
161 String contentTypeControl = HttpUtils.getStringParameter( this, Constants.CONTROL_CONTENT_TYPE );
162
163 if ( ( contentTypeControl != null ) && ( contentTypeControl.trim( ).length( ) > 0 ) )
164 {
165 contentType = contentTypeControl;
166 }
167 }
168 else
169 {
170
171 StringBuilder sb = new StringBuilder( );
172
173 InputStreamReader sr = new InputStreamReader( request.getInputStream( ), "UTF-8" );
174 char[] buffer = new char[4096];
175 int c = 0;
176
177 while ( ( c = sr.read( buffer ) ) > -1 )
178 {
179 sb.append( buffer, 0, c );
180 }
181
182 parseFormData( sb.toString( ) );
183 }
184 }
185
186 private void parseFormData( String data ) throws Exception
187 {
188 if ( ( data == null ) || ( data.length( ) < 3 ) )
189 {
190 return;
191 }
192
193 String[] nameValuePairs = data.split( "&" );
194
195 for ( String nameValuePair : nameValuePairs )
196 {
197 int x = nameValuePair.indexOf( '=' );
198
199 if ( x > 0 )
200 {
201 String name = URLDecoder.decode( nameValuePair.substring( 0, x ), "UTF-8" );
202 String value = ( ( x == ( nameValuePair.length( ) - 1 ) ) ? ""
203 : URLDecoder.decode( nameValuePair.substring( x +
204 1 ), "UTF-8" ) );
205 addParameter( name, value );
206 }
207 }
208 }
209
210 private void addParameter( String name, String value )
211 {
212 String[] values = parameters.get( name );
213
214 if ( values == null )
215 {
216 parameters.put( name, new String[] { value } );
217 }
218 else
219 {
220 String[] newValues = new String[values.length + 1];
221 System.arraycopy( values, 0, newValues, 0, values.length );
222 newValues[newValues.length - 1] = value;
223 parameters.put( name, newValues );
224 }
225 }
226
227 @Override
228 public String getParameter( String name )
229 {
230 String[] values = parameters.get( name );
231
232 if ( ( values == null ) || ( values.length == 0 ) )
233 {
234 return null;
235 }
236
237 return values[0];
238 }
239
240 @Override
241 public Map<String, String[]> getParameterMap( )
242 {
243 return parameters;
244 }
245
246 @Override
247 public Enumeration<String> getParameterNames( )
248 {
249 return Collections.enumeration( parameters.keySet( ) );
250 }
251
252 @Override
253 public String[] getParameterValues( String name )
254 {
255 return parameters.get( name );
256 }
257
258 public String getFilename( )
259 {
260 return filename;
261 }
262
263 public String getContentType( )
264 {
265 return contentType;
266 }
267
268 public BigInteger getSize( )
269 {
270 return size;
271 }
272
273 public InputStream getStream( )
274 {
275 return stream;
276 }
277 }