KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > servlet > util > PostDataParser


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.servlet.util;
24
25 import java.util.HashMap JavaDoc;
26 import java.io.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30
31 /**
32  * This class processes the "multipart/form-data" sent by html form.
33  *
34  */

35 public class PostDataParser {
36   static final int PAD_LENGTH = "-----------------------------".length();
37     
38   /**
39    * The data to parse.
40    */

41   public StringBuffer JavaDoc data;
42     
43   public String JavaDoc queryString;
44     
45   HashMap JavaDoc parameterMap;
46   /**
47    * The http request identifier or the web browser identifier?
48    * This 12-digit Hexa number is used as field delimiter within the stream.
49    */

50   String JavaDoc RequestID;
51     
52     
53   /**
54    * @param data the StringBuffer containing the input data.
55    */

56   public PostDataParser(String JavaDoc data)
57   {
58     this.data = new StringBuffer JavaDoc(data);
59     queryString = new String JavaDoc(data);
60     this.parameterMap = new HashMap JavaDoc();
61     parseRequestID();
62     parseParameters();
63   }
64     
65     
66   public PostDataParser(HttpServletRequest request)
67     throws IOException
68   {
69     ServletInputStream in = request.getInputStream();
70     this.data = new StringBuffer JavaDoc();
71     int c;
72     while((c=in.read()) != -1) this.data.append((char)c);
73         
74     queryString = new String JavaDoc(data);
75     //PrintWriter out = new PrintWriter(new FileWriter("c:\\1.txt"));
76
//out.print(queryString);
77
//out.close();
78
this.parameterMap = new HashMap JavaDoc();
79     parseRequestID();
80     parseParameters();
81   }
82     
83     
84   /**
85    * Extracts the request ID from the input stream. It is a 12-digits hexa-dicimal number
86    * preceeded by 28 '-' chars.
87    */

88   void parseRequestID()
89   {
90     this.RequestID = data.substring(PAD_LENGTH, PAD_LENGTH+12);
91     this.data = data.delete(0, PAD_LENGTH + this.RequestID.length());
92   }
93      
94      
95   private String JavaDoc getRequestID()
96   {
97     return this.RequestID;
98   }
99      
100      
101   private void parseParameters()
102   {
103     String JavaDoc s = null;
104          
105     while((s=extractParameter()) != null)
106       parseParameter(s);
107   }
108      
109      
110   private String JavaDoc extractParameter()
111   {
112     int begin = 0;
113     int end = 0;
114          
115     begin = data.toString().indexOf("Content-Disposition: form-data; ") + "Content-Disposition: form-data; ".length();
116     // 32 is the length of "Content-Disposition: form-data; "
117

118         
119     if(begin == 31) // 31 (=32-1) means the requested string not found.
120
return null;
121         
122     end = data.toString().indexOf(RequestID) - PAD_LENGTH;
123     // 29 is the length of " ----------------------------" preceeding the Request ID ;
124

125     String JavaDoc parameter = data.substring(begin, end);
126         
127     this.data = this.data.delete(0, end+PAD_LENGTH+this.RequestID.length()+1);
128     // 43 is the length of " ----------------------------" plus the length of the Request ID
129
return parameter;
130   }
131      
132      
133   private void parseParameter(String JavaDoc s)
134   {
135     if(s== null)
136       return;
137          
138     int begin = s.indexOf("\"");
139     int end = s.indexOf("\"", begin+1);
140     //String name = s.substring(begin+1, end).trim();
141
String JavaDoc name = s.substring(begin+1, end).trim();
142     String JavaDoc value = s.substring(end+2, s.length()-1);
143         
144     if(isFileEntry(value))
145       {
146     this.parameterMap.put(name, parseUploadFile(value));
147       }
148     else
149       {
150     this.parameterMap.put(name, value.trim());
151       }
152   }
153      
154   boolean isFileEntry(String JavaDoc data)
155   {
156     return data.startsWith(" filename=\"");
157   }
158      
159      
160   private FileEntry parseUploadFile(String JavaDoc data)
161   {
162         
163     int begin = " filename=\"".length();
164     int end = data.indexOf("\"", begin);
165              
166     String JavaDoc filename = data.substring(begin, end);
167             
168     begin = data.indexOf("\r\nContent-Type: ", end) + "\r\nContent-Type: ".length();
169     end = data.indexOf("\r\n", begin);
170             
171     String JavaDoc contentType = data.substring(begin, end);
172     String JavaDoc fileContent = data.substring(end+1, data.length()-1);
173
174                 
175     /*
176         
177       System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
178       System.out.println("FILENAME="+filename);
179       System.out.println("CONTENT-TYPE="+contentType);
180       System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
181             
182       System.out.println("FILE=<"+fileContent+">=FILE");
183       System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
184
185     */

186         
187     return new FileEntry(filename, contentType, fileContent);
188   }
189      
190      
191      
192   /**
193    * Get the value of an parameter. To conform the behaviour of HttpServletRequest, the
194    * parameter names are case-sensitive.
195    *
196    * @return the value of the parameter; a String object for string parameters and
197    * a FileEntry object if the parameter is a file upload.
198    */

199   public String JavaDoc getParameter(String JavaDoc name)
200   {
201     return (String JavaDoc) this.parameterMap.get(name);
202   }
203      
204      
205   public String JavaDoc getFileContent(String JavaDoc name)
206   {
207     return ((FileEntry)this.parameterMap.get(name)).getContent();
208   }
209      
210      
211   public FileEntry getFileEntry(String JavaDoc name)
212   {
213     return (FileEntry) this.parameterMap.get(name);
214   }
215      
216      
217   /**
218    * Test program
219    */

220   public static void main(String JavaDoc args[])
221     throws Exception JavaDoc
222   {
223     StringBuffer JavaDoc data = new StringBuffer JavaDoc();
224     FileReader in = new FileReader("post.txt");
225     int c;
226     while((c=in.read()) != -1)
227       data.append((char)c);
228     PostDataParser p = new PostDataParser(data.toString());
229     //System.out.println("");
230
}
231 }
232
Popular Tags