1 15 package org.apache.tapestry.multipart; 16 17 import java.io.UnsupportedEncodingException ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import javax.servlet.http.HttpServletRequest ; 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 39 public class MultipartDecoderImpl implements MultipartDecoder 40 { 41 44 45 private Map _uploadParts = new HashMap (); 46 47 50 private Map _valueParts = new HashMap (); 51 52 private int _maxSize = 10000000; 53 54 private int _thresholdSize = 1024; 55 56 private String _repositoryPath = System.getProperty("java.io.tmpdir"); 57 58 private String _encoding; 59 60 public HttpServletRequest decode(HttpServletRequest request) 61 { 62 _encoding = request.getCharacterEncoding(); 63 64 DiskFileUpload upload = createUpload(request); 65 66 try 67 { 68 List 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 parameterMap = buildParameterMap(); 79 80 return new UploadFormParametersWrapper(request, parameterMap); 81 } 82 83 private Map buildParameterMap() 84 { 85 Map result = new HashMap (); 86 87 Iterator i = _valueParts.entrySet().iterator(); 88 while (i.hasNext()) 89 { 90 Map.Entry e = (Map.Entry ) i.next(); 91 92 String name = (String ) 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 parts) 102 { 103 if (parts == null) 104 return; 105 106 Iterator 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 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 name = item.getFieldName(); 139 140 String 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 extractFileItemValue(FileItem item) 151 { 152 try 153 { 154 return (_encoding == null) ? item.getString() : item.getString(_encoding); 155 } 156 catch (UnsupportedEncodingException ex) 157 { 158 throw new ApplicationRuntimeException(MultipartMessages.unsupportedEncoding( 159 _encoding, 160 ex), ex); 161 } 162 } 163 164 protected DiskFileUpload createUpload(HttpServletRequest 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 parameterName) 175 { 176 return (IUploadFile) _uploadParts.get(parameterName); 177 } 178 179 public void cleanup() 180 { 181 Iterator 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 |