001: /*
002: * @(#)readParameters.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.servlet;
010:
011: import pnuts.servlet.*;
012: import pnuts.lang.Context;
013: import pnuts.lang.PnutsFunction;
014: import pnuts.lang.PnutsException;
015: import javax.servlet.*;
016: import javax.servlet.http.*;
017: import java.util.*;
018: import java.io.*;
019: import org.pnuts.net.URLEncoding;
020:
021: /*
022: * readParameters( { request { , encoding } } )
023: */
024: public class readParameters extends PnutsFunction {
025:
026: static readParameters instance = new readParameters();
027:
028: static PnutsFunction getInstance() {
029: return instance;
030: }
031:
032: public readParameters() {
033: super ("readParameters");
034: }
035:
036: static Map getParameterMap(HttpServletRequest request,
037: String encoding, Context context) {
038: String qs = request.getQueryString();
039: Map map, map2;
040: if (qs != null) {
041: try {
042: map = URLEncoding.parseQueryString(qs, encoding);
043: } catch (UnsupportedEncodingException e) {
044: throw new PnutsException(e, context);
045: }
046: } else {
047: map = new HashMap();
048: }
049: String contentType = request.getContentType();
050: if ("POST".equals(request.getMethod())) {
051: contentType = request.getContentType();
052: if (contentType != null
053: && contentType.startsWith("multipart/form-data")) {
054: Map t = (Map) context
055: .get(PnutsServlet.SERVLET_MULTIPART_PARAM);
056: map2 = new HashMap();
057: for (Iterator it = t.keySet().iterator(); it.hasNext();) {
058: String key = (String) it.next();
059: try {
060: map2.put(key, new String[] { new String(
061: (byte[]) t.get(key), encoding) });
062: } catch (UnsupportedEncodingException e) {
063: throw new PnutsException(e, context);
064: }
065: }
066: } else {
067: ByteArrayOutputStream bout = new ByteArrayOutputStream();
068: byte[] buf = new byte[512];
069: int n;
070: try {
071: InputStream in = request.getInputStream();
072: while ((n = in.read(buf, 0, buf.length)) != -1) {
073: bout.write(buf, 0, n);
074: }
075: map2 = URLEncoding.parseQueryString(new String(bout
076: .toByteArray()), encoding);
077: in.close();
078: } catch (IOException e) {
079: throw new PnutsException(e, context);
080: }
081: }
082: for (Iterator it = map2.keySet().iterator(); it.hasNext();) {
083: String key = (String) it.next();
084: String[] v2 = (String[]) map2.get(key);
085: if (v2 != null) {
086: String[] v1 = (String[]) map.get(key);
087: if (v1 != null) {
088: String[] v3 = new String[v1.length + v2.length];
089: System.arraycopy(v1, 0, v3, 0, v1.length);
090: System.arraycopy(v2, 0, v3, v1.length,
091: v2.length);
092: map.put(key, v3);
093: } else {
094: map.put(key, v2);
095: }
096: }
097: }
098: }
099: return map;
100: }
101:
102: public boolean defined(int narg) {
103: return (narg == 0 || narg == 1 || narg == 2);
104: }
105:
106: protected Object exec(Object[] args, Context context) {
107: int nargs = args.length;
108: HttpServletRequest request;
109: String enc;
110:
111: if (nargs == 0) {
112: request = (HttpServletRequest) context
113: .get(PnutsServlet.SERVLET_REQUEST);
114: enc = ServletEncoding.getDefaultInputEncoding(context);
115: } else if (nargs == 1) {
116: request = (HttpServletRequest) args[0];
117: enc = ServletEncoding.getDefaultInputEncoding(context);
118: } else if (nargs == 2) {
119: request = (HttpServletRequest) args[0];
120: enc = (String) args[1];
121: } else {
122: undefined(args, context);
123: return null;
124: }
125: Map m = new ServletParameter(getParameterMap(request, enc,
126: context));
127: context.set(PnutsServlet.SERVLET_PARAM, m);
128: return m;
129: }
130:
131: public String toString() {
132: return "function readParameters( { request { , encoding } } )";
133: }
134: }
|