KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > upload > DiskMultipartRequestHandler


1 /*
2  * $Id: DiskMultipartRequestHandler.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.upload;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionServlet;
34 import org.apache.struts.config.ModuleConfig;
35 import org.apache.struts.util.ModuleUtils;
36
37 /**
38  * This is a MultipartRequestHandler that writes file data directly to
39  * to temporary files on disk.
40  *
41  * @deprecated Use the Commons FileUpload based multipart handler instead. This
42  * class will be removed after Struts 1.2.
43  */

44 public class DiskMultipartRequestHandler implements MultipartRequestHandler {
45
46     /**
47      * Commons Logging instance.
48      */

49     protected static Log log =
50             LogFactory.getLog(DiskMultipartRequestHandler.class);
51
52     /**
53      * The ActionServlet instance used for this class.
54      */

55     protected ActionServlet servlet;
56
57     /**
58      * The ActionMapping instance used for this class.
59      */

60     protected ActionMapping mapping;
61
62     /**
63      * A Hashtable representing the form files uploaded.
64      */

65     protected Hashtable JavaDoc fileElements;
66
67     /**
68      * A Hashtable representing the form text input names and values.
69      */

70     protected Hashtable JavaDoc textElements;
71
72     /**
73      * A Hashtable representing all elemnents.
74      */

75     protected Hashtable JavaDoc allElements;
76
77     /**
78      * The temporary directory.
79      */

80     protected String JavaDoc tempDir;
81
82
83     /**
84      * This method populates the internal hashtables with multipart request data.
85      * If the request argument is an instance of MultipartRequestWrapper,
86      * the request wrapper will be populated as well.
87      */

88     public void handleRequest(HttpServletRequest JavaDoc request) throws ServletException JavaDoc {
89         ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request);
90         this.retrieveTempDir(moduleConfig);
91         
92         try {
93             MultipartIterator iterator =
94                 new MultipartIterator(
95                     request,
96                     moduleConfig.getControllerConfig().getBufferSize(),
97                     getMaxSize(moduleConfig.getControllerConfig().getMaxFileSize()),
98                     tempDir);
99                     
100             MultipartElement element;
101
102             textElements = new Hashtable JavaDoc();
103             fileElements = new Hashtable JavaDoc();
104             allElements = new Hashtable JavaDoc();
105
106             while ((element = iterator.getNextElement()) != null) {
107                 if (!element.isFile()) {
108                     createTextElement(request, element);
109                 } else {
110                     createDiskFile(element);
111                 }
112             }
113             
114             //take care of maximum length being exceeded
115
if (iterator.isMaxLengthExceeded()) {
116                 request.setAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED, Boolean.TRUE);
117             }
118             
119         } catch(IOException JavaDoc ioe) {
120             throw new ServletException JavaDoc(ioe);
121         }
122     }
123
124     protected void createTextElement(HttpServletRequest JavaDoc request, MultipartElement element) {
125         if (request instanceof MultipartRequestWrapper) {
126             ((MultipartRequestWrapper) request).setParameter(element.getName(), element.getValue());
127         }
128         String JavaDoc[] textValues = (String JavaDoc[]) textElements.get(element.getName());
129
130         if (textValues != null) {
131             String JavaDoc[] textValues2 = new String JavaDoc[textValues.length + 1];
132             System.arraycopy(textValues, 0, textValues2, 0, textValues.length);
133             textValues2[textValues.length] = element.getValue();
134             textValues = textValues2;
135         } else {
136             textValues = new String JavaDoc[1];
137             textValues[0] = element.getValue();
138         }
139         textElements.put(element.getName(), textValues);
140         allElements.put(element.getName(), textValues);
141     }
142
143     protected void createDiskFile(MultipartElement element) {
144         File JavaDoc tempFile = element.getFile();
145         if (tempFile.exists()) {
146             DiskFile theFile = new DiskFile(tempFile.getAbsolutePath());
147             theFile.setContentType(element.getContentType());
148             theFile.setFileName(element.getFileName());
149             theFile.setFileSize((int) tempFile.length());
150             fileElements.put(element.getName(), theFile);
151             allElements.put(element.getName(), theFile);
152         }
153     }
154
155     public Hashtable JavaDoc getAllElements() {
156         return allElements;
157     }
158
159     public Hashtable JavaDoc getTextElements() {
160         return textElements;
161     }
162
163     public Hashtable JavaDoc getFileElements() {
164         return fileElements;
165     }
166
167     /**
168      * Delete all the files uploaded.
169      */

