PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/Remote.java

#
Java | 215 lines | 126 code | 24 blank | 65 comment | 12 complexity | b5dfcc28cf6d214298543e138cd26f26 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package org.gjt.sp.jedit.bsh;
  34. import java.io.*;
  35. import java.net.*;
  36. /**
  37. Remote executor class. Posts a script from the command line to a BshServlet
  38. or embedded interpreter using (respectively) HTTP or the bsh telnet
  39. service. Output is printed to stdout and a numeric return value is scraped
  40. from the result.
  41. */
  42. public class Remote
  43. {
  44. public static void main( String args[] )
  45. throws Exception
  46. {
  47. if ( args.length < 2 ) {
  48. System.out.println(
  49. "usage: Remote URL(http|bsh) file [ file ] ... ");
  50. System.exit(1);
  51. }
  52. String url = args[0];
  53. String text = getFile(args[1]);
  54. int ret = eval( url, text );
  55. System.exit( ret );
  56. }
  57. /**
  58. Evaluate text in the interpreter at url, returning a possible integer
  59. return value.
  60. */
  61. public static int eval( String url, String text )
  62. throws IOException
  63. {
  64. String returnValue = null;
  65. if ( url.startsWith( "http:" ) ) {
  66. returnValue = doHttp( url, text );
  67. } else if ( url.startsWith( "bsh:" ) ) {
  68. returnValue = doBsh( url, text );
  69. } else
  70. throw new IOException( "Unrecognized URL type."
  71. +"Scheme must be http:// or bsh://");
  72. try {
  73. return Integer.parseInt( returnValue );
  74. } catch ( Exception e ) {
  75. // this convention may change...
  76. return 0;
  77. }
  78. }
  79. static String doBsh( String url, String text )
  80. {
  81. OutputStream out;
  82. InputStream in;
  83. String host = "";
  84. String port = "";
  85. String returnValue = "-1";
  86. String orgURL = url;
  87. // Need some format checking here
  88. try {
  89. url = url.substring(6); // remove the bsh://
  90. // get the index of the : between the host and the port is located
  91. int index = url.indexOf(":");
  92. host = url.substring(0,index);
  93. port = url.substring(index+1,url.length());
  94. } catch ( Exception ex ) {
  95. System.err.println("Bad URL: "+orgURL+": "+ex );
  96. return returnValue;
  97. }
  98. try {
  99. System.out.println("Connecting to host : "
  100. + host + " at port : " + port);
  101. Socket s = new Socket(host, Integer.parseInt(port) + 1);
  102. out = s.getOutputStream();
  103. in = s.getInputStream();
  104. sendLine( text, out );
  105. BufferedReader bin = new BufferedReader(
  106. new InputStreamReader(in));
  107. String line;
  108. while ( (line=bin.readLine()) != null )
  109. System.out.println( line );
  110. // Need to scrape a value from the last line?
  111. returnValue="1";
  112. return returnValue;
  113. } catch(Exception ex) {
  114. System.err.println("Error communicating with server: "+ex);
  115. return returnValue;
  116. }
  117. }
  118. private static void sendLine( String line, OutputStream outPipe )
  119. throws IOException
  120. {
  121. outPipe.write( line.getBytes() );
  122. outPipe.flush();
  123. }
  124. /*
  125. TODO: this is not unicode friendly, nor is getFile()
  126. The output is urlencoded 8859_1 text.
  127. should probably be urlencoded UTF-8... how does the servlet determine
  128. the encoded charset? I guess we're supposed to add a ";charset" clause
  129. to the content type?
  130. */
  131. static String doHttp( String postURL, String text )
  132. {
  133. String returnValue = null;
  134. StringBuilder sb = new StringBuilder();
  135. sb.append( "bsh.client=Remote" );
  136. sb.append( "&bsh.script=" );
  137. sb.append( URLEncoder.encode( text ) );
  138. /*
  139. // This requires Java 1.3
  140. try {
  141. sb.append( URLEncoder.encode( text, "8859_1" ) );
  142. } catch ( UnsupportedEncodingException e ) {
  143. e.printStackTrace();
  144. }
  145. */
  146. String formData = sb.toString( );
  147. try {
  148. URL url = new URL( postURL );
  149. HttpURLConnection urlcon =
  150. (HttpURLConnection) url.openConnection( );
  151. urlcon.setRequestMethod("POST");
  152. urlcon.setRequestProperty("Content-type",
  153. "application/x-www-form-urlencoded");
  154. urlcon.setDoOutput(true);
  155. urlcon.setDoInput(true);
  156. PrintWriter pout = new PrintWriter( new OutputStreamWriter(
  157. urlcon.getOutputStream(), "8859_1"), true );
  158. pout.print( formData );
  159. pout.flush();
  160. // read results...
  161. int rc = urlcon.getResponseCode();
  162. if ( rc != HttpURLConnection.HTTP_OK )
  163. System.out.println("Error, HTTP response: "+rc );
  164. returnValue = urlcon.getHeaderField("Bsh-Return");
  165. BufferedReader bin = new BufferedReader(
  166. new InputStreamReader( urlcon.getInputStream() ) );
  167. String line;
  168. while ( (line=bin.readLine()) != null )
  169. System.out.println( line );
  170. System.out.println( "Return Value: "+returnValue );
  171. } catch (MalformedURLException e) {
  172. System.out.println(e); // bad postURL
  173. } catch (IOException e2) {
  174. System.out.println(e2); // I/O error
  175. }
  176. return returnValue;
  177. }
  178. /*
  179. Note: assumes default character encoding
  180. */
  181. static String getFile( String name )
  182. throws FileNotFoundException, IOException
  183. {
  184. StringBuilder sb = new StringBuilder();
  185. BufferedReader bin = new BufferedReader( new FileReader( name ) );
  186. String line;
  187. while ( (line=bin.readLine()) != null )
  188. sb.append( line ).append( "\n" );
  189. return sb.toString();
  190. }
  191. }