This repository has been archived by the owner on Jan 1, 2021. It is now read-only.
forked from openstreetmap/potlatch2
-
Notifications
You must be signed in to change notification settings - Fork 23
/
XMLConnection.as
580 lines (498 loc) · 24.3 KB
/
XMLConnection.as
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
package net.systemeD.halcyon.connection {
import flash.events.*;
import mx.rpc.http.HTTPService;
import mx.rpc.events.*;
import flash.system.Security;
import flash.net.*;
import org.iotashan.oauth.*;
import net.systemeD.halcyon.AttentionEvent;
import net.systemeD.halcyon.MapEvent;
import net.systemeD.halcyon.ExtendedURLLoader;
import net.systemeD.halcyon.connection.bboxes.*;
/**
* XMLConnection provides all the methods required to connect to a live
* OSM server. See OSMConnection for connecting to a read-only .osm file
*
* @see OSMConnection
*/
public class XMLConnection extends XMLBaseConnection {
private const MARGIN:Number=0.05;
private var discardTags:Array=["created_by",
"tiger:upload_uuid", "tiger:tlid", "tiger:source", "tiger:separated",
"geobase:datasetName", "geobase:uuid", "sub_sea:type",
"odbl", "odbl:note",
"yh:LINE_NAME", "yh:LINE_NUM", "yh:STRUCTURE", "yh:TOTYUMONO",
"yh:TYPE", "yh:WIDTH_RANK","SK53_bulk:load"];
/**
* Create a new XML connection
* @param name The name of the connection
* @param api The url of the OSM API server, e.g. http://api06.dev.openstreetmap.org/api/0.6/
* @param policy The url of the flash crossdomain policy to load,
e.g. http://api06.dev.openstreetmap.org/api/crossdomain.xml
* @param initparams Any further parameters for the connection, such as the serverName
*/
public function XMLConnection(name:String,api:String,policy:String,initparams:Object) {
super(name,api,policy,initparams);
if (policyURL != "") Security.loadPolicyFile(policyURL);
var oauthPolicy:String = getParam("oauth_policy", "");
if (oauthPolicy != "") Security.loadPolicyFile(oauthPolicy);
}
override public function loadBbox(left:Number,right:Number,
top:Number,bottom:Number):void {
purgeIfFull(left,right,top,bottom);
var requestBox:Box=new Box().fromBbox(left,bottom,right,top);
var boxes:Array;
try {
boxes=fetchSet.getBoxes(requestBox,MAX_BBOXES);
} catch(err:Error) {
boxes=[requestBox];
}
for each (var box:Box in boxes) {
// enlarge bbox by given margin on each edge
var xmargin:Number=(box.right-box.left)*MARGIN;
var ymargin:Number=(box.top-box.bottom)*MARGIN;
left =box.left -xmargin; right=box.right+xmargin;
bottom=box.bottom-ymargin; top =box.top +ymargin;
dispatchEvent(new MapEvent(MapEvent.DOWNLOAD, {minlon:left, maxlon:right, maxlat:top, minlat:bottom} ));
// send HTTP request
var mapVars:URLVariables = new URLVariables();
mapVars.bbox=left+","+bottom+","+right+","+top;
var mapRequest:URLRequest = new URLRequest(apiBaseURL+"map");
mapRequest.data = mapVars;
sendLoadRequest(mapRequest);
}
}
override public function loadEntityByID(type:String, id:Number):void {
var url:String=apiBaseURL + type + "/" + id;
if (type=='way') url+="/full";
sendLoadRequest(new URLRequest(url));
}
private function sendLoadRequest(request:URLRequest):void {
var mapLoader:URLLoader = new URLLoader();
var errorHandler:Function = function(event:Event):void {
errorOnMapLoad(event, request);
}
mapLoader.addEventListener(Event.COMPLETE, loadedMap);
mapLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
mapLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, mapLoadStatus);
mapLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
request.requestHeaders.push(new URLRequestHeader("X-Error-Format", "XML"));
mapLoader.load(request);
dispatchEvent(new Event(LOAD_STARTED));
}
private function errorOnMapLoad(event:Event, request:URLRequest):void {
var url:String = request.url + '?' + URLVariables(request.data).toString(); // for get reqeusts, at least
dispatchEvent(new MapEvent(MapEvent.ERROR, { message: "There was a problem loading the map data.\nPlease check your internet connection, or try zooming in.\n\n" + url } ));
dispatchEvent(new Event(LOAD_COMPLETED));
}
private function mapLoadStatus(event:HTTPStatusEvent):void {
}
protected var appID:OAuthConsumer;
protected var authToken:OAuthToken;
override public function setAuthToken(id:Object):void {
authToken = OAuthToken(id);
}
override public function hasAccessToken():Boolean {
return !(getAccessToken() == null);
}
override public function setAccessToken(key:String, secret:String):void {
if (key && secret) {
authToken = new OAuthToken(key, secret);
}
}
/* Get the stored access token, or try setting it up from loader params */
private function getAccessToken():OAuthToken {
if (authToken == null) {
var key:String = getParam("oauth_token", null);
var secret:String = getParam("oauth_token_secret", null);
if ( key != null && secret != null ) {
authToken = new OAuthToken(key, secret);
}
}
return authToken;
}
private function getConsumer():OAuthConsumer {
if (appID == null) {
var key:String = getParam("oauth_consumer_key", null);
var secret:String = getParam("oauth_consumer_secret", null);
if ( key != null && secret != null ) {
appID = new OAuthConsumer(key, secret);
}
}
return appID;
}
private var httpStatus:int = 0;
private function recordStatus(event:HTTPStatusEvent):void {
httpStatus = event.status;
}
private var lastUploadedChangesetTags:Object;
override public function createChangeset(tags:Object):void {
lastUploadedChangesetTags = tags;
var changesetXML:XML = <osm version="0.6"><changeset /></osm>;
var changeset:XML = <changeset />;
for (var tagKey:Object in tags) {
var tagXML:XML = <tag/>;
tagXML.@k = tagKey;
tagXML.@v = tags[tagKey];
changesetXML.changeset.appendChild(tagXML);
}
sendOAuthPut(apiBaseURL+"changeset/create",
changesetXML,
changesetCreateComplete, changesetCreateError, recordStatus);
}
private function changesetCreateComplete(event:Event):void {
var result:String = URLLoader(event.target).data;
if (result.match(/^^\d+$/)) {
// response should be a Number changeset id
var id:Number = Number(URLLoader(event.target).data);
// which means we now have a new changeset!
setActiveChangeset(new Changeset(this, id, lastUploadedChangesetTags));
} else {
var results:XML = XML(result);
throwServerError(results.message);
}
}
private function changesetCreateError(event:IOErrorEvent):void {
dispatchEvent(new Event(NEW_CHANGESET_ERROR));
}
override public function closeChangeset():void {
var cs:Changeset = getActiveChangeset();
if (!cs) return;
sendOAuthPut(apiBaseURL+"changeset/"+cs.id+"/close",
null,
changesetCloseComplete, changesetCloseError, recordStatus);
closeActiveChangeset();
}
private function changesetCloseComplete(event:Event):void {
dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Changeset closed"));
}
private function changesetCloseError(event:Event):void {
dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Couldn't close changeset", 1));
}
private function signedOAuthURL(url:String, method:String):String {
// method should be PUT, GET, POST or DELETE
var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
var oauthRequest:OAuthRequest = new OAuthRequest(method, url, null, getConsumer(), authToken);
var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING);
return String(urlStr);
}
private function sendOAuthPut(url:String, xml:XML, onComplete:Function, onError:Function, onStatus:Function):void {
// build the request
var urlReq:URLRequest = new URLRequest(signedOAuthURL(url, "PUT"));
urlReq.method = "POST";
if (xml) { urlReq.data = xml.toXMLString(); } else { urlReq.data = true; }
urlReq.contentType = "application/xml";
urlReq.requestHeaders = [ new URLRequestHeader("X-HTTP-Method-Override", "PUT"),
new URLRequestHeader("X-Error-Format", "XML") ];
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
loader.load(urlReq);
}
private function sendOAuthGet(url:String, onComplete:Function, onError:Function, onStatus:Function):void {
var urlReq:URLRequest = new URLRequest(signedOAuthURL(url, "GET"));
urlReq.method = "GET";
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
loader.load(urlReq);
}
/** Create XML changeset and send it to the server. Returns the XML string for use in the 'Show data' button.
(We don't mind what's returned as long as it implements .toString() ) */
override public function uploadChanges():* {
var changeset:Changeset = getActiveChangeset();
var upload:XML = <osmChange version="0.6"/>
upload.appendChild(addCreated(changeset, getAllNodeIDs, getNode, serialiseNode));
upload.appendChild(addCreated(changeset, getAllWayIDs, getWay, serialiseWay));
upload.appendChild(addCreated(changeset, getAllRelationIDs, getRelation, serialiseRelation));
upload.appendChild(addModified(changeset, getAllNodeIDs, getNode, serialiseNode));
upload.appendChild(addModified(changeset, getAllWayIDs, getWay, serialiseWay));
upload.appendChild(addModified(changeset, getAllRelationIDs, getRelation, serialiseRelation));
upload.appendChild(addDeleted(changeset, getAllRelationIDs, getRelation, serialiseEntityRoot, false));
upload.appendChild(addDeleted(changeset, getAllRelationIDs, getRelation, serialiseEntityRoot, true));
upload.appendChild(addDeleted(changeset, getAllWayIDs, getWay, serialiseEntityRoot, false));
upload.appendChild(addDeleted(changeset, getAllWayIDs, getWay, serialiseEntityRoot, true));
upload.appendChild(addDeleted(changeset, getAllNodeIDs, getNode, serialiseEntityRoot, false));
upload.appendChild(addDeleted(changeset, getAllNodeIDs, getNode, serialiseEntityRoot, true));
// now actually upload them
// make an OAuth query
var url:String = apiBaseURL+"changeset/" + changeset.id + "/upload";
// build the actual request
var serv:HTTPService=new HTTPService();
serv.method="POST";
serv.url=signedOAuthURL(url, "POST");
serv.contentType = "text/xml";
serv.headers={'X-Error-Format':'xml'};
serv.request=" ";
serv.resultFormat="e4x";
serv.requestTimeout=0;
serv.addEventListener(ResultEvent.RESULT, diffUploadComplete);
serv.addEventListener(FaultEvent.FAULT, diffUploadIOError);
serv.send(upload);
dispatchEvent(new Event(SAVE_STARTED));
return upload;
}
private function diffUploadComplete(event:ResultEvent):void {
var results:XML = XML(event.result);
// was it an error document?
if (results.name().localName=='osmError') {
dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, false));
diffUploadAPIError(results.status, results.message);
return;
}
// response should be XML describing the progress
for each( var update:XML in results.child("*") ) {
var oldID:Number = Number(update.@old_id);
var newID:Number = Number(update.@new_id);
var version:uint = uint(update.@new_version);
var type:String = update.name();
if (newID==0) {
// delete
if (type == "node" ) { killNode(oldID); }
else if (type == "way" ) { killWay(oldID); }
else if (type == "relation") { killRelation(oldID); }
} else {
// create/update
if (type == "node" ) { renumberNode(oldID, newID, version); getNode(newID).markClean(); }
else if (type == "way" ) { renumberWay(oldID, newID, version); getWay(newID).markClean(); }
else if (type == "relation") { renumberRelation(oldID, newID, version); getRelation(newID).markClean(); }
}
}
dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, true));
freshenActiveChangeset();
markClean(); // marks the connection clean. Pressing undo from this point on leads to unexpected results
MainUndoStack.getGlobalStack().breakUndo(); // so, for now, break the undo stack
}
private function diffUploadIOError(event:FaultEvent):void {
trace(event.fault);
dispatchEvent(new MapEvent(MapEvent.ERROR, { message: "Couldn't upload data: "+event.fault.faultString } ));
dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, false));
}
private function diffUploadAPIError(status:String, message:String):void {
var matches:Array;
switch (status) {
case '409 Conflict':
if (message.match(/changeset/i)) { throwChangesetError(message); return; }
matches=message.match(/mismatch.+had: (\d+) of (\w+) (\d+)/i);
if (matches) { throwConflictError(findEntity(matches[2],matches[3]), Number(matches[1]), message); return; }
break;
case '410 Gone':
matches=message.match(/The (\w+) with the id (\d+)/i);
if (matches) { throwAlreadyDeletedError(findEntity(matches[1],matches[2]), message); return; }
break;
case '412 Precondition Failed':
matches=message.match(/Node (\d+) is still used/i);
if (matches) { throwInUseError(findEntity('Node',matches[1]), message); return; }
matches=message.match(/relation (\d+) is used/i);
if (matches) { throwInUseError(findEntity('Relation',matches[1]), message); return; }
matches=message.match(/Way (\d+) still used/i);
if (matches) { throwInUseError(findEntity('Way',matches[1]), message); return; }
matches=message.match(/Cannot update (\w+) (\d+)/i);
if (matches) { throwEntityError(findEntity(matches[1],matches[2]), message); return; }
matches=message.match(/Relation with id (\d+)/i);
if (matches) { throwEntityError(findEntity('Relation',matches[1]), message); return; }
matches=message.match(/Way (\d+) requires the nodes/i);
if (matches) { throwEntityError(findEntity('Way',matches[1]), message); return; }
throwBugError(message); return;
case '404 Not Found':
throwBugError(message); return;
case '400 Bad Request':
matches=message.match(/Element (\w+)\/(\d+)/i);
if (matches) { throwEntityError(findEntity(matches[1],matches[2]), message); return; }
matches=message.match(/You tried to add \d+ nodes to way (\d+)/i);
if (matches) { throwEntityError(findEntity('Way',matches[1]), message); return; }
throwBugError(message); return;
}
// Not caught, so just throw a generic server error
throwServerError(message);
}
private function addCreated(changeset:Changeset, getIDs:Function, get:Function, serialise:Function):XML {
var create:XML = <create version="0.6"/>
for each( var id:Number in getIDs() ) {
var entity:Entity = get(id);
if ( id >= 0 || entity.deleted )
continue;
var xml:XML = serialise(entity);
xml.@changeset = changeset.id;
create.appendChild(xml);
}
return create.hasComplexContent() ? create : <!-- blank create section -->;
}
private function addDeleted(changeset:Changeset, getIDs:Function, get:Function, serialise:Function, ifUnused:Boolean):XML {
var del:XML = <delete version="0.6"/>
if (ifUnused) del.@["if-unused"] = "true";
for each( var id:Number in getIDs() ) {
var entity:Entity = get(id);
// creates are already included
if ( id < 0 || !entity.deleted || entity.parentsLoaded==ifUnused)
continue;
var xml:XML = serialise(entity);
xml.@changeset = changeset.id;
del.appendChild(xml);
}
return del.hasComplexContent() ? del : <!-- blank delete section -->;
}
private function addModified(changeset:Changeset, getIDs:Function, get:Function, serialise:Function):XML {
var modify:XML = <modify version="0.6"/>
for each( var id:Number in getIDs() ) {
var entity:Entity = get(id);
// creates and deletes are already included
if ( id < 0 || entity.deleted || !entity.isDirty )
continue;
var xml:XML = serialise(entity);
xml.@changeset = changeset.id;
modify.appendChild(xml);
}
return modify.hasComplexContent() ? modify : <!-- blank modify section -->;
}
private function serialiseNode(node:Node):XML {
var xml:XML = serialiseEntityRoot(node); //<node/>
serialiseEntityTags(node, xml);
xml.@lat = node.lat;
xml.@lon = node.lon;
return xml;
}
private function serialiseWay(way:Way):XML {
var xml:XML = serialiseEntityRoot(way); //<node/>
serialiseEntityTags(way, xml);
for ( var i:uint = 0; i < way.length; i++ ) {
var nd:XML = <nd/>
nd.@ref = way.getNode(i).id;
xml.appendChild(nd);
}
return xml;
}
private function serialiseRelation(relation:Relation):XML {
var xml:XML = serialiseEntityRoot(relation); //<node/>
serialiseEntityTags(relation, xml);
for ( var i:uint = 0; i < relation.length; i++ ) {
var relMember:RelationMember = relation.getMember(i);
var member:XML = <member/>
member.@ref = relMember.entity.id;
member.@type = relMember.entity.getType();
member.@role = relMember.role;
xml.appendChild(member);
}
return xml;
}
private function serialiseEntityRoot(entity:Object):XML {
var xml:XML;
if (entity is Way ) { xml = <way/> }
else if (entity is Node ) { xml = <node/> }
else if (entity is Relation) { xml = <relation/> }
xml.@id = entity.id;
xml.@version = entity.version;
return xml;
}
private function serialiseEntityTags(entity:Entity, xml:XML):void {
xml.@id = entity.id;
xml.@version = entity.version;
for each( var tag:Tag in entity.getTagArray() ) {
if (discardTags.indexOf(tag.key) > -1) {
entity.setTag(tag.key, null, MainUndoStack.getGlobalStack().addAction);
continue;
}
var tagXML:XML = <tag/>
tagXML.@k = tag.key;
tagXML.@v = tag.value;
xml.appendChild(tagXML);
}
}
override public function fetchUserTraces(refresh:Boolean=false):void {
if (traces_loaded && !refresh) {
dispatchEvent(new Event(TRACES_LOADED));
} else {
sendOAuthGet(apiBaseURL+"user/gpx_files", tracesLoadComplete, errorOnMapLoad, mapLoadStatus); //needs error handlers
dispatchEvent(new Event(LOAD_STARTED)); //specific to map or reusable?
}
}
private function tracesLoadComplete(event:Event):void {
var files:XML = new XML(URLLoader(event.target).data);
for each(var traceData:XML in files.gpx_file) {
var t:Trace = findTrace(traceData.@id);
if (!t) { t=new Trace(this); addTrace(t); }
t.fromXML(traceData);
}
traces_loaded = true;
dispatchEvent(new Event(LOAD_COMPLETED));
dispatchEvent(new Event(TRACES_LOADED));
}
override public function fetchTrace(id:Number, callback:Function):void {
sendOAuthGet(apiBaseURL+"gpx/"+id+"/data.xml",
function(e:Event):void {
dispatchEvent(new Event(LOAD_COMPLETED));
callback(e);
}, errorOnTraceLoad, mapLoadStatus); // needs error handlers
dispatchEvent(new Event(LOAD_STARTED)); //specifc to map or reusable?
}
private function errorOnTraceLoad(event:Event):void {
trace("Trace load error");
dispatchEvent(new Event(LOAD_COMPLETED));
}
/** Fetch the history for the given entity. The callback function will be given an array of entities of that type, representing the different versions */
override public function fetchHistory(entity:Entity, callback:Function):void {
if (entity.id >= 0) {
var request:URLRequest = new URLRequest(apiBaseURL + entity.getType() + "/" + entity.id + "/history");
var loader:ExtendedURLLoader = new ExtendedURLLoader();
loader.addEventListener(Event.COMPLETE, loadedHistory);
loader.addEventListener(IOErrorEvent.IO_ERROR, errorOnMapLoad); //needs error handlers
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, mapLoadStatus);
loader.info['callback'] = callback; //store the callback so we can use it later
loader.load(request);
dispatchEvent(new Event(LOAD_STARTED));
} else {
// objects created locally only have one state, their current one
callback([entity]);
}
}
private function loadedHistory(event:Event):void {
var _xml:XML = new XML(ExtendedURLLoader(event.target).data);
var results:Array = [];
var dummyConn:Connection = new Connection("dummy", null, null);
dispatchEvent(new Event(LOAD_COMPLETED));
// only one type of entity should be returned, but this handles any
for each(var nodeData:XML in _xml.node) {
var newNode:Node = new Node(
dummyConn,
Number(nodeData.@id),
uint(nodeData.@version),
parseTags(nodeData.tag),
true,
Number(nodeData.@lat),
Number(nodeData.@lon),
Number(nodeData.@uid),
nodeData.@timestamp,
nodeData.@user
);
newNode.lastChangeset=nodeData.@changeset;
results.push(newNode);
}
for each(var wayData:XML in _xml.way) {
var nodes:Array = [];
for each(var nd:XML in wayData.nd) {
nodes.push(new Node(dummyConn,Number(nd.@ref), NaN, null, false, NaN, NaN));
}
var newWay:Way = new Way(
dummyConn,
Number(wayData.@id),
uint(wayData.@version),
parseTags(wayData.tag),
true,
nodes,
Number(wayData.@uid),
wayData.@timestamp,
wayData.@user
);
newWay.lastChangeset=wayData.@changeset;
results.push(newWay);
}
for each(var relData:XML in _xml.relation) {
trace("relation history not implemented");
}
// use the callback we stored earlier, and pass it the results
ExtendedURLLoader(event.target).info['callback'](results);
}
}
}