View Javadoc
1 package org.apache.turbine.util.parser; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001-2002 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.net.URLDecoder; 58 import java.util.Enumeration; 59 import java.util.StringTokenizer; 60 61 import javax.servlet.http.HttpServletRequest; 62 63 import org.apache.turbine.util.Log; 64 import org.apache.turbine.util.ParameterParser; 65 import org.apache.turbine.util.TurbineException; 66 import org.apache.turbine.util.pool.Recyclable; 67 import org.apache.turbine.util.upload.FileItem; 68 import org.apache.turbine.services.upload.TurbineUpload; 69 70 /*** 71 * DefaultParameterParser is a utility object to handle parsing and 72 * retrieving the data passed via the GET/POST/PATH_INFO arguments. 73 * 74 * <p>NOTE: The name= portion of a name=value pair may be converted 75 * to lowercase or uppercase when the object is initialized and when 76 * new data is added. This behaviour is determined by the url.case.folding 77 * property in TurbineResources.properties. Adding a name/value pair may 78 * overwrite existing name=value pairs if the names match: 79 * 80 * <pre> 81 * ParameterParser pp = data.getParameters(); 82 * pp.add("ERROR",1); 83 * pp.add("eRrOr",2); 84 * int result = pp.getInt("ERROR"); 85 * </pre> 86 * 87 * In the above example, result is 2. 88 * 89 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a> 90 * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a> 91 * @author <a href="mailto:sean@informage.net">Sean Legassick</a> 92 * @version $Id: DefaultParameterParser.java,v 1.4 2002/07/11 18:21:03 mpoeschl Exp $ 93 */ 94 public class DefaultParameterParser 95 extends BaseValueParser 96 implements ParameterParser, 97 Recyclable 98 { 99 /*** 100 * The servlet request to parse. 101 */ 102 private HttpServletRequest request = null; 103 104 /*** 105 * The raw data of a file upload. 106 */ 107 private byte[] uploadData = null; 108 109 /*** 110 * Create a new empty instance of ParameterParser. Uses the 111 * default character encoding (US-ASCII). 112 * 113 * <p>To add name/value pairs to this set of parameters, use the 114 * <code>add()</code> methods. 115 * 116 */ 117 public DefaultParameterParser() 118 { 119 super(); 120 } 121 122 /*** 123 * Create a new empty instance of ParameterParser. Takes a 124 * character encoding name to use when converting strings to 125 * bytes. 126 * 127 * <p>To add name/value pairs to this set of parameters, use the 128 * <code>add()</code> methods. 129 * 130 * @param characterEncoding The character encoding of strings. 131 */ 132 public DefaultParameterParser(String characterEncoding) 133 { 134 super(characterEncoding); 135 } 136 137 /*** 138 * Disposes the parser. 139 */ 140 public void dispose() 141 { 142 this.request = null; 143 this.uploadData = null; 144 super.dispose(); 145 } 146 147 /*** 148 * Gets the parsed servlet request. 149 * 150 * @return the parsed servlet request or null. 151 */ 152 public HttpServletRequest getRequest() 153 { 154 return this.request; 155 } 156 157 /*** 158 * Sets the servlet request to be parser. This requires a 159 * valid HttpServletRequest object. It will attempt to parse out 160 * the GET/POST/PATH_INFO data and store the data into a Hashtable. 161 * There are convenience methods for retrieving the data as a 162 * number of different datatypes. The PATH_INFO data must be a 163 * URLEncoded() string. 164 * 165 * <p>To add name/value pairs to this set of parameters, use the 166 * <code>add()</code> methods. 167 * 168 * @param req An HttpServletRequest. 169 */ 170 public void setRequest(HttpServletRequest req) 171 { 172 clear(); 173 174 uploadData = null; 175 176 String enc = req.getCharacterEncoding(); 177 setCharacterEncoding(enc != null ? enc : "US-ASCII"); 178 179 // String object re-use at its best. 180 String tmp = null; 181 182 tmp = req.getHeader("Content-type"); 183 if (TurbineUpload.getAutomatic() && 184 tmp != null && tmp.startsWith("multipart/form-data")) 185 { 186 try 187 { 188 TurbineUpload.parseRequest(req, this); 189 } 190 catch (TurbineException e) 191 { 192 Log.error(new TurbineException("File upload failed", e)); 193 } 194 } 195 196 Enumeration names = req.getParameterNames(); 197 if (names != null) 198 { 199 while(names.hasMoreElements()) 200 { 201 tmp = (String) names.nextElement(); 202 parameters.put(convert(tmp), 203 (Object) req.getParameterValues(tmp)); 204 } 205 } 206 207 // Also cache any pathinfo variables that are passed around as 208 // if they are query string data. 209 try 210 { 211 StringTokenizer st = new StringTokenizer(req.getPathInfo(), "/"); 212 boolean name = true; 213 String pathPart = null; 214 while (st.hasMoreTokens()) 215 { 216 if (name == true) 217 { 218 tmp = URLDecoder.decode(st.nextToken()); 219 name = false; 220 } 221 else 222 { 223 pathPart = URLDecoder.decode(st.nextToken()); 224 if (tmp.length() != 0) 225 { 226 add(convert(tmp), pathPart); 227 } 228 name = true; 229 } 230 } 231 } 232 catch (Exception e) 233 { 234 // If anything goes wrong above, don't worry about it. 235 // Chances are that the path info was wrong anyways and 236 // things that depend on it being right will fail later 237 // and should be caught later. 238 } 239 240 this.request = req; 241 } 242 243 /*** 244 * Sets the uploadData byte[] 245 * 246 * @param uploadData A byte[] with data. 247 */ 248 public void setUploadData(byte[] uploadData) 249 { 250 this.uploadData = uploadData; 251 } 252 253 /*** 254 * Gets the uploadData byte[] 255 * 256 * @return uploadData A byte[] with data. 257 */ 258 public byte[] getUploadData() 259 { 260 return this.uploadData; 261 } 262 263 264 /*** 265 * Add a FileItem object as a parameters. If there are any 266 * FileItems already associated with the name, append to the 267 * array. The reason for this is that RFC 1867 allows multiple 268 * files to be associated with single HTML input element. 269 * 270 * @param name A String with the name. 271 * @param value A FileItem with the value. 272 */ 273 public void append(String name, FileItem value) 274 { 275 FileItem[] items = this.getFileItems(name); 276 if (items == null) 277 { 278 items = new FileItem[1]; 279 items[0] = value; 280 parameters.put(convert(name), items); 281 } 282 else 283 { 284 FileItem[] newItems = new FileItem[items.length + 1]; 285 System.arraycopy(items, 0, newItems, 0, items.length); 286 newItems[items.length] = value; 287 parameters.put(convert(name), newItems); 288 } 289 } 290 291 /*** 292 * Return a FileItem object for the given name. If the name does 293 * not exist or the object stored is not a FileItem, return null. 294 * 295 * @param name A String with the name. 296 * @return A FileItem. 297 */ 298 public FileItem getFileItem(String name) 299 { 300 try 301 { 302 FileItem value = null; 303 Object object = parameters.get(convert(name)); 304 if (object != null) 305 { 306 value = ((FileItem[]) object)[0]; 307 } 308 return value; 309 } 310 catch (ClassCastException e) 311 { 312 return null; 313 } 314 } 315 316 /*** 317 * Return an array of FileItem objects for the given name. If the 318 * name does not exist or the object stored is not a FileItem 319 * array, return null. 320 * 321 * @param name A String with the name. 322 * @return A FileItem[]. 323 */ 324 public FileItem[] getFileItems(String name) 325 { 326 try 327 { 328 return (FileItem[]) parameters.get(convert(name)); 329 } 330 catch (ClassCastException e) 331 { 332 return null; 333 } 334 } 335 }

This page was automatically generated by Maven