01: /*
02: * @(#)getCookie.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.servlet;
10:
11: import pnuts.servlet.*;
12: import pnuts.lang.*;
13: import java.io.*;
14: import java.util.*;
15: import javax.servlet.*;
16: import javax.servlet.http.*;
17: import org.pnuts.net.URLEncoding;
18:
19: /*
20: * getCookie(name)
21: */
22: public class getCookie extends PnutsFunction {
23:
24: public getCookie() {
25: super ("getCookie");
26: }
27:
28: public boolean defined(int narg) {
29: return (narg == 1);
30: }
31:
32: protected Object exec(Object[] args, Context context) {
33: int nargs = args.length;
34: if (nargs != 1) {
35: undefined(args, context);
36: return null;
37: }
38: String name = (String) args[0];
39: Hashtable tab = (Hashtable) context
40: .get(PnutsServlet.SERVLET_COOKIE);
41: if (tab == null) {
42: HttpServletRequest request = (HttpServletRequest) context
43: .get(PnutsServlet.SERVLET_REQUEST);
44: String encoding = "UTF8";
45: Cookie[] cookies = request.getCookies();
46: if (cookies == null) {
47: return null;
48: }
49: tab = new Hashtable();
50: for (int i = 0; i < cookies.length; i++) {
51: try {
52: tab.put(URLEncoding.decode(cookies[i].getName(),
53: encoding), URLEncoding.decode(cookies[i]
54: .getValue(), encoding));
55: } catch (UnsupportedEncodingException e) {
56: throw new PnutsException(e, context);
57: }
58: }
59: context.set(PnutsServlet.SERVLET_COOKIE, tab);
60: }
61: return tab.get(name);
62: }
63:
64: public String toString() {
65: return "function getCookie(name)";
66: }
67: }
|