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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 3 Revision 4
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the 16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA. 18 * Boston, MA 02110-1301, USA.
19 */ 19 */
20
21 20
22package com.zoffcc.applications.aagtl; 21package com.zoffcc.applications.aagtl;
23 22
24import java.io.BufferedReader; 23import java.io.BufferedReader;
25import java.io.ByteArrayInputStream; 24import java.io.ByteArrayInputStream;
42import java.util.Enumeration; 41import java.util.Enumeration;
43import java.util.Hashtable; 42import java.util.Hashtable;
44import java.util.Iterator; 43import java.util.Iterator;
45import java.util.List; 44import java.util.List;
46import java.util.Map; 45import java.util.Map;
46import java.util.Map.Entry;
47import java.util.Set; 47import java.util.Set;
48import java.util.TimeZone; 48import java.util.TimeZone;
49import java.util.Map.Entry;
50import java.util.regex.Matcher; 49import java.util.regex.Matcher;
51import java.util.regex.Pattern; 50import java.util.regex.Pattern;
52 51
53import moz.http.HttpData; 52import moz.http.HttpData;
54import moz.http.HttpRequest; 53import moz.http.HttpRequest;
55 54
56import org.apache.http.NameValuePair; 55import org.apache.http.NameValuePair;
57import org.apache.http.message.BasicNameValuePair; 56import org.apache.http.message.BasicNameValuePair;
58 57
59
60public class FieldnotesUploader 58public class FieldnotesUploader
61{ 59{
62 String URL = "http://www.geocaching.com/my/uploadfieldnotes.aspx"; 60 String URL = "http://www.geocaching.com/my/uploadfieldnotes.aspx";
63 HTMLDownloader downloader = null; 61 HTMLDownloader downloader = null;
64 List<GeocacheCoordinate> gc_with_fn = null; 62 List<GeocacheCoordinate> gc_with_fn = null;
65 63
66 public class data_ret 64 public class data_ret
67 { 65 {
68 String encoding; 66 String encoding;
69 ByteArrayOutputStream data; 67 ByteArrayOutputStream data;
70 } 68 }
71 69
72 public FieldnotesUploader(HTMLDownloader dl, List<GeocacheCoordinate> gc_with_fieldnotes) 70 public FieldnotesUploader(HTMLDownloader dl, List<GeocacheCoordinate> gc_with_fieldnotes)
73 { 71 {
74 this.gc_with_fn = gc_with_fieldnotes; 72 this.gc_with_fn = gc_with_fieldnotes;
93 Writer writer = new StringWriter(); 91 Writer writer = new StringWriter();
94 92
95 char[] buffer = new char[HTMLDownloader.large_buffer_size]; 93 char[] buffer = new char[HTMLDownloader.large_buffer_size];
96 try 94 try
97 { 95 {
98 Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 96 Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), HTMLDownloader.large_buffer_size);
99 HTMLDownloader.large_buffer_size);
100 int n; 97 int n;
101 while ((n = reader.read(buffer)) != -1) 98 while ((n = reader.read(buffer)) != -1)
102 { 99 {
103 writer.write(buffer, 0, n); 100 writer.write(buffer, 0, n);
104 } 101 }
124 connection.setRequestMethod("POST"); 121 connection.setRequestMethod("POST");
125 connection.setDoOutput(true); 122 connection.setDoOutput(true);
126 connection.setRequestProperty("Connection", "Keep-Alive"); 123 connection.setRequestProperty("Connection", "Keep-Alive");
127 124
128 //System.out.println("C=" + _cookie); 125 //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"); 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");
132 connection.setRequestProperty("Cookie", _cookie); 127 connection.setRequestProperty("Cookie", _cookie);
133 connection.connect(); 128 connection.connect();
134 129
135 if (data != "") 130 if (data != "")
136 { 131 {
139 out.flush(); 134 out.flush();
140 out.close(); 135 out.close();
141 } 136 }
142 137
143 // Save Cookie 138 // Save Cookie
144 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()), 139 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()), HTMLDownloader.default_buffer_size);
145 HTMLDownloader.default_buffer_size);
146 String headerName = null; 140 String headerName = null;
147 //_cookies.clear(); 141 //_cookies.clear();
148 if (_cookie == "") 142 if (_cookie == "")
149 { 143 {
150 for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) 144 for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++)
167 in.close(); 161 in.close();
168 162
169 return getData; 163 return getData;
170 } 164 }
171 165
172 public data_ret _encode_multipart_formdata(Hashtable<String, String> values, String file_field, 166 public data_ret _encode_multipart_formdata(Hashtable<String, String> values, String file_field, String filename, byte[] file_data) throws IOException
173 String filename, byte[] file_data) throws IOException
174 { 167 {
175 data_ret ret2 = new data_ret(); 168 data_ret ret2 = new data_ret();
176 //byte[] ret = new byte[1]; 169 //byte[] ret = new byte[1];
177 ByteArrayOutputStream b = new ByteArrayOutputStream(); 170 ByteArrayOutputStream b = new ByteArrayOutputStream();
178
179 171
180 String BOUNDARY = "----------ThIs_Is_tHe_bouNdaRY_$"; 172 String BOUNDARY = "----------ThIs_Is_tHe_bouNdaRY_$";
181 String CRLF = "\r\n"; 173 String CRLF = "\r\n";
182 174
183 Enumeration<String> i = values.keys(); 175 Enumeration<String> i = values.keys();
215 } 207 }
216 else 208 else
217 { 209 {
218 //ret = ""; 210 //ret = "";
219 } 211 }
220 b 212 b.write(("--" + BOUNDARY + CRLF + "Content-Disposition: form-data; name=\"" + file_field + "\"; filename=\"" + filename + "\"" + CRLF + "Content-Type: " + "text/plain" + CRLF + CRLF).getBytes());
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); 213 b.write(file_data);
225 214
226 // finish 215 // finish
227 if (b.size() > 0) 216 if (b.size() > 0)
228 { 217 {
245 Boolean succ = true; 234 Boolean succ = true;
246 235
247 this.downloader.login(); 236 this.downloader.login();
248 String page = this.downloader.getUrlData(this.URL); 237 String page = this.downloader.getUrlData(this.URL);
249 String viewstate = ""; 238 String viewstate = "";
250 Pattern p = Pattern 239
240 System.out.println("page=" + page);
241
242 try
243 {
251 .compile("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"([^\"]+)\" />"); 244 Pattern p = Pattern.compile("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"([^\"]+)\" />");
252 Matcher m = p.matcher(page); 245 Matcher m = p.matcher(page);
253 m.find(); 246 m.find();
254 viewstate = m.group(1); 247 viewstate = m.group(1);
248 }
249 catch (Exception e)
250 {
251 e.printStackTrace();
252 return false;
253 }
255 254
256 //System.out.println("viewstate=" + viewstate); 255 //System.out.println("viewstate=" + viewstate);
257 // got viewstate 256 // got viewstate
258 257
259 Calendar cal = Calendar.getInstance(); 258 Calendar cal = Calendar.getInstance();
297 e.printStackTrace(); 296 e.printStackTrace();
298 System.out.println("Unknown LOG_AS type!!"); 297 System.out.println("Unknown LOG_AS type!!");
299 break; 298 break;
300 } 299 }
301 300
302 String raw = element.name + "," + sdf.format(cal.getTime()) + "," + fn_status_string 301 String raw = element.name + "," + sdf.format(cal.getTime()) + "," + fn_status_string + ",\"" + element.fieldnotes + "\"";
303 + ",\"" + element.fieldnotes + "\"";
304 //System.out.println(raw); 302 //System.out.println(raw);
305 303
306 try 304 try
307 { 305 {
308 raw_upload_data2 = raw.getBytes("UTF-16"); 306 raw_upload_data2 = raw.getBytes("UTF-16");
314 312
315 values_list = new ArrayList<NameValuePair>(); 313 values_list = new ArrayList<NameValuePair>();
316 temp = null; 314 temp = null;
317 try 315 try
318 { 316 {
319 temp = this._encode_multipart_formdata(ht, "ctl00$ContentBody$FieldNoteLoader", 317 temp = this._encode_multipart_formdata(ht, "ctl00$ContentBody$FieldNoteLoader", "geocache_visits.txt", raw_upload_data2);
320 "geocache_visits.txt", raw_upload_data2);
321 } 318 }
322 catch (IOException e) 319 catch (IOException e)
323 { 320 {
324 e.printStackTrace(); 321 e.printStackTrace();
325 } 322 }
326 323
327 //System.out.println("\r\n1" + temp.encoding); 324 //System.out.println("\r\n1" + temp.encoding);
328 //System.out.println("\r\n2" + temp.data); 325 //System.out.println("\r\n2" + temp.data);
329 326
330 values_list.add(new BasicNameValuePair("Content-Type", temp.encoding)); 327 values_list.add(new BasicNameValuePair("Content-Type", temp.encoding));
331 values_list
332 .add(new BasicNameValuePair("Content-Length", String.valueOf(temp.data.size()))); 328 values_list.add(new BasicNameValuePair("Content-Length", String.valueOf(temp.data.size())));
333 values_list.add(new BasicNameValuePair("User-Agent", 329 values_list.add(new BasicNameValuePair("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)"));
334 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)"));
335 values_list.add(new BasicNameValuePair("Pragma", "no-cache")); 330 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); 331 String the_page = this.downloader.get_reader_stream(this.URL, values_list, temp.data, true);
338
339 332
340 // check here if fieldnotes uploaded ok!!!!!!!!! 333 // check here if fieldnotes uploaded ok!!!!!!!!!
341 // check here if fieldnotes uploaded ok!!!!!!!!! 334 // check here if fieldnotes uploaded ok!!!!!!!!!
342 // check here if fieldnotes uploaded ok!!!!!!!!! 335 // check here if fieldnotes uploaded ok!!!!!!!!!
343 // check here if fieldnotes uploaded ok!!!!!!!!! 336 // check here if fieldnotes uploaded ok!!!!!!!!!
372 // check here if fieldnotes uploaded ok!!!!!!!!! 365 // check here if fieldnotes uploaded ok!!!!!!!!!
373 // check here if fieldnotes uploaded ok!!!!!!!!! 366 // check here if fieldnotes uploaded ok!!!!!!!!!
374 // check here if fieldnotes uploaded ok!!!!!!!!! 367 // check here if fieldnotes uploaded ok!!!!!!!!!
375 // check here if fieldnotes uploaded ok!!!!!!!!! 368 // check here if fieldnotes uploaded ok!!!!!!!!!
376 369
377
378 // loop through fieldnotes 370 // loop through fieldnotes
379 // loop through fieldnotes 371 // loop through fieldnotes
380 // loop through fieldnotes 372 // loop through fieldnotes
381 // loop through fieldnotes 373 // loop through fieldnotes
382 } 374 }
391 public Boolean upload_v2() 383 public Boolean upload_v2()
392 { 384 {
393 this.downloader.login(); 385 this.downloader.login();
394 String page = this.downloader.getUrlData(this.URL); 386 String page = this.downloader.getUrlData(this.URL);
395 String viewstate = ""; 387 String viewstate = "";
396 Pattern p = Pattern
397 .compile("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"([^\"]+)\" />"); 388 Pattern p = Pattern.compile("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"([^\"]+)\" />");
398 Matcher m = p.matcher(page); 389 Matcher m = p.matcher(page);
399 m.find(); 390 m.find();
400 viewstate = m.group(1); 391 viewstate = m.group(1);
401 392
402 //System.out.println("viewstate=" + viewstate); 393 //System.out.println("viewstate=" + viewstate);
404 395
405 InputStream fn_is = null; 396 InputStream fn_is = null;
406 String raw_upload_data = ""; 397 String raw_upload_data = "";
407 try 398 try
408 { 399 {
409 fn_is = new ByteArrayInputStream(("GC2BNHP,2010-11-07T14:00Z,Write note,\"bla bla\"") 400 fn_is = new ByteArrayInputStream(("GC2BNHP,2010-11-07T14:00Z,Write note,\"bla bla\"").getBytes("UTF-8"));
410 .getBytes("UTF-8"));
411 raw_upload_data = "GC2BNHP,2010-11-07T20:50Z,Write note,\"bla bla\"".getBytes("UTF-8") 401 raw_upload_data = "GC2BNHP,2010-11-07T20:50Z,Write note,\"bla bla\"".getBytes("UTF-8").toString();
412 .toString();
413 } 402 }
414 catch (UnsupportedEncodingException e) 403 catch (UnsupportedEncodingException e)
415 { 404 {
416 e.printStackTrace(); 405 e.printStackTrace();
417 } 406 }
418
419 407
420 String cookies_string = this.downloader.getCookies(); 408 String cookies_string = this.downloader.getCookies();
421 409
422 ArrayList<InputStream> files = new ArrayList(); 410 ArrayList<InputStream> files = new ArrayList();
423 files.add(fn_is); 411 files.add(fn_is);
429 ht.put("__VIEWSTATE", viewstate); 417 ht.put("__VIEWSTATE", viewstate);
430 418
431 HttpData data = HttpRequest.post(this.URL, ht, files, cookies_string); 419 HttpData data = HttpRequest.post(this.URL, ht, files, cookies_string);
432 //System.out.println(data.content); 420 //System.out.println(data.content);
433 421
434
435 String boundary = "----------ThIs_Is_tHe_bouNdaRY_$"; 422 String boundary = "----------ThIs_Is_tHe_bouNdaRY_$";
436 String crlf = "\r\n"; 423 String crlf = "\r\n";
437 424
438 URL url = null; 425 URL url = null;
439 try 426 try
472 con.setRequestProperty("Pragma", "no-cache"); 459 con.setRequestProperty("Pragma", "no-cache");
473 //con.setRequestProperty("Connection", "Keep-Alive"); 460 //con.setRequestProperty("Connection", "Keep-Alive");
474 String content_type = String.format("multipart/form-data; boundary=%s", boundary); 461 String content_type = String.format("multipart/form-data; boundary=%s", boundary);
475 con.setRequestProperty("Content-Type", content_type); 462 con.setRequestProperty("Content-Type", content_type);
476 463
477
478 DataOutputStream dos = null; 464 DataOutputStream dos = null;
479 try 465 try
480 { 466 {
481 dos = new DataOutputStream(con.getOutputStream()); 467 dos = new DataOutputStream(con.getOutputStream());
482 } 468 }
487 473
488 String raw_data = ""; 474 String raw_data = "";
489 475
490 // 476 //
491 raw_data = raw_data + "--" + boundary + crlf; 477 raw_data = raw_data + "--" + boundary + crlf;
492 raw_data = raw_data 478 raw_data = raw_data + String.format("Content-Disposition: form-data; name=\"%s\"", "ctl00$ContentBody$btnUpload") + crlf;
493 + String.format("Content-Disposition: form-data; name=\"%s\"",
494 "ctl00$ContentBody$btnUpload") + crlf;
495 raw_data = raw_data + crlf; 479 raw_data = raw_data + crlf;
496 raw_data = raw_data + "Upload Field Note" + crlf; 480 raw_data = raw_data + "Upload Field Note" + crlf;
497 // 481 //
498 482
499 // 483 //
500 raw_data = raw_data + "--" + boundary + crlf; 484 raw_data = raw_data + "--" + boundary + crlf;
501 raw_data = raw_data 485 raw_data = raw_data + String.format("Content-Disposition: form-data; name=\"%s\"", "ctl00$ContentBody$chkSuppressDate") + crlf;
502 + String.format("Content-Disposition: form-data; name=\"%s\"",
503 "ctl00$ContentBody$chkSuppressDate") + crlf;
504 raw_data = raw_data + crlf; 486 raw_data = raw_data + crlf;
505 raw_data = raw_data + "" + crlf; 487 raw_data = raw_data + "" + crlf;
506 // 488 //
507 489
508 // 490 //
509 raw_data = raw_data + "--" + boundary + crlf; 491 raw_data = raw_data + "--" + boundary + crlf;
510 raw_data = raw_data
511 + String.format("Content-Disposition: form-data; name=\"%s\"", "__VIEWSTATE") + crlf; 492 raw_data = raw_data + String.format("Content-Disposition: form-data; name=\"%s\"", "__VIEWSTATE") + crlf;
512 raw_data = raw_data + crlf; 493 raw_data = raw_data + crlf;
513 raw_data = raw_data + viewstate + crlf; 494 raw_data = raw_data + viewstate + crlf;
514 // 495 //
515 496
516 // 497 //
517 raw_data = raw_data + "--" + boundary + crlf; 498 raw_data = raw_data + "--" + boundary + crlf;
518 raw_data = raw_data 499 raw_data = raw_data + String.format("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"", "ctl00$ContentBody$FieldNoteLoader", "geocache_visits.txt") + crlf;
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; 500 raw_data = raw_data + String.format("Content-Type: %s", "text/plain") + crlf;
522 raw_data = raw_data + crlf; 501 raw_data = raw_data + crlf;
523 raw_data = raw_data + raw_upload_data + crlf; 502 raw_data = raw_data + raw_upload_data + crlf;
524 // 503 //
525 504
534 catch (IOException e1) 513 catch (IOException e1)
535 { 514 {
536 e1.printStackTrace(); 515 e1.printStackTrace();
537 } 516 }
538 517
539
540 //System.out.println(raw_data); 518 //System.out.println(raw_data);
541 519
542 try 520 try
543 { 521 {
544 dos.writeBytes(raw_data); 522 dos.writeBytes(raw_data);
552 530
553 HttpData ret2 = new HttpData(); 531 HttpData ret2 = new HttpData();
554 BufferedReader rd = null; 532 BufferedReader rd = null;
555 try 533 try
556 { 534 {
557 rd = new BufferedReader(new InputStreamReader(con.getInputStream()), 535 rd = new BufferedReader(new InputStreamReader(con.getInputStream()), HTMLDownloader.large_buffer_size);
558 HTMLDownloader.large_buffer_size);
559 String line; 536 String line;
560 while ((line = rd.readLine()) != null) 537 while ((line = rd.readLine()) != null)
561 { 538 {
562 ret2.content += line + "\r\n"; 539 ret2.content += line + "\r\n";
563 } 540 }
573 { 550 {
574 Entry<String, List<String>> m99 = i.next(); 551 Entry<String, List<String>> m99 = i.next();
575 552
576 //System.out.println("HEADER_KEY" + m99.getKey() + "=" + m99.getValue()); 553 //System.out.println("HEADER_KEY" + m99.getKey() + "=" + m99.getValue());
577 ret2.headers.put(m99.getKey(), m99.getValue().toString()); 554 ret2.headers.put(m99.getKey(), m99.getValue().toString());
578 if (m99.getKey().equals("set-cookie"))
579 ret2.cookies.put(m99.getKey(), m99.getValue().toString()); 555 if (m99.getKey().equals("set-cookie")) ret2.cookies.put(m99.getKey(), m99.getValue().toString());
580 } 556 }
581 try 557 try
582 { 558 {
583 dos.close(); 559 dos.close();
584 rd.close(); 560 rd.close();
587 { 563 {
588 e.printStackTrace(); 564 e.printStackTrace();
589 } 565 }
590 566
591 //System.out.println(ret2.content); 567 //System.out.println(ret2.content);
592
593 568
594 //System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFFF"); 569 //System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFFF");
595 ClientHttpRequest client_req; 570 ClientHttpRequest client_req;
596 try 571 try
597 { 572 {
623 ht2.put("__VIEWSTATE", viewstate); 598 ht2.put("__VIEWSTATE", viewstate);
624 599
625 HttpData data3 = HttpRequest.post(this.URL, ht2, files, cookies_string); 600 HttpData data3 = HttpRequest.post(this.URL, ht2, files, cookies_string);
626 //System.out.println(data3.content); 601 //System.out.println(data3.content);
627 602
628
629 // String the_page2 = this.downloader.get_reader_mpf(this.URL, raw_data, null, true, boundary); 603 // String the_page2 = this.downloader.get_reader_mpf(this.URL, raw_data, null, true, boundary);
630 //System.out.println("page2=\n" + the_page2); 604 //System.out.println("page2=\n" + the_page2);
631 605
632 Boolean ret = false; 606 Boolean ret = false;
633 return ret; 607 return ret;

Legend:
Removed from v.3  
changed lines
  Added in v.4

   
Visit the aagtl Website