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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sun Aug 5 14:00:28 2012 UTC (11 years, 8 months ago) by zoffadmin
File size: 17857 byte(s)
license text correction
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 <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
22 package com.zoffcc.applications.aagtl;
23
24 import java.io.BufferedReader;
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.DataOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.OutputStreamWriter;
32 import java.io.Reader;
33 import java.io.StringWriter;
34 import java.io.UnsupportedEncodingException;
35 import java.io.Writer;
36 import java.net.HttpURLConnection;
37 import java.net.MalformedURLException;
38 import java.net.URL;
39 import java.text.SimpleDateFormat;
40 import java.util.ArrayList;
41 import java.util.Calendar;
42 import java.util.Enumeration;
43 import java.util.Hashtable;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Set;
48 import java.util.TimeZone;
49 import java.util.Map.Entry;
50 import java.util.regex.Matcher;
51 import java.util.regex.Pattern;
52
53 import moz.http.HttpData;
54 import moz.http.HttpRequest;
55
56 import org.apache.http.NameValuePair;
57 import org.apache.http.message.BasicNameValuePair;
58
59
60 public class FieldnotesUploader
61 {
62 String URL = "http://www.geocaching.com/my/uploadfieldnotes.aspx";
63 HTMLDownloader downloader = null;
64 List<GeocacheCoordinate> gc_with_fn = null;
65
66 public class data_ret
67 {
68 String encoding;
69 ByteArrayOutputStream data;
70 }
71
72 public FieldnotesUploader(HTMLDownloader dl, List<GeocacheCoordinate> gc_with_fieldnotes)
73 {
74 this.gc_with_fn = gc_with_fieldnotes;
75 this.downloader = dl;
76 }
77
78 public Boolean upload()
79 {
80 return this.upload_v3();
81 }
82
83 public String convertStreamToString(InputStream is) throws IOException
84 {
85 /*
86 * To convert the InputStream to String we use the
87 * Reader.read(char[] buffer) method. We iterate until the
88 * Reader return -1 which means there's no more data to
89 * read. We use the StringWriter class to produce the string.
90 */
91 if (is != null)
92 {
93 Writer writer = new StringWriter();
94
95 char[] buffer = new char[HTMLDownloader.large_buffer_size];
96 try
97 {
98 Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),
99 HTMLDownloader.large_buffer_size);
100 int n;
101 while ((n = reader.read(buffer)) != -1)
102 {
103 writer.write(buffer, 0, n);
104 }
105 }
106 finally
107 {
108 is.close();
109 }
110 return writer.toString();
111 }
112 else
113 {
114 return "";
115 }
116 }
117
118 public String SendPost(String httpURL, String data, String _cookie) throws IOException
119 {
120 URL url = new URL(httpURL);
121 //URL url = new URL("http://zoff.cc/xx.cgi");
122
123 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
124 connection.setRequestMethod("POST");
125 connection.setDoOutput(true);
126 connection.setRequestProperty("Connection", "Keep-Alive");
127
128 //System.out.println("C=" + _cookie);
129 connection
130 .setRequestProperty("User-Agent",
131 "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10");
132 connection.setRequestProperty("Cookie", _cookie);
133 connection.connect();
134
135 if (data != "")
136 {
137 OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
138 out.write(data);
139 out.flush();
140 out.close();
141 }
142
143 // Save Cookie
144 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()),
145 HTMLDownloader.default_buffer_size);
146 String headerName = null;
147 //_cookies.clear();
148 if (_cookie == "")
149 {
150 for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++)
151 {
152 if (headerName.equalsIgnoreCase("Set-Cookie"))
153 {
154 String cookie = connection.getHeaderField(i);
155 _cookie += cookie.substring(0, cookie.indexOf(";")) + "; ";
156 }
157 }
158 }
159
160 // Get HTML from Server
161 String getData = "";
162 String decodedString;
163 while ((decodedString = in.readLine()) != null)
164 {
165 getData += decodedString + "\n";
166 }
167 in.close();
168
169 return getData;
170 }
171
172 public data_ret _encode_multipart_formdata(Hashtable<String, String> values, String file_field,
173 String filename, byte[] file_data) throws IOException
174 {
175 data_ret ret2 = new data_ret();
176 //byte[] ret = new byte[1];
177 ByteArrayOutputStream b = new ByteArrayOutputStream();
178
179
180 String BOUNDARY = "----------ThIs_Is_tHe_bouNdaRY_$";
181 String CRLF = "\r\n";
182
183 Enumeration<String> i = values.keys();
184 String x = null;
185 String y = null;
186
187 // values
188 while (i.hasMoreElements())
189 {
190 x = i.nextElement();
191 y = values.get(x);
192
193 if (b.size() > 0)
194 {
195 b.write(CRLF.getBytes());
196 }
197 else
198 {
199 //ret = "";
200 }
201 //ret = ret + "--" + BOUNDARY + CRLF;
202 //ret = ret + "Content-Disposition: form-data; name=\"" + x + "\"" + CRLF;
203 //ret = ret + CRLF;
204 //ret = ret + y;
205 b.write(("--" + BOUNDARY + CRLF).getBytes());
206 b.write(("Content-Disposition: form-data; name=\"" + x + "\"" + CRLF).getBytes());
207 b.write((CRLF + y).getBytes());
208 }
209
210 // file
211 if (b.size() > 0)
212 {
213 //ret = ret + CRLF;
214 b.write(CRLF.getBytes());
215 }
216 else
217 {
218 //ret = "";
219 }
220 b
221 .write(("--" + BOUNDARY + CRLF + "Content-Disposition: form-data; name=\"" + file_field
222 + "\"; filename=\"" + filename + "\"" + CRLF + "Content-Type: " + "text/plain"
223 + CRLF + CRLF).getBytes());
224 b.write(file_data);
225
226 // finish
227 if (b.size() > 0)
228 {
229 // ret = ret + CRLF;
230 b.write(CRLF.getBytes());
231 }
232 else
233 {
234 // ret = "";
235 }
236 b.write(("--" + BOUNDARY + "--" + CRLF + CRLF).getBytes());
237
238 ret2.data = b;
239 ret2.encoding = String.format("multipart/form-data; boundary=%s", BOUNDARY);
240 return ret2;
241 }
242
243 public Boolean upload_v3()
244 {
245 Boolean succ = true;
246
247 this.downloader.login();
248 String page = this.downloader.getUrlData(this.URL);
249 String viewstate = "";
250 Pattern p = Pattern
251 .compile("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"([^\"]+)\" />");
252 Matcher m = p.matcher(page);
253 m.find();
254 viewstate = m.group(1);
255
256 //System.out.println("viewstate=" + viewstate);
257 // got viewstate
258
259 Calendar cal = Calendar.getInstance();
260 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
261 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
262 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
263
264 String cookies_string = this.downloader.getCookies();
265
266 // loop through fieldnotes
267 // loop through fieldnotes
268 // loop through fieldnotes
269 // loop through fieldnotes
270 byte[] raw_upload_data2 = null;
271 GeocacheCoordinate element = null;
272 Hashtable<String, String> ht = new Hashtable<String, String>();
273 ht.put("ctl00$ContentBody$btnUpload", "Upload Field Note");
274 ht.put("ctl00$ContentBody$chkSuppressDate", "");
275 ht.put("__VIEWSTATE", viewstate);
276 List<NameValuePair> values_list = null;
277 data_ret temp = null;
278 Iterator<GeocacheCoordinate> itr = this.gc_with_fn.iterator();
279 while (itr.hasNext())
280 {
281 // loop through fieldnotes
282 // loop through fieldnotes
283 // loop through fieldnotes
284 // loop through fieldnotes
285 element = itr.next();
286
287 raw_upload_data2 = null;
288
289 String fn_status_string = null;
290 try
291 {
292 fn_status_string = GeocacheCoordinate.LOG_AS_HASH.get(element.log_as);
293 //System.out.println("LOG_AS=" + element.log_as);
294 }
295 catch (Exception e)
296 {
297 e.printStackTrace();
298 System.out.println("Unknown LOG_AS type!!");
299 break;
300 }
301
302 String raw = element.name + "," + sdf.format(cal.getTime()) + "," + fn_status_string
303 + ",\"" + element.fieldnotes + "\"";
304 //System.out.println(raw);
305
306 try
307 {
308 raw_upload_data2 = raw.getBytes("UTF-16");
309 }
310 catch (UnsupportedEncodingException e)
311 {
312 e.printStackTrace();
313 }
314
315 values_list = new ArrayList<NameValuePair>();
316 temp = null;
317 try
318 {
319 temp = this._encode_multipart_formdata(ht, "ctl00$ContentBody$FieldNoteLoader",
320 "geocache_visits.txt", raw_upload_data2);
321 }
322 catch (IOException e)
323 {
324 e.printStackTrace();
325 }
326
327 //System.out.println("\r\n1" + temp.encoding);
328 //System.out.println("\r\n2" + temp.data);
329
330 values_list.add(new BasicNameValuePair("Content-Type", temp.encoding));
331 values_list
332 .add(new BasicNameValuePair("Content-Length", String.valueOf(temp.data.size())));
333 values_list.add(new BasicNameValuePair("User-Agent",
334 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)"));
335 values_list.add(new BasicNameValuePair("Pragma", "no-cache"));
336 String the_page = this.downloader
337 .get_reader_stream(this.URL, values_list, temp.data, true);
338
339
340 // check here if fieldnotes uploaded ok!!!!!!!!!
341 // check here if fieldnotes uploaded ok!!!!!!!!!
342 // check here if fieldnotes uploaded ok!!!!!!!!!
343 // check here if fieldnotes uploaded ok!!!!!!!!!
344 // check here if fieldnotes uploaded ok!!!!!!!!!
345 if (the_page.indexOf("records were successfully uploaded") != -1)
346 {
347 //System.out.println("Fieldnote uploaded OK");
348 // reset status in DB, so field note won't get uploaded again
349
350 GeocacheCoordinate temp_gc2 = element;
351 temp_gc2.log_as = GeocacheCoordinate.LOG_NO_LOG;
352 this.downloader.main_aagtl.pv.begin_trans();
353 try
354 {
355 this.downloader.main_aagtl.pv.reset_point_fn(temp_gc2);
356 this.downloader.main_aagtl.pv.commit();
357 }
358 finally
359 {
360 this.downloader.main_aagtl.pv.end_trans();
361 }
362 }
363 else
364 {
365 System.out.println(the_page);
366 System.out.println("Fieldnote upload ERROR");
367
368 // set error status
369 succ = false;
370 }
371 // check here if fieldnotes uploaded ok!!!!!!!!!
372 // check here if fieldnotes uploaded ok!!!!!!!!!
373 // check here if fieldnotes uploaded ok!!!!!!!!!
374 // check here if fieldnotes uploaded ok!!!!!!!!!
375 // check here if fieldnotes uploaded ok!!!!!!!!!
376
377
378 // loop through fieldnotes
379 // loop through fieldnotes
380 // loop through fieldnotes
381 // loop through fieldnotes
382 }
383 // loop through fieldnotes
384 // loop through fieldnotes
385 // loop through fieldnotes
386 // loop through fieldnotes
387
388 return succ;
389 }
390
391 public Boolean upload_v2()
392 {
393 this.downloader.login();
394 String page = this.downloader.getUrlData(this.URL);
395 String viewstate = "";
396 Pattern p = Pattern
397 .compile("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"([^\"]+)\" />");
398 Matcher m = p.matcher(page);
399 m.find();
400 viewstate = m.group(1);
401
402 //System.out.println("viewstate=" + viewstate);
403 // got viewstate
404
405 InputStream fn_is = null;
406 String raw_upload_data = "";
407 try
408 {
409 fn_is = new ByteArrayInputStream(("GC2BNHP,2010-11-07T14:00Z,Write note,\"bla bla\"")
410 .getBytes("UTF-8"));
411 raw_upload_data = "GC2BNHP,2010-11-07T20:50Z,Write note,\"bla bla\"".getBytes("UTF-8")
412 .toString();
413 }
414 catch (UnsupportedEncodingException e)
415 {
416 e.printStackTrace();
417 }
418
419
420 String cookies_string = this.downloader.getCookies();
421
422 ArrayList<InputStream> files = new ArrayList();
423 files.add(fn_is);
424
425 Hashtable<String, String> ht = new Hashtable<String, String>();
426 ht.put("ctl00$ContentBody$btnUpload", "Upload Field Note");
427 ht.put("ctl00$ContentBody$chkSuppressDate", "");
428 // ht.put("ctl00$ContentBody$FieldNoteLoader", "geocache_visits.txt");
429 ht.put("__VIEWSTATE", viewstate);
430
431 HttpData data = HttpRequest.post(this.URL, ht, files, cookies_string);
432 //System.out.println(data.content);
433
434
435 String boundary = "----------ThIs_Is_tHe_bouNdaRY_$";
436 String crlf = "\r\n";
437
438 URL url = null;
439 try
440 {
441 url = new URL(this.URL);
442 }
443 catch (MalformedURLException e2)
444 {
445 e2.printStackTrace();
446 }
447 HttpURLConnection con = null;
448 try
449 {
450 con = (HttpURLConnection) url.openConnection();
451 }
452 catch (IOException e2)
453 {
454 e2.printStackTrace();
455 }
456 con.setDoInput(true);
457 con.setDoOutput(true);
458 con.setUseCaches(false);
459 try
460 {
461 con.setRequestMethod("POST");
462 }
463 catch (java.net.ProtocolException e)
464 {
465 e.printStackTrace();
466 }
467
468 con.setRequestProperty("Cookie", cookies_string);
469 //System.out.println("Cookie: " + cookies_string[0] + "=" + cookies_string[1]);
470
471 con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
472 con.setRequestProperty("Pragma", "no-cache");
473 //con.setRequestProperty("Connection", "Keep-Alive");
474 String content_type = String.format("multipart/form-data; boundary=%s", boundary);
475 con.setRequestProperty("Content-Type", content_type);
476
477
478 DataOutputStream dos = null;
479 try
480 {
481 dos = new DataOutputStream(con.getOutputStream());
482 }
483 catch (IOException e)
484 {
485 e.printStackTrace();
486 }
487
488 String raw_data = "";
489
490 //
491 raw_data = raw_data + "--" + boundary + crlf;
492 raw_data = raw_data
493 + String.format("Content-Disposition: form-data; name=\"%s\"",
494 "ctl00$ContentBody$btnUpload") + crlf;
495 raw_data = raw_data + crlf;
496 raw_data = raw_data + "Upload Field Note" + crlf;
497 //
498
499 //
500 raw_data = raw_data + "--" + boundary + crlf;
501 raw_data = raw_data
502 + String.format("Content-Disposition: form-data; name=\"%s\"",
503 "ctl00$ContentBody$chkSuppressDate") + crlf;
504 raw_data = raw_data + crlf;
505 raw_data = raw_data + "" + crlf;
506 //
507
508 //
509 raw_data = raw_data + "--" + boundary + crlf;
510 raw_data = raw_data
511 + String.format("Content-Disposition: form-data; name=\"%s\"", "__VIEWSTATE") + crlf;
512 raw_data = raw_data + crlf;
513 raw_data = raw_data + viewstate + crlf;
514 //
515
516 //
517 raw_data = raw_data + "--" + boundary + crlf;
518 raw_data = raw_data
519 + String.format("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"",
520 "ctl00$ContentBody$FieldNoteLoader", "geocache_visits.txt") + crlf;
521 raw_data = raw_data + String.format("Content-Type: %s", "text/plain") + crlf;
522 raw_data = raw_data + crlf;
523 raw_data = raw_data + raw_upload_data + crlf;
524 //
525
526 //
527 raw_data = raw_data + "--" + boundary + "--" + crlf;
528 raw_data = raw_data + crlf;
529
530 try
531 {
532 this.SendPost(this.URL, raw_data, cookies_string);
533 }
534 catch (IOException e1)
535 {
536 e1.printStackTrace();
537 }
538
539
540 //System.out.println(raw_data);
541
542 try
543 {
544 dos.writeBytes(raw_data);
545 //dos.writeChars(raw_data);
546 dos.flush();
547 }
548 catch (IOException e)
549 {
550 e.printStackTrace();
551 }
552
553 HttpData ret2 = new HttpData();
554 BufferedReader rd = null;
555 try
556 {
557 rd = new BufferedReader(new InputStreamReader(con.getInputStream()),
558 HTMLDownloader.large_buffer_size);
559 String line;
560 while ((line = rd.readLine()) != null)
561 {
562 ret2.content += line + "\r\n";
563 }
564 }
565 catch (IOException e)
566 {
567 e.printStackTrace();
568 }
569 //get headers
570 Map<String, List<String>> headers = con.getHeaderFields();
571 Set<Entry<String, List<String>>> hKeys = headers.entrySet();
572 for (Iterator<Entry<String, List<String>>> i = hKeys.iterator(); i.hasNext();)
573 {
574 Entry<String, List<String>> m99 = i.next();
575
576 //System.out.println("HEADER_KEY" + m99.getKey() + "=" + m99.getValue());
577 ret2.headers.put(m99.getKey(), m99.getValue().toString());
578 if (m99.getKey().equals("set-cookie"))
579 ret2.cookies.put(m99.getKey(), m99.getValue().toString());
580 }
581 try
582 {
583 dos.close();
584 rd.close();
585 }
586 catch (IOException e)
587 {
588 e.printStackTrace();
589 }
590
591 //System.out.println(ret2.content);
592
593
594 //System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFFF");
595 ClientHttpRequest client_req;
596 try
597 {
598 client_req = new ClientHttpRequest(this.URL);
599 String[] cookies_string2 = this.downloader.getCookies2();
600 for (int jk = 0; jk < cookies_string2.length; jk++)
601 {
602 System.out.println(cookies_string2[jk * 2] + "=" + cookies_string2[(jk * 2) + 1]);
603 client_req.setCookie(cookies_string2[jk * 2], cookies_string2[(jk * 2) + 1]);
604 }
605 client_req.setParameter("ctl00$ContentBody$btnUpload", "Upload Field Note");
606 client_req.setParameter("ctl00$ContentBody$FieldNoteLoader", "geocache_visits.txt", fn_is);
607 InputStream response = client_req.post();
608 //System.out.println(this.convertStreamToString(response));
609 }
610 catch (IOException e)
611 {
612 e.printStackTrace();
613 }
614
615 //ArrayList<InputStream> files = new ArrayList();
616 files.clear();
617 files.add(fn_is);
618
619 Hashtable<String, String> ht2 = new Hashtable<String, String>();
620 ht2.put("ctl00$ContentBody$btnUpload", "Upload Field Note");
621 ht2.put("ctl00$ContentBody$chkSuppressDate", "");
622 // ht.put("ctl00$ContentBody$FieldNoteLoader", "geocache_visits.txt");
623 ht2.put("__VIEWSTATE", viewstate);
624
625 HttpData data3 = HttpRequest.post(this.URL, ht2, files, cookies_string);
626 //System.out.println(data3.content);
627
628
629 // String the_page2 = this.downloader.get_reader_mpf(this.URL, raw_data, null, true, boundary);
630 //System.out.println("page2=\n" + the_page2);
631
632 Boolean ret = false;
633 return ret;
634 }
635
636 }

   
Visit the aagtl Website