KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > multipart > JakartaMultiPartRequest


1 /*
2  * Copyright (c) 2002-2004 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher.multipart;
6
7 import org.apache.commons.fileupload.DefaultFileItem;
8 import org.apache.commons.fileupload.DiskFileUpload;
9 import org.apache.commons.fileupload.FileItem;
10 import org.apache.commons.fileupload.FileUploadException;
11
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.*;
16
17 /**
18  * Multipart form data request adapter for Jakarta's file upload package.
19  *
20  * @author Bruce Ritchie
21  */

22 public class JakartaMultiPartRequest extends MultiPartRequest {
23     // maps parameter name -> List of FileItem objects
24
private Map files = new HashMap();
25     // maps parameter name -> List of param values
26
private Map params = new HashMap();
27     // any errors while processing this request
28
private List errors = new ArrayList();
29
30     /**
31      * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
32      * multipart classes (see class description).
33      *
34      * @param maxSize maximum size post allowed
35      * @param saveDir the directory to save off the file
36      * @param servletRequest the request containing the multipart
37      */

38     public JakartaMultiPartRequest(HttpServletRequest JavaDoc servletRequest, String JavaDoc saveDir, int maxSize)
39             throws IOException JavaDoc {
40         DiskFileUpload upload = new DiskFileUpload();
41         // we must store all uploads on disk because the ww multipart API is missing streaming
42
// capabilities
43
upload.setSizeThreshold(0);
44         upload.setSizeMax(maxSize);
45         upload.setHeaderEncoding(servletRequest.getCharacterEncoding());
46         if (saveDir != null) {
47             upload.setRepositoryPath(saveDir);
48         }
49
50         // Parse the request
51
try {
52             List items = upload.parseRequest(servletRequest);
53
54             for (int i = 0; i < items.size(); i++) {
55                 FileItem item = (FileItem) items.get(i);
56                 log.debug("Found item " + item.getFieldName());
57                 if (item.isFormField()) {
58                     log.debug("Item is a normal form field");
59                     List values;
60                     if (params.get(item.getFieldName()) != null) {
61                         values = (List) params.get(item.getFieldName());
62                     } else {
63                         values = new ArrayList();
64                     }
65
66                     // note: see http://jira.opensymphony.com/browse/WW-633
67
// basically, in some cases the charset may be null, so
68
// we're just going to try to "other" method (no idea if this
69
// will work)
70
String JavaDoc charset = servletRequest.getCharacterEncoding();
71                     if (charset != null) {
72                         values.add(item.getString(charset));
73                     } else {
74                         values.add(item.getString());
75                     }
76                     params.put(item.getFieldName(), values);
77                 } else if (item.getSize() == 0) {
78                     log.debug("Item is a file upload of 0 size, ignoring");
79                 } else {
80                     log.debug("Item is a file upload");
81
82                     List values;
83                     if (files.get(item.getFieldName()) != null) {
84                         values = (List) files.get(item.getFieldName());
85                     } else {
86                         values = new ArrayList();
87                     }
88
89                     values.add(item);
90                     files.put(item.getFieldName(), values);
91                 }
92             }
93         } catch (FileUploadException e) {
94             log.error(e);
95             errors.add(e.getMessage());
96         }
97     }
98
99     public Enumeration getFileParameterNames() {
100         return Collections.enumeration(files.keySet());
101     }
102
103     public String JavaDoc[] getContentType(String JavaDoc fieldName) {
104         List items = (List) files.get(fieldName);
105
106         if (items == null) {
107             return null;
108         }
109
110         List contentTypes = new ArrayList(items.size());
111         for (int i = 0; i < items.size(); i++) {
112             FileItem fileItem = (FileItem) items.get(i);
113             contentTypes.add(fileItem.getContentType());
114         }
115
116         return (String JavaDoc[]) contentTypes.toArray(new String JavaDoc[contentTypes.size()]);
117     }
118
119     public File JavaDoc[] getFile(String JavaDoc fieldName) {
120         List items = (List) files.get(fieldName);
121
122         if (items == null) {
123             return null;
124         }
125
126         List fileList = new ArrayList(items.size());
127         for (int i = 0; i < items.size(); i++) {
128             DefaultFileItem fileItem = (DefaultFileItem) items.get(i);
129             fileList.add(fileItem.getStoreLocation());
130         }
131
132         return (File JavaDoc[]) fileList.toArray(new File JavaDoc[fileList.size()]);
133     }
134
135     /**
136      * Returns the canonical name of the given file
137      */

138     private String JavaDoc getCanonicalName(String JavaDoc filename) {
139         int forwardSlash = filename.lastIndexOf("/");
140         int backwardSlash = filename.lastIndexOf("\\");
141         if (forwardSlash != -1 && forwardSlash > backwardSlash) {
142             filename = filename.substring(forwardSlash + 1, filename.length());
143         } else if (backwardSlash != -1 && backwardSlash >= forwardSlash) {
144             filename = filename.substring(backwardSlash + 1, filename.length());
145         }
146
147         return filename;
148     }
149
150     public String JavaDoc[] getFileNames(String JavaDoc fieldName) {
151         List items = (List) files.get(fieldName);
152
153         if (items == null) {
154             return null;
155         }
156
157         List fileNames = new ArrayList(items.size());
158         for (int i = 0; i < items.size(); i++) {
159             DefaultFileItem fileItem = (DefaultFileItem) items.get(i);
160             fileNames.add(getCanonicalName(fileItem.getName()));
161         }
162
163         return (String JavaDoc[]) fileNames.toArray(new String JavaDoc[fileNames.size()]);
164     }
165
166     public String JavaDoc[] getFilesystemName(String JavaDoc fieldName) {
167         List items = (List) files.get(fieldName);
168
169         if (items == null) {
170             return null;
171         }
172
173         List fileNames = new ArrayList(items.size());
174         for (int i = 0; i < items.size(); i++) {
175             DefaultFileItem fileItem = (DefaultFileItem) items.get(i);
176             fileNames.add(fileItem.getStoreLocation().getName());
177         }
178
179         return (String JavaDoc[]) fileNames.toArray(new String JavaDoc[fileNames.size()]);
180     }
181
182     public String JavaDoc getParameter(String JavaDoc name) {
183         List v = (List) params.get(name);
184         if (v != null && v.size() > 0) {
185             return (String JavaDoc) v.get(0);
186         }
187
188         return null;
189     }
190
191     public Enumeration getParameterNames() {
192         return Collections.enumeration(params.keySet());
193     }
194
195     public String JavaDoc[] getParameterValues(String JavaDoc name) {
196         List v = (List) params.get(name);
197         if (v != null && v.size() > 0) {
198             return (String JavaDoc[]) v.toArray(new String JavaDoc[v.size()]);
199         }
200
201         return null;
202     }
203
204     public List getErrors() {
205         return errors;
206     }
207 }
208
Popular Tags