KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > upload > DefaultParameterParser


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.misc.upload;
66
67 /* ====================================================================
68  * The Apache Software License, Version 1.1
69  *
70  * Copyright (c) 2001 The Apache Software Foundation. All rights
71  * reserved.
72  *
73  * Redistribution and use in source and binary forms, with or without
74  * modification, are permitted provided that the following conditions
75  * are met:
76  *
77  * 1. Redistributions of source code must retain the above copyright
78  * notice, this list of conditions and the following disclaimer.
79  *
80  * 2. Redistributions in binary form must reproduce the above copyright
81  * notice, this list of conditions and the following disclaimer in
82  * the documentation and/or other materials provided with the
83  * distribution.
84  *
85  * 3. The end-user documentation included with the redistribution,
86  * if any, must include the following acknowledgment:
87  * "This product includes software developed by the
88  * Apache Software Foundation (http://www.apache.org/)."
89  * Alternately, this acknowledgment may appear in the software itself,
90  * if and wherever such third-party acknowledgments normally appear.
91  *
92  * 4. The names "Apache" and "Apache Software Foundation" and
93  * "Apache Turbine" must not be used to endorse or promote products
94  * derived from this software without prior written permission. For
95  * written permission, please contact apache@apache.org.
96  *
97  * 5. Products derived from this software may not be called "Apache",
98  * "Apache Turbine", nor may "Apache" appear in their name, without
99  * 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

122 import javax.servlet.http.HttpServletRequest JavaDoc;
123 import java.util.Enumeration JavaDoc;
124 import java.util.Vector JavaDoc;
125
126
127 // JServ Utilities
128
//import org.apache.jserv.JServUtils;
129

130 /**
131  * DefaultParameterParser is a utility object to handle parsing and
132  * retrieving the data passed via the GET/POST/PATH_INFO arguments.
133  * <p/>
134  * <p>NOTE: The name= portion of a name=value pair may be converted
135  * to lowercase or uppercase when the object is initialized and when
136  * new data is added. This behaviour is determined by the url.case.folding
137  * property in TurbineResources.properties. Adding a name/value pair may
138  * overwrite existing name=value pairs if the names match:
139  * <p/>
140  * <pre>
141  * ParameterParser pp = data.getParameters();
142  * pp.add("ERROR",1);
143  * pp.add("eRrOr",2);
144  * int result = pp.getInt("ERROR");
145  * </pre>
146  * <p/>
147  * In the above example, result is 2.
148  *
149  * @author <a HREF="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
150  * @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
151  * @author <a HREF="mailto:sean@informage.net">Sean Legassick</a>
152  * @version $Id: DefaultParameterParser.java,v 1.6 2004/11/17 20:48:13 lhamel Exp $
153  */

