001: /*****************************************************************************
002: * *
003: * This file is part of the BeanShell Java Scripting distribution. *
004: * Documentation and updates may be found at http://www.beanshell.org/ *
005: * *
006: * Sun Public License Notice: *
007: * *
008: * The contents of this file are subject to the Sun Public License Version *
009: * 1.0 (the "License"); you may not use this file except in compliance with *
010: * the License. A copy of the License is available at http://www.sun.com *
011: * *
012: * The Original Code is BeanShell. The Initial Developer of the Original *
013: * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014: * (C) 2000. All Rights Reserved. *
015: * *
016: * GNU Public License Notice: *
017: * *
018: * Alternatively, the contents of this file may be used under the terms of *
019: * the GNU Lesser General Public License (the "LGPL"), in which case the *
020: * provisions of LGPL are applicable instead of those above. If you wish to *
021: * allow use of your version of this file only under the terms of the LGPL *
022: * and not to allow others to use your version of this file under the SPL, *
023: * indicate your decision by deleting the provisions above and replace *
024: * them with the notice and other provisions required by the LGPL. If you *
025: * do not delete the provisions above, a recipient may use your version of *
026: * this file under either the SPL or the LGPL. *
027: * *
028: * Patrick Niemeyer (pat@pat.net) *
029: * Author of Learning Java, O'Reilly & Associates *
030: * http://www.pat.net/~pat/ *
031: * *
032: *****************************************************************************/package bsh;
033:
034: import java.io.*;
035: import java.net.*;
036: import java.text.*;
037:
038: /**
039: Remote executor class. Posts a script from the command line to a BshServlet
040: or embedded interpreter using (respectively) HTTP or the bsh telnet
041: service. Output is printed to stdout and a numeric return value is scraped
042: from the result.
043: */
044: public class Remote {
045: public static void main(String args[]) throws Exception {
046: if (args.length < 2) {
047: System.out
048: .println("usage: Remote URL(http|bsh) file [ file ] ... ");
049: System.exit(1);
050: }
051: String url = args[0];
052: String text = getFile(args[1]);
053: int ret = eval(url, text);
054: System.exit(ret);
055: }
056:
057: /**
058: Evaluate text in the interpreter at url, returning a possible integer
059: return value.
060: */
061: public static int eval(String url, String text) throws IOException {
062: String returnValue = null;
063: if (url.startsWith("http:")) {
064: returnValue = doHttp(url, text);
065: } else if (url.startsWith("bsh:")) {
066: returnValue = doBsh(url, text);
067: } else
068: throw new IOException("Unrecognized URL type."
069: + "Scheme must be http:// or bsh://");
070:
071: try {
072: return Integer.parseInt(returnValue);
073: } catch (Exception e) {
074: // this convention may change...
075: return 0;
076: }
077: }
078:
079: static String doBsh(String url, String text) {
080: OutputStream out;
081: InputStream in;
082: String host = "";
083: String port = "";
084: String returnValue = "-1";
085: String orgURL = url;
086:
087: // Need some format checking here
088: try {
089: url = url.substring(6); // remove the bsh://
090: // get the index of the : between the host and the port is located
091: int index = url.indexOf(":");
092: host = url.substring(0, index);
093: port = url.substring(index + 1, url.length());
094: } catch (Exception ex) {
095: System.err.println("Bad URL: " + orgURL + ": " + ex);
096: return returnValue;
097: }
098:
099: try {
100: System.out.println("Connecting to host : " + host
101: + " at port : " + port);
102: Socket s = new Socket(host, Integer.parseInt(port) + 1);
103:
104: out = s.getOutputStream();
105: in = s.getInputStream();
106:
107: sendLine(text, out);
108:
109: BufferedReader bin = new BufferedReader(
110: new InputStreamReader(in));
111: String line;
112: while ((line = bin.readLine()) != null)
113: System.out.println(line);
114:
115: // Need to scrape a value from the last line?
116: returnValue = "1";
117: return returnValue;
118: } catch (Exception ex) {
119: System.err
120: .println("Error communicating with server: " + ex);
121: return returnValue;
122: }
123: }
124:
125: private static void sendLine(String line, OutputStream outPipe)
126: throws IOException {
127: outPipe.write(line.getBytes());
128: outPipe.flush();
129: }
130:
131: /*
132: TODO: this is not unicode friendly, nor is getFile()
133: The output is urlencoded 8859_1 text.
134: should probably be urlencoded UTF-8... how does the servlet determine
135: the encoded charset? I guess we're supposed to add a ";charset" clause
136: to the content type?
137: */
138: static String doHttp(String postURL, String text) {
139: String returnValue = null;
140: StringBuffer sb = new StringBuffer();
141: sb.append("bsh.client=Remote");
142: sb.append("&bsh.script=");
143: sb.append(URLEncoder.encode(text));
144: /*
145: // This requires Java 1.3
146: try {
147: sb.append( URLEncoder.encode( text, "8859_1" ) );
148: } catch ( UnsupportedEncodingException e ) {
149: e.printStackTrace();
150: }
151: */
152: String formData = sb.toString();
153:
154: try {
155: URL url = new URL(postURL);
156: HttpURLConnection urlcon = (HttpURLConnection) url
157: .openConnection();
158: urlcon.setRequestMethod("POST");
159: urlcon.setRequestProperty("Content-type",
160: "application/x-www-form-urlencoded");
161: urlcon.setDoOutput(true);
162: urlcon.setDoInput(true);
163: PrintWriter pout = new PrintWriter(new OutputStreamWriter(
164: urlcon.getOutputStream(), "8859_1"), true);
165: pout.print(formData);
166: pout.flush();
167:
168: // read results...
169: int rc = urlcon.getResponseCode();
170: if (rc != HttpURLConnection.HTTP_OK)
171: System.out.println("Error, HTTP response: " + rc);
172:
173: returnValue = urlcon.getHeaderField("Bsh-Return");
174:
175: BufferedReader bin = new BufferedReader(
176: new InputStreamReader(urlcon.getInputStream()));
177: String line;
178: while ((line = bin.readLine()) != null)
179: System.out.println(line);
180:
181: System.out.println("Return Value: " + returnValue);
182:
183: } catch (MalformedURLException e) {
184: System.out.println(e); // bad postURL
185: } catch (IOException e2) {
186: System.out.println(e2); // I/O error
187: }
188:
189: return returnValue;
190: }
191:
192: /*
193: Note: assumes default character encoding
194: */
195: static String getFile(String name) throws FileNotFoundException,
196: IOException {
197: StringBuffer sb = new StringBuffer();
198: BufferedReader bin = new BufferedReader(new FileReader(name));
199: String line;
200: while ((line = bin.readLine()) != null)
201: sb.append(line).append("\n");
202: return sb.toString();
203: }
204:
205: }
|