KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > util > FileUploadRequest


1 /*
2  * FileUploadRequest.java
3  *
4  * Version: $Revision: 1.11 $
5  *
6  * Date: $Date: 2005/12/22 09:39:58 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.util;
41
42 import java.io.File JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.util.Collection JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.Enumeration JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.Vector JavaDoc;
52
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
55
56 import org.apache.commons.fileupload.DiskFileUpload;
57 import org.apache.commons.fileupload.FileItem;
58 import org.dspace.core.ConfigurationManager;
59
60 /**
61  * Based on the com.oreilly.servlet.MultipartWrapper object, this is an HTTP
62  * request wrapper for multi-part (MIME) POSTs. It uses DSpace configuration
63  * properties to determine the temporary directory to use and the maximum
64  * allowable upload size.
65  *
66  * @author Robert Tansley
67  * @version $Revision: 1.11 $
68  */

69 public class FileUploadRequest extends HttpServletRequestWrapper JavaDoc
70 {
71
72     /** Multipart request */
73     private List JavaDoc items = null;
74
75     private HashMap JavaDoc parameters = new HashMap JavaDoc();
76
77     private HashMap JavaDoc fileitems = new HashMap JavaDoc();
78
79     private Vector JavaDoc filenames = new Vector JavaDoc();
80
81     private String JavaDoc tempDir = null;
82
83     /** Original request */
84     private HttpServletRequest JavaDoc original = null;
85
86     /**
87      * Parse a multipart request and extracts the files
88      *
89      * @param req
90      * the original request
91      */

92     public FileUploadRequest(HttpServletRequest JavaDoc req) throws IOException JavaDoc
93     {
94         super(req);
95
96         original = req;
97
98         tempDir = ConfigurationManager.getProperty("upload.temp.dir");
99         int maxSize = ConfigurationManager.getIntProperty("upload.max");
100
101         DiskFileUpload upload = new DiskFileUpload();
102
103         try
104         {
105             upload.setRepositoryPath(tempDir);
106             upload.setSizeMax(maxSize);
107             items = upload.parseRequest(req);
108             for (Iterator JavaDoc i = items.iterator(); i.hasNext();)
109             {
110                 FileItem item = (FileItem) i.next();
111                 if (item.isFormField())
112                 {
113                     parameters
114                             .put(item.getFieldName(), item.getString("UTF-8"));
115                 }
116                 else
117                 {
118                     parameters.put(item.getFieldName(), item.getName());
119                     fileitems.put(item.getFieldName(), item);
120                     filenames.add(item.getName());
121
122                     String JavaDoc filename = getFilename(item.getName());
123                     if (filename != null && !"".equals(filename))
124                     {
125                         item.write(new File JavaDoc(tempDir + File.separator
126                                         + filename));
127                     }
128                 }
129             }
130         }
131         catch (Exception JavaDoc e)
132         {
133             IOException JavaDoc t = new IOException JavaDoc(e.getMessage());
134             t.initCause(e);
135             throw t;
136         }
137     }
138
139     // Methods to replace HSR methods
140
public Enumeration JavaDoc getParameterNames()
141     {
142         Collection JavaDoc c = parameters.keySet();
143         return Collections.enumeration(c);
144     }
145
146     public String JavaDoc getParameter(String JavaDoc name)
147     {
148         return (String JavaDoc) parameters.get(name);
149     }
150
151     public String JavaDoc[] getParameterValues(String JavaDoc name)
152     {
153         return (String JavaDoc[]) parameters.values().toArray();
154     }
155
156     public Map JavaDoc getParameterMap()
157     {
158         Map JavaDoc map = new HashMap JavaDoc();
159         Enumeration JavaDoc eNum = getParameterNames();
160
161         while (eNum.hasMoreElements())
162         {
163             String JavaDoc name = (String JavaDoc) eNum.nextElement();
164             map.put(name, getParameterValues(name));
165         }
166
167         return map;
168     }
169
170     public String JavaDoc getFilesystemName(String JavaDoc name)
171     {
172         String JavaDoc filename = getFilename(((FileItem) fileitems.get(name))
173                 .getName());
174         return tempDir + File.separator + filename;
175     }
176
177     public String JavaDoc getContentType(String JavaDoc name)
178     {
179         return ((FileItem) fileitems.get(name)).getContentType();
180     }
181
182     public File JavaDoc getFile(String JavaDoc name)
183     {
184         String JavaDoc filename = getFilename(((FileItem) fileitems.get(name))
185                 .getName());
186         if ("".equals(filename.trim()))
187         {
188             return null;
189         }
190         return new File JavaDoc(tempDir + File.separator + filename);
191     }
192
193     public Enumeration JavaDoc getFileNames()
194     {
195         return filenames.elements();
196     }
197
198     /**
199      * Get back the original HTTP request object
200      *
201      * @return the original HTTP request
202      */

203     public HttpServletRequest JavaDoc getOriginalRequest()
204     {
205         return original;
206     }
207
208     // Required due to the fact the contents of getName() may vary based on
209
// browser
210
private String JavaDoc getFilename(String JavaDoc filepath)
211     {
212         String JavaDoc filename = filepath.trim();
213
214         int index = filepath.lastIndexOf(File.separator);
215         if (index > -1)
216         {
217             filename = filepath.substring(index);
218         }
219         return filename;
220     }
221 }
Popular Tags