KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > util > HttpParameterParser


1 /* $Id: HttpParameterParser.java,v 1.6 2001/04/25 22:16:09 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

14
15 package webEditor.util;
16
17 import java.io.*;
18 import java.util.*;
19
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import com.oreilly.servlet.MultipartRequest;
23 import org.apache.regexp.RE;
24
25
26 /**
27  * This class will provide a transparent interface for all the HTTP conections
28  *
29  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
30  */

31
32 public class HttpParameterParser
33 {
34    private HttpServletRequest myRequest;
35
36    public HttpParameterParser (HttpServletRequest request) {
37     this.myRequest = request;
38    }
39
40    /**
41     * Returns the HTTP parameters of the request, even if this
42     * parameters comes from a multipart form.
43     * @param tmpDir Temporal directory.
44     * @return Hashtable HTTP parameters of the request.
45     */

46    public Hashtable getAllParameters (String JavaDoc tmpDir)
47    {
48     Hashtable myParameters = new Hashtable();
49
50 try {
51     String JavaDoc contentType = this.myRequest.getContentType();
52     RE r = new RE("multipart/form-data");
53     if ( r.match (" " + contentType) ) {
54         // We are dealing with a multipart form
55
MultipartRequest formHandler = new MultipartRequest
56                     (this.myRequest, tmpDir);
57         Enumeration paramList = formHandler.getParameterNames();
58         for (; paramList.hasMoreElements() ;) {
59             String JavaDoc paramName = (String JavaDoc) paramList.nextElement();
60             myParameters.put (paramName,
61                 (String JavaDoc) formHandler.getParameter (paramName));
62             }
63         // Ok, and then , the we store the uploaded files
64
paramList = formHandler.getFileNames();
65         for (; paramList.hasMoreElements() ;) {
66             String JavaDoc paramName = (String JavaDoc) paramList.nextElement();
67             String JavaDoc fileName = null;
68             fileName = formHandler.getFilesystemName (paramName);
69             if ( fileName != null ) {
70                 myParameters.put (paramName, (String JavaDoc) fileName);
71             }
72             }
73     }
74     else {
75         // We will try to read the http parameters
76
Enumeration paramList = this.myRequest.getParameterNames();
77         for (; paramList.hasMoreElements() ;) {
78             String JavaDoc paramName = (String JavaDoc) paramList.nextElement();
79             myParameters.put (paramName,
80                  (String JavaDoc) this.myRequest.getParameter (paramName));
81             }
82     }
83 } catch (Exception JavaDoc e) {
84     e.printStackTrace();
85 }
86     return (myParameters);
87    }
88
89   /**
90     * Returns the HTTP headers of the request.
91     * @return Hashtable HTTP headers of the request.
92     */

93    public Hashtable getAllHeaders ()
94    {
95     Hashtable myHeaders = new Hashtable();
96
97     // We will try to read the http parameters
98
Enumeration headersList = this.myRequest.getHeaderNames();
99     for (; headersList.hasMoreElements() ;) {
100         String JavaDoc headerName = (String JavaDoc) headersList.nextElement();
101         myHeaders.put (headerName,
102              (String JavaDoc) this.myRequest.getHeader (headerName));
103         }
104
105     // We also add to the headers the requested URI
106
String JavaDoc requestURI = this.myRequest.getRequestURI();
107     myHeaders.put ("requestURI", (String JavaDoc) requestURI);
108     return (myHeaders);
109    }
110
111 }
112
Popular Tags