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 org.gjt.sp.jedit.bsh;
033:
034: import java.io.*;
035: import java.net.*;
036:
037: /**
038: Remote executor class. Posts a script from the command line to a BshServlet
039: or embedded interpreter using (respectively) HTTP or the bsh telnet
040: service. Output is printed to stdout and a numeric return value is scraped
041: from the result.
042: */
043: public class Remote {
044: public static void main(String args[]) throws Exception {
045: if (args.length < 2) {
046: System.out
047: .println("usage: Remote URL(http|bsh) file [ file ] ... ");
048: System.exit(1);
049: }
050: String url = args[0];
051: String text = getFile(args[1]);
052: int ret = eval(url, text);
053: System.exit(ret);
054: }
055:
056: /**
057: Evaluate text in the interpreter at url, returning a possible integer
058: return value.
059: */
060: public static int eval(String url, String text) throws IOException {
061: String returnValue = null;
062: if (url.startsWith("http:")) {
063: returnValue = doHttp(url, text);
064: } else if (url.startsWith("bsh:")) {
065: returnValue = doBsh(url, text);
066: } else
067: throw new IOException("Unrecognized URL type."
068: + "Scheme must be http:// or bsh://");
069:
070: try {
071: return Integer.parseInt(returnValue);
072: } catch (Exception e) {
073: // this convention may change...
074: return 0;
075: }
076: }
077:
078: static String doBsh(String url, String text) {
079: OutputStream out;
080: InputStream in;
081: String host = "";
082: String port = "";
083: String returnValue = "-1";
084: String orgURL = url;
085:
086: // Need some format checking here
087: try {
088: url = url.substring(6); // remove the bsh://
089: // get the index of the : between the host and the port is located
090: int index = url.indexOf(":");
091: host = url.substring(0, index);
092: port = url.substring(index + 1, url.length());
093: } catch (Exception ex) {
094: System.err.println("Bad URL: " + orgURL + ": " + ex);
095: return returnValue;
096: }
097:
098: try {
099: System.out.println("Connecting to host : " + host
100: + " at port : " + port);
101: Socket s = new Socket(host, Integer.parseInt(port) + 1);
102:
103: out = s.getOutputStream();
104: in = s.getInputStream();
105:
106: sendLine(text, out);
107:
108: BufferedReader bin = new BufferedReader(
109: new InputStreamReader(in));
110: String line;
111: while ((line = bin.readLine()) != null)
112: System.out.println(line);
113:
114: // Need to scrape a value from the last line?
115: returnValue = "1";
116: return returnValue;
117: } catch (Exception ex) {
118: System.err
119: .println("Error communicating with server: " + ex);
120: return returnValue;
121: }
122: }
123:
124: private static void sendLine(String line, OutputStream outPipe)
125: throws IOException {
126: outPipe.write(line.getBytes());
127: outPipe.flush();
128: }
129:
130: /*
131: TODO: this is not unicode friendly, nor is getFile()
132: The output is urlencoded 8859_1 text.
133: should probably be urlencoded UTF-8... how does the servlet determine
134: the encoded charset? I guess we're supposed to add a ";charset" clause
135: to the content type?
136: */
137: static String doHttp(String postURL, String text) {
138: String returnValue = null;
139: StringBuffer sb = new StringBuffer();
140: sb.append("bsh.client=Remote");
141: sb.append("&bsh.script=");
142: sb.append(URLEncoder.encode(text));
143: /*
144: // This requires Java 1.3
145: try {
146: sb.append( URLEncoder.encode( text, "8859_1" ) );
147: } catch ( UnsupportedEncodingException e ) {
148: e.printStackTrace();
149: }
150: */
151: String formData = sb.toString();
152:
153: try {
154: URL url = new URL(postURL);
155: HttpURLConnection urlcon = (HttpURLConnection) url
156: .openConnection();
157: urlcon.setRequestMethod("POST");
158: urlcon.setRequestProperty("Content-type",
159: "application/x-www-form-urlencoded");
160: urlcon.setDoOutput(true);
161: urlcon.setDoInput(true);
162: PrintWriter pout = new PrintWriter(new OutputStreamWriter(
163: urlcon.getOutputStream(), "8859_1"), true);
164: pout.print(formData);
165: pout.flush();
166:
167: // read results...
168: int rc = urlcon.getResponseCode();
169: if (rc != HttpURLConnection.HTTP_OK)
170: System.out.println("Error, HTTP response: " + rc);
171:
172: returnValue = urlcon.getHeaderField("Bsh-Return");
173:
174: BufferedReader bin = new BufferedReader(
175: new InputStreamReader(urlcon.getInputStream()));
176: String line;
177: while ((line = bin.readLine()) != null)
178: System.out.println(line);
179:
180: System.out.println("Return Value: " + returnValue);
181:
182: } catch (MalformedURLException e) {
183: System.out.println(e); // bad postURL
184: } catch (IOException e2) {
185: System.out.println(e2); // I/O error
186: }
187:
188: return returnValue;
189: }
190:
191: /*
192: Note: assumes default character encoding
193: */
194: static String getFile(String name) throws FileNotFoundException,
195: IOException {
196: StringBuffer sb = new StringBuffer();
197: BufferedReader bin = new BufferedReader(new FileReader(name));
198: String line;
199: while ((line = bin.readLine()) != null)
200: sb.append(line).append("\n");
201: return sb.toString();
202: }
203:
204: }
|