-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaps.java
More file actions
676 lines (625 loc) · 20.2 KB
/
Maps.java
File metadata and controls
676 lines (625 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// All Rights Reserved.
package sample.maps;
import com.google.gdata.util.common.base.StringUtil;
import com.google.gdata.util.common.xml.XmlWriter;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.maps.MapsService;
import com.google.gdata.data.BaseEntry;
import com.google.gdata.data.BaseFeed;
import com.google.gdata.data.ExtensionProfile;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.batch.BatchOperationType;
import com.google.gdata.data.batch.BatchUtils;
import com.google.gdata.data.extensions.CustomProperty;
import com.google.gdata.data.extensions.ResourceId;
import com.google.gdata.data.maps.FeatureEntry;
import com.google.gdata.data.maps.FeatureFeed;
import com.google.gdata.data.maps.MapEntry;
import com.google.gdata.data.maps.MapFeed;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
/**
* Sample client for Maps GData API.
*
* maps VERB FEED FLAG*
* VERB is one of {query, create, read, update, delete, batch, clear}
* FEED is one of {maps, features}
* FLAG (note the non-gnu style) can be
* -chunk CHUNK-SIZE
* -content KML-CONTENT
* -count CHUNK-COUNT
* -fid FEATURE-ID
* -file FILENAME
* -host HOST:PORT
* -id ATOM-ID
* -maxresults MAX-RESULTS
* -mid MAP-ID
* -output OUTPUT-FILENAME
* -password PASSWORD
* -previd PREV_ID
* -preview
* -projection (one of full, public, unlisted, owned, bookmarked)
* -prop NAME VALUE
* -sep SEPARATOR (default ".")
* -startindex START-INDEX
* -title TITLE
* -uid USER-ID (default "default")
* -user USER-EMAIL
* -v VERSION
*/
public class Maps {
private static final HashSet XML_PP = new HashSet(Arrays.asList(
XmlWriter.WriterFlags.PRETTY_PRINT));
private static boolean draft = false;
protected Maps() {
}
/**
* Generic client for Maps API.
*/
public static class ApiClient<E extends BaseEntry, F extends BaseFeed> {
private Class<E> entryClass;
private Class<F> feedClass;
private MapsService maps;
private String base; // URI of base feed
private String uriFormat;
private List<String> params;
/**
* @param entryClass must be supplied, used to direct Atom parser
* @param feedClass must be supplied, used to direct Atom parser
*/
private ApiClient(Class<E> entryClass, Class<F> feedClass, String uriFormat,
List<String> params) {
this.entryClass = entryClass;
this.feedClass = feedClass;
this.uriFormat = uriFormat;
this.params = params;
}
private MapsService authenticate(String base, String u, String p)
throws Exception {
this.base = base;
maps = new MapsService("client");
if (!StringUtil.isEmpty(u) && !StringUtil.isEmpty(p)) {
maps.setUserCredentials(u, p);
}
return maps;
}
private F createFeed(F feed, String... id) throws Exception {
return maps.batch(makeUri(id), feed);
}
private F readFeed(String... id) throws Exception {
return maps.getFeed(makeUri(id), feedClass);
}
private E createEntry(E entry, String... id) throws Exception {
return maps.insert(makeUri(id), entry);
}
private E readEntry(String... id) throws Exception {
return maps.getEntry(makeUri(id), entryClass);
}
private E updateEntry(E entry, String... id) throws Exception {
return maps.update(makeUri(id), entry);
}
/**
* Attempt to delete an entry.
*
* @param id list of id parts
* @return null on success, an error message on failure
*/
private String deleteEntry(String... id) {
try {
maps.delete(makeUri(id));
return null;
} catch (Throwable t) {
return t.getMessage();
}
}
/**
* Construct a URI from a list of id components.
*/
private URL makeUri(String... id) throws Exception {
String uri = base + String.format(uriFormat, id);
boolean first = true;
for (String param : params) {
uri += (first ? "?" : "&") + param;
first = false;
}
System.err.println(uri);
return new URL(uri);
}
/**
* Hack helper to allow callers to provide one less param on query.
*/
private void trimFormat(String tail) {
if (uriFormat.endsWith(tail)) {
uriFormat = uriFormat.substring(0, uriFormat.lastIndexOf(tail));
} else {
System.err.println(uriFormat + " has no " + tail);
}
}
private void addFormat(String tail) {
uriFormat += tail;
}
private E parseAtom(InputStream is) throws Exception {
E entry = entryClass.newInstance();
entry.parseAtom(maps.getExtensionProfile(), is);
return entry;
}
private F parseFeed(InputStream is) throws Exception {
F feed = feedClass.newInstance();
feed.parseAtom(maps.getExtensionProfile(), is);
return feed;
}
public E buildEntry(String title, String content, String summary)
throws Exception {
E entry = entryClass.newInstance();
entry.setTitle(new PlainTextConstruct(title));
if (!StringUtil.isEmpty(content)) {
entry.setContent(new PlainTextConstruct(content));
}
if (!StringUtil.isEmpty(summary)) {
entry.setSummary(new PlainTextConstruct(summary));
}
if (draft) {
entry.setDraft(true);
}
return entry;
}
public E newEntry() throws Exception {
return entryClass.newInstance();
}
public F newFeed() throws Exception {
return feedClass.newInstance();
}
}
/** Pretty-print a feed. */
private static void pp(GoogleService service, BaseFeed feed, Writer w)
throws IOException {
XmlWriter xw = new XmlWriter(w, XML_PP, null);
ExtensionProfile extProfile = service.getExtensionProfile();
feed.generateAtom(xw, extProfile);
xw.flush();
w.write("\n");
w.flush();
}
/** Pretty-print an entry. */
private static void pp(GoogleService service, BaseEntry entry, Writer w)
throws IOException {
XmlWriter xw = new XmlWriter(w, XML_PP, null);
ExtensionProfile extProfile = service.getExtensionProfile();
entry.generateAtom(xw, extProfile);
xw.flush();
w.write("\n");
w.flush();
}
/**
* Create an Entry from an Atom input stream.
*
* @param actor
* @param is
* @param maps
* @param preview if true, print parsed entry to pw
* @param pw
* @return
* @throws Exception
*/
private static BaseEntry parseEntry(ApiClient actor, InputStream is,
MapsService maps, boolean preview, PrintWriter pw) throws Exception {
BaseEntry entry;
entry = actor.parseAtom(is);
if (draft) {
entry.setDraft(true);
}
if (preview) {
pp(maps, entry, pw);
}
return entry;
}
/**
* Create a Feed from an Atom input stream.
*
* @param actor
* @param is
* @param maps
* @param preview if true, print parsed entry to pw
* @param pw
* @return
* @throws Exception
*/
private static BaseFeed parseFeed(ApiClient actor, InputStream is,
MapsService maps, boolean preview, PrintWriter pw) throws Exception {
BaseFeed feed;
feed = actor.parseFeed(is);
if (preview) {
pp(maps, feed, pw);
}
return feed;
}
enum Action {query, create, read, update, delete, batch, clear, bulk, rawpost, revise};
enum Feed {maps, features};
enum Arg {
CHUNK, // bulk chunk size
CONTENT, // kml content
COUNT, // bulk chunk count
DRAFT, // true if a private map
FID, // feature id
FILE, // file name for uploads
FUDGE, // mess with stuff
HOST, // name of API server
ID, // atom id
MAXRESULTS, // limit number of results (query)
MID, // map id
OUTPUT, // output filename
PASSWORD, // for authentication
PREVID, // previd (previous id)
PREVIEW, // show upload data before sending
PROJECTION, // map feed projection
PROP, // custom property
SEP, // uid/mid separator (default ".")
STARTINDEX, // starting index (query)
SUMMARY, // atom:summary
TITLE, // of a new map or feature
UID, // user id ("default" by default)
USER, // for authentication
V // version
};
public static void main(String arg[]) throws Exception {
Action action = enumValueOrDie(Action.class, arg[0]);
Feed feed = enumValueOrDie(Feed.class, arg[1]);
String content = " "; // avoid creating ghost maps
String fid = "";
String host = "maps.google.com";
String id = ""; // client-supplied id
String mid = "";
String password = "";
String previd = null;
String projection = "full";
String sep = "/";
String summary = null;
String title = null;
String uid = "default";
String user = "";
boolean fudge = false;
boolean preview = false;
int chunk = 10;
int count = 10;
int maxResults = 0;
int startIndex = 0;
int version = 2;
InputStream is = System.in;
OutputStream os = System.out;
Map<String, String> props = new HashMap<String, String>();
// Parse command line arguments
for (int i = 2; i < arg.length;) {
String key = arg[i++].substring(1).toUpperCase();
switch (enumValueOrDie(Arg.class, key)) {
case CHUNK:
chunk = Integer.parseInt(arg[i++]);
break;
case CONTENT:
content = arg[i++];
break;
case COUNT:
count = Integer.parseInt(arg[i++]);
break;
case DRAFT:
draft = true;
break;
case FID:
fid = arg[i++];
break;
case FILE:
is = new FileInputStream(arg[i++]);
break;
case FUDGE:
fudge = true;
break;
case HOST:
host = arg[i++];
break;
case ID:
id = arg[i++];
break;
case MID:
mid = arg[i++];
break;
case MAXRESULTS:
maxResults = Integer.parseInt(arg[i++]);
break;
case OUTPUT:
os = new FileOutputStream(arg[i++]);
break;
case PASSWORD:
password = arg[i++];
break;
case PREVID:
previd = arg[i++];
break;
case PROJECTION:
projection = arg[i++];
break;
case PREVIEW:
preview = !preview;
break;
case PROP:
props.put(arg[i], arg[i+1]);
i += 2;
break;
case SEP:
sep = arg[i++];
break;
case STARTINDEX:
startIndex = Integer.parseInt(arg[i++]);
break;
case SUMMARY:
summary = arg[i++];
break;
case TITLE:
title = arg[i++];
break;
case UID:
uid = arg[i++];
break;
case USER:
user = arg[i++];
break;
case V:
version = Integer.parseInt(arg[i++]);
break;
}
}
// Set up API client.
PrintWriter pw = new PrintWriter(os);
String base = "http://" + host + "/maps/feeds/" + feed.toString() + "/";
List<String> params = new ArrayList<String>();
if (startIndex > 0) {
params.add("start-index=" + startIndex);
}
if (maxResults > 0) {
params.add("max-results=" + maxResults);
}
if (null != previd) {
params.add("previd=" + previd);
}
ApiClient client = Feed.maps == feed
? new ApiClient(MapEntry.class, MapFeed.class,
"%s/" + projection + "/%s", params)
: new ApiClient(FeatureEntry.class, FeatureFeed.class,
"%s" + sep + "%s/" + projection + "/%s", params);
MapsService maps = client.authenticate(base, user, password);
if (version < 2) {
maps.setProtocolVersion(MapsService.Versions.V1);
} else if (version == 2){
maps.setProtocolVersion(MapsService.Versions.V2);
}
// Perform action.
BaseEntry entry;
switch (action) {
// For now, just get the whole feed.
case query:
pp(maps, client.readFeed(uid, mid, fid), pw);
break;
// Create an entry from scratch or read from input.
case create:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
entry = title == null
? parseEntry(client, is, maps, preview, pw)
: client.buildEntry(title, content, summary);
if (id.length() > 0) {
entry.setExtension(new ResourceId(id));
}
setProperties(props, entry);
if (preview) {
pp(maps, entry, pw);
}
pp(maps, client.createEntry(entry, uid, mid), pw);
break;
// Read an individual entry.
case read:
pp(maps, client.readEntry(uid, mid, fid), pw);
break;
// Update an existing entry.
case update:
entry = title == null
? parseEntry(client, is, maps, preview, pw)
: client.buildEntry(title, content, summary);
setProperties(props, entry);
if (preview) {
pp(maps, entry, pw);
}
pp(maps, client.updateEntry(entry, uid, mid, fid), pw);
break;
// Delete an existing entry.
case delete:
String err = client.deleteEntry(uid, mid, fid);
if (null != err) {
System.err.println("delete error: " + err);
}
break;
case batch:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
client.addFormat("/batch");
BaseFeed bf = parseFeed(client, is, maps, preview, pw);
pp(maps, client.createFeed(bf, uid, mid), pw);
break;
// Clear out a feed.
case clear:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
bf = client.readFeed(uid, mid, fid);
client.addFormat("/batch");
BaseFeed batch = client.newFeed();
count = 0;
for (Object o : bf.getEntries()) {
String entryId = ((BaseEntry) o).getSelfLink().getHref();
if (count == 0 && fudge) {
entryId += "-xx";
}
BaseEntry delete = client.newEntry();
delete.setId(replaceUserId(uid, entryId));
BatchUtils.setBatchOperationType(delete, BatchOperationType.DELETE);
BatchUtils.setBatchId(delete, Integer.toString(++count));
batch.getEntries().add(delete);
}
if (preview) {
pp(maps, batch, pw);
}
BaseFeed response = maps.batch(client.makeUri(uid, mid, fid), batch);
pp(maps, response, pw);
break;
// Test bulk insert latency.
case bulk:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
String baseXml = "";
if (title == null) {
char buf[] = new char[1024 * 64];
int n = new InputStreamReader(is).read(buf);
baseXml = new String(buf, 0, n);
}
client.addFormat("/batch");
for (int i = 0; i < count; i++) {
batch = client.newFeed();
long t0 = System.currentTimeMillis();
for (int j = 0; j < chunk; j++) {
String entryId = "" + i + "-" + j;
String xml = baseXml.replace("COUNT", entryId);
System.err.println(xml);
entry = title == null
? parseEntry(client, new ByteArrayInputStream(xml.getBytes()),
maps, preview, pw)
: client.buildEntry(title, content, summary);
setProperties(props, entry);
entry.setId(entryId);
BatchUtils.setBatchId(entry, entryId);
BatchUtils.setBatchOperationType(entry, BatchOperationType.INSERT);
batch.getEntries().add(entry);
}
if (preview) {
pp(maps, batch, pw);
}
response = maps.batch(
client.makeUri(uid, mid, fid), batch);
pp(maps, response, pw);
System.err.println("chunk insert time: " +
(System.currentTimeMillis() - t0) + "ms");
}
break;
// Post a file as is, no interpretation.
case rawpost:
break;
// Update a bloc of entries.
case revise:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
baseXml = "";
if (title == null) {
char buf[] = new char[1024 * 64];
int n = new InputStreamReader(is).read(buf);
baseXml = new String(buf, 0, n);
}
bf = client.readFeed(uid, mid, fid);
int n = bf.getEntries().size();
client.addFormat("/batch");
int dex = 0;
count = Math.min(count, n / chunk);
for (int i = 0; i < count; i++) {
long t0 = System.currentTimeMillis();
batch = client.newFeed();
for (int j = 0; j < Math.min(n, chunk); j++) {
Object o = bf.getEntries().get(j + i*chunk);
String entryId = ((BaseEntry) o).getSelfLink().
getHref().replaceAll("\\.", sep);
if (dex == 0 && fudge) {
entryId += "-xx";
}
String xml = baseXml.replace("COUNT", entryId.substring(entryId.lastIndexOf('/')));
BaseEntry update = title == null
? parseEntry(client, new ByteArrayInputStream(xml.getBytes()),
maps, preview, pw)
: client.buildEntry(title, content, summary);
update.setId(replaceUserId(uid, entryId));
setProperties(props, update);
BatchUtils.setBatchOperationType(update, BatchOperationType.UPDATE);
BatchUtils.setBatchId(update, Integer.toString(++dex));
batch.getEntries().add(update);
}
n -= chunk;
if (preview) {
pp(maps, batch, pw);
}
response = maps.batch(
client.makeUri(uid, mid, fid), batch);
pp(maps, response, pw);
System.err.println("chunk update time: " +
(System.currentTimeMillis() - t0) + "ms");
}
break;
}
}
/**
* Returns the enum value of a string, or exits after printing
* the list of known values.
*
* @param ec enum class
* @param key string to turn into an enum
* @return the right enum
*/
private static <T extends Enum<T>> T enumValueOrDie(Class<T> ec, String key) {
try {
return T.valueOf(ec, key);
} catch (IllegalArgumentException e) {
System.err.println("unknown " + ec.getSimpleName() + ": " + key);
System.err.println("allowed: {" +
StringUtil.join(ec.getEnumConstants(), ", ").toLowerCase() + "}");
System.exit(1);
return null;
}
}
/**
* Attempts to replace the user component in an entry id with the given uid.
*/
private static String replaceUserId(String uid, String s) {
String[] parts = s.split("/");
for (int i = 0; i < parts.length; i++) {
if (parts[i].matches("[0-9]+")) {
parts[i] = uid;
break;
}
}
return StringUtil.join(parts, "/");
}
private static void setProperties(Map<String, String> props,
BaseEntry entry) {
List<CustomProperty> cust = (entry instanceof MapEntry)
? ((MapEntry) entry).getCustomProperties()
: ((FeatureEntry) entry).getCustomProperties();
cust.clear();
for (String key : props.keySet()) {
cust.add(new CustomProperty(key, null, null, props.get(key)));
}
}
}