File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\MultipartFormDataParser.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import org.apache.commons.fileupload.servlet.ServletFileUpload;
27 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
28 import org.apache.commons.fileupload.FileItem;
29 import org.apache.commons.fileupload.FileUploadException;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.ServletException;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.HashMap;
36
37 /**
38 * Wraps commons file-upload and handles a "multipart/form-data" form submisison
39 * (that often includes file upload.)
40 *
41 * @author Kohsuke Kawaguchi
42 */
43 public class MultipartFormDataParser {
44 private final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
45 private final Map<String,FileItem> byName = new HashMap<String,FileItem>();
46
47 public MultipartFormDataParser(HttpServletRequest request) throws ServletException {
48 try {
49 for( FileItem fi : (List<FileItem>)upload.parseRequest(request) )
50 byName.put(fi.getFieldName(),fi);
51 } catch (FileUploadException e) {
52 throw new ServletException(e);
53 }
54 }
55
56 public String get(String key) {
57 FileItem fi = byName.get(key);
58 if(fi==null) return null;
59 return fi.getString();
60 }
61
62 public FileItem getFileItem(String key) {
63 return byName.get(key);
64 }
65
66 /**
67 * If any file is created on the disk, delete them all.
68 * Even if this method is not called, the resource will be still cleaned up later by GC.
69 */
70 public void cleanUp() {
71 for (FileItem item : byName.values())
72 item.delete();
73 }
74 }
75