1 package org.apache.jserv;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 // Java stuff.
58 import java.net.URLEncoder;
59 import java.util.Date;
60 import java.util.Vector;
61 import java.util.StringTokenizer;
62 import java.util.Locale;
63 import java.util.TimeZone;
64 import java.util.NoSuchElementException;
65 import java.text.SimpleDateFormat;
66 import javax.servlet.http.Cookie;
67
68 /***
69 * Various utility methods used by the servlet engine.
70 *
71 * @author <a href="mailto:unknown">Francis J. Lacoste</a>
72 * @author <a href="mailto:unknown">Ian Kluft</a>
73 * @version $Id: JServUtils.java,v 1.2 2002/03/15 12:50:17 mpoeschl Exp $
74 * @deprecated
75 */
76 public final class JServUtils
77 {
78 private static SimpleDateFormat cookieDate =
79 new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss zz", Locale.US );
80
81 static
82 {
83 cookieDate.setTimeZone(TimeZone.getTimeZone("GMT"));
84 }
85
86 /***
87 * Encode a cookie as per the Netscape Cookies specification. The
88 * resulting string can be used in a Set-Cookie header.
89 *
90 * @param cookie The Cookie to encode.
91 * @return A string following Netscape Cookies specification.
92 */
93 public static String encodeCookie(Cookie cookie)
94 {
95 StringBuffer buf = new StringBuffer( cookie.getName() );
96 buf.append('=');
97 buf.append(cookie.getValue());
98
99 long age = cookie.getMaxAge();
100 if (age > 0)
101 {
102 buf.append("; expires=");
103 buf.append(cookieDate.format(new Date(System.currentTimeMillis() +
104 (long)age * 1000 )));
105 }
106 else if (age == 0)
107 {
108 buf.append("; expires=");
109
110 // Set expiration to the epoch to delete the cookie.
111 buf.append(cookieDate.format(new Date(0)));
112 }
113
114 if (cookie.getDomain() != null)
115 {
116 buf.append("; domain=");
117 buf.append(cookie.getDomain());
118 }
119
120 if (cookie.getPath() != null)
121 {
122 buf.append("; path=");
123 buf.append(cookie.getPath());
124 }
125
126 if (cookie.getSecure())
127 {
128 buf.append("; secure");
129 }
130
131 return buf.toString();
132 }
133
134 /***
135 * Parse a content-type header for the character encoding. If the
136 * content-type is null or there is no explicit character
137 * encoding, ISO-8859-1 is returned.
138 *
139 * @param contentType A content type header.
140 * @return A String.
141 */
142 public static String parseCharacterEncoding(String contentType)
143 {
144 int start;
145 int end;
146
147 if ((contentType == null) ||
148 ((start = contentType.indexOf("charset="))) == -1 )
149 {
150 return "ISO-8859-1";
151 }
152
153 String encoding = contentType.substring(start + 8);
154
155 if ((end = encoding.indexOf(";")) > -1)
156 {
157 return encoding.substring(0, end);
158 }
159 else
160 {
161 return encoding;
162 }
163 }
164
165 /***
166 * Parse a cookie header into an array of cookies as per RFC2109 -
167 * HTTP Cookies.
168 *
169 * @param cookieHdr The Cookie header value.
170 * @return A Cookie[].
171 */
172 public static Cookie[] parseCookieHeader(String cookieHdr)
173 {
174 Vector cookieJar = new Vector();
175
176 if(cookieHdr == null || cookieHdr.length() == 0)
177 return new Cookie[0];
178
179 StringTokenizer stok = new StringTokenizer(cookieHdr, "; ");
180 while (stok.hasMoreTokens())
181 {
182 try
183 {
184 String tok = stok.nextToken();
185 int equals_pos = tok.indexOf('=');
186 if (equals_pos > 0)
187 {
188 String name = URLDecode(tok.substring(0, equals_pos));
189 String value = URLDecode(tok.substring(equals_pos + 1));
190 cookieJar.addElement(new Cookie(name, value));
191 }
192 else if ( tok.length() > 0 && equals_pos == -1 )
193 {
194 String name = URLDecode(tok);
195 cookieJar.addElement(new Cookie(name, ""));
196 }
197 }
198 catch (IllegalArgumentException badcookie)
199 {
200 }
201 catch (NoSuchElementException badcookie)
202 {
203 }
204 }
205
206 Cookie[] cookies = new Cookie[cookieJar.size()];
207 cookieJar.copyInto(cookies);
208 return cookies;
209 }
210
211 /***
212 * This method decodes the given URL-encoded string.
213 *
214 * @param str The URL-encoded string.
215 * @return The decoded string.
216 * @exception IllegalArgumentException, if a '%' is not followed
217 * by a valid 2-digit hex number.
218 */
219 public final static String URLDecode(String str)
220 throws IllegalArgumentException
221 {
222 if (str == null)
223 return null;
224
225 // Decoded string output.
226 StringBuffer dec = new StringBuffer();
227
228 int strPos = 0;
229 int strLen = str.length();
230
231 dec.ensureCapacity(str.length());
232 while (strPos < strLen)
233 {
234 // Look ahead position.
235 int laPos;
236
237 // Look ahead to next URLencoded metacharacter, if any.
238 for (laPos = strPos; laPos < strLen; laPos++)
239 {
240 char laChar = str.charAt(laPos);
241 if ((laChar == '+') || (laChar == '%'))
242 {
243 break;
244 }
245 }
246
247 // If there were non-metacharacters, copy them all as a
248 // block.
249 if (laPos > strPos)
250 {
251 dec.append(str.substring(strPos,laPos));
252 strPos = laPos;
253 }
254
255 // Shortcut out of here if we're at the end of the string.
256 if (strPos >= strLen)
257 {
258 break;
259 }
260
261 // Process next metacharacter.
262 char metaChar = str.charAt(strPos);
263 if (metaChar == '+')
264 {
265 dec.append(' ');
266 strPos++;
267 continue;
268 }
269 else if (metaChar == '%')
270 {
271 try
272 {
273 dec.append((char)
274 Integer.parseInt(str.substring(strPos + 1,
275 strPos + 3),
276 16));
277 }
278 catch (NumberFormatException e)
279 {
280 throw new IllegalArgumentException("invalid hexadecimal "
281 + str.substring(strPos + 1, strPos + 3)
282 + " in URLencoded string (illegal unescaped '%'?)" );
283 }
284 catch (StringIndexOutOfBoundsException e)
285 {
286 throw new IllegalArgumentException("illegal unescaped '%' "
287 + " in URLencoded string" );
288 }
289 strPos += 3;
290 }
291 }
292
293 return dec.toString();
294 }
295
296 /***
297 * This method URL-encodes the given string. This method is here
298 * for symmetry and simplicity reasons and just calls
299 * URLEncoder.encode().
300 *
301 * @param str The string.
302 * @return The URL-encoded string.
303 */
304 public final static String URLEncode(String str)
305 {
306 if (str == null)
307 return null;
308 return URLEncoder.encode(str);
309 }
310 }
This page was automatically generated by Maven