01: /*
02: * @(#)readGetParameters.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.PnutsFunction;
13: import pnuts.lang.Context;
14: import pnuts.lang.PnutsException;
15: import javax.servlet.*;
16: import javax.servlet.http.*;
17: import java.util.*;
18: import java.io.*;
19: import org.pnuts.net.URLEncoding;
20:
21: /*
22: * readGetParameters(request {, encoding})
23: */
24: public class readGetParameters extends PnutsFunction {
25:
26: public readGetParameters() {
27: super ("readGetParameters");
28: }
29:
30: public boolean defined(int narg) {
31: return (narg == 1 || narg == 2);
32: }
33:
34: protected Object exec(Object[] args, Context context) {
35: int nargs = args.length;
36: if (nargs != 1 && nargs != 2) {
37: undefined(args, context);
38: return null;
39: }
40: HttpServletRequest request = (HttpServletRequest) args[0];
41: String enc;
42: if (nargs == 1) {
43: enc = ServletEncoding.getDefaultInputEncoding(context);
44: } else {
45: enc = (String) args[1];
46: }
47: String qs = request.getQueryString();
48: Map map;
49: try {
50: if (qs != null) {
51: map = URLEncoding.parseQueryString(qs, enc);
52: } else {
53: map = new Hashtable();
54: }
55: return new ServletParameter(map);
56: } catch (UnsupportedEncodingException e) {
57: throw new PnutsException(e, context);
58: }
59: }
60:
61: public String toString() {
62: return "function readGetParameters(request {, encoding})";
63: }
64: }
|