1 package org.apache.turbine.util.parser; 2 3 18 19 import java.net.URLDecoder ; 20 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 29 import org.apache.commons.fileupload.FileItem; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import org.apache.turbine.services.upload.TurbineUpload; 35 import org.apache.turbine.services.upload.UploadService; 36 37 import org.apache.turbine.util.TurbineException; 38 import org.apache.turbine.util.pool.Recyclable; 39 40 65 public class DefaultParameterParser 66 extends BaseValueParser 67 implements ParameterParser, Recyclable 68 { 69 70 private static Log log = LogFactory.getLog(DefaultParameterParser.class); 71 72 73 private HttpServletRequest request = null; 74 75 76 private byte[] uploadData = null; 77 78 79 private Map fileParameters = new HashMap (); 80 81 82 private static UploadService uploadService = null; 83 84 85 private static boolean uploadServiceIsAvailable = false; 86 87 94 public DefaultParameterParser() 95 { 96 super(); 97 configureUploadService(); 98 } 99 100 110 public DefaultParameterParser(String characterEncoding) 111 { 112 super (characterEncoding); 113 configureUploadService(); 114 } 115 116 122 private void configureUploadService() 123 { 124 uploadServiceIsAvailable = TurbineUpload.isAvailable(); 125 if (uploadServiceIsAvailable) 126 { 127 uploadService = TurbineUpload.getService(); 128 } 129 } 130 131 134 public void dispose() 135 { 136 this.request = null; 137 this.uploadData = null; 138 this.fileParameters.clear(); 139 super.dispose(); 140 } 141 142 147 public HttpServletRequest getRequest() 148 { 149 return this.request; 150 } 151 152 165 public void setRequest(HttpServletRequest request) 166 { 167 clear(); 168 169 uploadData = null; 170 171 String enc = request.getCharacterEncoding(); 172 setCharacterEncoding(enc != null ? enc : "US-ASCII"); 173 174 String tmp = null; 176 177 tmp = request.getHeader("Content-type"); 178 179 if (uploadServiceIsAvailable 180 && uploadService.getAutomatic() 181 && tmp != null 182 && tmp.startsWith("multipart/form-data")) 183 { 184 log.debug("Running the Turbine Upload Service"); 185 try 186 { 187 TurbineUpload.parseRequest(request, this); 188 } 189 catch (TurbineException e) 190 { 191 log.error("File upload failed", e); 192 } 193 } 194 195 for (Enumeration names = request.getParameterNames(); 196 names.hasMoreElements();) 197 { 198 tmp = (String ) names.nextElement(); 199 add(convert(tmp), 200 request.getParameterValues(tmp)); 201 } 202 203 try 206 { 207 StringTokenizer st = 208 new StringTokenizer (request.getPathInfo(), "/"); 209 boolean isNameTok = true; 210 String pathPart = null; 211 while (st.hasMoreTokens()) 212 { 213 if (isNameTok) 214 { 215 tmp = URLDecoder.decode(st.nextToken()); 216 isNameTok = false; 217 } 218 else 219 { 220 pathPart = URLDecoder.decode(st.nextToken()); 221 if (tmp.length() > 0) 222 { 223 add(convert(tmp), pathPart); 224 } 225 isNameTok = true; 226 } 227 } 228 } 229 catch (Exception e) 230 { 231 } 236 237 this.request = request; 238 239 if (log.isDebugEnabled()) 240 { 241 log.debug("Parameters found in the Request:"); 242 for (Iterator it = keySet().iterator(); it.hasNext();) 243 { 244 String key = (String ) it.next(); 245 log.debug("Key: " + key + " -> " + getString(key)); 246 } 247 } 248 } 249 250 255 public void setUploadData(byte[] uploadData) 256 { 257 this.uploadData = uploadData; 258 } 259 260 265 public byte[] getUploadData() 266 { 267 return this.uploadData; 268 } 269 270 279 public void append(String name, FileItem value) 280 { 281 FileItem[] items = this.getFileItems(name); 282 if (items == null) 283 { 284 items = new FileItem[1]; 285 items[0] = value; 286 fileParameters.put(convert(name), items); 287 } 288 else 289 { 290 FileItem[] newItems = new FileItem[items.length + 1]; 291 System.arraycopy(items, 0, newItems, 0, items.length); 292 newItems[items.length] = value; 293 fileParameters.put(convert(name), newItems); 294 } 295 } 296 297 304 public FileItem getFileItem(String name) 305 { 306 try 307 { 308 FileItem value = null; 309 Object object = fileParameters.get(convert(name)); 310 if (object != null) 311 { 312 value = ((FileItem[]) object)[0]; 313 } 314 return value; 315 } 316 catch (ClassCastException e) 317 { 318 log.error("Parameter (" 319 + name + ") is not an instance of FileItem", e); 320 return null; 321 } 322 } 323 324 332 public FileItem[] getFileItems(String name) 333 { 334 try 335 { 336 return (FileItem[]) fileParameters.get(convert(name)); 337 } 338 catch (ClassCastException e) 339 { 340 log.error("Parameter (" 341 + name + ") is not an instance of FileItem[]", e); 342 return null; 343 } 344 } 345 } 346 | Popular Tags |