source: josm/trunk/src/org/openstreetmap/josm/io/OsmApi.java@ 2474

Last change on this file since 2474 was 2273, checked in by jttt, 15 years ago

Replace testing for id <= 0 with isNew() method

File size: 25.4 KB
Line 
1//License: GPL. See README for details.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.BufferedWriter;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.io.OutputStream;
12import java.io.OutputStreamWriter;
13import java.io.PrintWriter;
14import java.io.StringReader;
15import java.io.StringWriter;
16import java.net.ConnectException;
17import java.net.HttpURLConnection;
18import java.net.SocketTimeoutException;
19import java.net.URL;
20import java.net.UnknownHostException;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.HashMap;
25
26import javax.xml.parsers.SAXParserFactory;
27
28import org.openstreetmap.josm.Main;
29import org.openstreetmap.josm.data.osm.Changeset;
30import org.openstreetmap.josm.data.osm.OsmPrimitive;
31import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
32import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor;
33import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
34import org.openstreetmap.josm.gui.progress.ProgressMonitor;
35import org.xml.sax.Attributes;
36import org.xml.sax.InputSource;
37import org.xml.sax.SAXException;
38import org.xml.sax.helpers.DefaultHandler;
39
40/**
41 * Class that encapsulates the communications with the OSM API.
42 *
43 * All interaction with the server-side OSM API should go through this class.
44 *
45 * It is conceivable to extract this into an interface later and create various
46 * classes implementing the interface, to be able to talk to various kinds of servers.
47 *
48 */
49public class OsmApi extends OsmConnection {
50 /** max number of retries to send a request in case of HTTP 500 errors or timeouts */
51 static public final int DEFAULT_MAX_NUM_RETRIES = 5;
52
53 /** the collection of instantiated OSM APIs */
54 private static HashMap<String, OsmApi> instances = new HashMap<String, OsmApi>();
55
56 /**
57 * replies the {@see OsmApi} for a given server URL
58 *
59 * @param serverUrl the server URL
60 * @return the OsmApi
61 * @throws IllegalArgumentException thrown, if serverUrl is null
62 *
63 */
64 static public OsmApi getOsmApi(String serverUrl) {
65 OsmApi api = instances.get(serverUrl);
66 if (api == null) {
67 api = new OsmApi(serverUrl);
68 instances.put(serverUrl,api);
69 }
70 return api;
71 }
72 /**
73 * replies the {@see OsmApi} for the URL given by the preference <code>osm-server.url</code>
74 *
75 * @return the OsmApi
76 * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set
77 *
78 */
79 static public OsmApi getOsmApi() {
80 String serverUrl = Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api");
81 if (serverUrl == null)
82 throw new IllegalStateException(tr("Preference ''{0}'' missing. Can't initialize OsmApi.", "osm-server.url"));
83 return getOsmApi(serverUrl);
84 }
85
86 /** the server URL */
87 private String serverUrl;
88
89 /**
90 * Object describing current changeset
91 */
92 private Changeset changeset;
93
94 /**
95 * API version used for server communications
96 */
97 private String version = null;
98
99 /** the api capabilities */
100 private Capabilities capabilities = new Capabilities();
101
102 /**
103 * true if successfully initialized
104 */
105 private boolean initialized = false;
106
107 private StringWriter swriter = new StringWriter();
108 private OsmWriter osmWriter = new OsmWriter(new PrintWriter(swriter), true, null);
109
110 /**
111 * A parser for the "capabilities" response XML
112 */
113 private class CapabilitiesParser extends DefaultHandler {
114 @Override
115 public void startDocument() throws SAXException {
116 capabilities.clear();
117 }
118
119 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
120 for (int i=0; i< qName.length(); i++) {
121 capabilities.put(qName, atts.getQName(i), atts.getValue(i));
122 }
123 }
124 }
125
126 /**
127 * creates an OSM api for a specific server URL
128 *
129 * @param serverUrl the server URL. Must not be null
130 * @exception IllegalArgumentException thrown, if serverUrl is null
131 */
132 protected OsmApi(String serverUrl) {
133 if (serverUrl == null)
134 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "serverUrl"));
135 this.serverUrl = serverUrl;
136 }
137
138 /**
139 * Returns the OSM protocol version we use to talk to the server.
140 * @return protocol version, or null if not yet negotiated.
141 */
142 public String getVersion() {
143 return version;
144 }
145
146 /**
147 * Returns true if the negotiated version supports diff uploads.
148 * @return true if the negotiated version supports diff uploads
149 */
150 public boolean hasSupportForDiffUploads() {
151 return ((version != null) && (version.compareTo("0.6")>=0));
152 }
153
154 /**
155 * Initializes this component by negotiating a protocol version with the server.
156 *
157 * @exception OsmApiInitializationException thrown, if an exception occurs
158 */
159 public void initialize(ProgressMonitor monitor) throws OsmApiInitializationException {
160 if (initialized)
161 return;
162 cancel = false;
163 initAuthentication();
164 try {
165 String s = sendRequest("GET", "capabilities", null,monitor, false);
166 InputSource inputSource = new InputSource(new StringReader(s));
167 SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new CapabilitiesParser());
168 if (capabilities.supportsVersion("0.6")) {
169 version = "0.6";
170 } else if (capabilities.supportsVersion("0.5")) {
171 version = "0.5";
172 } else {
173 System.err.println(tr("This version of JOSM is incompatible with the configured server."));
174 System.err.println(tr("It supports protocol versions 0.5 and 0.6, while the server says it supports {0} to {1}.",
175 capabilities.get("version", "minimum"), capabilities.get("version", "maximum")));
176 initialized = false;
177 }
178 System.out.println(tr("Communications with {0} established using protocol version {1}.",
179 serverUrl,
180 version));
181 osmWriter.setVersion(version);
182 initialized = true;
183 } catch (Exception ex) {
184 initialized = false;
185 throw new OsmApiInitializationException(ex);
186 }
187 }
188
189 /**
190 * Makes an XML string from an OSM primitive. Uses the OsmWriter class.
191 * @param o the OSM primitive
192 * @param addBody true to generate the full XML, false to only generate the encapsulating tag
193 * @return XML string
194 */
195 private String toXml(OsmPrimitive o, boolean addBody) {
196 swriter.getBuffer().setLength(0);
197 osmWriter.setWithBody(addBody);
198 osmWriter.setChangeset(changeset);
199 osmWriter.header();
200 o.visit(osmWriter);
201 osmWriter.footer();
202 osmWriter.out.flush();
203 return swriter.toString();
204 }
205
206 /**
207 * Makes an XML string from an OSM primitive. Uses the OsmWriter class.
208 * @param o the OSM primitive
209 * @param addBody true to generate the full XML, false to only generate the encapsulating tag
210 * @return XML string
211 */
212 private String toXml(Changeset s) {
213 swriter.getBuffer().setLength(0);
214 osmWriter.header();
215 s.visit(osmWriter);
216 osmWriter.footer();
217 osmWriter.out.flush();
218 return swriter.toString();
219 }
220
221 /**
222 * Returns the base URL for API requests, including the negotiated version number.
223 * @return base URL string
224 */
225 public String getBaseUrl() {
226 StringBuffer rv = new StringBuffer(serverUrl);
227 if (version != null) {
228 rv.append("/");
229 rv.append(version);
230 }
231 rv.append("/");
232 // this works around a ruby (or lighttpd) bug where two consecutive slashes in
233 // an URL will cause a "404 not found" response.
234 int p; while ((p = rv.indexOf("//", 6)) > -1) { rv.delete(p, p + 1); }
235 return rv.toString();
236 }
237
238 /**
239 * Creates an OSM primitive on the server. The OsmPrimitive object passed in
240 * is modified by giving it the server-assigned id.
241 *
242 * @param osm the primitive
243 * @throws OsmTransferException if something goes wrong
244 */
245 public void createPrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
246 String ret = "";
247 try {
248 ensureValidChangeset();
249 initialize(monitor);
250 ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/create", toXml(osm, true),monitor);
251 osm.setOsmId(Long.parseLong(ret.trim()), 1);
252 } catch(NumberFormatException e){
253 throw new OsmTransferException(tr("Unexpected format of ID replied by the server. Got ''{0}''.", ret));
254 }
255 }
256
257 /**
258 * Modifies an OSM primitive on the server. For protocols greater than 0.5,
259 * the OsmPrimitive object passed in is modified by giving it the server-assigned
260 * version.
261 *
262 * @param osm the primitive. Must not be null
263 * @throws OsmTransferException if something goes wrong
264 */
265 public void modifyPrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
266 String ret = null;
267 try {
268 ensureValidChangeset();
269 initialize(monitor);
270 if (version.equals("0.5")) {
271 // legacy mode does not return the new object version.
272 sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.getId(), toXml(osm, true),monitor);
273 } else {
274 // normal mode (0.6 and up) returns new object version.
275 ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.getId(), toXml(osm, true), monitor);
276 osm.setOsmId(osm.getId(), Integer.parseInt(ret.trim()));
277 }
278 } catch(NumberFormatException e) {
279 throw new OsmTransferException(tr("Unexpected format of new version of modified primitive ''{0}''. Got ''{1}''.", osm.getId(), ret));
280 }
281 }
282
283 /**
284 * Deletes an OSM primitive on the server.
285 * @param osm the primitive
286 * @throws OsmTransferException if something goes wrong
287 */
288 public void deletePrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
289 ensureValidChangeset();
290 initialize(monitor);
291 // can't use a the individual DELETE method in the 0.6 API. Java doesn't allow
292 // submitting a DELETE request with content, the 0.6 API requires it, however. Falling back
293 // to diff upload.
294 //
295 uploadDiff(Collections.singleton(osm), monitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
296 }
297
298
299 /**
300 * Creates a new changeset based on the keys in <code>changeset</code>. If this
301 * method succeeds, changeset.getId() replies the id the server assigned to the new
302 * changeset
303 *
304 * The changeset must not be null, but its key/value-pairs may be empty.
305 *
306 * @param changeset the changeset toe be created. Must not be null.
307 * @param progressMonitor the progress monitor
308 * @throws OsmTransferException signifying a non-200 return code, or connection errors
309 * @throws IllegalArgumentException thrown if changeset is null
310 */
311 public void openChangeset(Changeset changeset, ProgressMonitor progressMonitor) throws OsmTransferException {
312 if (changeset == null)
313 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "changeset"));
314 try {
315 progressMonitor.beginTask((tr("Creating changeset...")));
316 initialize(progressMonitor);
317 String ret = "";
318 try {
319 ret = sendRequest("PUT", "changeset/create", toXml(changeset),progressMonitor);
320 changeset.setId(Long.parseLong(ret.trim()));
321 changeset.setOpen(true);
322 } catch(NumberFormatException e){
323 throw new OsmTransferException(tr("Unexpected format of ID replied by the server. Got ''{0}''.", ret));
324 }
325 progressMonitor.setCustomText((tr("Successfully opened changeset {0}",changeset.getId())));
326 } finally {
327 progressMonitor.finishTask();
328 }
329 }
330
331 /**
332 * Updates a changeset with the keys in <code>changesetUpdate</code>. The changeset must not
333 * be null and id > 0 must be true.
334 *
335 * @param changeset the changeset to update. Must not be null.
336 * @param monitor the progress monitor. If null, uses the {@see NullProgressMonitor#INSTANCE}.
337 *
338 * @throws OsmTransferException if something goes wrong.
339 * @throws IllegalArgumentException if changeset is null
340 * @throws IllegalArgumentException if changeset.getId() == 0
341 *
342 */
343 public void updateChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
344 if (changeset == null)
345 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "changeset"));
346 if (monitor == null) {
347 monitor = NullProgressMonitor.INSTANCE;
348 }
349 if (changeset.getId() <= 0)
350 throw new IllegalArgumentException(tr("Changeset ID > 0 expected. Got {0}.", changeset.getId()));
351 try {
352 monitor.beginTask(tr("Updating changeset..."));
353 initialize(monitor);
354 monitor.setCustomText(tr("Updating changeset {0}...", changeset.getId()));
355 sendRequest(
356 "PUT",
357 "changeset/" + changeset.getId(),
358 toXml(changeset),
359 monitor
360 );
361 } finally {
362 monitor.finishTask();
363 }
364 }
365
366
367 /**
368 * Closes a changeset on the server. Sets changeset.setOpen(false) if this operation
369 * succeeds.
370 *
371 * @param changeset the changeset to be closed. Must not be null. changeset.getId() > 0 required.
372 * @param monitor the progress monitor. If null, uses {@see NullProgressMonitor#INSTANCE}
373 *
374 * @throws OsmTransferException if something goes wrong.
375 * @throws IllegalArgumentException thrown if changeset is null
376 * @throws IllegalArgumentException thrown if changeset.getId() <= 0
377 */
378 public void closeChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
379 if (changeset == null)
380 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "changeset"));
381 if (monitor == null) {
382 monitor = NullProgressMonitor.INSTANCE;
383 }
384 if (changeset.getId() <= 0)
385 throw new IllegalArgumentException(tr("Changeset ID > 0 expected. Got {0}.", changeset.getId()));
386 try {
387 monitor.beginTask(tr("Closing changeset..."));
388 initialize(monitor);
389 sendRequest("PUT", "changeset" + "/" + changeset.getId() + "/close", null, monitor);
390 changeset.setOpen(false);
391 } finally {
392 monitor.finishTask();
393 }
394 }
395
396 /**
397 * Uploads a list of changes in "diff" form to the server.
398 *
399 * @param list the list of changed OSM Primitives
400 * @param monitor the progress monitor
401 * @return list of processed primitives
402 * @throws OsmTransferException if something is wrong
403 */
404 public Collection<OsmPrimitive> uploadDiff(Collection<OsmPrimitive> list, ProgressMonitor monitor) throws OsmTransferException {
405 try {
406 monitor.beginTask("", list.size() * 2);
407 if (changeset == null)
408 throw new OsmTransferException(tr("No changeset present for diff upload."));
409
410 initialize(monitor);
411 final ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>();
412
413 CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this);
414
415 monitor.subTask(tr("Preparing..."));
416 for (OsmPrimitive osm : list) {
417 osm.visit(duv);
418 monitor.worked(1);
419 }
420 monitor.indeterminateSubTask(tr("Uploading..."));
421
422 String diff = duv.getDocument();
423 String diffresult = sendRequest("POST", "changeset/" + changeset.getId() + "/upload", diff,monitor);
424 DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(),
425 monitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
426 return processed;
427 } catch(OsmTransferException e) {
428 throw e;
429 } catch(Exception e) {
430 throw new OsmTransferException(e);
431 } finally {
432 monitor.finishTask();
433 }
434 }
435
436 private void sleepAndListen(int retry, ProgressMonitor monitor) throws OsmTransferCancelledException {
437 System.out.print(tr("Waiting 10 seconds ... "));
438 for(int i=0; i < 10; i++) {
439 if (monitor != null) {
440 monitor.setCustomText(tr("Starting retry {0} of {1} in {2} seconds ...", getMaxRetries() - retry,getMaxRetries(), 10-i));
441 }
442 if (cancel || isAuthCancelled())
443 throw new OsmTransferCancelledException();
444 try {
445 Thread.sleep(1000);
446 } catch (InterruptedException ex) {}
447 }
448 System.out.println(tr("OK - trying again."));
449 }
450
451 /**
452 * Replies the max. number of retries in case of 5XX errors on the server
453 *
454 * @return the max number of retries
455 */
456 protected int getMaxRetries() {
457 int ret = Main.pref.getInteger("osm-server.max-num-retries", DEFAULT_MAX_NUM_RETRIES);
458 return Math.max(ret,0);
459 }
460
461 private String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor) throws OsmTransferException {
462 return sendRequest(requestMethod, urlSuffix, requestBody, monitor, true);
463 }
464
465 /**
466 * Generic method for sending requests to the OSM API.
467 *
468 * This method will automatically re-try any requests that are answered with a 5xx
469 * error code, or that resulted in a timeout exception from the TCP layer.
470 *
471 * @param requestMethod The http method used when talking with the server.
472 * @param urlSuffix The suffix to add at the server url, not including the version number,
473 * but including any object ids (e.g. "/way/1234/history").
474 * @param requestBody the body of the HTTP request, if any.
475 *
476 * @return the body of the HTTP response, if and only if the response code was "200 OK".
477 * @exception OsmTransferException if the HTTP return code was not 200 (and retries have
478 * been exhausted), or rewrapping a Java exception.
479 */
480 private String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor, boolean doAuthenticate) throws OsmTransferException {
481 StringBuffer responseBody = new StringBuffer();
482 int retries = getMaxRetries();
483
484 while(true) { // the retry loop
485 try {
486 URL url = new URL(new URL(getBaseUrl()), urlSuffix);
487 System.out.print(requestMethod + " " + url + "... ");
488 activeConnection = (HttpURLConnection)url.openConnection();
489 activeConnection.setConnectTimeout(15000);
490 activeConnection.setRequestMethod(requestMethod);
491 if (doAuthenticate) {
492 addAuth(activeConnection);
493 }
494
495 if (requestMethod.equals("PUT") || requestMethod.equals("POST") || requestMethod.equals("DELETE")) {
496 activeConnection.setDoOutput(true);
497 activeConnection.setRequestProperty("Content-type", "text/xml");
498 OutputStream out = activeConnection.getOutputStream();
499
500 // It seems that certain bits of the Ruby API are very unhappy upon
501 // receipt of a PUT/POST message withtout a Content-length header,
502 // even if the request has no payload.
503 // Since Java will not generate a Content-length header unless
504 // we use the output stream, we create an output stream for PUT/POST
505 // even if there is no payload.
506 if (requestBody != null) {
507 BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
508 bwr.write(requestBody);
509 bwr.flush();
510 }
511 out.close();
512 }
513
514 activeConnection.connect();
515 System.out.println(activeConnection.getResponseMessage());
516 int retCode = activeConnection.getResponseCode();
517
518 if (retCode >= 500) {
519 if (retries-- > 0) {
520 sleepAndListen(retries, monitor);
521 System.out.println(tr("Starting retry {0} of {1}.", getMaxRetries() - retries,getMaxRetries()));
522 continue;
523 }
524 }
525
526 // populate return fields.
527 responseBody.setLength(0);
528
529 // If the API returned an error code like 403 forbidden, getInputStream
530 // will fail with an IOException.
531 InputStream i = null;
532 try {
533 i = activeConnection.getInputStream();
534 } catch (IOException ioe) {
535 i = activeConnection.getErrorStream();
536 }
537 BufferedReader in = new BufferedReader(new InputStreamReader(i));
538
539 String s;
540 while((s = in.readLine()) != null) {
541 responseBody.append(s);
542 responseBody.append("\n");
543 }
544 String errorHeader = null;
545 // Look for a detailed error message from the server
546 if (activeConnection.getHeaderField("Error") != null) {
547 errorHeader = activeConnection.getHeaderField("Error");
548 System.err.println("Error header: " + errorHeader);
549 } else if (retCode != 200 && responseBody.length()>0) {
550 System.err.println("Error body: " + responseBody);
551 }
552 activeConnection.disconnect();
553
554 errorHeader = errorHeader == null? null : errorHeader.trim();
555 String errorBody = responseBody == null ? null : responseBody.toString().trim();
556 switch(retCode) {
557 case HttpURLConnection.HTTP_OK:
558 break; // do nothing
559 case HttpURLConnection.HTTP_GONE:
560 throw new OsmApiPrimitiveGoneException(errorHeader, errorBody);
561 default:
562 throw new OsmApiException(retCode, errorHeader, errorBody);
563
564 }
565 return responseBody.toString();
566 } catch (UnknownHostException e) {
567 throw new OsmTransferException(e);
568 } catch (SocketTimeoutException e) {
569 if (retries-- > 0) {
570 continue;
571 }
572 throw new OsmTransferException(e);
573 } catch (ConnectException e) {
574 if (retries-- > 0) {
575 continue;
576 }
577 throw new OsmTransferException(e);
578 } catch(OsmTransferException e) {
579 throw e;
580 } catch (Exception e) {
581 throw new OsmTransferException(e);
582 }
583 }
584 }
585
586 /**
587 * returns the API capabilities; null, if the API is not initialized yet
588 *
589 * @return the API capabilities
590 */
591 public Capabilities getCapabilities() {
592 return capabilities;
593 }
594
595
596 /**
597 * Ensures that the current changeset can be used for uploading data
598 *
599 * @throws OsmTransferException thrown if the current changeset can't be used for
600 * uploading data
601 */
602 protected void ensureValidChangeset() throws OsmTransferException {
603 if (changeset == null)
604 throw new OsmTransferException(tr("Current changeset is null. Can't upload data."));
605 if (changeset.getId() <= 0)
606 throw new OsmTransferException(tr("ID of current changeset > 0 required. Current ID is {0}.", changeset.getId()));
607 }
608 /**
609 * Replies the changeset data uploads are currently directed to
610 *
611 * @return the changeset data uploads are currently directed to
612 */
613 public Changeset getChangeset() {
614 return changeset;
615 }
616
617 /**
618 * Sets the changesets to which further data uploads are directed. The changeset
619 * can be null. If it isn't null it must have been created, i.e. id > 0 is required. Furthermore,
620 * it must be open.
621 *
622 * @param changeset the changeset
623 * @throws IllegalArgumentException thrown if changeset.getId() <= 0
624 * @throws IllegalArgumentException thrown if !changeset.isOpen()
625 */
626 public void setChangeset(Changeset changeset) {
627 if (changeset == null) {
628 this.changeset = null;
629 return;
630 }
631 if (changeset.getId() <= 0)
632 throw new IllegalArgumentException(tr("Changeset ID > 0 expected. Got {0}.", changeset.getId()));
633 if (!changeset.isOpen())
634 throw new IllegalArgumentException(tr("Open changeset expected. Got closed changeset with id {0}.", changeset.getId()));
635 this.changeset = changeset;
636 }
637
638}
Note: See TracBrowser for help on using the repository browser.