01: /*
02: * @(#)addCookie.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 javax.servlet.*;
15: import javax.servlet.http.*;
16: import org.pnuts.net.URLEncoding;
17:
18: /*
19: * addCookie(name, value {, maxAge })
20: */
21: public class addCookie extends PnutsFunction {
22:
23: public addCookie() {
24: super ("addCookie");
25: }
26:
27: public boolean defined(int narg) {
28: return (narg == 2 || narg == 3);
29: }
30:
31: protected Object exec(Object[] args, Context context) {
32: int nargs = args.length;
33: HttpServletResponse response = (HttpServletResponse) context
34: .get(PnutsServlet.SERVLET_RESPONSE);
35: try {
36: if (nargs == 2 || nargs == 3) {
37: String name = (String) args[0];
38: String value = (String) args[1];
39: String encoding = "UTF8";
40: Cookie cookie = new Cookie(URLEncoding.encode(name,
41: encoding), URLEncoding.encode(value, encoding));
42: if (nargs == 3) {
43: int maxAge = ((Integer) args[2]).intValue();
44: cookie.setMaxAge(maxAge);
45: }
46: response.addCookie(cookie);
47: } else {
48: undefined(args, context);
49: return null;
50: }
51: return null;
52: } catch (UnsupportedEncodingException e) {
53: throw new PnutsException(e, context);
54: }
55: }
56:
57: public String toString() {
58: return "function addCookie(name, value {, maxAge })";
59: }
60: }
|