Mercurial Hosting > luan
comparison core/src/luan/modules/url/MultipartClient.java @ 725:a741a3a33423
add url support for multipart/form-data
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 08 Jun 2016 23:13:10 -0600 |
parents | |
children | 14f136a4641f |
comparison
equal
deleted
inserted
replaced
724:4f8e30a3ffd0 | 725:a741a3a33423 |
---|---|
1 package luan.modules.url; | |
2 | |
3 import java.io.OutputStream; | |
4 import java.io.IOException; | |
5 import java.net.HttpURLConnection; | |
6 import java.util.List; | |
7 import java.util.ArrayList; | |
8 import java.util.Map; | |
9 import java.util.HashMap; | |
10 import luan.LuanTable; | |
11 import luan.LuanException; | |
12 | |
13 | |
14 public final class MultipartClient { | |
15 private static final byte[] __CRLF = {'\r','\n'}; | |
16 private static final byte[] __DASHDASH = {'-','-'}; | |
17 private static final String __ISO_8859_1 = "ISO-8859-1"; | |
18 | |
19 private final Map params = new HashMap(); | |
20 | |
21 MultipartClient(Map params) throws LuanException { | |
22 for( Object hack : params.entrySet() ) { | |
23 Map.Entry entry = (Map.Entry)hack; | |
24 String key = (String)entry.getKey(); | |
25 Object val = entry.getValue(); | |
26 List list = new ArrayList(); | |
27 if( val instanceof String ) { | |
28 list.add(val); | |
29 } else { | |
30 if( !(val instanceof LuanTable) ) | |
31 throw new LuanException( "parameter '"+key+"' must be string or table" ); | |
32 LuanTable t = (LuanTable)val; | |
33 if( !t.isList() ) | |
34 throw new LuanException( "parameter '"+key+"' table must be list" ); | |
35 for( Object obj : t.asList() ) { | |
36 if( !(obj instanceof String) ) | |
37 throw new LuanException( "parameter '"+key+"' values must be strings" ); | |
38 list.add(obj); | |
39 } | |
40 } | |
41 this.params.put(key,list); | |
42 } | |
43 } | |
44 | |
45 public OutputStream write(HttpURLConnection httpCon) throws IOException { | |
46 String boundary = "luan" + System.identityHashCode(this) + Long.toString(System.currentTimeMillis(),36); | |
47 byte[] boundaryBytes = boundary.getBytes(__ISO_8859_1); | |
48 | |
49 httpCon.setRequestProperty("Content-Type","multipart/form-data; boundary="+boundary); | |
50 OutputStream out = httpCon.getOutputStream(); | |
51 for( Object hack : params.entrySet() ) { | |
52 Map.Entry entry = (Map.Entry)hack; | |
53 String name = (String)entry.getKey(); | |
54 List list = (List)entry.getValue(); | |
55 for( Object obj : list ) { | |
56 String val = (String)obj; | |
57 out.write(__DASHDASH); | |
58 out.write(boundaryBytes); | |
59 out.write(__CRLF); | |
60 // if (contentType != null) | |
61 // out.write(("Content-Type: "+contentType).getBytes(__ISO_8859_1)); | |
62 // out.write(__CRLF); | |
63 out.write(("Content-Disposition: form-data; name=\""+name+"\"").getBytes(__ISO_8859_1)); | |
64 out.write(__CRLF); | |
65 out.write(__CRLF); | |
66 out.write(val.getBytes()); | |
67 out.write(__CRLF); | |
68 } | |
69 } | |
70 out.write(__DASHDASH); | |
71 out.write(boundaryBytes); | |
72 out.write(__DASHDASH); | |
73 out.write(__CRLF); | |
74 return out; | |
75 } | |
76 } |