001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014 package org.apache.tapestry.multipart;
015
016 import org.apache.commons.fileupload.FileItem;
017 import org.apache.hivemind.ApplicationRuntimeException;
018 import org.apache.tapestry.request.IUploadFile;
019
020 import java.io.UnsupportedEncodingException;
021 import java.util.HashMap;
022 import java.util.Iterator;
023 import java.util.List;
024 import java.util.Map;
025
026 /**
027 * @author Raphael Jean
028 */
029 public abstract class AbstractMultipartDecoder
030 {
031
032 protected int _maxSize = 10000000;
033
034 protected int _thresholdSize = 1024;
035
036 protected String _repositoryPath = System.getProperty("java.io.tmpdir");
037
038 protected String _encoding;
039
040 /**
041 * Map of UploadPart (which implements IUploadFile), keyed on parameter
042 * name.
043 */
044 protected Map _uploadParts = new HashMap();
045
046 /**
047 * Map of ValuePart, keyed on parameter name.
048 */
049 private Map _valueParts = new HashMap();
050
051 public IUploadFile getFileUpload(String parameterName)
052 {
053 return (IUploadFile) _uploadParts.get(parameterName);
054 }
055
056 public void cleanup()
057 {
058 Iterator i = _uploadParts.values().iterator();
059
060 while(i.hasNext())
061 {
062 UploadPart part = (UploadPart) i.next();
063
064 part.cleanup();
065 }
066 }
067
068 protected Map buildParameterMap()
069 {
070 Map result = new HashMap();
071
072 Iterator i = _valueParts.entrySet().iterator();
073 while(i.hasNext())
074 {
075 Map.Entry e = (Map.Entry) i.next();
076
077 String name = (String) e.getKey();
078 ValuePart part = (ValuePart) e.getValue();
079
080 result.put(name, part.getValues());
081 }
082
083 return result;
084 }
085
086 protected void processFileItems(List parts)
087 {
088 if (parts == null) return;
089
090 Iterator i = parts.iterator();
091
092 while(i.hasNext())
093 {
094 FileItem item = (FileItem) i.next();
095
096 processFileItem(item);
097 }
098 }
099
100 private void processFileItem(FileItem item)
101 {
102 if (item.isFormField())
103 {
104 processFormFieldItem(item);
105 return;
106 }
107
108 processUploadFileItem(item);
109 }
110
111 private void processUploadFileItem(FileItem item)
112 {
113 String name = item.getFieldName();
114
115 UploadPart part = new UploadPart(item);
116
117 _uploadParts.put(name, part);
118 }
119
120 void processFormFieldItem(FileItem item)
121 {
122 String name = item.getFieldName();
123
124 String value = extractFileItemValue(item);
125
126 ValuePart part = (ValuePart) _valueParts.get(name);
127
128 if (part == null)
129 _valueParts.put(name, new ValuePart(value));
130 else
131 part.add(value);
132 }
133
134 private String extractFileItemValue(FileItem item)
135 {
136 try
137 {
138 return (_encoding == null) ? item.getString() : item.getString(_encoding);
139 }
140 catch (UnsupportedEncodingException ex)
141 {
142 throw new ApplicationRuntimeException(MultipartMessages
143 .unsupportedEncoding(_encoding, ex), ex);
144 }
145 }
146 }