1 16 package org.apache.myfaces.component.html.util; 17 18 import org.apache.commons.fileupload.*; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletRequestWrapper ; 24 import java.io.UnsupportedEncodingException ; 25 import java.util.*; 26 27 56 public class MultipartRequestWrapper 57 extends HttpServletRequestWrapper 58 { 59 private static Log log = LogFactory.getLog(MultipartRequestWrapper.class); 60 61 HttpServletRequest request = null; 62 HashMap parametersMap = null; 63 DiskFileUpload fileUpload = null; 64 HashMap fileItems = null; 65 int maxSize; 66 int thresholdSize; 67 String repositoryPath; 68 69 public MultipartRequestWrapper(HttpServletRequest request, 70 int maxSize, int thresholdSize, 71 String repositoryPath){ 72 super( request ); 73 this.request = request; 74 this.maxSize = maxSize; 75 this.thresholdSize = thresholdSize; 76 this.repositoryPath = repositoryPath; 77 } 78 79 private void parseRequest() { 80 fileUpload = new DiskFileUpload(); 81 fileUpload.setFileItemFactory(new DefaultFileItemFactory()); 82 fileUpload.setSizeMax(maxSize); 83 84 fileUpload.setSizeThreshold(thresholdSize); 85 86 if(repositoryPath != null && repositoryPath.trim().length()>0) 87 fileUpload.setRepositoryPath(repositoryPath); 88 89 String charset = request.getCharacterEncoding(); 90 fileUpload.setHeaderEncoding(charset); 91 92 93 List requestParameters = null; 94 try{ 95 requestParameters = fileUpload.parseRequest(request); 96 } catch (FileUploadBase.SizeLimitExceededException e) { 97 98 100 if(log.isInfoEnabled()) 101 log.info("user tried to upload a file that exceeded file-size limitations.",e); 102 103 requestParameters = Collections.EMPTY_LIST; 104 105 }catch(FileUploadException fue){ 106 log.error("Exception while uploading file.", fue); 107 requestParameters = Collections.EMPTY_LIST; 108 } 109 110 parametersMap = new HashMap( requestParameters.size() ); 111 fileItems = new HashMap(); 112 113 for (Iterator iter = requestParameters.iterator(); iter.hasNext(); ){ 114 FileItem fileItem = (FileItem) iter.next(); 115 116 if (fileItem.isFormField()) { 117 String name = fileItem.getFieldName(); 118 119 String value = null; 125 if ( charset == null) { 126 value = fileItem.getString(); 127 } else { 128 try { 129 value = new String (fileItem.get(), charset); 130 } catch (UnsupportedEncodingException e){ 131 value = fileItem.getString(); 132 } 133 } 134 135 addTextParameter(name, value); 136 } else { if (fileItem.getName() != null) { 138 fileItems.put(fileItem.getFieldName(), fileItem); 139 } 140 } 141 } 142 143 for (Iterator it = request.getParameterMap().entrySet().iterator(); it.hasNext(); ) 145 { 146 Map.Entry entry = (Map.Entry)it.next(); 147 String [] valuesArray = (String [])entry.getValue(); 148 for (int i = 0; i < valuesArray.length; i++) 149 { 150 addTextParameter((String )entry.getKey(), valuesArray[i]); 151 } 152 } 153 } 154 155 private void addTextParameter(String name, String value){ 156 if( ! parametersMap.containsKey( name ) ){ 157 String [] valuesArray = {value}; 158 parametersMap.put(name, valuesArray); 159 }else{ 160 String [] storedValues = (String [])parametersMap.get( name ); 161 int lengthSrc = storedValues.length; 162 String [] valuesArray = new String [lengthSrc+1]; 163 System.arraycopy(storedValues, 0, valuesArray, 0, lengthSrc); 164 valuesArray[lengthSrc] = value; 165 parametersMap.put(name, valuesArray); 166 } 167 } 168 169 public Enumeration getParameterNames() { 170 if( parametersMap == null ) parseRequest(); 171 172 return Collections.enumeration( parametersMap.keySet() ); 173 } 174 175 public String getParameter(String name) { 176 if( parametersMap == null ) parseRequest(); 177 178 String [] values = (String [])parametersMap.get( name ); 179 if( values == null ) 180 return null; 181 return values[0]; 182 } 183 184 public String [] getParameterValues(String name) { 185 if( parametersMap == null ) parseRequest(); 186 187 return (String [])parametersMap.get( name ); 188 } 189 190 public Map getParameterMap() { 191 if( parametersMap == null ) parseRequest(); 192 193 return parametersMap; 194 } 195 196 public FileItem getFileItem(String fieldName) { 198 if( fileItems == null ) parseRequest(); 199 200 return (FileItem) fileItems.get( fieldName ); 201 } 202 203 207 public Map getFileItems(){ 208 return fileItems; 209 } 210 } 211 | Popular Tags |