This project has retired. For details please refer to its Attic page.
POSTHttpServletRequestWrapper xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl.browser;
20  
21  import java.io.BufferedInputStream;
22  import java.io.File;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.math.BigInteger;
26  import java.net.URLDecoder;
27  import java.util.Collections;
28  import java.util.Enumeration;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletRequestWrapper;
34  
35  import org.apache.chemistry.opencmis.commons.impl.Constants;
36  import org.apache.chemistry.opencmis.server.shared.HttpUtils;
37  import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStream;
38  import org.apache.commons.fileupload.FileItemIterator;
39  import org.apache.commons.fileupload.FileItemStream;
40  import org.apache.commons.fileupload.servlet.ServletFileUpload;
41  
42  public class POSTHttpServletRequestWrapper extends HttpServletRequestWrapper {
43      private final boolean isMultipart;
44      private Map<String, String[]> parameters;
45      private String filename;
46      private String contentType;
47      private BigInteger size;
48      private InputStream stream;
49  
50      public POSTHttpServletRequestWrapper(HttpServletRequest request, File tempDir, int memoryThreshold)
51              throws Exception {
52          super(request);
53  
54          parameters = new HashMap<String, String[]>();
55  
56          // parse query string
57          parseFormData(request.getQueryString());
58  
59          // check multipart
60          isMultipart = ServletFileUpload.isMultipartContent(request);
61  
62          if (isMultipart) {
63              ServletFileUpload upload = new ServletFileUpload();
64              FileItemIterator iter = upload.getItemIterator(request);
65  
66              while (iter.hasNext()) {
67                  FileItemStream item = iter.next();
68                  String name = item.getFieldName();
69                  InputStream itemStream = new BufferedInputStream(item.openStream());
70  
71                  if (item.isFormField()) {
72                      InputStreamReader reader = new InputStreamReader(itemStream, "UTF-8");
73  
74                      try {
75                          StringBuilder sb = new StringBuilder();
76  
77                          char[] buffer = new char[64 * 1024];
78                          int b = 0;
79                          while ((b = reader.read(buffer)) > -1) {
80                              sb.append(buffer, 0, b);
81                          }
82  
83                          addParameter(name, sb.toString());
84                      } finally {
85                          try {
86                              reader.close();
87                          } catch (Exception e) {
88                              // ignore
89                          }
90                      }
91                  } else {
92                      filename = item.getName();
93                      contentType = (item.getContentType() == null ? Constants.MEDIATYPE_OCTETSTREAM : item
94                              .getContentType());
95  
96                      ThresholdOutputStream os = new ThresholdOutputStream(tempDir, memoryThreshold);
97  
98                      try {
99                          byte[] buffer = new byte[64 * 1024];
100                         int b = 0;
101                         while ((b = itemStream.read(buffer)) > -1) {
102                             os.write(buffer, 0, b);
103                         }
104 
105                         os.close();
106 
107                         size = BigInteger.valueOf(os.getSize());
108                         stream = os.getInputStream();
109                     } catch (Exception e) {
110                         // if something went wrong, make sure the temp file will
111                         // be deleted
112                         os.destroy();
113                         throw e;
114                     } finally {
115                         try {
116                             itemStream.close();
117                         } catch (Exception e) {
118                             // ignore
119                         }
120                     }
121                 }
122             }
123 
124             String filenameControl = HttpUtils.getStringParameter(this, Constants.CONTROL_FILENAME);
125             if ((filenameControl) != null && (filenameControl.trim().length() > 0)) {
126                 filename = filenameControl;
127             }
128 
129             String contentTypeControl = HttpUtils.getStringParameter(this, Constants.CONTROL_CONTENT_TYPE);
130             if ((contentTypeControl != null) && (contentTypeControl.trim().length() > 0)) {
131                 contentType = contentTypeControl;
132             }
133         } else {
134             // form data processing
135             StringBuilder sb = new StringBuilder();
136 
137             InputStreamReader sr = new InputStreamReader(request.getInputStream(), "UTF-8");
138             char[] buffer = new char[4096];
139             int c = 0;
140             while ((c = sr.read(buffer)) > -1) {
141                 sb.append(buffer, 0, c);
142             }
143 
144             parseFormData(sb.toString());
145         }
146     }
147 
148     private void parseFormData(String data) throws Exception {
149         if (data == null || data.length() < 3) {
150             return;
151         }
152 
153         String[] nameValuePairs = data.split("&");
154         for (String nameValuePair : nameValuePairs) {
155             int x = nameValuePair.indexOf('=');
156             if (x > 0) {
157                 String name = URLDecoder.decode(nameValuePair.substring(0, x), "UTF-8");
158                 String value = (x == nameValuePair.length() - 1 ? "" : URLDecoder.decode(
159                         nameValuePair.substring(x + 1), "UTF-8"));
160                 addParameter(name, value);
161             }
162         }
163     }
164 
165     private void addParameter(String name, String value) {
166         String[] values = parameters.get(name);
167 
168         if (values == null) {
169             parameters.put(name, new String[] { value });
170         } else {
171             String[] newValues = new String[values.length + 1];
172             System.arraycopy(values, 0, newValues, 0, values.length);
173             newValues[newValues.length - 1] = value;
174             parameters.put(name, newValues);
175         }
176     }
177 
178     @Override
179     public String getParameter(String name) {
180         String[] values = parameters.get(name);
181         if ((values == null) || (values.length == 0)) {
182             return null;
183         }
184 
185         return values[0];
186     }
187 
188     @Override
189     public Map<String, String[]> getParameterMap() {
190         return parameters;
191     }
192 
193     @Override
194     public Enumeration<String> getParameterNames() {
195         return Collections.enumeration(parameters.keySet());
196     }
197 
198     @Override
199     public String[] getParameterValues(String name) {
200         return parameters.get(name);
201     }
202 
203     public String getFilename() {
204         return filename;
205     }
206 
207     public String getContentType() {
208         return contentType;
209     }
210 
211     public BigInteger getSize() {
212         return size;
213     }
214 
215     public InputStream getStream() {
216         return stream;
217     }
218 }