KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > multipart > MultipartDecoderImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.multipart;
16
17 import java.io.UnsupportedEncodingException JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.commons.fileupload.DiskFileUpload;
26 import org.apache.commons.fileupload.FileItem;
27 import org.apache.commons.fileupload.FileUploadException;
28 import org.apache.hivemind.ApplicationRuntimeException;
29 import org.apache.tapestry.request.IUploadFile;
30
31 /**
32  * Implementation of {@link org.apache.tapestry.multipart.MultipartDecoder}that is based on <a
33  * HREF="http://jakarta.apache.org/commons/fileupload/">Jakarta FileUpload </a>.
34  *
35  * @author Howard M. Lewis Ship
36  * @author Joe Panico
37  * @since 4.0
38  */

39 public class MultipartDecoderImpl implements MultipartDecoder
40 {
41     /**
42      * Map of UploadPart (which implements IUploadFile), keyed on parameter name.
43      */

44
45     private Map JavaDoc _uploadParts = new HashMap JavaDoc();
46
47     /**
48      * Map of ValuePart, keyed on parameter name.
49      */

50     private Map JavaDoc _valueParts = new HashMap JavaDoc();
51
52     private int _maxSize = 10000000;
53
54     private int _thresholdSize = 1024;
55
56     private String JavaDoc _repositoryPath = System.getProperty("java.io.tmpdir");
57
58     private String JavaDoc _encoding;
59
60     public HttpServletRequest JavaDoc decode(HttpServletRequest JavaDoc request)
61     {
62         _encoding = request.getCharacterEncoding();
63
64         DiskFileUpload upload = createUpload(request);
65
66         try
67         {
68             List JavaDoc fileItems = upload
69                     .parseRequest(request, _thresholdSize, _maxSize, _repositoryPath);
70
71             processFileItems(fileItems);
72         }
73         catch (FileUploadException ex)
74         {
75             throw new ApplicationRuntimeException(MultipartMessages.unableToDecode(ex), ex);
76         }
77
78         Map JavaDoc parameterMap = buildParameterMap();
79
80         return new UploadFormParametersWrapper(request, parameterMap);
81     }
82
83     private Map JavaDoc buildParameterMap()
84     {
85         Map JavaDoc result = new HashMap JavaDoc();
86
87         Iterator JavaDoc i = _valueParts.entrySet().iterator();
88         while (i.hasNext())
89         {
90             Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
91
92             String JavaDoc name = (String JavaDoc) e.getKey();
93             ValuePart part = (ValuePart) e.getValue();
94
95             result.put(name, part.getValues());
96         }
97
98         return result;
99     }
100
101     private void processFileItems(List JavaDoc parts)
102     {
103         if (parts == null)
104             return;
105
106         Iterator JavaDoc i = parts.iterator();
107
108         while (i.hasNext())
109         {
110             FileItem item = (FileItem) i.next();
111
112             processFileItem(item);
113         }
114     }
115
116     private void processFileItem(FileItem item)
117     {
118         if (item.isFormField())
119         {
120             processFormFieldItem(item);
121             return;
122         }
123
124         processUploadFileItem(item);
125     }
126
127     private void processUploadFileItem(FileItem item)
128     {
129         String JavaDoc name = item.getFieldName();
130
131         UploadPart part = new UploadPart(item);
132
133         _uploadParts.put(name, part);
134     }
135
136     void processFormFieldItem(FileItem item)
137     {
138         String JavaDoc name = item.getFieldName();
139
140         String JavaDoc value = extractFileItemValue(item);
141
142         ValuePart part = (ValuePart) _valueParts.get(name);
143
144         if (part == null)
145             _valueParts.put(name, new ValuePart(value));
146         else
147             part.add(value);
148     }
149
150     private String JavaDoc extractFileItemValue(FileItem item)
151     {
152         try
153         {
154             return (_encoding == null) ? item.getString() : item.getString(_encoding);
155         }
156         catch (UnsupportedEncodingException JavaDoc ex)
157         {
158             throw new ApplicationRuntimeException(MultipartMessages.unsupportedEncoding(
159                     _encoding,
160                     ex), ex);
161         }
162     }
163
164     protected DiskFileUpload createUpload(HttpServletRequest JavaDoc request)
165     {
166         DiskFileUpload upload = new DiskFileUpload();
167
168         if (_encoding != null)
169             upload.setHeaderEncoding(_encoding);
170
171         return upload;
172     }
173
174     public IUploadFile getFileUpload(String JavaDoc parameterName)
175     {
176         return (IUploadFile) _uploadParts.get(parameterName);
177     }
178
179     public void cleanup()
180     {
181         Iterator JavaDoc i = _uploadParts.values().iterator();
182
183         while (i.hasNext())
184         {
185             UploadPart part = (UploadPart) i.next();
186
187             part.cleanup();
188         }
189     }
190
191 }
Popular Tags