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

Last change on this file since 2181 was 2181, checked in by stoecker, 15 years ago

lots of i18n fixes

File size: 24.9 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);
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 * @return list of processed primitives
401 * @throws OsmTransferException if something is wrong
402 */
403 public Collection<OsmPrimitive> uploadDiff(Collection<OsmPrimitive> list, ProgressMonitor progressMonitor) throws OsmTransferException {
404 try {
405 progressMonitor.beginTask("", list.size() * 2);
406 if (changeset == null)
407 throw new OsmTransferException(tr("No changeset present for diff upload."));
408
409 initialize(progressMonitor);
410 final ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>();
411
412 CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this);
413
414 progressMonitor.subTask(tr("Preparing..."));
415 for (OsmPrimitive osm : list) {
416 osm.visit(duv);
417 progressMonitor.worked(1);
418 }
419 progressMonitor.indeterminateSubTask(tr("Uploading..."));
420
421 String diff = duv.getDocument();
422 String diffresult = sendRequest("POST", "changeset/" + changeset.getId() + "/upload", diff,progressMonitor);
423 DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(),
424 progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
425 return processed;
426 } catch(OsmTransferException e) {
427 throw e;
428 } catch(Exception e) {
429 throw new OsmTransferException(e);
430 } finally {
431 progressMonitor.finishTask();
432 }
433 }
434
435
436
437 private void sleepAndListen(int retry, ProgressMonitor monitor) throws OsmTransferCancelledException {
438 System.out.print(tr("Waiting 10 seconds ... "));
439 for(int i=0; i < 10; i++) {
440 if (monitor != null) {
441 monitor.setCustomText(tr("Starting retry {0} of {1} in {2} seconds ...", getMaxRetries() - retry,getMaxRetries(), 10-i));
442 }
443 if (cancel || isAuthCancelled())
444 throw new OsmTransferCancelledException();
445 try {
446 Thread.sleep(1000);
447 } catch (InterruptedException ex) {}
448 }
449 System.out.println(tr("OK - trying again."));
450 }
451
452 /**
453 * Replies the max. number of retries in case of 5XX errors on the server
454 *
455 * @return the max number of retries
456 */
457 protected int getMaxRetries() {
458 int ret = Main.pref.getInteger("osm-server.max-num-retries", DEFAULT_MAX_NUM_RETRIES);
459 return Math.max(ret,0);
460 }
461
462 /**
463 * Generic method for sending requests to the OSM API.
464 *
465 * This method will automatically re-try any requests that are answered with a 5xx
466 * error code, or that resulted in a timeout exception from the TCP layer.
467 *
468 * @param requestMethod The http method used when talking with the server.
469 * @param urlSuffix The suffix to add at the server url, not including the version number,
470 * but including any object ids (e.g. "/way/1234/history").
471 * @param requestBody the body of the HTTP request, if any.
472 *
473 * @return the body of the HTTP response, if and only if the response code was "200 OK".
474 * @exception OsmTransferException if the HTTP return code was not 200 (and retries have
475 * been exhausted), or rewrapping a Java exception.
476 */
477 private String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor) throws OsmTransferException {
478 StringBuffer responseBody = new StringBuffer();
479
480 int retries = getMaxRetries();
481
482 while(true) { // the retry loop
483 try {
484 URL url = new URL(new URL(getBaseUrl()), urlSuffix);
485 System.out.print(requestMethod + " " + url + "... ");
486 activeConnection = (HttpURLConnection)url.openConnection();
487 activeConnection.setConnectTimeout(15000);
488 activeConnection.setRequestMethod(requestMethod);
489 addAuth(activeConnection);
490
491 if (requestMethod.equals("PUT") || requestMethod.equals("POST") || requestMethod.equals("DELETE")) {
492 activeConnection.setDoOutput(true);
493 activeConnection.setRequestProperty("Content-type", "text/xml");
494 OutputStream out = activeConnection.getOutputStream();
495
496 // It seems that certain bits of the Ruby API are very unhappy upon
497 // receipt of a PUT/POST message withtout a Content-length header,
498 // even if the request has no payload.
499 // Since Java will not generate a Content-length header unless
500 // we use the output stream, we create an output stream for PUT/POST
501 // even if there is no payload.
502 if (requestBody != null) {
503 BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
504 bwr.write(requestBody);
505 bwr.flush();
506 }
507 out.close();
508 }
509
510 activeConnection.connect();
511 System.out.println(activeConnection.getResponseMessage());
512 int retCode = activeConnection.getResponseCode();
513
514 if (retCode >= 500) {
515 if (retries-- > 0) {
516 sleepAndListen(retries, monitor);
517 System.out.println(tr("Starting retry {0} of {1}.", getMaxRetries() - retries,getMaxRetries()));
518 continue;
519 }
520 }
521
522 // populate return fields.
523 responseBody.setLength(0);
524
525 // If the API returned an error code like 403 forbidden, getInputStream
526 // will fail with an IOException.
527 InputStream i = null;
528 try {
529 i = activeConnection.getInputStream();
530 } catch (IOException ioe) {
531 i = activeConnection.getErrorStream();
532 }
533 BufferedReader in = new BufferedReader(new InputStreamReader(i));
534
535 String s;
536 while((s = in.readLine()) != null) {
537 responseBody.append(s);
538 responseBody.append("\n");
539 }
540 String errorHeader = null;
541 // Look for a detailed error message from the server
542 if (activeConnection.getHeaderField("Error") != null) {
543 errorHeader = activeConnection.getHeaderField("Error");
544 System.err.println("Error header: " + errorHeader);
545 } else if (retCode != 200 && responseBody.length()>0) {
546 System.err.println("Error body: " + responseBody);
547 }
548 activeConnection.disconnect();
549
550 if (retCode != 200)
551 throw new OsmApiException(
552 retCode,
553 errorHeader == null? null : errorHeader.trim(),
554 responseBody == null ? null : responseBody.toString().trim()
555 );
556
557 return responseBody.toString();
558 } catch (UnknownHostException e) {
559 throw new OsmTransferException(e);
560 } catch (SocketTimeoutException e) {
561 if (retries-- > 0) {
562 continue;
563 }
564 throw new OsmTransferException(e);
565 } catch (ConnectException e) {
566 if (retries-- > 0) {
567 continue;
568 }
569 throw new OsmTransferException(e);
570 } catch(OsmTransferException e) {
571 throw e;
572 } catch (Exception e) {
573 throw new OsmTransferException(e);
574 }
575 }
576 }
577
578 /**
579 * returns the API capabilities; null, if the API is not initialized yet
580 *
581 * @return the API capabilities
582 */
583 public Capabilities getCapabilities() {
584 return capabilities;
585 }
586
587
588 /**
589 * Ensures that the current changeset can be used for uploading data
590 *
591 * @throws OsmTransferException thrown if the current changeset can't be used for
592 * uploading data
593 */
594 protected void ensureValidChangeset() throws OsmTransferException {
595 if (changeset == null)
596 throw new OsmTransferException(tr("Current changeset is null. Can't upload data."));
597 if (changeset.getId() <= 0)
598 throw new OsmTransferException(tr("ID of current changeset > 0 required. Current ID is {0}.", changeset.getId()));
599 }
600 /**
601 * Replies the changeset data uploads are currently directed to
602 *
603 * @return the changeset data uploads are currently directed to
604 */
605 public Changeset getChangeset() {
606 return changeset;
607 }
608
609 /**
610 * Sets the changesets to which further data uploads are directed. The changeset
611 * can be null. If it isn't null it must have been created, i.e. id > 0 is required. Furthermore,
612 * it must be open.
613 *
614 * @param changeset the changeset
615 * @throws IllegalArgumentException thrown if changeset.getId() <= 0
616 * @throws IllegalArgumentException thrown if !changeset.isOpen()
617 */
618 public void setChangeset(Changeset changeset) {
619 if (changeset == null) {
620 this.changeset = null;
621 return;
622 }
623 if (changeset.getId() <= 0)
624 throw new IllegalArgumentException(tr("Changeset ID > 0 expected. Got {0}.", changeset.getId()));
625 if (!changeset.isOpen())
626 throw new IllegalArgumentException(tr("Open changeset expected. Got closed changeset with id {0}.", changeset.getId()));
627 this.changeset = changeset;
628 }
629
630}
Note: See TracBrowser for help on using the repository browser.