001: /*
002: * sendPostRequest.java
003: *
004: * Copyright (c) 1997-2005 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 java.io.DataOutputStream;
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.net.URL;
015: import java.net.URLConnection;
016: import java.util.Iterator;
017: import java.util.Map;
018: import pnuts.lang.Context;
019: import pnuts.lang.PnutsException;
020: import pnuts.lang.PnutsFunction;
021: import org.pnuts.net.*;
022:
023: /*
024: * sendPostRequest(url, map, encoding {, handler(urlConnection), { requestProperties } })
025: */
026: public class sendPostRequest extends PnutsFunction {
027:
028: public sendPostRequest() {
029: super ("sendPostRequest");
030: }
031:
032: public boolean defined(int narg) {
033: return (narg == 3 || narg == 4 || narg == 5);
034: }
035:
036: protected Object exec(Object[] args, Context context) {
037: int nargs = args.length;
038: URL url;
039: Map param;
040: String encoding;
041: PnutsFunction handler = null;
042: Map hdrs = null;
043:
044: if (nargs == 3) {
045: url = (URL) args[0];
046: param = (Map) args[1];
047: encoding = (String) args[2];
048: } else if (nargs == 4) {
049: url = (URL) args[0];
050: param = (Map) args[1];
051: encoding = (String) args[2];
052: handler = (PnutsFunction) args[3];
053: } else if (nargs == 5) {
054: url = (URL) args[0];
055: param = (Map) args[1];
056: encoding = (String) args[2];
057: handler = (PnutsFunction) args[3];
058: hdrs = (Map) args[4];
059: } else {
060: undefined(args, context);
061: return null;
062: }
063: post(url, hdrs, param, encoding, handler, context);
064: return null;
065: }
066:
067: static void post(URL url, Map hdrs, Map param, String encoding,
068: PnutsFunction handler, Context context) {
069: try {
070: URLConnection con = url.openConnection();
071: if (hdrs != null) {
072: for (Iterator it = hdrs.entrySet().iterator(); it
073: .hasNext();) {
074: Map.Entry entry = (Map.Entry) it.next();
075: con.setRequestProperty((String) entry.getKey(),
076: (String) entry.getValue());
077: }
078: }
079: con.setDoOutput(true);
080: con.setUseCaches(false);
081: con.setAllowUserInteraction(false);
082: con.setRequestProperty("content-type",
083: "application/x-www-form-urlencoded");
084:
085: String message = URLEncoding.makeQueryString(param,
086: encoding);
087: DataOutputStream out = new DataOutputStream(con
088: .getOutputStream());
089: out.writeBytes(message);
090: out.close();
091: if (handler != null) {
092: handler.call(new Object[] { con }, context);
093: } else {
094: InputStream in = con.getInputStream();
095: byte[] buf = new byte[512];
096: while (in.read(buf) != -1) {
097: }
098: }
099: } catch (IOException e) {
100: throw new PnutsException(e, context);
101: }
102: }
103:
104: public String toString() {
105: return "function sendPostRequest(url, map, encoding {, handler(urlConnection) { , requestProperties }})";
106: }
107: }
|