1 33 34 package bsh; 35 36 import java.io.*; 37 import java.net.*; 38 import java.text.*; 39 45 public class Remote 46 { 47 public static void main( String args[] ) 48 throws Exception 49 { 50 if ( args.length < 2 ) { 51 System.out.println( 52 "usage: Remote URL(http|bsh) file [ file ] ... "); 53 System.exit(1); 54 } 55 String url = args[0]; 56 String text = getFile(args[1]); 57 int ret = eval( url, text ); 58 System.exit( ret ); 59 } 60 61 65 public static int eval( String url, String text ) 66 throws IOException 67 { 68 String returnValue = null; 69 if ( url.startsWith( "http:" ) ) { 70 returnValue = doHttp( url, text ); 71 } else if ( url.startsWith( "bsh:" ) ) { 72 returnValue = doBsh( url, text ); 73 } else 74 throw new IOException( "Unrecognized URL type." 75 +"Scheme must be http:// or bsh://"); 76 77 try { 78 return Integer.parseInt( returnValue ); 79 } catch ( Exception e ) { 80 return 0; 82 } 83 } 84 85 static String doBsh( String url, String text ) 86 { 87 OutputStream out; 88 InputStream in; 89 String host = ""; 90 String port = ""; 91 String returnValue = "-1"; 92 String orgURL = url; 93 94 try { 96 url = url.substring(6); int index = url.indexOf(":"); 99 host = url.substring(0,index); 100 port = url.substring(index+1,url.length()); 101 } catch ( Exception ex ) { 102 System.err.println("Bad URL: "+orgURL+": "+ex ); 103 return returnValue; 104 } 105 106 try { 107 System.out.println("Connecting to host : " 108 + host + " at port : " + port); 109 Socket s = new Socket(host, Integer.parseInt(port) + 1); 110 111 out = s.getOutputStream(); 112 in = s.getInputStream(); 113 114 sendLine( text, out ); 115 116 BufferedReader bin = new BufferedReader( 117 new InputStreamReader(in)); 118 String line; 119 while ( (line=bin.readLine()) != null ) 120 System.out.println( line ); 121 122 returnValue="1"; 124 return returnValue; 125 } catch(Exception ex) { 126 System.err.println("Error communicating with server: "+ex); 127 return returnValue; 128 } 129 } 130 131 private static void sendLine( String line, OutputStream outPipe ) 132 throws IOException 133 { 134 outPipe.write( line.getBytes() ); 135 outPipe.flush(); 136 } 137 138 139 146 static String doHttp( String postURL, String text ) 147 { 148 String returnValue = null; 149 StringBuffer sb = new StringBuffer (); 150 sb.append( "bsh.client=Remote" ); 151 sb.append( "&bsh.script=" ); 152 sb.append( URLEncoder.encode( text ) ); 153 161 String formData = sb.toString( ); 162 163 try { 164 URL url = new URL( postURL ); 165 HttpURLConnection urlcon = 166 (HttpURLConnection) url.openConnection( ); 167 urlcon.setRequestMethod("POST"); 168 urlcon.setRequestProperty("Content-type", 169 "application/x-www-form-urlencoded"); 170 urlcon.setDoOutput(true); 171 urlcon.setDoInput(true); 172 PrintWriter pout = new PrintWriter( new OutputStreamWriter( 173 urlcon.getOutputStream(), "8859_1"), true ); 174 pout.print( formData ); 175 pout.flush(); 176 177 int rc = urlcon.getResponseCode(); 179 if ( rc != HttpURLConnection.HTTP_OK ) 180 System.out.println("Error, HTTP response: "+rc ); 181 182 returnValue = urlcon.getHeaderField("Bsh-Return"); 183 184 BufferedReader bin = new BufferedReader( 185 new InputStreamReader( urlcon.getInputStream() ) ); 186 String line; 187 while ( (line=bin.readLine()) != null ) 188 System.out.println( line ); 189 190 System.out.println( "Return Value: "+returnValue ); 191 192 } catch (MalformedURLException e) { 193 System.out.println(e); } catch (IOException e2) { 195 System.out.println(e2); } 197 198 return returnValue; 199 } 200 201 204 static String getFile( String name ) 205 throws FileNotFoundException, IOException 206 { 207 StringBuffer sb = new StringBuffer (); 208 BufferedReader bin = new BufferedReader( new FileReader( name ) ); 209 String line; 210 while ( (line=bin.readLine()) != null ) 211 sb.append( line ).append( "\n" ); 212 return sb.toString(); 213 } 214 215 } 216 | Popular Tags |