154 public class DefaultParameterParser
155         extends BaseValueParser
156         implements ParameterParser {
157
158     /**
159      * The servlet request to parse.
160      */

161     private HttpServletRequest JavaDoc request = null;
162
163     /**
164      * The raw data of a file upload.
165      */

166     private byte[] uploadData = null;
167     private Vector JavaDoc fileItems = null;
168
169     /**
170      * Create a new empty instance of ParameterParser. Uses the
171      * default character encoding (US-ASCII).
172      * <p/>
173      * <p>To add name/value pairs to this set of parameters, use the
174      * <code>add()</code> methods.
175      */

176     public DefaultParameterParser() {
177         super();
178     }
179
180     /* Does the given parameter have a file item associated with it? */
181     public boolean hasFileItem(String JavaDoc paramName) {
182         if (fileItems == null) {
183             return false;
184         }
185
186         String JavaDoc oneName = null;
187
188         for (Enumeration JavaDoc e = fileItems.elements(); e.hasMoreElements();) {
189             oneName = (String JavaDoc) e.nextElement();
190
191             if (oneName.equals(paramName)) {
192                 return true;
193             }
194         }
195
196         return false;
197     } /* hasFileItem(String) */
198
199     /**
200      * Create a new empty instance of ParameterParser. Takes a
201      * character encoding name to use when converting strings to
202      * bytes.
203      * <p/>
204      * <p>To add name/value pairs to this set of parameters, use the
205      * <code>add()</code> methods.
206      *
207      * @param characterEncoding The character encoding of strings.
208      */

209     public DefaultParameterParser(String JavaDoc characterEncoding) {
210         super(characterEncoding);
211     }
212
213     /**
214      * Disposes the parser.
215      */

216     public void dispose() {
217         this.request = null;
218         this.uploadData = null;
219         super.dispose();
220     }
221
222     /**
223      * Gets the parsed servlet request.
224      *
225      * @return the parsed servlet request or null.
226      */

227     public HttpServletRequest JavaDoc getRequest() {
228         return this.request;
229     }
230
231     public void setRequest(HttpServletRequest JavaDoc req) {
232     }
233     /**
234      * Sets the servlet request to be parser. This requires a
235      * valid HttpServletRequest object. It will attempt to parse out
236      * the GET/POST/PATH_INFO data and store the data into a Hashtable.
237      * There are convenience methods for retrieving the data as a
238      * number of different datatypes. The PATH_INFO data must be a
239      * URLEncoded() string.
240      *
241      * <p>To add name/value pairs to this set of parameters, use the
242      * <code>add()</code> methods.
243      *
244      * @param req An HttpServletRequest.
245      */

246     /*public void setRequest(HttpServletRequest req)
247
248 {
249
250         clear();
251
252         uploadData = null;
253
254         String enc = req.getCharacterEncoding();
255
256         setCharacterEncoding(enc != null ? enc : "US-ASCII");
257
258         // String object re-use at its best.
259
260         String tmp = null;
261
262         tmp = req.getHeader("Content-type");
263
264         if (tmp != null && tmp.startsWith("multipart/form-data") &&
265
266             TurbineUpload.getAutomatic())
267
268         {
269
270             try
271
272             {
273
274                 TurbineUpload.parseRequest(req, this);
275
276             }
277
278             catch(TurbineException e)
279
280             {
281
282                 Log.error(new TurbineException("File upload failed", e));
283
284             }
285
286         }
287
288         Enumeration names = req.getParameterNames();
289
290         if ( names != null )
291
292         {
293
294             while(names.hasMoreElements())
295
296             {
297
298                 tmp = (String) names.nextElement();
299
300                 parameters.put( convert(tmp), (Object) req.getParameterValues(tmp) );
301
302             }
303
304         }
305
306         // Also cache any pathinfo variables that are passed around as
307
308         // if they are query string data.
309
310         try
311
312         {
313
314             StringTokenizer st = new StringTokenizer(req.getPathInfo(), "/");
315
316             boolean name = true;
317
318             String tmp2 = null;
319
320             while(st.hasMoreTokens())
321
322             {
323
324                 if ( name == true )
325
326                 {
327
328                     tmp = JServUtils.URLDecode(st.nextToken());
329
330                     name = false;
331
332                 }
333
334                 else
335
336                 {
337
338                     tmp2 = JServUtils.URLDecode(st.nextToken());
339
340                     if ( tmp.length() != 0 )
341
342                     {
343
344                         add (convert(tmp), tmp2);
345
346                     }
347
348                     name = true;
349
350                 }
351
352             }
353
354         }
355
356         catch ( Exception e )
357
358         {
359
360             // If anything goes wrong above, don't worry about it.
361
362             // Chances are that the path info was wrong anyways and
363
364             // things that depend on it being right will fail later
365
366             // and should be caught later.
367
368         }
369
370         this.request = req;
371
372 } */

373     /**
374      * Sets the uploadData byte[]
375      *
376      * @param uploadData A byte[] with data.
377      */

378     public void setUploadData(byte[] uploadData) {
379         this.uploadData = uploadData;
380     }
381
382     /**
383      * Gets the uploadData byte[]
384      *
385      * @return uploadData A byte[] with data.
386      */

387     public byte[] setUploadData() {
388         return this.uploadData;
389     }
390
391     /**
392      * Add a FileItem object as a parameters. If there are any
393      * FileItems already associated with the name, append to the
394      * array. The reason for this is that RFC 1867 allows multiple
395      * files to be associated with single HTML input element.
396      *
397      * @param name A String with the name.
398      * @param value A FileItem with the value.
399      */

400     public void append(String JavaDoc name, FileItem value) {
401         FileItem[] items = this.getFileItems(name);
402
403         if (items == null) {
404             items = new FileItem[1];
405             items[0] = value;
406             parameters.put(convert(name), items);
407         } else {
408             FileItem[] newItems = new FileItem[items.length + 1];
409             System.arraycopy(items, 0, newItems, 0, items.length);
410             newItems[items.length] = value;
411             parameters.put(convert(name), newItems);
412         }
413         if (fileItems == null) {
414             fileItems = new Vector JavaDoc();
415         }
416
417         fileItems.addElement(name);
418     }
419
420     /**
421      * Return a FileItem object for the given name. If the name does
422      * not exist or the object stored is not a FileItem, return null.
423      *
424      * @param name A String with the name.
425      * @return A FileItem.
426      */

427     public FileItem getFileItem(String JavaDoc name) {
428         try {
429             FileItem value = null;
430             Object JavaDoc object = parameters.get(convert(name));
431
432             if (object != null) {
433                 value = ((FileItem[]) object)[0];
434             }
435
436             return value;
437         } catch (ClassCastException JavaDoc e) {
438             return null;
439         }
440     }
441
442     /**
443      * Return an array of FileItem objects for the given name. If the
444      * name does not exist or the object stored is not a FileItem
445      * array, return null.
446      *
447      * @param name A String with the name.
448      * @return A FileItem[].
449      */

450     public FileItem[] getFileItems(String JavaDoc name) {
451         try {
452             return (FileItem[]) parameters.get(convert(name));
453         } catch (ClassCastException JavaDoc e) {
454             return null;
455         }
456     }
457 }
Popular Tags