1 3 package jodd.servlet.upload; 4 5 import jodd.servlet.ServletUtil; 6 import jodd.servlet.upload.impl.MemoryFileUpload; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import java.io.IOException ; 10 import java.io.UnsupportedEncodingException ; 11 import java.util.*; 12 13 35 public class MultipartRequest { 36 37 39 private Map requestParameters; 40 private Map requestFiles; 41 private HttpServletRequest request; 42 private String characterEncoding; 43 private FileUploadFactory fileUploadFactory; 44 45 48 public HttpServletRequest getServletRequest() { 49 return request; 50 } 51 52 55 public int getContentLength() { 56 return request.getContentLength(); 57 } 58 59 62 public String getCharacterEncoding() { 63 return characterEncoding; 64 } 65 66 68 69 72 public MultipartRequest(HttpServletRequest request) { 73 this(request, null, null); 74 } 75 76 79 public MultipartRequest(HttpServletRequest request, FileUploadFactory fileUploadFactory) { 80 this(request, fileUploadFactory, null); 81 } 82 83 86 public MultipartRequest(HttpServletRequest request, String encoding) { 87 this(request, null, encoding); 88 } 89 90 107 public MultipartRequest(HttpServletRequest request, FileUploadFactory fileUploadFactory, String encoding) { 108 this.request = request; 109 if (encoding != null) { 110 this.characterEncoding = encoding; 111 } else { 112 this.characterEncoding = request.getCharacterEncoding(); 113 } 114 this.fileUploadFactory = (fileUploadFactory == null ? new MemoryFileUpload.Factory() : fileUploadFactory); 115 } 116 117 119 private static String MREQ_ATTR_NAME = "jodd.servlet.upload.MultipartRequest"; 120 121 124 public static MultipartRequest getInstance(HttpServletRequest request, FileUploadFactory fileUploadFactory, String encoding) { 125 MultipartRequest mreq = (MultipartRequest) request.getAttribute(MREQ_ATTR_NAME); 126 if (mreq == null) { 127 mreq = new MultipartRequest(request, fileUploadFactory, encoding); 128 request.setAttribute(MREQ_ATTR_NAME, mreq); 129 } 130 return mreq; 131 } 132 133 136 public static MultipartRequest getParsedInstance(HttpServletRequest request, FileUploadFactory fileUploadFactory, String encoding) throws IOException { 137 MultipartRequest mreq = getInstance(request, fileUploadFactory, encoding); 138 if (mreq.isLoaded() == false) { 139 mreq.parseRequest(); 140 } 141 return mreq; 142 } 143 144 public static MultipartRequest getInstance(HttpServletRequest request, String encoding) { 145 return getInstance(request, null, encoding); 146 } 147 public static MultipartRequest getParsedInstance(HttpServletRequest request, String encoding) throws IOException { 148 MultipartRequest mreq = getInstance(request, null, encoding); 149 if (mreq.isLoaded() == false) { 150 mreq.parseRequest(); 151 } 152 return mreq; 153 } 154 155 156 public static MultipartRequest getInstance(HttpServletRequest request, FileUploadFactory fileUploadFactory) { 157 return getInstance(request, fileUploadFactory, null); 158 } 159 public static MultipartRequest getParsedInstance(HttpServletRequest request, FileUploadFactory fileUploadFactory) throws IOException { 160 MultipartRequest mreq = getInstance(request, fileUploadFactory, null); 161 if (mreq.isLoaded() == false) { 162 mreq.parseRequest(); 163 } 164 return mreq; 165 } 166 167 public static MultipartRequest getInstance(HttpServletRequest request) { 168 return getInstance(request, null, null); 169 } 170 public static MultipartRequest getParsedInstance(HttpServletRequest request) throws IOException { 171 MultipartRequest mreq = getInstance(request, null, null); 172 if (mreq.isLoaded() == false) { 173 mreq.parseRequest(); 174 } 175 return mreq; 176 } 177 178 180 private boolean loaded = false; 181 182 185 public boolean isLoaded() { 186 return loaded; 187 } 188 189 193 public void parseMultipartRequest() throws IOException { 194 if (loaded == true) { 195 throw new IOException ("Multipart request already loaded and parsed."); 196 } 197 loaded = true; 198 parseRequestStream(request, characterEncoding); 199 } 200 201 207 public void parseRequest() throws IOException { 208 if (loaded == true) { 209 throw new IOException ("Multipart request already loaded and parsed."); 210 } 211 loaded = true; 212 if (ServletUtil.isMultipartRequest(request) == true) { 213 parseRequestStream(request, characterEncoding); 214 } else { 215 requestParameters = new HashMap(); 216 requestFiles = new HashMap(); 217 Enumeration names = request.getParameterNames(); 218 while (names.hasMoreElements()) { 219 String paramName = (String ) names.nextElement(); 220 String [] values = request.getParameterValues(paramName); 221 requestParameters.put(paramName, values); 222 } 223 } 224 } 225 226 228 229 235 public String getParameter(String paramName) { 236 String [] values = (String []) requestParameters.get(paramName); 237 if ((values != null) && (values.length > 0)) { 238 return values[0]; 239 } 240 return null; 241 } 242 243 246 public Set getParameterNames() { 247 return requestParameters.keySet(); 248 } 249 250 253 public String [] getParameterValues(String paramName) { 254 return (String []) requestParameters.get(paramName); 255 } 256 257 258 263 public FileUpload getFile(String paramName) { 264 FileUpload[] values = (FileUpload[]) requestFiles.get(paramName); 265 if ((values != null) && (values.length > 0)) { 266 return values[0]; 267 } 268 return null; 269 } 270 271 272 275 public FileUpload[] getFiles(String paramName) { 276 return (FileUpload[]) requestFiles.get(paramName); 277 } 278 279 282 public Set getFileParameterNames() { 283 return requestFiles.keySet(); 284 } 285 286 287 289 private void put(Map destination, String name, Object value) { 290 ArrayList valuesList = (ArrayList) destination.get(name); 291 if (valuesList == null) { 292 valuesList = new ArrayList(); 293 destination.put(name, valuesList); 294 } 295 valuesList.add(value); 296 } 297 298 301 protected void parseRequestStream(HttpServletRequest request, String encoding) throws IOException { 302 requestParameters = new HashMap(); 303 requestFiles = new HashMap(); 304 305 MultipartRequestInputStream input = new MultipartRequestInputStream(request.getInputStream()); 306 input.readBoundary(); 307 while (true) { 308 FileUploadHeader header = input.readDataHeader(encoding); 309 if (header == null) { 310 break; 311 } 312 313 if (header.isFile && (header.fileName.length() > 0)) { 314 if (header.contentType.indexOf("application/x-macbinary") > 0) { 315 input.skipBytes(128); 316 } 317 FileUpload newFile = fileUploadFactory.create(input); 318 newFile.processStream(); 319 put(requestFiles, header.formFieldName, newFile); 320 } else { 321 MemoryFileUpload newFile = new MemoryFileUpload(input); 322 newFile.processStream(); 323 if (header.isFile == true) { 324 newFile.uploaded = false; 326 put(requestFiles, header.formFieldName, newFile); 327 } else { 328 String value = null; 330 if (encoding != null) { 331 try { 332 value = new String (newFile.getFileData(), encoding); 333 } catch (UnsupportedEncodingException ucex) { 334 value = null; 335 } 336 } 337 if (value == null) { 338 value = new String (newFile.getFileData()); 339 } 340 put(requestParameters, header.formFieldName, value); 341 } 342 } 343 input.skipBytes(1); 344 input.mark(1); 345 if (input.readByte() == '-') { 346 input.reset(); 347 break; 348 } 349 input.reset(); 350 } 351 352 Iterator names = requestParameters.keySet().iterator(); 354 while (names.hasNext()) { 355 String paramName = (String ) names.next(); 356 ArrayList valuesList = (ArrayList) requestParameters.get(paramName); 357 if (valuesList != null) { 358 String [] result = new String [valuesList.size()]; 359 for (int i = 0; i < result.length; i++) { 360 result[i] = (String ) valuesList.get(i); 361 } 362 requestParameters.put(paramName, result); 363 } 364 } 365 names = requestFiles.keySet().iterator(); 366 while (names.hasNext()) { 367 String paramName = (String ) names.next(); 368 ArrayList valuesList = (ArrayList) requestFiles.get(paramName); 369 if (valuesList != null) { 370 FileUpload[] result = new FileUpload[valuesList.size()]; 371 for (int i = 0; i < result.length; i++) { 372 result[i] = (FileUpload) valuesList.get(i); 373 } 374 requestFiles.put(paramName, result); 375 } 376 } 377 } 378 } | Popular Tags |