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 java.io.UnsupportedEncodingException;
017 import java.util.HashMap;
018 import java.util.Iterator;
019 import java.util.List;
020 import java.util.Map;
021
022 import org.apache.commons.fileupload.FileItem;
023 import org.apache.hivemind.ApplicationRuntimeException;
024 import org.apache.tapestry.request.IUploadFile;
025
026 /**
027 *
028 * @author Raphael Jean
029 */
030 public abstract class AbstractMultipartDecoder {
031
032 /**
033 * Map of UploadPart (which implements IUploadFile), keyed on parameter
034 * name.
035 */
036 protected Map _uploadParts = new HashMap();
037
038 /**
039 * Map of ValuePart, keyed on parameter name.
040 */
041 private Map _valueParts = new HashMap();
042
043 protected int _thresholdSize = 1024;
044
045 protected String _repositoryPath = System.getProperty("java.io.tmpdir");
046
047 protected String _encoding;
048
049 public IUploadFile getFileUpload(String parameterName) {
050 return (IUploadFile) _uploadParts.get(parameterName);
051 }
052
053 public void cleanup() {
054 Iterator i = _uploadParts.values().iterator();
055
056 while (i.hasNext()) {
057 UploadPart part = (UploadPart) i.next();
058
059 part.cleanup();
060 }
061 }
062
063 protected Map buildParameterMap() {
064 Map result = new HashMap();
065
066 Iterator i = _valueParts.entrySet().iterator();
067 while (i.hasNext()) {
068 Map.Entry e = (Map.Entry) i.next();
069
070 String name = (String) e.getKey();
071 ValuePart part = (ValuePart) e.getValue();
072
073 result.put(name, part.getValues());
074 }
075
076 return result;
077 }
078
079 protected void processFileItems(List parts) {
080 if (parts == null)
081 return;
082
083 Iterator i = parts.iterator();
084
085 while (i.hasNext()) {
086 FileItem item = (FileItem) i.next();
087
088 processFileItem(item);
089 }
090 }
091
092 private void processFileItem(FileItem item) {
093 if (item.isFormField()) {
094 processFormFieldItem(item);
095 return;
096 }
097
098 processUploadFileItem(item);
099 }
100
101 private void processUploadFileItem(FileItem item) {
102 String name = item.getFieldName();
103
104 UploadPart part = new UploadPart(item);
105
106 _uploadParts.put(name, part);
107 }
108
109 void processFormFieldItem(FileItem item) {
110 String name = item.getFieldName();
111
112 String value = extractFileItemValue(item);
113
114 ValuePart part = (ValuePart) _valueParts.get(name);
115
116 if (part == null)
117 _valueParts.put(name, new ValuePart(value));
118 else
119 part.add(value);
120 }
121
122 private String extractFileItemValue(FileItem item) {
123 try {
124 return (_encoding == null) ? item.getString() : item
125 .getString(_encoding);
126 } catch (UnsupportedEncodingException ex) {
127 throw new ApplicationRuntimeException(MultipartMessages
128 .unsupportedEncoding(_encoding, ex), ex);
129 }
130 }
131 }