/[aagtl_public1]/src/com/zoffcc/applications/aagtl/ClientHttpRequest.java
aagtl

Contents of /src/com/zoffcc/applications/aagtl/ClientHttpRequest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Sun Aug 5 13:48:36 2012 UTC (11 years, 7 months ago) by zoffadmin
File size: 18551 byte(s)
initial import of aagtl source code
1 /**
2 * aagtl Advanced Geocaching Tool for Android
3 * loosely based on agtl by Daniel Fett <fett@danielfett.de>
4 * Copyright (C) 2010 - 2012 Zoff.cc <aagtl@work.zoff.cc>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 package com.zoffcc.applications.aagtl;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.net.URL;
29 import java.net.URLConnection;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Random;
34
35 /**
36 * <p>
37 * Title: Client HTTP Request class
38 * </p>
39 * <p>
40 * Description: this class helps to send POST HTTP requests with various form data, including files. Cookies can be added to be included in the request.
41 * </p>
42 *
43 * @author Vlad Patryshev
44 * @version 1.0
45 */
46 public class ClientHttpRequest
47 {
48 URLConnection connection;
49 OutputStream os = null;
50 Map cookies = new HashMap();
51
52 protected void connect() throws IOException
53 {
54 if (os == null) os = connection.getOutputStream();
55 }
56
57 protected void write(char c) throws IOException
58 {
59 connect();
60 System.out.println(c);
61 os.write(c);
62 }
63
64 protected void write(String s) throws IOException
65 {
66 connect();
67 System.out.println(s.getBytes());
68 os.write(s.getBytes());
69 }
70
71 protected void newline() throws IOException
72 {
73 connect();
74 System.out.println("\r\n");
75 write("\r\n");
76 }
77
78 protected void writeln(String s) throws IOException
79 {
80 connect();
81 System.out.println(s);
82 write(s);
83 newline();
84 }
85
86 private static Random random = new Random();
87
88 protected static String randomString()
89 {
90 return Long.toString(random.nextLong(), 36);
91 }
92
93 String boundary = "---------------------------" + randomString() + randomString() + randomString();
94
95 private void boundary() throws IOException
96 {
97 write("--");
98 write(boundary);
99 }
100
101 /**
102 * Creates a new multipart POST HTTP request on a freshly opened URLConnection
103 *
104 * @param connection
105 * an already open URL connection
106 * @throws IOException
107 */
108 public ClientHttpRequest(URLConnection connection) throws IOException
109 {
110 this.connection = connection;
111 connection.setDoOutput(true);
112 connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
113 }
114
115 /**
116 * Creates a new multipart POST HTTP request for a specified URL
117 *
118 * @param url
119 * the URL to send request to
120 * @throws IOException
121 */
122 public ClientHttpRequest(URL url) throws IOException
123 {
124 this(url.openConnection());
125 }
126
127 /**
128 * Creates a new multipart POST HTTP request for a specified URL string
129 *
130 * @param urlString
131 * the string representation of the URL to send request to
132 * @throws IOException
133 */
134 public ClientHttpRequest(String urlString) throws IOException
135 {
136 this(new URL(urlString));
137 }
138
139 private void postCookies()
140 {
141 StringBuffer cookieList = new StringBuffer();
142
143 for (Iterator i = cookies.entrySet().iterator(); i.hasNext();)
144 {
145 Map.Entry entry = (Map.Entry) (i.next());
146 cookieList.append(entry.getKey().toString() + "=" + entry.getValue());
147
148 if (i.hasNext())
149 {
150 cookieList.append("; ");
151 }
152 }
153 if (cookieList.length() > 0)
154 {
155 // try
156 // {
157 // writeln("Cookie: " + cookieList.toString() + "");
158 // }
159 // catch (IOException e)
160 // {
161 // e.printStackTrace();
162 // }
163 //System.out.println("Cookie: " + cookieList.toString());
164 connection.setRequestProperty("Cookie", cookieList.toString());
165 }
166 }
167
168 /**
169 * adds a cookie to the requst
170 *
171 * @param name
172 * cookie name
173 * @param value
174 * cookie value
175 * @throws IOException
176 */
177 public void setCookie(String name, String value) throws IOException
178 {
179 cookies.put(name, value);
180 postCookies();
181 }
182
183 /**
184 * adds cookies to the request
185 *
186 * @param cookies
187 * the cookie "name-to-value" map
188 * @throws IOException
189 */
190 public void setCookies(Map cookies) throws IOException
191 {
192 if (cookies == null) return;
193 this.cookies.putAll(cookies);
194 }
195
196 /**
197 * adds cookies to the request
198 *
199 * @param cookies
200 * array of cookie names and values (cookies[2*i] is a name, cookies[2*i + 1] is a value)
201 * @throws IOException
202 */
203 public void setCookies(String[] cookies) throws IOException
204 {
205 if (cookies == null) return;
206 for (int i = 0; i < cookies.length - 1; i += 2)
207 {
208 setCookie(cookies[i], cookies[i + 1]);
209 }
210 }
211
212 private void writeName(String name) throws IOException
213 {
214 newline();
215 write("Content-Disposition: form-data; name=\"");
216 write(name);
217 write('"');
218 }
219
220 /**
221 * adds a string parameter to the request
222 *
223 * @param name
224 * parameter name
225 * @param value
226 * parameter value
227 * @throws IOException
228 */
229 public void setParameter(String name, String value) throws IOException
230 {
231 boundary();
232 writeName(name);
233 newline();
234 newline();
235 writeln(value);
236 }
237
238 private static void pipe(InputStream in, OutputStream out) throws IOException
239 {
240 byte[] buf = new byte[500000];
241 int nread;
242 int navailable;
243 int total = 0;
244 synchronized (in)
245 {
246 while ((nread = in.read(buf, 0, buf.length)) >= 0)
247 {
248 out.write(buf, 0, nread);
249 total += nread;
250 }
251 }
252 out.flush();
253 buf = null;
254 }
255
256 /**
257 * adds a file parameter to the request
258 *
259 * @param name
260 * parameter name
261 * @param filename
262 * the name of the file
263 * @param is
264 * input stream to read the contents of the file from
265 * @throws IOException
266 */
267 public void setParameter(String name, String filename, InputStream is) throws IOException
268 {
269 boundary();
270 writeName(name);
271 write("; filename=\"");
272 write(filename);
273 write('"');
274 newline();
275 write("Content-Type: ");
276 String type = connection.guessContentTypeFromName(filename);
277 if (type == null) type = "application/octet-stream";
278 writeln(type);
279 newline();
280 pipe(is, os);
281 newline();
282 }
283
284 /**
285 * adds a file parameter to the request
286 *
287 * @param name
288 * parameter name
289 * @param file
290 * the file to upload
291 * @throws IOException
292 */
293 public void setParameter(String name, File file) throws IOException
294 {
295 setParameter(name, file.getPath(), new FileInputStream(file));
296 }
297
298 /**
299 * adds a parameter to the request; if the parameter is a File, the file is uploaded, otherwise the string value of the parameter is passed in the request
300 *
301 * @param name
302 * parameter name
303 * @param object
304 * parameter value, a File or anything else that can be stringified
305 * @throws IOException
306 */
307 public void setParameter(String name, Object object) throws IOException
308 {
309 if (object instanceof File)
310 {
311 setParameter(name, (File) object);
312 }
313 else
314 {
315 setParameter(name, object.toString());
316 }
317 }
318
319 /**
320 * adds parameters to the request
321 *
322 * @param parameters
323 * "name-to-value" map of parameters; if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request
324 * @throws IOException
325 */
326 public void setParameters(Map parameters) throws IOException
327 {
328 if (parameters == null) return;
329 for (Iterator i = parameters.entrySet().iterator(); i.hasNext();)
330 {
331 Map.Entry entry = (Map.Entry) i.next();
332 setParameter(entry.getKey().toString(), entry.getValue());
333 }
334 }
335
336 /**
337 * adds parameters to the request
338 *
339 * @param parameters
340 * array of parameter names and values (parameters[2*i] is a name, parameters[2*i + 1] is a value); if a value is a file, the file is uploaded, otherwise it is stringified and sent in the
341 * request
342 * @throws IOException
343 */
344 public void setParameters(Object[] parameters) throws IOException
345 {
346 if (parameters == null) return;
347 for (int i = 0; i < parameters.length - 1; i += 2)
348 {
349 setParameter(parameters[i].toString(), parameters[i + 1]);
350 }
351 }
352
353 /**
354 * posts the requests to the server, with all the cookies and parameters that were added
355 *
356 * @return input stream with the server response
357 * @throws IOException
358 */
359 public InputStream post() throws IOException
360 {
361 boundary();
362 writeln("--");
363 os.close();
364 return connection.getInputStream();
365 }
366
367 /**
368 * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument
369 *
370 * @param parameters
371 * request parameters
372 * @return input stream with the server response
373 * @throws IOException
374 * @see setParameters
375 */
376 public InputStream post(Map parameters) throws IOException
377 {
378 setParameters(parameters);
379 return post();
380 }
381
382 /**
383 * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument
384 *
385 * @param parameters
386 * request parameters
387 * @return input stream with the server response
388 * @throws IOException
389 * @see setParameters
390 */
391 public InputStream post(Object[] parameters) throws IOException
392 {
393 setParameters(parameters);
394 return post();
395 }
396
397 /**
398 * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments
399 *
400 * @param cookies
401 * request cookies
402 * @param parameters
403 * request parameters
404 * @return input stream with the server response
405 * @throws IOException
406 * @see setParameters
407 * @see setCookies
408 */
409 public InputStream post(Map cookies, Map parameters) throws IOException
410 {
411 setCookies(cookies);
412 setParameters(parameters);
413 return post();
414 }
415
416 /**
417 * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments
418 *
419 * @param cookies
420 * request cookies
421 * @param parameters
422 * request parameters
423 * @return input stream with the server response
424 * @throws IOException
425 * @see setParameters
426 * @see setCookies
427 */
428 public InputStream post(String[] cookies, Object[] parameters) throws IOException
429 {
430 setCookies(cookies);
431 setParameters(parameters);
432 return post();
433 }
434
435 /**
436 * post the POST request to the server, with the specified parameter
437 *
438 * @param name
439 * parameter name
440 * @param value
441 * parameter value
442 * @return input stream with the server response
443 * @throws IOException
444 * @see setParameter
445 */
446 public InputStream post(String name, Object value) throws IOException
447 {
448 setParameter(name, value);
449 return post();
450 }
451
452 /**
453 * post the POST request to the server, with the specified parameters
454 *
455 * @param name1
456 * first parameter name
457 * @param value1
458 * first parameter value
459 * @param name2
460 * second parameter name
461 * @param value2
462 * second parameter value
463 * @return input stream with the server response
464 * @throws IOException
465 * @see setParameter
466 */
467 public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException
468 {
469 setParameter(name1, value1);
470 return post(name2, value2);
471 }
472
473 /**
474 * post the POST request to the server, with the specified parameters
475 *
476 * @param name1
477 * first parameter name
478 * @param value1
479 * first parameter value
480 * @param name2
481 * second parameter name
482 * @param value2
483 * second parameter value
484 * @param name3
485 * third parameter name
486 * @param value3
487 * third parameter value
488 * @return input stream with the server response
489 * @throws IOException
490 * @see setParameter
491 */
492 public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException
493 {
494 setParameter(name1, value1);
495 return post(name2, value2, name3, value3);
496 }
497
498 /**
499 * post the POST request to the server, with the specified parameters
500 *
501 * @param name1
502 * first parameter name
503 * @param value1
504 * first parameter value
505 * @param name2
506 * second parameter name
507 * @param value2
508 * second parameter value
509 * @param name3
510 * third parameter name
511 * @param value3
512 * third parameter value
513 * @param name4
514 * fourth parameter name
515 * @param value4
516 * fourth parameter value
517 * @return input stream with the server response
518 * @throws IOException
519 * @see setParameter
520 */
521 public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException
522 {
523 setParameter(name1, value1);
524 return post(name2, value2, name3, value3, name4, value4);
525 }
526
527 /**
528 * posts a new request to specified URL, with parameters that are passed in the argument
529 *
530 * @param parameters
531 * request parameters
532 * @return input stream with the server response
533 * @throws IOException
534 * @see setParameters
535 */
536 public static InputStream post(URL url, Map parameters) throws IOException
537 {
538 return new ClientHttpRequest(url).post(parameters);
539 }
540
541 /**
542 * posts a new request to specified URL, with parameters that are passed in the argument
543 *
544 * @param parameters
545 * request parameters
546 * @return input stream with the server response
547 * @throws IOException
548 * @see setParameters
549 */
550 public static InputStream post(URL url, Object[] parameters) throws IOException
551 {
552 return new ClientHttpRequest(url).post(parameters);
553 }
554
555 /**
556 * posts a new request to specified URL, with cookies and parameters that are passed in the argument
557 *
558 * @param cookies
559 * request cookies
560 * @param parameters
561 * request parameters
562 * @return input stream with the server response
563 * @throws IOException
564 * @see setCookies
565 * @see setParameters
566 */
567 public static InputStream post(URL url, Map cookies, Map parameters) throws IOException
568 {
569 return new ClientHttpRequest(url).post(cookies, parameters);
570 }
571
572 /**
573 * posts a new request to specified URL, with cookies and parameters that are passed in the argument
574 *
575 * @param cookies
576 * request cookies
577 * @param parameters
578 * request parameters
579 * @return input stream with the server response
580 * @throws IOException
581 * @see setCookies
582 * @see setParameters
583 */
584 public static InputStream post(URL url, String[] cookies, Object[] parameters) throws IOException
585 {
586 return new ClientHttpRequest(url).post(cookies, parameters);
587 }
588
589 /**
590 * post the POST request specified URL, with the specified parameter
591 *
592 * @param name
593 * parameter name
594 * @param value
595 * parameter value
596 * @return input stream with the server response
597 * @throws IOException
598 * @see setParameter
599 */
600 public static InputStream post(URL url, String name1, Object value1) throws IOException
601 {
602 return new ClientHttpRequest(url).post(name1, value1);
603 }
604
605 /**
606 * post the POST request to specified URL, with the specified parameters
607 *
608 * @param name1
609 * first parameter name
610 * @param value1
611 * first parameter value
612 * @param name2
613 * second parameter name
614 * @param value2
615 * second parameter value
616 * @return input stream with the server response
617 * @throws IOException
618 * @see setParameter
619 */
620 public static InputStream post(URL url, String name1, Object value1, String name2, Object value2) throws IOException
621 {
622 return new ClientHttpRequest(url).post(name1, value1, name2, value2);
623 }
624
625 /**
626 * post the POST request to specified URL, with the specified parameters
627 *
628 * @param name1
629 * first parameter name
630 * @param value1
631 * first parameter value
632 * @param name2
633 * second parameter name
634 * @param value2
635 * second parameter value
636 * @param name3
637 * third parameter name
638 * @param value3
639 * third parameter value
640 * @return input stream with the server response
641 * @throws IOException
642 * @see setParameter
643 */
644 public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException
645 {
646 return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3);
647 }
648
649 /**
650 * post the POST request to specified URL, with the specified parameters
651 *
652 * @param name1
653 * first parameter name
654 * @param value1
655 * first parameter value
656 * @param name2
657 * second parameter name
658 * @param value2
659 * second parameter value
660 * @param name3
661 * third parameter name
662 * @param value3
663 * third parameter value
664 * @param name4
665 * fourth parameter name
666 * @param value4
667 * fourth parameter value
668 * @return input stream with the server response
669 * @throws IOException
670 * @see setParameter
671 */
672 public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException
673 {
674 return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3, name4, value4);
675 }
676 }

   
Visit the aagtl Website