comparison src/luan/modules/url/LuanUrl.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents 25746915a241
children 0cceff521abb
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
193 private static Map getMap(Map map,String key) throws LuanException { 193 private static Map getMap(Map map,String key) throws LuanException {
194 LuanTable t = getTable(map,key); 194 LuanTable t = getTable(map,key);
195 return t==null ? null : t.asMap(); 195 return t==null ? null : t.asMap();
196 } 196 }
197 197
198 @Override public InputStream inputStream(Luan luan) throws IOException, LuanException { 198 @Override public InputStream inputStream() throws IOException, LuanException {
199 try { 199 try {
200 return inputStream(luan,null); 200 return inputStream(null);
201 } catch(AuthException e) { 201 } catch(AuthException e) {
202 try { 202 try {
203 return inputStream(luan,e.authorization); 203 return inputStream(e.authorization);
204 } catch(AuthException e2) { 204 } catch(AuthException e2) {
205 throw new RuntimeException(e2); // never 205 throw new RuntimeException(e2); // never
206 } 206 }
207 } 207 }
208 } 208 }
209 209
210 private InputStream inputStream(Luan luan,String authorization) 210 private InputStream inputStream(String authorization)
211 throws IOException, LuanException, AuthException 211 throws IOException, LuanException, AuthException
212 { 212 {
213 URLConnection con = url.openConnection(); 213 URLConnection con = url.openConnection();
214 if( timeout != 0 ) { 214 if( timeout != 0 ) {
215 con.setConnectTimeout(timeout); 215 con.setConnectTimeout(timeout);
236 } 236 }
237 237
238 HttpURLConnection httpCon = (HttpURLConnection)con; 238 HttpURLConnection httpCon = (HttpURLConnection)con;
239 239
240 if( method==Method.GET ) { 240 if( method==Method.GET ) {
241 return getInputStream(luan,httpCon,authorization); 241 return getInputStream(httpCon,authorization);
242 } 242 }
243 243
244 if( method==Method.DELETE ) { 244 if( method==Method.DELETE ) {
245 httpCon.setRequestMethod("DELETE"); 245 httpCon.setRequestMethod("DELETE");
246 return getInputStream(luan,httpCon,authorization); 246 return getInputStream(httpCon,authorization);
247 } 247 }
248 248
249 // POST 249 // POST
250 250
251 // httpCon.setRequestProperty("content-type","application/x-www-form-urlencoded"); 251 // httpCon.setRequestProperty("content-type","application/x-www-form-urlencoded");
261 out = httpCon.getOutputStream(); 261 out = httpCon.getOutputStream();
262 out.write(post); 262 out.write(post);
263 } 263 }
264 out.flush(); 264 out.flush();
265 try { 265 try {
266 return getInputStream(luan,httpCon,authorization); 266 return getInputStream(httpCon,authorization);
267 } finally { 267 } finally {
268 out.close(); 268 out.close();
269 } 269 }
270 } 270 }
271 271
272 private InputStream getInputStream(Luan luan,HttpURLConnection httpCon,String authorization) 272 private InputStream getInputStream(HttpURLConnection httpCon,String authorization)
273 throws IOException, LuanException, AuthException 273 throws IOException, LuanException, AuthException
274 { 274 {
275 try { 275 try {
276 return httpCon.getInputStream(); 276 return httpCon.getInputStream();
277 } catch(FileNotFoundException e) { 277 } catch(FileNotFoundException e) {
333 throw e; 333 throw e;
334 Reader in = new InputStreamReader(is); 334 Reader in = new InputStreamReader(is);
335 String msg = Utils.readAll(in); 335 String msg = Utils.readAll(in);
336 in.close(); 336 in.close();
337 LuanException le = new LuanException(msg,e); 337 LuanException le = new LuanException(msg,e);
338 LuanTable tbl = le.table(luan); 338 le.put("response_code",responseCode);
339 tbl.rawPut("response_code",responseCode); 339 le.put("response_message",responseMessage);
340 tbl.rawPut("response_message",responseMessage);
341 throw le; 340 throw le;
342 } 341 }
343 } 342 }
344 343
345 @Override public String to_string() { 344 @Override public String to_string() {