comparison core/src/luan/modules/IoLuan.java @ 722:647602e8291a

add url options
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 03 Jun 2016 17:51:58 -0600
parents 63cda9eec9a0
children a741a3a33423
comparison
equal deleted inserted replaced
721:524ea0056573 722:647602e8291a
25 import java.net.Socket; 25 import java.net.Socket;
26 import java.net.ServerSocket; 26 import java.net.ServerSocket;
27 import java.net.InetAddress; 27 import java.net.InetAddress;
28 import java.net.MalformedURLException; 28 import java.net.MalformedURLException;
29 import java.net.UnknownHostException; 29 import java.net.UnknownHostException;
30 import java.util.List;
31 import java.util.ArrayList;
32 import java.util.Map; 30 import java.util.Map;
33 import java.util.zip.ZipOutputStream; 31 import java.util.zip.ZipOutputStream;
34 import java.util.zip.ZipEntry; 32 import java.util.zip.ZipEntry;
35 import luan.Luan; 33 import luan.Luan;
36 import luan.LuanState; 34 import luan.LuanState;
167 } 165 }
168 166
169 167
170 168
171 public static abstract class LuanIn { 169 public static abstract class LuanIn {
172 abstract InputStream inputStream() throws IOException; 170 abstract InputStream inputStream() throws IOException, LuanException;
173 public abstract String to_string(); 171 public abstract String to_string();
174 public abstract String to_uri_string(); 172 public abstract String to_uri_string();
175 173
176 public Reader reader() throws IOException { 174 public Reader reader() throws IOException, LuanException {
177 return new InputStreamReader(inputStream()); 175 return new InputStreamReader(inputStream());
178 } 176 }
179 177
180 public String read_text() throws IOException { 178 public String read_text() throws IOException, LuanException {
181 Reader in = reader(); 179 Reader in = reader();
182 String s = Utils.readAll(in); 180 String s = Utils.readAll(in);
183 in.close(); 181 in.close();
184 return s; 182 return s;
185 } 183 }
186 184
187 public byte[] read_binary() throws IOException { 185 public byte[] read_binary() throws IOException, LuanException {
188 InputStream in = inputStream(); 186 InputStream in = inputStream();
189 byte[] a = Utils.readAll(in); 187 byte[] a = Utils.readAll(in);
190 in.close(); 188 in.close();
191 return a; 189 return a;
192 } 190 }
193 191
194 public LuanFunction read_lines() throws IOException { 192 public LuanFunction read_lines() throws IOException, LuanException {
195 return lines(new BufferedReader(reader())); 193 return lines(new BufferedReader(reader()));
196 } 194 }
197 195
198 public LuanFunction read_blocks(Integer blockSize) throws IOException { 196 public LuanFunction read_blocks(Integer blockSize) throws IOException, LuanException {
199 int n = blockSize!=null ? blockSize : Utils.bufSize; 197 int n = blockSize!=null ? blockSize : Utils.bufSize;
200 return blocks(inputStream(),n); 198 return blocks(inputStream(),n);
201 } 199 }
202 200
203 public boolean exists() throws IOException { 201 public boolean exists() throws IOException, LuanException {
204 try { 202 try {
205 inputStream().close(); 203 inputStream().close();
206 return true; 204 return true;
207 } catch(FileNotFoundException e) { 205 } catch(FileNotFoundException e) {
208 return false; 206 return false;
392 public void close() throws IOException { 390 public void close() throws IOException {
393 s = out.toString(); 391 s = out.toString();
394 } 392 }
395 }; 393 };
396 return writer(luanWriter); 394 return writer(luanWriter);
397 }
398 }
399
400 public static final class LuanUrl extends LuanIn {
401 private final URL url;
402
403 private LuanUrl(URL url) {
404 this.url = url;
405 }
406
407 @Override InputStream inputStream() throws IOException {
408 return url.openStream();
409 }
410
411 @Override public String to_string() {
412 return url.toString();
413 }
414
415 @Override public String to_uri_string() {
416 return url.toString();
417 }
418
419 public String post(String postS) throws IOException {
420 return new UrlCall(url).post(postS);
421 }
422
423 @Override public LuanTable table() {
424 LuanTable tbl = super.table();
425 try {
426 tbl.rawPut( "post", new LuanJavaFunction(
427 LuanUrl.class.getMethod( "post", String.class ), this
428 ) );
429 } catch(NoSuchMethodException e) {
430 throw new RuntimeException(e);
431 }
432 return tbl;
433 } 395 }
434 } 396 }
435 397
436 public static final class LuanFile extends LuanIO { 398 public static final class LuanFile extends LuanIO {
437 public final File file; 399 public final File file;
579 } 541 }
580 } 542 }
581 } 543 }
582 } 544 }
583 if( url != null ) 545 if( url != null )
584 return new LuanUrl(url).table(); 546 return new LuanUrl(luan,url,null).table();
585 547
586 return null; 548 return null;
587 } 549 }
588 550
589 private static LuanTable url(String url) throws IOException { 551 private static LuanTable url(LuanState luan,String url,LuanTable options) throws IOException, LuanException {
590 return new LuanUrl(new URL(url)).table(); 552 return new LuanUrl(luan,new URL(url),options).table();
591 } 553 }
592 554
593 public static LuanTable http(String path) throws IOException { 555 public static LuanTable http(LuanState luan,String path,LuanTable options) throws IOException, LuanException {
594 return url("http:"+path); 556 return url(luan,"http:"+path,options);
595 } 557 }
596 558
597 public static LuanTable https(String path) throws IOException { 559 public static LuanTable https(LuanState luan,String path,LuanTable options) throws IOException, LuanException {
598 return url("https:"+path); 560 return url(luan,"https:"+path,options);
599 } 561 }
600 562
601 public static LuanTable luan(LuanState luan,String path) throws LuanException { 563 public static LuanTable luan(LuanState luan,String path) throws LuanException {
602 return classpath( luan, "luan/modules/" + path ); 564 return classpath( luan, "luan/modules/" + path );
603 } 565 }
613 schemes.rawPut( "null", new LuanJavaFunction(IoLuan.class.getMethod("null_"),null) ); 575 schemes.rawPut( "null", new LuanJavaFunction(IoLuan.class.getMethod("null_"),null) );
614 add( schemes, "string", String.class ); 576 add( schemes, "string", String.class );
615 add( schemes, "file", LuanState.class, String.class ); 577 add( schemes, "file", LuanState.class, String.class );
616 add( schemes, "classpath", LuanState.class, String.class ); 578 add( schemes, "classpath", LuanState.class, String.class );
617 add( schemes, "socket", String.class ); 579 add( schemes, "socket", String.class );
618 add( schemes, "http", String.class ); 580 add( schemes, "http", LuanState.class, String.class, LuanTable.class );
619 add( schemes, "https", String.class ); 581 add( schemes, "https", LuanState.class, String.class, LuanTable.class );
620 add( schemes, "luan", LuanState.class, String.class ); 582 add( schemes, "luan", LuanState.class, String.class );
621 add( schemes, "stdin", LuanState.class ); 583 add( schemes, "stdin", LuanState.class );
622 } catch(NoSuchMethodException e) { 584 } catch(NoSuchMethodException e) {
623 throw new RuntimeException(e); 585 throw new RuntimeException(e);
624 } 586 }
633 if( t == null ) 595 if( t == null )
634 return newSchemes(); 596 return newSchemes();
635 return t; 597 return t;
636 } 598 }
637 599
638 public static LuanTable uri(LuanState luan,String name) throws LuanException { 600 public static LuanTable uri(LuanState luan,String name,LuanTable options) throws LuanException {
639 int i = name.indexOf(':'); 601 int i = name.indexOf(':');
640 if( i == -1 ) 602 if( i == -1 )
641 throw new LuanException( "invalid Io.uri name '"+name+"', missing scheme" ); 603 throw new LuanException( "invalid Io.uri name '"+name+"', missing scheme" );
642 String scheme = name.substring(0,i); 604 String scheme = name.substring(0,i);
643 String location = name.substring(i+1); 605 String location = name.substring(i+1);
644 LuanTable schemes = schemes(luan); 606 LuanTable schemes = schemes(luan);
645 LuanFunction opener = (LuanFunction)schemes.get(luan,scheme); 607 LuanFunction opener = (LuanFunction)schemes.get(luan,scheme);
646 if( opener == null ) 608 if( opener == null )
647 throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" ); 609 throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" );
648 return (LuanTable)Luan.first(opener.call(luan,new Object[]{location})); 610 return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
649 } 611 }
650 612
651 public static final class LuanSocket extends LuanIO { 613 public static final class LuanSocket extends LuanIO {
652 private final Socket socket; 614 private final Socket socket;
653 615
715 } 677 }
716 678
717 679
718 // files maps zip name to uri 680 // files maps zip name to uri
719 public static void zip(LuanState luan,String zipUri,LuanTable files) throws LuanException, IOException { 681 public static void zip(LuanState luan,String zipUri,LuanTable files) throws LuanException, IOException {
720 Object obj = uri(luan,zipUri).rawGet("java"); 682 Object obj = uri(luan,zipUri,null).rawGet("java");
721 if( !(obj instanceof LuanIO) ) 683 if( !(obj instanceof LuanIO) )
722 throw new LuanException("invalid uri for zip"); 684 throw new LuanException("invalid uri for zip");
723 LuanIO zipIo = (LuanIO)obj; 685 LuanIO zipIo = (LuanIO)obj;
724 ZipOutputStream out = new ZipOutputStream(zipIo.outputStream()); 686 ZipOutputStream out = new ZipOutputStream(zipIo.outputStream());
725 for( Map.Entry<Object,Object> entry : files.iterable(luan) ) { 687 for( Map.Entry<Object,Object> entry : files.iterable(luan) ) {
730 obj = entry.getValue(); 692 obj = entry.getValue();
731 if( !(obj instanceof String) ) 693 if( !(obj instanceof String) )
732 throw new LuanException("zip file table values must be strings"); 694 throw new LuanException("zip file table values must be strings");
733 String uriStr = (String)obj; 695 String uriStr = (String)obj;
734 out.putNextEntry(new ZipEntry(fileName)); 696 out.putNextEntry(new ZipEntry(fileName));
735 obj = uri(luan,uriStr).rawGet("java"); 697 obj = uri(luan,uriStr,null).rawGet("java");
736 if( !(obj instanceof LuanIn) ) 698 if( !(obj instanceof LuanIn) )
737 throw new LuanException("invalid uri for zip"); 699 throw new LuanException("invalid uri for zip");
738 LuanIn zipIn = (LuanIn)obj; 700 LuanIn zipIn = (LuanIn)obj;
739 InputStream in = zipIn.inputStream(); 701 InputStream in = zipIn.inputStream();
740 Utils.copyAll(in,out); 702 Utils.copyAll(in,out);