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

   
Visit the aagtl Website