]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/FormPoster.java
9f9edc4b7057bf052a8379fe7d59557f137b2478
[GpsPrune.git] / tim / prune / function / gpsies / FormPoster.java
1 package tim.prune.function.gpsies;\r
2 \r
3 import java.net.URLConnection;\r
4 import java.net.URL;\r
5 import java.io.IOException;\r
6 import java.io.File;\r
7 import java.io.InputStream;\r
8 import java.util.Random;\r
9 import java.io.OutputStream;\r
10 import java.io.FileInputStream;\r
11 \r
12 /**\r
13  * Taken from Client HTTP Request class\r
14  * @author Vlad Patryshev\r
15  */\r
16 public class FormPoster\r
17 {\r
18         private URLConnection _connection;\r
19         private OutputStream _os = null;\r
20         private static final Random random = new Random();\r
21         private static final String boundary = "---------------------------"\r
22                 + randomString() + randomString() + randomString();\r
23 \r
24 \r
25         protected void connect() throws IOException {\r
26                 if (_os == null) _os = _connection.getOutputStream();\r
27         }\r
28 \r
29         protected void write(char c) throws IOException {\r
30                 connect();\r
31                 _os.write(c);\r
32         }\r
33 \r
34         protected void write(String s) throws IOException {\r
35                 connect();\r
36                 _os.write(s.getBytes());\r
37         }\r
38 \r
39         protected void newline() throws IOException {\r
40                 connect();\r
41                 write("\r\n");\r
42         }\r
43 \r
44         protected void writeln(String s) throws IOException {\r
45                 connect();\r
46                 write(s);\r
47                 newline();\r
48         }\r
49 \r
50         protected static String randomString() {\r
51                 return Long.toString(random.nextLong(), 36);\r
52         }\r
53 \r
54         private void boundary() throws IOException {\r
55                 write("--");\r
56                 write(boundary);\r
57         }\r
58 \r
59         /**\r
60          * Creates a new multipart POST HTTP request on a freshly opened URLConnection\r
61          *\r
62          * @param inConnection an already open URL connection\r
63          * @throws IOException\r
64          */\r
65         public FormPoster(URLConnection inConnection) throws IOException\r
66         {\r
67                 _connection = inConnection;\r
68                 _connection.setDoOutput(true);\r
69                 _connection.setRequestProperty("Content-Type",\r
70                                 "multipart/form-data; boundary=" + boundary);\r
71         }\r
72 \r
73         /**\r
74          * Creates a new multipart POST HTTP request for a specified URL\r
75          *\r
76          * @param url the URL to send request to\r
77          * @throws IOException\r
78          */\r
79         public FormPoster(URL url) throws IOException {\r
80                 this(url.openConnection());\r
81         }\r
82 \r
83         /**\r
84          * Creates a new multipart POST HTTP request for a specified URL string\r
85          *\r
86          * @param urlString the string representation of the URL to send request to\r
87          * @throws IOException\r
88          */\r
89         public FormPoster(String urlString) throws IOException {\r
90                 this(new URL(urlString));\r
91         }\r
92 \r
93 \r
94         private void writeName(String name) throws IOException\r
95         {\r
96                 newline();\r
97                 write("Content-Disposition: form-data; name=\"");\r
98                 write(name);\r
99                 write('"');\r
100         }\r
101 \r
102         /**\r
103          * adds a string parameter to the request\r
104          * @param name parameter name\r
105          * @param value parameter value\r
106          * @throws IOException\r
107          */\r
108         public void setParameter(String name, String value) throws IOException\r
109         {\r
110                 boundary();\r
111                 writeName(name);\r
112                 newline(); newline();\r
113                 writeln(value);\r
114         }\r
115 \r
116         private static void pipe(InputStream in, OutputStream out) throws IOException\r
117         {\r
118                 byte[] buf = new byte[500000];\r
119                 int nread;\r
120                 int total = 0;\r
121                 synchronized (in) {\r
122                         while((nread = in.read(buf, 0, buf.length)) >= 0) {\r
123                                 out.write(buf, 0, nread);\r
124                                 total += nread;\r
125                         }\r
126                 }\r
127                 out.flush();\r
128                 buf = null;\r
129         }\r
130 \r
131         /**\r
132          * adds a file parameter to the request\r
133          * @param name parameter name\r
134          * @param filename the name of the file\r
135          * @param is input stream to read the contents of the file from\r
136          * @throws IOException\r
137          */\r
138         public void setParameter(String name, String filename, InputStream is) throws IOException\r
139         {\r
140                 boundary();\r
141                 writeName(name);\r
142                 write("; filename=\"");\r
143                 write(filename);\r
144                 write('"');\r
145                 newline();\r
146                 write("Content-Type: ");\r
147                 String type = URLConnection.guessContentTypeFromName(filename);\r
148                 if (type == null) type = "application/octet-stream";\r
149                 writeln(type);\r
150                 newline();\r
151                 pipe(is, _os);\r
152                 newline();\r
153         }\r
154 \r
155         /**\r
156          * adds a file parameter to the request\r
157          * @param name parameter name\r
158          * @param file the file to upload\r
159          * @throws IOException\r
160          */\r
161         public void setParameter(String name, File file) throws IOException {\r
162                 setParameter(name, file.getPath(), new FileInputStream(file));\r
163         }\r
164 \r
165         /**\r
166          * adds a parameter to the request; if the parameter is a File, the file is uploaded,\r
167          * otherwise the string value of the parameter is passed in the request\r
168          * @param name parameter name\r
169          * @param object parameter value, a File or anything else that can be stringified\r
170          * @throws IOException\r
171          */\r
172         public void setParameter(String name, Object object) throws IOException\r
173         {\r
174                 if (object instanceof File) {\r
175                         setParameter(name, (File) object);\r
176                 } else {\r
177                         setParameter(name, object.toString());\r
178                 }\r
179         }\r
180 \r
181         /**\r
182          * posts the requests to the server\r
183          * @return input stream with the server response\r
184          * @throws IOException\r
185          */\r
186         public InputStream post() throws IOException {\r
187                 boundary();\r
188                 writeln("--");\r
189                 _os.close();\r
190                 return _connection.getInputStream();\r
191         }\r
192 \r
193         /**\r
194          * post the POST request to the server, with the specified parameter\r
195          * @param name parameter name\r
196          * @param value parameter value\r
197          * @return input stream with the server response\r
198          * @throws IOException\r
199          * @see setParameter\r
200          */\r
201         public InputStream post(String name, Object value) throws IOException {\r
202                 setParameter(name, value);\r
203                 return post();\r
204         }\r
205 \r
206         /**\r
207          * post the POST request to the server, with the specified parameters\r
208          * @param name1 first parameter name\r
209          * @param value1 first parameter value\r
210          * @param name2 second parameter name\r
211          * @param value2 second parameter value\r
212          * @return input stream with the server response\r
213          * @throws IOException\r
214          * @see setParameter\r
215          */\r
216         public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException {\r
217                 setParameter(name1, value1);\r
218                 return post(name2, value2);\r
219         }\r
220 \r
221         /**\r
222          * post the POST request to the server, with the specified parameters\r
223          * @param name1 first parameter name\r
224          * @param value1 first parameter value\r
225          * @param name2 second parameter name\r
226          * @param value2 second parameter value\r
227          * @param name3 third parameter name\r
228          * @param value3 third parameter value\r
229          * @return input stream with the server response\r
230          * @throws IOException\r
231          * @see setParameter\r
232          */\r
233         public InputStream post(String name1, Object value1, String name2, Object value2,\r
234                 String name3, Object value3) throws IOException\r
235         {\r
236                 setParameter(name1, value1);\r
237                 return post(name2, value2, name3, value3);\r
238         }\r
239 \r
240         /**\r
241          * post the POST request to the server, with the specified parameters\r
242          * @param name1 first parameter name\r
243          * @param value1 first parameter value\r
244          * @param name2 second parameter name\r
245          * @param value2 second parameter value\r
246          * @param name3 third parameter name\r
247          * @param value3 third parameter value\r
248          * @param name4 fourth parameter name\r
249          * @param value4 fourth parameter value\r
250          * @return input stream with the server response\r
251          * @throws IOException\r
252          * @see setParameter\r
253          */\r
254         public InputStream post(String name1, Object value1, String name2, Object value2,\r
255                 String name3, Object value3, String name4, Object value4) throws IOException\r
256         {\r
257                 setParameter(name1, value1);\r
258                 return post(name2, value2, name3, value3, name4, value4);\r
259         }\r
260 \r
261         /**\r
262          * post the POST request specified URL, with the specified parameter\r
263          * @param name parameter name\r
264          * @param value parameter value\r
265          * @return input stream with the server response\r
266          * @throws IOException\r
267          * @see setParameter\r
268          */\r
269         public static InputStream post(URL url, String name1, Object value1) throws IOException {\r
270                 return new FormPoster(url).post(name1, value1);\r
271         }\r
272 \r
273         /**\r
274          * post the POST request to specified URL, with the specified parameters\r
275          * @param name1 first parameter name\r
276          * @param value1 first parameter value\r
277          * @param name2 second parameter name\r
278          * @param value2 second parameter value\r
279          * @return input stream with the server response\r
280          * @throws IOException\r
281          * @see setParameter\r
282          */\r
283         public static InputStream post(URL url, String name1, Object value1,\r
284                 String name2, Object value2) throws IOException\r
285         {\r
286                 return new FormPoster(url).post(name1, value1, name2, value2);\r
287         }\r
288 \r
289         /**\r
290          * post the POST request to specified URL, with the specified parameters\r
291          * @param name1 first parameter name\r
292          * @param value1 first parameter value\r
293          * @param name2 second parameter name\r
294          * @param value2 second parameter value\r
295          * @param name3 third parameter name\r
296          * @param value3 third parameter value\r
297          * @return input stream with the server response\r
298          * @throws IOException\r
299          * @see setParameter\r
300          */\r
301         public static InputStream post(URL url, String name1, Object value1, String name2, Object value2,\r
302                 String name3, Object value3) throws IOException\r
303         {\r
304                 return new FormPoster(url).post(name1, value1, name2, value2, name3, value3);\r
305         }\r
306 \r
307         /**\r
308          * post the POST request to specified URL, with the specified parameters\r
309          * @param name1 first parameter name\r
310          * @param value1 first parameter value\r
311          * @param name2 second parameter name\r
312          * @param value2 second parameter value\r
313          * @param name3 third parameter name\r
314          * @param value3 third parameter value\r
315          * @param name4 fourth parameter name\r
316          * @param value4 fourth parameter value\r
317          * @return input stream with the server response\r
318          * @throws IOException\r
319          * @see setParameter\r
320          */\r
321         public static InputStream post(URL url, String name1, Object value1, String name2, Object value2,\r
322                 String name3, Object value3, String name4, Object value4) throws IOException\r
323         {\r
324                 return new FormPoster(url).post(name1, value1, name2, value2, name3, value3, name4, value4);\r
325         }\r
326 }\r