Mercurial Hosting > luan
annotate src/luan/modules/url/LuanUrl.java @ 1244:d1911842c2be
minor url post fix
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 09 Jul 2018 00:52:25 -0600 |
parents | 668f29bc52ea |
children | 9fa8b8389578 |
rev | line source |
---|---|
725
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
1 package luan.modules.url; |
722 | 2 |
3 import java.io.InputStream; | |
4 import java.io.InputStreamReader; | |
5 import java.io.OutputStream; | |
6 import java.io.Reader; | |
7 import java.io.IOException; | |
8 import java.io.UnsupportedEncodingException; | |
9 import java.net.URL; | |
10 import java.net.URLConnection; | |
11 import java.net.HttpURLConnection; | |
12 import java.net.URLEncoder; | |
13 import java.util.Map; | |
14 import java.util.HashMap; | |
15 import java.util.List; | |
16 import java.util.Base64; | |
733
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
17 import luan.Luan; |
722 | 18 import luan.LuanState; |
19 import luan.LuanTable; | |
20 import luan.LuanJavaFunction; | |
21 import luan.LuanException; | |
725
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
22 import luan.modules.IoLuan; |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
23 import luan.modules.Utils; |
722 | 24 |
25 | |
26 public final class LuanUrl extends IoLuan.LuanIn { | |
725
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
27 |
722 | 28 private static enum Method { GET, POST, DELETE } |
29 | |
30 private URL url; | |
31 private Method method = Method.GET; | |
32 private Map headers; | |
1244 | 33 private String content = ""; |
725
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
34 private MultipartClient multipart = null; |
733
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
35 private int timeout = 0; |
722 | 36 |
725
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
37 public LuanUrl(LuanState luan,URL url,LuanTable options) throws LuanException { |
722 | 38 this.url = url; |
39 if( options != null ) { | |
40 Map map = options.asMap(luan); | |
41 String methodStr = getString(map,"method"); | |
42 if( methodStr != null ) { | |
43 methodStr = methodStr.toUpperCase(); | |
44 try { | |
45 this.method = Method.valueOf(methodStr); | |
46 } catch(IllegalArgumentException e) { | |
47 throw new LuanException( "invalid method: "+methodStr ); | |
48 } | |
49 } | |
723 | 50 Map headerMap = getMap(luan,map,"headers"); |
51 if( headerMap != null ) { | |
52 headers = new HashMap(); | |
53 for( Object hack : headerMap.entrySet() ) { | |
54 Map.Entry entry = (Map.Entry)hack; | |
1150
0842b9b570f8
change http headers interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1094
diff
changeset
|
55 String name = (String)entry.getKey(); |
723 | 56 Object val = entry.getValue(); |
57 if( val instanceof String ) { | |
58 headers.put(name,val); | |
59 } else { | |
60 if( !(val instanceof LuanTable) ) | |
1150
0842b9b570f8
change http headers interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1094
diff
changeset
|
61 throw new LuanException( "header '"+name+"' must be string or table" ); |
723 | 62 LuanTable t = (LuanTable)val; |
63 if( !t.isList() ) | |
1150
0842b9b570f8
change http headers interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1094
diff
changeset
|
64 throw new LuanException( "header '"+name+"' table must be list" ); |
723 | 65 headers.put(name,t.asList()); |
66 } | |
67 } | |
68 } | |
722 | 69 Map auth = getMap(luan,map,"authorization"); |
70 if( auth != null ) { | |
1151 | 71 if( headers!=null && headers.containsKey("authorization") ) |
72 throw new LuanException( "can't define authorization with header 'authorization' defined" ); | |
722 | 73 String user = getString(auth,"user"); |
74 String password = getString(auth,"password"); | |
75 if( !auth.isEmpty() ) | |
76 throw new LuanException( "unrecognized authorization options: "+auth ); | |
77 StringBuilder sb = new StringBuilder(); | |
78 if( user != null ) | |
79 sb.append(user); | |
80 sb.append(':'); | |
81 if( password != null ) | |
82 sb.append(password); | |
83 String val = "Basic " + Base64.getEncoder().encodeToString(sb.toString().getBytes()); | |
84 if( headers == null ) | |
85 headers = new HashMap(); | |
1151 | 86 headers.put("authorization",val); |
722 | 87 } |
88 Map params = getMap(luan,map,"parameters"); | |
726
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
89 String enctype = getString(map,"enctype"); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
90 if( enctype != null ) { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
91 if( !enctype.equals("multipart/form-data") ) |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
92 throw new LuanException( "unrecognized enctype: "+enctype ); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
93 if( this.method!=Method.POST ) |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
94 throw new LuanException( "multipart/form-data can only be used with POST" ); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
95 if( params==null ) |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
96 throw new LuanException( "multipart/form-data requires parameters" ); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
97 if( params.isEmpty() ) |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
98 throw new LuanException( "multipart/form-data parameters can't be empty" ); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
99 multipart = new MultipartClient(params); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
100 } |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
101 else if( params != null ) { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
102 StringBuilder sb = new StringBuilder(); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
103 for( Object hack : params.entrySet() ) { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
104 Map.Entry entry = (Map.Entry)hack; |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
105 String key = (String)entry.getKey(); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
106 Object val = entry.getValue(); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
107 String keyEnc = encode(key); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
108 if( val instanceof String ) { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
109 and(sb); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
110 sb.append( keyEnc ).append( '=' ).append( encode((String)val) ); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
111 } else { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
112 if( !(val instanceof LuanTable) ) |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
113 throw new LuanException( "parameter '"+key+"' must be string or table" ); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
114 LuanTable t = (LuanTable)val; |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
115 if( !t.isList() ) |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
116 throw new LuanException( "parameter '"+key+"' table must be list" ); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
117 for( Object obj : t.asList() ) { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
118 if( !(obj instanceof String) ) |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
119 throw new LuanException( "parameter '"+key+"' values must be strings" ); |
722 | 120 and(sb); |
726
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
121 sb.append( keyEnc ).append( '=' ).append( encode((String)obj) ); |
722 | 122 } |
123 } | |
726
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
124 } |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
125 if( this.method==Method.POST ) { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
126 content = sb.toString(); |
780
6a87d51ae0ed
allow parameters for HTTP DELETE
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
127 } else { |
726
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
128 String urlS = this.url.toString(); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
129 if( urlS.indexOf('?') == -1 ) { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
130 urlS += '?'; |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
131 } else { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
132 urlS += '&'; |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
133 } |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
134 urlS += sb; |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
135 try { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
136 this.url = new URL(urlS); |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
137 } catch(IOException e) { |
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
138 throw new RuntimeException(e); |
722 | 139 } |
140 } | |
141 } | |
733
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
142 Integer timeout = getInt(map,"time_out"); |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
143 if( timeout != null ) |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
144 this.timeout = timeout; |
722 | 145 if( !map.isEmpty() ) |
146 throw new LuanException( "unrecognized options: "+map ); | |
147 } | |
148 } | |
149 | |
150 private static void and(StringBuilder sb) { | |
151 if( sb.length() > 0 ) | |
152 sb.append('&'); | |
153 } | |
154 | |
155 private static String encode(String s) { | |
156 try { | |
157 return URLEncoder.encode(s,"UTF-8"); | |
158 } catch(UnsupportedEncodingException e) { | |
159 throw new RuntimeException(e); | |
160 } | |
161 } | |
162 | |
163 private static String getString(Map map,String key) throws LuanException { | |
164 Object val = map.remove(key); | |
165 if( val!=null && !(val instanceof String) ) | |
166 throw new LuanException( "parameter '"+key+"' must be a string" ); | |
167 return (String)val; | |
168 } | |
169 | |
733
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
170 private static Integer getInt(Map map,String key) throws LuanException { |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
171 Object val = map.remove(key); |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
172 if( val==null ) |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
173 return null; |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
174 Integer i = Luan.asInteger(val); |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
175 if( i==null ) |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
176 throw new LuanException( "parameter '"+key+"' must be an integer" ); |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
177 return i; |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
178 } |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
179 |
722 | 180 private static LuanTable getTable(Map map,String key) throws LuanException { |
181 Object val = map.remove(key); | |
182 if( val!=null && !(val instanceof LuanTable) ) | |
183 throw new LuanException( "parameter '"+key+"' must be a table" ); | |
184 return (LuanTable)val; | |
185 } | |
186 | |
187 private static Map getMap(LuanState luan,Map map,String key) throws LuanException { | |
188 LuanTable t = getTable(map,key); | |
189 return t==null ? null : t.asMap(luan); | |
190 } | |
191 | |
725
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
192 @Override public InputStream inputStream() throws IOException, LuanException { |
722 | 193 URLConnection con = url.openConnection(); |
733
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
194 if( timeout != 0 ) { |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
195 con.setConnectTimeout(timeout); |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
196 con.setReadTimeout(timeout); |
ffbbe25dab09
add http option time_out
Franklin Schmidt <fschmidt@gmail.com>
parents:
726
diff
changeset
|
197 } |
722 | 198 if( headers != null ) { |
199 for( Object hack : headers.entrySet() ) { | |
200 Map.Entry entry = (Map.Entry)hack; | |
201 String key = (String)entry.getKey(); | |
202 Object val = entry.getValue(); | |
203 if( val instanceof String ) { | |
204 con.addRequestProperty(key,(String)val); | |
205 } else { | |
206 List list = (List)val; | |
207 for( Object obj : list ) { | |
208 con.addRequestProperty(key,(String)obj); | |
209 } | |
210 } | |
211 } | |
212 } | |
1087
4aab4dd3ac9c
improve url error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1085
diff
changeset
|
213 if( !(con instanceof HttpURLConnection) ) { |
4aab4dd3ac9c
improve url error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1085
diff
changeset
|
214 if( method!=Method.GET ) |
4aab4dd3ac9c
improve url error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1085
diff
changeset
|
215 throw new LuanException("method must be GET but is "+method); |
722 | 216 return con.getInputStream(); |
217 } | |
218 | |
219 HttpURLConnection httpCon = (HttpURLConnection)con; | |
220 | |
1087
4aab4dd3ac9c
improve url error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1085
diff
changeset
|
221 if( method==Method.GET ) { |
4aab4dd3ac9c
improve url error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1085
diff
changeset
|
222 return getInputStream(httpCon); |
4aab4dd3ac9c
improve url error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1085
diff
changeset
|
223 } |
4aab4dd3ac9c
improve url error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1085
diff
changeset
|
224 |
722 | 225 if( method==Method.DELETE ) { |
226 httpCon.setRequestMethod("DELETE"); | |
1085
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
227 return getInputStream(httpCon); |
722 | 228 } |
229 | |
230 // POST | |
231 | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1151
diff
changeset
|
232 // httpCon.setRequestProperty("content-type","application/x-www-form-urlencoded"); |
722 | 233 httpCon.setDoOutput(true); |
234 httpCon.setRequestMethod("POST"); | |
235 | |
725
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
236 OutputStream out; |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
237 if( multipart != null ) { |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
238 out = multipart.write(httpCon); |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
239 } else { |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
240 byte[] post = content.getBytes(); |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
241 // httpCon.setRequestProperty("Content-Length",Integer.toString(post.length)); |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
242 out = httpCon.getOutputStream(); |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
243 out.write(post); |
a741a3a33423
add url support for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
723
diff
changeset
|
244 } |
726
14f136a4641f
use enctype for multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
725
diff
changeset
|
245 out.flush(); |
722 | 246 try { |
1085
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
247 return getInputStream(httpCon); |
722 | 248 } finally { |
249 out.close(); | |
250 } | |
251 } | |
252 | |
1085
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
253 private static InputStream getInputStream(HttpURLConnection httpCon) throws IOException, LuanException { |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
254 try { |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
255 return httpCon.getInputStream(); |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
256 } catch(IOException e) { |
1094
cb4c20fce7d0
add "response_code" and "response_message" to http url luan exceptions
Franklin Schmidt <fschmidt@gmail.com>
parents:
1087
diff
changeset
|
257 int responseCode = httpCon.getResponseCode(); |
cb4c20fce7d0
add "response_code" and "response_message" to http url luan exceptions
Franklin Schmidt <fschmidt@gmail.com>
parents:
1087
diff
changeset
|
258 String responseMessage = httpCon.getResponseMessage(); |
1085
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
259 InputStream is = httpCon.getErrorStream(); |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
260 if( is == null ) |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
261 throw e; |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
262 Reader in = new InputStreamReader(is); |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
263 String msg = Utils.readAll(in); |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
264 in.close(); |
1094
cb4c20fce7d0
add "response_code" and "response_message" to http url luan exceptions
Franklin Schmidt <fschmidt@gmail.com>
parents:
1087
diff
changeset
|
265 LuanException le = new LuanException(msg,e); |
cb4c20fce7d0
add "response_code" and "response_message" to http url luan exceptions
Franklin Schmidt <fschmidt@gmail.com>
parents:
1087
diff
changeset
|
266 LuanTable tbl = le.table(); |
cb4c20fce7d0
add "response_code" and "response_message" to http url luan exceptions
Franklin Schmidt <fschmidt@gmail.com>
parents:
1087
diff
changeset
|
267 tbl.rawPut("response_code",responseCode); |
cb4c20fce7d0
add "response_code" and "response_message" to http url luan exceptions
Franklin Schmidt <fschmidt@gmail.com>
parents:
1087
diff
changeset
|
268 tbl.rawPut("response_message",responseMessage); |
cb4c20fce7d0
add "response_code" and "response_message" to http url luan exceptions
Franklin Schmidt <fschmidt@gmail.com>
parents:
1087
diff
changeset
|
269 throw le; |
1085
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
270 } |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
271 } |
a04da9a3e9eb
improve url delete error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
780
diff
changeset
|
272 |
722 | 273 @Override public String to_string() { |
274 return url.toString(); | |
275 } | |
276 | |
277 @Override public String to_uri_string() { | |
278 return url.toString(); | |
279 } | |
280 /* | |
281 public String post(String postS) throws IOException { | |
282 return new UrlCall(url).post(postS); | |
283 } | |
284 | |
285 @Override public LuanTable table() { | |
286 LuanTable tbl = super.table(); | |
287 try { | |
288 tbl.rawPut( "post", new LuanJavaFunction( | |
289 LuanUrl.class.getMethod( "post", String.class ), this | |
290 ) ); | |
291 } catch(NoSuchMethodException e) { | |
292 throw new RuntimeException(e); | |
293 } | |
294 return tbl; | |
295 } | |
296 */ | |
297 } |