comparison src/luan/modules/url/LuanUrl.java @ 1389:eb8b35dccd99

cleanup and stringify change
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 03 Sep 2019 22:54:31 -0600
parents 45363886f256
children 002152af497a
comparison
equal deleted inserted replaced
1388:2024d23ddd64 1389:eb8b35dccd99
42 public HttpURLConnection httpCon; 42 public HttpURLConnection httpCon;
43 43
44 public LuanUrl(URL url,LuanTable options) throws LuanException { 44 public LuanUrl(URL url,LuanTable options) throws LuanException {
45 if( options != null ) { 45 if( options != null ) {
46 Map map = options.asMap(); 46 Map map = options.asMap();
47 String methodStr = getString(map,"method"); 47 String methodStr = Utils.removeString(map,"method");
48 if( methodStr != null ) { 48 if( methodStr != null ) {
49 methodStr = methodStr.toUpperCase(); 49 methodStr = methodStr.toUpperCase();
50 try { 50 try {
51 this.method = Method.valueOf(methodStr); 51 this.method = Method.valueOf(methodStr);
52 } catch(IllegalArgumentException e) { 52 } catch(IllegalArgumentException e) {
53 throw new LuanException( "invalid method: "+methodStr ); 53 throw new LuanException( "invalid method: "+methodStr );
54 } 54 }
55 } 55 }
56 Map headerMap = getMap(map,"headers"); 56 Map headerMap = removeMap(map,"headers");
57 if( headerMap != null ) { 57 if( headerMap != null ) {
58 for( Object hack : headerMap.entrySet() ) { 58 for( Object hack : headerMap.entrySet() ) {
59 Map.Entry entry = (Map.Entry)hack; 59 Map.Entry entry = (Map.Entry)hack;
60 String name = (String)entry.getKey(); 60 String name = (String)entry.getKey();
61 Object val = entry.getValue(); 61 Object val = entry.getValue();
69 throw new LuanException( "header '"+name+"' table must be list" ); 69 throw new LuanException( "header '"+name+"' table must be list" );
70 headers.put(name,t.asList()); 70 headers.put(name,t.asList());
71 } 71 }
72 } 72 }
73 } 73 }
74 Map auth = getMap(map,"authorization"); 74 Map auth = removeMap(map,"authorization");
75 if( auth != null ) { 75 if( auth != null ) {
76 if( headers!=null && headers.containsKey("authorization") ) 76 if( headers!=null && headers.containsKey("authorization") )
77 throw new LuanException( "can't define authorization with header 'authorization' defined" ); 77 throw new LuanException( "can't define authorization with header 'authorization' defined" );
78 String username = getString(auth,"username"); 78 String username = Utils.removeString(auth,"username");
79 if( username==null ) username = ""; 79 if( username==null ) username = "";
80 String password = getString(auth,"password"); 80 String password = Utils.removeString(auth,"password");
81 if( password==null ) password = ""; 81 if( password==null ) password = "";
82 String type = getString(auth,"type"); 82 String type = Utils.removeString(auth,"type");
83 if( !auth.isEmpty() ) 83 if( !auth.isEmpty() )
84 throw new LuanException( "unrecognized authorization options: "+auth ); 84 throw new LuanException( "unrecognized authorization options: "+auth );
85 if( type != null ) { 85 if( type != null ) {
86 if( !type.toLowerCase().equals("basic") ) 86 if( !type.toLowerCase().equals("basic") )
87 throw new LuanException( "authorization type can only be 'basic' or nil" ); 87 throw new LuanException( "authorization type can only be 'basic' or nil" );
90 } else { 90 } else {
91 authUsername = username; 91 authUsername = username;
92 authPassword = password; 92 authPassword = password;
93 } 93 }
94 } 94 }
95 Map params = getMap(map,"parameters"); 95 Map params = removeMap(map,"parameters");
96 String enctype = getString(map,"enctype"); 96 String enctype = Utils.removeString(map,"enctype");
97 Object content = map.remove("content"); 97 Object content = map.remove("content");
98 if( content != null ) { 98 if( content != null ) {
99 if( this.method != Method.POST ) 99 if( this.method != Method.POST )
100 throw new LuanException( "content can only be used with POST" ); 100 throw new LuanException( "content can only be used with POST" );
101 if( params != null ) 101 if( params != null )
157 } catch(IOException e) { 157 } catch(IOException e) {
158 throw new RuntimeException(e); 158 throw new RuntimeException(e);
159 } 159 }
160 } 160 }
161 } 161 }
162 Integer timeout = getInt(map,"time_out"); 162 Integer timeout = Utils.removeInt(map,"time_out");
163 if( timeout != null ) 163 if( timeout != null )
164 this.timeout = timeout; 164 this.timeout = timeout;
165 if( !map.isEmpty() ) 165 Utils.checkEmpty(map);
166 throw new LuanException( "unrecognized options: "+map );
167 } 166 }
168 this.url = url; 167 this.url = url;
169 } 168 }
170 169
171 private static void and(StringBuilder sb) { 170 private static void and(StringBuilder sb) {
179 } catch(UnsupportedEncodingException e) { 178 } catch(UnsupportedEncodingException e) {
180 throw new RuntimeException(e); 179 throw new RuntimeException(e);
181 } 180 }
182 } 181 }
183 182
184 private static String getString(Map map,String key) throws LuanException { 183 private static Map removeMap(Map map,String key) throws LuanException {
185 Object val = map.remove(key); 184 LuanTable t = Utils.removeTable(map,key);
186 if( val!=null && !(val instanceof String) )
187 throw new LuanException( "parameter '"+key+"' must be a string" );
188 return (String)val;
189 }
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
201 private static LuanTable getTable(Map map,String key) throws LuanException {
202 Object val = map.remove(key);
203 if( val!=null && !(val instanceof LuanTable) )
204 throw new LuanException( "parameter '"+key+"' must be a table" );
205 return (LuanTable)val;
206 }
207
208 private static Map getMap(Map map,String key) throws LuanException {
209 LuanTable t = getTable(map,key);
210 return t==null ? null : t.asMap(); 185 return t==null ? null : t.asMap();
211 } 186 }
212 187
213 @Override public InputStream inputStream() throws IOException, LuanException { 188 @Override public InputStream inputStream() throws IOException, LuanException {
214 try { 189 try {