001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.misc.upload;
066:
067: /* ====================================================================
068: * The Apache Software License, Version 1.1
069: *
070: * Copyright (c) 2001 The Apache Software Foundation. All rights
071: * reserved.
072: *
073: * Redistribution and use in source and binary forms, with or without
074: * modification, are permitted provided that the following conditions
075: * are met:
076: *
077: * 1. Redistributions of source code must retain the above copyright
078: * notice, this list of conditions and the following disclaimer.
079: *
080: * 2. Redistributions in binary form must reproduce the above copyright
081: * notice, this list of conditions and the following disclaimer in
082: * the documentation and/or other materials provided with the
083: * distribution.
084: *
085: * 3. The end-user documentation included with the redistribution,
086: * if any, must include the following acknowledgment:
087: * "This product includes software developed by the
088: * Apache Software Foundation (http://www.apache.org/)."
089: * Alternately, this acknowledgment may appear in the software itself,
090: * if and wherever such third-party acknowledgments normally appear.
091: *
092: * 4. The names "Apache" and "Apache Software Foundation" and
093: * "Apache Turbine" must not be used to endorse or promote products
094: * derived from this software without prior written permission. For
095: * written permission, please contact apache@apache.org.
096: *
097: * 5. Products derived from this software may not be called "Apache",
098: * "Apache Turbine", nor may "Apache" appear in their name, without
099: * prior written permission of the Apache Software Foundation.
100: *
101: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
102: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
103: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
104: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
105: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
106: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
107: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
108: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
109: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
110: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
111: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
112: * SUCH DAMAGE.
113: * ====================================================================
114: *
115: * This software consists of voluntary contributions made by many
116: * individuals on behalf of the Apache Software Foundation. For more
117: * information on the Apache Software Foundation, please see
118: * <http://www.apache.org/>.
119: */
120: // Java Core Classes
121: import javax.servlet.http.HttpServletRequest;
122: import java.util.Enumeration;
123: import java.util.Vector;
124:
125: // JServ Utilities
126: //import org.apache.jserv.JServUtils;
127:
128: /**
129: * DefaultParameterParser is a utility object to handle parsing and
130: * retrieving the data passed via the GET/POST/PATH_INFO arguments.
131: * <p/>
132: * <p>NOTE: The name= portion of a name=value pair may be converted
133: * to lowercase or uppercase when the object is initialized and when
134: * new data is added. This behaviour is determined by the url.case.folding
135: * property in TurbineResources.properties. Adding a name/value pair may
136: * overwrite existing name=value pairs if the names match:
137: * <p/>
138: * <pre>
139: * ParameterParser pp = data.getParameters();
140: * pp.add("ERROR",1);
141: * pp.add("eRrOr",2);
142: * int result = pp.getInt("ERROR");
143: * </pre>
144: * <p/>
145: * In the above example, result is 2.
146: *
147: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
148: * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
149: * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
150: * @version $Id: DefaultParameterParser.java,v 1.6 2004/11/17 20:48:13 lhamel Exp $
151: */
152: public class DefaultParameterParser extends BaseValueParser implements
153: ParameterParser {
154:
155: /**
156: * The servlet request to parse.
157: */
158: private HttpServletRequest request = null;
159:
160: /**
161: * The raw data of a file upload.
162: */
163: private byte[] uploadData = null;
164: private Vector fileItems = null;
165:
166: /**
167: * Create a new empty instance of ParameterParser. Uses the
168: * default character encoding (US-ASCII).
169: * <p/>
170: * <p>To add name/value pairs to this set of parameters, use the
171: * <code>add()</code> methods.
172: */
173: public DefaultParameterParser() {
174: super ();
175: }
176:
177: /* Does the given parameter have a file item associated with it? */
178: public boolean hasFileItem(String paramName) {
179: if (fileItems == null) {
180: return false;
181: }
182:
183: String oneName = null;
184:
185: for (Enumeration e = fileItems.elements(); e.hasMoreElements();) {
186: oneName = (String) e.nextElement();
187:
188: if (oneName.equals(paramName)) {
189: return true;
190: }
191: }
192:
193: return false;
194: } /* hasFileItem(String) */
195:
196: /**
197: * Create a new empty instance of ParameterParser. Takes a
198: * character encoding name to use when converting strings to
199: * bytes.
200: * <p/>
201: * <p>To add name/value pairs to this set of parameters, use the
202: * <code>add()</code> methods.
203: *
204: * @param characterEncoding The character encoding of strings.
205: */
206: public DefaultParameterParser(String characterEncoding) {
207: super (characterEncoding);
208: }
209:
210: /**
211: * Disposes the parser.
212: */
213: public void dispose() {
214: this .request = null;
215: this .uploadData = null;
216: super .dispose();
217: }
218:
219: /**
220: * Gets the parsed servlet request.
221: *
222: * @return the parsed servlet request or null.
223: */
224: public HttpServletRequest getRequest() {
225: return this .request;
226: }
227:
228: public void setRequest(HttpServletRequest req) {
229: }
230:
231: /**
232: * Sets the servlet request to be parser. This requires a
233: * valid HttpServletRequest object. It will attempt to parse out
234: * the GET/POST/PATH_INFO data and store the data into a Hashtable.
235: * There are convenience methods for retrieving the data as a
236: * number of different datatypes. The PATH_INFO data must be a
237: * URLEncoded() string.
238: *
239: * <p>To add name/value pairs to this set of parameters, use the
240: * <code>add()</code> methods.
241: *
242: * @param req An HttpServletRequest.
243: */
244: /*public void setRequest(HttpServletRequest req)
245:
246: {
247:
248: clear();
249:
250: uploadData = null;
251:
252: String enc = req.getCharacterEncoding();
253:
254: setCharacterEncoding(enc != null ? enc : "US-ASCII");
255:
256: // String object re-use at its best.
257:
258: String tmp = null;
259:
260: tmp = req.getHeader("Content-type");
261:
262: if (tmp != null && tmp.startsWith("multipart/form-data") &&
263:
264: TurbineUpload.getAutomatic())
265:
266: {
267:
268: try
269:
270: {
271:
272: TurbineUpload.parseRequest(req, this);
273:
274: }
275:
276: catch(TurbineException e)
277:
278: {
279:
280: Log.error(new TurbineException("File upload failed", e));
281:
282: }
283:
284: }
285:
286: Enumeration names = req.getParameterNames();
287:
288: if ( names != null )
289:
290: {
291:
292: while(names.hasMoreElements())
293:
294: {
295:
296: tmp = (String) names.nextElement();
297:
298: parameters.put( convert(tmp), (Object) req.getParameterValues(tmp) );
299:
300: }
301:
302: }
303:
304: // Also cache any pathinfo variables that are passed around as
305:
306: // if they are query string data.
307:
308: try
309:
310: {
311:
312: StringTokenizer st = new StringTokenizer(req.getPathInfo(), "/");
313:
314: boolean name = true;
315:
316: String tmp2 = null;
317:
318: while(st.hasMoreTokens())
319:
320: {
321:
322: if ( name == true )
323:
324: {
325:
326: tmp = JServUtils.URLDecode(st.nextToken());
327:
328: name = false;
329:
330: }
331:
332: else
333:
334: {
335:
336: tmp2 = JServUtils.URLDecode(st.nextToken());
337:
338: if ( tmp.length() != 0 )
339:
340: {
341:
342: add (convert(tmp), tmp2);
343:
344: }
345:
346: name = true;
347:
348: }
349:
350: }
351:
352: }
353:
354: catch ( Exception e )
355:
356: {
357:
358: // If anything goes wrong above, don't worry about it.
359:
360: // Chances are that the path info was wrong anyways and
361:
362: // things that depend on it being right will fail later
363:
364: // and should be caught later.
365:
366: }
367:
368: this.request = req;
369:
370: } */
371: /**
372: * Sets the uploadData byte[]
373: *
374: * @param uploadData A byte[] with data.
375: */
376: public void setUploadData(byte[] uploadData) {
377: this .uploadData = uploadData;
378: }
379:
380: /**
381: * Gets the uploadData byte[]
382: *
383: * @return uploadData A byte[] with data.
384: */
385: public byte[] setUploadData() {
386: return this .uploadData;
387: }
388:
389: /**
390: * Add a FileItem object as a parameters. If there are any
391: * FileItems already associated with the name, append to the
392: * array. The reason for this is that RFC 1867 allows multiple
393: * files to be associated with single HTML input element.
394: *
395: * @param name A String with the name.
396: * @param value A FileItem with the value.
397: */
398: public void append(String name, FileItem value) {
399: FileItem[] items = this .getFileItems(name);
400:
401: if (items == null) {
402: items = new FileItem[1];
403: items[0] = value;
404: parameters.put(convert(name), items);
405: } else {
406: FileItem[] newItems = new FileItem[items.length + 1];
407: System.arraycopy(items, 0, newItems, 0, items.length);
408: newItems[items.length] = value;
409: parameters.put(convert(name), newItems);
410: }
411: if (fileItems == null) {
412: fileItems = new Vector();
413: }
414:
415: fileItems.addElement(name);
416: }
417:
418: /**
419: * Return a FileItem object for the given name. If the name does
420: * not exist or the object stored is not a FileItem, return null.
421: *
422: * @param name A String with the name.
423: * @return A FileItem.
424: */
425: public FileItem getFileItem(String name) {
426: try {
427: FileItem value = null;
428: Object object = parameters.get(convert(name));
429:
430: if (object != null) {
431: value = ((FileItem[]) object)[0];
432: }
433:
434: return value;
435: } catch (ClassCastException e) {
436: return null;
437: }
438: }
439:
440: /**
441: * Return an array of FileItem objects for the given name. If the
442: * name does not exist or the object stored is not a FileItem
443: * array, return null.
444: *
445: * @param name A String with the name.
446: * @return A FileItem[].
447: */
448: public FileItem[] getFileItems(String name) {
449: try {
450: return (FileItem[]) parameters.get(convert(name));
451: } catch (ClassCastException e) {
452: return null;
453: }
454: }
455: }
|