comparison core/src/luan/modules/url/LuanUrl.java @ 733:ffbbe25dab09

add http option time_out
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Jun 2016 00:04:08 -0600
parents 14f136a4641f
children
comparison
equal deleted inserted replaced
732:d7371dc8c2e7 733:ffbbe25dab09
12 import java.net.URLEncoder; 12 import java.net.URLEncoder;
13 import java.util.Map; 13 import java.util.Map;
14 import java.util.HashMap; 14 import java.util.HashMap;
15 import java.util.List; 15 import java.util.List;
16 import java.util.Base64; 16 import java.util.Base64;
17 import luan.Luan;
17 import luan.LuanState; 18 import luan.LuanState;
18 import luan.LuanTable; 19 import luan.LuanTable;
19 import luan.LuanJavaFunction; 20 import luan.LuanJavaFunction;
20 import luan.LuanException; 21 import luan.LuanException;
21 import luan.modules.IoLuan; 22 import luan.modules.IoLuan;
29 private URL url; 30 private URL url;
30 private Method method = Method.GET; 31 private Method method = Method.GET;
31 private Map headers; 32 private Map headers;
32 private String content = null; 33 private String content = null;
33 private MultipartClient multipart = null; 34 private MultipartClient multipart = null;
35 private int timeout = 0;
34 36
35 public LuanUrl(LuanState luan,URL url,LuanTable options) throws LuanException { 37 public LuanUrl(LuanState luan,URL url,LuanTable options) throws LuanException {
36 this.url = url; 38 this.url = url;
37 if( options != null ) { 39 if( options != null ) {
38 Map map = options.asMap(luan); 40 Map map = options.asMap(luan);
138 } catch(IOException e) { 140 } catch(IOException e) {
139 throw new RuntimeException(e); 141 throw new RuntimeException(e);
140 } 142 }
141 } 143 }
142 } 144 }
145 Integer timeout = getInt(map,"time_out");
146 if( timeout != null )
147 this.timeout = timeout;
143 if( !map.isEmpty() ) 148 if( !map.isEmpty() )
144 throw new LuanException( "unrecognized options: "+map ); 149 throw new LuanException( "unrecognized options: "+map );
145 } 150 }
146 } 151 }
147 152
181 if( val!=null && !(val instanceof String) ) 186 if( val!=null && !(val instanceof String) )
182 throw new LuanException( "parameter '"+key+"' must be a string" ); 187 throw new LuanException( "parameter '"+key+"' must be a string" );
183 return (String)val; 188 return (String)val;
184 } 189 }
185 190
191 private static Integer getInt(Map map,String key) throws LuanException {
192 Object val = map.remove(key);
193 if( val==null )
194 return null;
195 Integer i = Luan.asInteger(val);
196 if( i==null )
197 throw new LuanException( "parameter '"+key+"' must be an integer" );
198 return i;
199 }
200
186 private static LuanTable getTable(Map map,String key) throws LuanException { 201 private static LuanTable getTable(Map map,String key) throws LuanException {
187 Object val = map.remove(key); 202 Object val = map.remove(key);
188 if( val!=null && !(val instanceof LuanTable) ) 203 if( val!=null && !(val instanceof LuanTable) )
189 throw new LuanException( "parameter '"+key+"' must be a table" ); 204 throw new LuanException( "parameter '"+key+"' must be a table" );
190 return (LuanTable)val; 205 return (LuanTable)val;
195 return t==null ? null : t.asMap(luan); 210 return t==null ? null : t.asMap(luan);
196 } 211 }
197 212
198 @Override public InputStream inputStream() throws IOException, LuanException { 213 @Override public InputStream inputStream() throws IOException, LuanException {
199 URLConnection con = url.openConnection(); 214 URLConnection con = url.openConnection();
215 if( timeout != 0 ) {
216 con.setConnectTimeout(timeout);
217 con.setReadTimeout(timeout);
218 }
200 if( headers != null ) { 219 if( headers != null ) {
201 for( Object hack : headers.entrySet() ) { 220 for( Object hack : headers.entrySet() ) {
202 Map.Entry entry = (Map.Entry)hack; 221 Map.Entry entry = (Map.Entry)hack;
203 String key = (String)entry.getKey(); 222 String key = (String)entry.getKey();
204 Object val = entry.getValue(); 223 Object val = entry.getValue();