KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > Form


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.connection;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.CharCursor;
33 import com.caucho.util.HashMapImpl;
34 import com.caucho.util.StringCharCursor;
35 import com.caucho.vfs.ByteToChar;
36
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.UnsupportedEncodingException JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Form handling.
45  */

46 public class Form {
47   static final Logger JavaDoc log = Log.open(Form.class);
48
49   private final ByteToChar _converter = ByteToChar.create();
50   
51   /**
52    * Parses the values from a query string.
53    *
54    * @param table the hashtable which will contain the results
55    * @param query the query string to evaluate
56    * @param javaEncoding the Java name for the charset
57    */

58   public void parseQueryString(HashMapImpl<String JavaDoc,String JavaDoc[]> table,
59                    String JavaDoc query,
60                    String JavaDoc javaEncoding,
61                    boolean isTop)
62     throws IOException JavaDoc
63   {
64     CharCursor is = new StringCharCursor(query);
65
66     ByteToChar converter = _converter;
67     try {
68       converter.setEncoding(javaEncoding);
69     } catch (UnsupportedEncodingException JavaDoc e) {
70       log.log(Level.FINE, e.toString(), e);
71     }
72
73     int ch = is.current();
74     while (ch != is.DONE) {
75       for (; Character.isWhitespace((char) ch) || ch == '&'; ch = is.next()) {
76       }
77
78       converter.clear();
79       for (; ch != is.DONE && ch != '=' && ch != '&'; ch = is.next())
80         readChar(converter, is, ch, isTop);
81
82       String JavaDoc key = converter.getConvertedString();
83
84       converter.clear();
85       if (ch == '=')
86         ch = is.next();
87       for (; ch != is.DONE && ch != '&'; ch = is.next())
88         readChar(converter, is, ch, isTop);
89       
90       String JavaDoc value = converter.getConvertedString();
91
92       if (log.isLoggable(Level.FINE))
93         log.fine("query: " + key + "=" + value);
94       
95       String JavaDoc []oldValue = table.get(key);
96       
97       if (key == null || key.equals("")) {
98       }
99       else if (oldValue == null)
100     table.put(key, new String JavaDoc[] { value });
101       else {
102     String JavaDoc []newValue = new String JavaDoc[oldValue.length + 1];
103     System.arraycopy(oldValue, 0, newValue, 0, oldValue.length);
104     newValue[oldValue.length] = value;
105     table.put(key, newValue);
106       }
107     }
108   }
109
110   /**
111    * Scans the next character from the input stream, adding it to the
112    * converter.
113    *
114    * @param converter the byte-to-character converter
115    * @param is the form's input stream
116    * @param ch the next character
117    */

118   private static void readChar(ByteToChar converter, CharCursor is,
119                    int ch, boolean isTop)
120     throws IOException JavaDoc
121   {
122     if (ch == '+')
123       converter.addByte(' ');
124     else if (ch == '%') {
125       int ch1 = is.next();
126
127       if (ch1 == 'u') {
128         ch1 = is.next();
129         int ch2 = is.next();
130         int ch3 = is.next();
131         int ch4 = is.next();
132
133         converter.addChar((char) ((toHex(ch1) << 12) +
134                                   (toHex(ch2) << 8) +
135                                   (toHex(ch3) << 4) +
136                                   (toHex(ch4))));
137       }
138       else {
139         int ch2 = is.next();
140         
141         converter.addByte(((toHex(ch1) << 4) + toHex(ch2)));
142       }
143     }
144     else if (isTop)
145       converter.addByte((byte) ch);
146     else
147       converter.addChar((char) ch);
148   }
149
150   /**
151    * Parses the values from a post data
152    *
153    * @param table the hashtable which will contain the results
154    * @param is an input stream containing the data
155    * @param javaEncoding the Java name for the charset
156    */

157   void parsePostData(HashMapImpl<String JavaDoc,String JavaDoc[]> table, InputStream JavaDoc is,
158              String JavaDoc javaEncoding)
159     throws IOException JavaDoc
160   {
161     ByteToChar converter = _converter;
162     try {
163       converter.setEncoding(javaEncoding);
164     } catch (UnsupportedEncodingException JavaDoc e) {
165       log.log(Level.FINE, e.toString(), e);
166     }
167
168     int ch = is.read();
169     while (ch >= 0) {
170       for (; Character.isWhitespace((char) ch) || ch == '&'; ch = is.read()) {
171       }
172
173       converter.clear();
174       for (;
175            ch >= 0 && ch != '=' && ch != '&' &&
176              ! Character.isWhitespace((char) ch);
177            ch = is.read()) {
178         readChar(converter, is, ch);
179       }
180
181       String JavaDoc key = converter.getConvertedString();
182
183       for (; Character.isWhitespace((char) ch); ch = is.read()) {
184       }
185       
186       converter.clear();
187       if (ch == '=') {
188         ch = is.read();
189     for (; Character.isWhitespace((char) ch); ch = is.read()) {
190     }
191       }
192       
193       for (; ch >= 0 && ch != '&'; ch = is.read())
194         readChar(converter, is, ch);
195       
196       String JavaDoc value = converter.getConvertedString();
197
198       /* Could show passwords
199       if (log.isLoggable(Level.FINE))
200         log.fine("post: " + key + "=" + value);
201       */

202       
203       String JavaDoc []oldValue = table.get(key);
204       
205       if (key == null || key.equals("")) {
206       }
207       else if (oldValue == null)
208     table.put(key, new String JavaDoc[] { value });
209       else {
210     String JavaDoc []newValue = new String JavaDoc[oldValue.length + 1];
211     System.arraycopy(oldValue, 0, newValue, 0, oldValue.length);
212     newValue[oldValue.length] = value;
213     table.put(key, newValue);
214       }
215     }
216   }
217
218   /**
219    * Scans the next character from the input stream, adding it to the
220    * converter.
221    *
222    * @param converter the byte-to-character converter
223    * @param is the form's input stream
224    * @param ch the next character
225    */

226   private static void readChar(ByteToChar converter, InputStream JavaDoc is, int ch)
227     throws IOException JavaDoc
228   {
229     if (ch == '+')
230       converter.addByte(' ');
231     else if (ch == '%') {
232       int ch1 = is.read();
233
234       if (ch1 == 'u') {
235         ch1 = is.read();
236         int ch2 = is.read();
237         int ch3 = is.read();
238         int ch4 = is.read();
239
240         converter.addChar((char) ((toHex(ch1) << 12) +
241                                   (toHex(ch2) << 8) +
242                                   (toHex(ch3) << 4) +
243                                   (toHex(ch4))));
244       }
245       else {
246         int ch2 = is.read();
247
248         converter.addByte(((toHex(ch1) << 4) + toHex(ch2)));
249       }
250     }
251     else
252       converter.addByte(ch);
253   }
254
255   /**
256    * Converts a hex character to a byte
257    */

258   private static int toHex(int ch)
259   {
260     if (ch >= '0' && ch <= '9')
261       return ch - '0';
262     else if (ch >= 'a' && ch <= 'f')
263       return ch - 'a' + 10;
264     else if (ch >= 'A' && ch <= 'F')
265       return ch - 'A' + 10;
266     else
267       return -1;
268   }
269 }
270
Popular Tags