170     public void rollback() {
171         Enumeration JavaDoc names = fileElements.keys();
172
173         while (names.hasMoreElements()) {
174             String JavaDoc name = (String JavaDoc) names.nextElement();
175             DiskFile theFile = (DiskFile) fileElements.get(name);
176             theFile.destroy();
177         }
178     }
179
180     /**
181      * Calls on {@link #rollback() rollback()} to delete
182      * temporary files.
183      */

184     public void finish() {
185         rollback();
186     }
187
188     public void setServlet(ActionServlet servlet) {
189         this.servlet = servlet;
190     }
191
192     public void setMapping(ActionMapping mapping) {
193         this.mapping = mapping;
194     }
195
196     public ActionServlet getServlet() {
197         return servlet;
198     }
199
200     public ActionMapping getMapping() {
201         return mapping;
202     }
203
204     /**
205      * Gets the maximum post data size in bytes from the string
206      * representation in the configuration file.
207      */

208     protected long getMaxSize(String JavaDoc stringSize) throws ServletException JavaDoc {
209         long size = -1;
210         int multiplier = 1;
211
212         if (stringSize.endsWith("K")) {
213             multiplier = 1024;
214             stringSize = stringSize.substring(0, stringSize.length() - 1);
215         }
216         if (stringSize.endsWith("M")) {
217             multiplier = 1024 * 1024;
218             stringSize = stringSize.substring(0, stringSize.length() - 1);
219         } else if (stringSize.endsWith("G")) {
220             multiplier = 1024 * 1024 * 1024;
221             stringSize = stringSize.substring(0, stringSize.length() - 1);
222         }
223
224         try {
225             size = Long.parseLong(stringSize);
226         } catch(NumberFormatException JavaDoc nfe) {
227             throw new ServletException JavaDoc("Invalid format for maximum file size");
228         }
229
230         return (size * multiplier);
231     }
232
233     /**
234      * Retrieves the temporary directory from either ActionServlet, a context
235      * property, or a system property, in that order.
236      */

237     protected void retrieveTempDir(ModuleConfig moduleConfig) {
238
239         //attempt to retrieve the servlet container's temporary directory
240
ActionServlet servlet = getServlet();
241         if (servlet != null) {
242             //attempt to retrieve the servlet container's temporary directory
243
ServletContext JavaDoc context = servlet.getServletContext();
244
245             try {
246                 tempDir =
247                         (String JavaDoc) context.getAttribute("javax.servlet.context.tempdir");
248             } catch(ClassCastException JavaDoc cce) {
249                 tempDir = ((File JavaDoc) context.getAttribute("javax.servlet.context.tempdir")).getAbsolutePath();
250             }
251         }
252
253         if (tempDir == null) {
254             //attempt to retrieve the temporary directory from the controller
255
tempDir = moduleConfig.getControllerConfig().getTempDir();
256
257             if (tempDir == null) {
258                 //default to system-wide tempdir
259
tempDir = System.getProperty("java.io.tmpdir");
260
261                 log.debug(
262                     "DiskMultipartRequestHandler.handleRequest(): "
263                         + "defaulting to java.io.tmpdir directory \""
264                         + tempDir);
265             }
266         }
267     }
268 }
269
Popular Tags