01: /*
02: * @(#)readPostParameters.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.Context;
13: import pnuts.lang.PnutsFunction;
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: * readPostParameters(request {, encoding})
23: */
24: public class readPostParameters extends PnutsFunction {
25:
26: public readPostParameters() {
27: super ("readPostParameters");
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 contentType = request.getContentType();
48: if (contentType != null
49: && contentType.startsWith("multipart/form-data")) {
50: throw new RuntimeException("not yet implemented");
51: } else {
52: try {
53: ByteArrayOutputStream bout = new ByteArrayOutputStream();
54: byte[] buf = new byte[512];
55: int n;
56: InputStream in = request.getInputStream();
57: while ((n = in.read(buf, 0, buf.length)) != -1) {
58: bout.write(buf, 0, n);
59: }
60: in.close();
61: String qs = new String(bout.toByteArray());
62: Map map = URLEncoding.parseQueryString(qs, enc);
63: return new ServletParameter(map);
64: } catch (IOException e) {
65: throw new PnutsException(e, context);
66: }
67: }
68: }
69:
70: public String toString() {
71: return "function readPostParameters(request {, encoding })";
72: }
73: }
|