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

   
Visit the aagtl Website