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

Last change on this file since 1894 was 1894, checked in by Gubaer, 15 years ago

fixed #3158: modify status is not removed for partial uploads

File size: 19.5 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;
25import java.util.Properties;
26
27import javax.xml.parsers.SAXParserFactory;
28
29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.data.osm.Changeset;
31import org.openstreetmap.josm.data.osm.OsmPrimitive;
32import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
33import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor;
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 changesets.
148 * @return true if the negotiated version supports changesets.
149 */
150 public boolean hasChangesetSupport() {
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() throws OsmApiInitializationException {
160 if (initialized)
161 return;
162 initAuthentication();
163 try {
164 String s = sendRequest("GET", "capabilities", null);
165 InputSource inputSource = new InputSource(new StringReader(s));
166 SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new CapabilitiesParser());
167 if (capabilities.supportsVersion("0.6")) {
168 version = "0.6";
169 } else if (capabilities.supportsVersion("0.5")) {
170 version = "0.5";
171 } else {
172 System.err.println(tr("This version of JOSM is incompatible with the configured server."));
173 System.err.println(tr("It supports protocol versions 0.5 and 0.6, while the server says it supports {0} to {1}.",
174 capabilities.get("version", "minimum"), capabilities.get("version", "maximum")));
175 initialized = false;
176 }
177 System.out.println(tr("Communications with {0} established using protocol version {1}",
178 serverUrl,
179 version));
180 osmWriter.setVersion(version);
181 initialized = true;
182 } catch (Exception ex) {
183 initialized = false;
184 throw new OsmApiInitializationException(ex);
185 }
186 }
187
188 /**
189 * Makes an XML string from an OSM primitive. Uses the OsmWriter class.
190 * @param o the OSM primitive
191 * @param addBody true to generate the full XML, false to only generate the encapsulating tag
192 * @return XML string
193 */
194 private String toXml(OsmPrimitive o, boolean addBody) {
195 swriter.getBuffer().setLength(0);
196 osmWriter.setWithBody(addBody);
197 osmWriter.setChangeset(changeset);
198 osmWriter.header();
199 o.visit(osmWriter);
200 osmWriter.footer();
201 osmWriter.out.flush();
202 return swriter.toString();
203 }
204
205 /**
206 * Returns the base URL for API requests, including the negotiated version number.
207 * @return base URL string
208 */
209 public String getBaseUrl() {
210 StringBuffer rv = new StringBuffer(serverUrl);
211 if (version != null) {
212 rv.append("/");
213 rv.append(version);
214 }
215 rv.append("/");
216 // this works around a ruby (or lighttpd) bug where two consecutive slashes in
217 // an URL will cause a "404 not found" response.
218 int p; while ((p = rv.indexOf("//", 6)) > -1) { rv.delete(p, p + 1); }
219 return rv.toString();
220 }
221
222 /**
223 * Creates an OSM primitive on the server. The OsmPrimitive object passed in
224 * is modified by giving it the server-assigned id.
225 *
226 * @param osm the primitive
227 * @throws OsmTransferException if something goes wrong
228 */
229 public void createPrimitive(OsmPrimitive osm) throws OsmTransferException {
230 initialize();
231 String ret = "";
232 try {
233 ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/create", toXml(osm, true));
234 osm.id = Long.parseLong(ret.trim());
235 osm.version = 1;
236 } catch(NumberFormatException e){
237 throw new OsmTransferException(tr("unexpected format of id replied by the server, got ''{0}''", ret));
238 }
239 }
240
241 /**
242 * Modifies an OSM primitive on the server. For protocols greater than 0.5,
243 * the OsmPrimitive object passed in is modified by giving it the server-assigned
244 * version.
245 *
246 * @param osm the primitive
247 * @throws OsmTransferException if something goes wrong
248 */
249 public void modifyPrimitive(OsmPrimitive osm) throws OsmTransferException {
250 initialize();
251 if (version.equals("0.5")) {
252 // legacy mode does not return the new object version.
253 sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.id, toXml(osm, true));
254 } else {
255 String ret = null;
256 // normal mode (0.6 and up) returns new object version.
257 try {
258 ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.id, toXml(osm, true));
259 osm.version = Integer.parseInt(ret.trim());
260 } catch(NumberFormatException e) {
261 throw new OsmTransferException(tr("unexpected format of new version of modified primitive ''{0}'', got ''{1}''", osm.id, ret));
262 }
263 }
264 }
265
266 /**
267 * Deletes an OSM primitive on the server.
268 * @param osm the primitive
269 * @throws OsmTransferException if something goes wrong
270 */
271 public void deletePrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
272 initialize();
273 // can't use a the individual DELETE method in the 0.6 API. Java doesn't allow
274 // submitting a DELETE request with content, the 0.6 API requires it, however. Falling back
275 // to diff upload.
276 //
277 uploadDiff(Collections.singleton(osm), monitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
278 }
279
280 /**
281 * Creates a new changeset on the server to use for subsequent calls.
282 * @param comment the "commit comment" for the new changeset
283 * @throws OsmTransferException signifying a non-200 return code, or connection errors
284 */
285 public void createChangeset(String comment, ProgressMonitor progressMonitor) throws OsmTransferException {
286 progressMonitor.beginTask((tr("Opening changeset...")));
287 try {
288 changeset = new Changeset();
289 Properties sysProp = System.getProperties();
290 Object ua = sysProp.get("http.agent");
291 changeset.put("created_by", (ua == null) ? "JOSM" : ua.toString());
292 changeset.put("comment", comment);
293 createPrimitive(changeset);
294 } finally {
295 progressMonitor.finishTask();
296 }
297 }
298
299 /**
300 * Closes a changeset on the server.
301 *
302 * @throws OsmTransferException if something goes wrong.
303 */
304 public void stopChangeset(ProgressMonitor progressMonitor) throws OsmTransferException {
305 progressMonitor.beginTask(tr("Closing changeset..."));
306 try {
307 initialize();
308 sendRequest("PUT", "changeset" + "/" + changeset.id + "/close", null);
309 changeset = null;
310 } finally {
311 progressMonitor.finishTask();
312 }
313 }
314
315 /**
316 * Uploads a list of changes in "diff" form to the server.
317 *
318 * @param list the list of changed OSM Primitives
319 * @return list of processed primitives
320 * @throws OsmTransferException if something is wrong
321 */
322 public Collection<OsmPrimitive> uploadDiff(final Collection<OsmPrimitive> list, ProgressMonitor progressMonitor) throws OsmTransferException {
323
324 progressMonitor.beginTask("", list.size() * 2);
325 try {
326 if (changeset == null)
327 throw new OsmTransferException(tr("No changeset present for diff upload"));
328
329 initialize();
330 final ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>();
331
332 CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this);
333
334 progressMonitor.subTask(tr("Preparing..."));
335 for (OsmPrimitive osm : list) {
336 osm.visit(duv);
337 progressMonitor.worked(1);
338 }
339 progressMonitor.indeterminateSubTask(tr("Uploading..."));
340
341 String diff = duv.getDocument();
342 try {
343 String diffresult = sendRequest("POST", "changeset/" + changeset.id + "/upload", diff);
344 DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(),
345 progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
346 } catch(OsmTransferException e) {
347 throw e;
348 } catch(Exception e) {
349 throw new OsmTransferException(e);
350 }
351
352 return processed;
353 } finally {
354 progressMonitor.finishTask();
355 }
356 }
357
358
359
360 private void sleepAndListen() throws OsmTransferCancelledException {
361 System.out.print(tr("Waiting 10 seconds ... "));
362 for(int i=0; i < 10; i++) {
363 if (cancel || isAuthCancelled())
364 throw new OsmTransferCancelledException();
365 try {
366 Thread.sleep(1000);
367 } catch (InterruptedException ex) {}
368 }
369 System.out.println(tr("OK - trying again."));
370 }
371
372 /**
373 * Replies the max. number of retries in case of 5XX errors on the server
374 *
375 * @return the max number of retries
376 */
377 protected int getMaxRetries() {
378 int ret = Main.pref.getInteger("osm-server.max-num-retries", DEFAULT_MAX_NUM_RETRIES);
379 return Math.max(ret,0);
380 }
381
382 /**
383 * Generic method for sending requests to the OSM API.
384 *
385 * This method will automatically re-try any requests that are answered with a 5xx
386 * error code, or that resulted in a timeout exception from the TCP layer.
387 *
388 * @param requestMethod The http method used when talking with the server.
389 * @param urlSuffix The suffix to add at the server url, not including the version number,
390 * but including any object ids (e.g. "/way/1234/history").
391 * @param requestBody the body of the HTTP request, if any.
392 *
393 * @return the body of the HTTP response, if and only if the response code was "200 OK".
394 * @exception OsmTransferException if the HTTP return code was not 200 (and retries have
395 * been exhausted), or rewrapping a Java exception.
396 */
397 private String sendRequest(String requestMethod, String urlSuffix,
398 String requestBody) throws OsmTransferException {
399
400 StringBuffer responseBody = new StringBuffer();
401
402 int retries = getMaxRetries();
403
404 while(true) { // the retry loop
405 try {
406 URL url = new URL(new URL(getBaseUrl()), urlSuffix);
407 System.out.print(requestMethod + " " + url + "... ");
408 activeConnection = (HttpURLConnection)url.openConnection();
409 activeConnection.setConnectTimeout(15000);
410 activeConnection.setRequestMethod(requestMethod);
411 addAuth(activeConnection);
412
413 if (requestMethod.equals("PUT") || requestMethod.equals("POST") || requestMethod.equals("DELETE")) {
414 activeConnection.setDoOutput(true);
415 activeConnection.setRequestProperty("Content-type", "text/xml");
416 OutputStream out = activeConnection.getOutputStream();
417
418 // It seems that certain bits of the Ruby API are very unhappy upon
419 // receipt of a PUT/POST message withtout a Content-length header,
420 // even if the request has no payload.
421 // Since Java will not generate a Content-length header unless
422 // we use the output stream, we create an output stream for PUT/POST
423 // even if there is no payload.
424 if (requestBody != null) {
425 BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
426 bwr.write(requestBody);
427 bwr.flush();
428 }
429 out.close();
430 }
431
432 activeConnection.connect();
433 System.out.println(activeConnection.getResponseMessage());
434 int retCode = activeConnection.getResponseCode();
435
436 if (retCode >= 500) {
437 if (retries-- > 0) {
438 sleepAndListen();
439 int maxRetries = getMaxRetries();
440 System.out.println(tr("Starting retry {0} of {1}.", maxRetries - retries,maxRetries));
441 continue;
442 }
443 }
444
445 // populate return fields.
446 responseBody.setLength(0);
447
448 // If the API returned an error code like 403 forbidden, getInputStream
449 // will fail with an IOException.
450 InputStream i = null;
451 try {
452 i = activeConnection.getInputStream();
453 } catch (IOException ioe) {
454 i = activeConnection.getErrorStream();
455 }
456 BufferedReader in = new BufferedReader(new InputStreamReader(i));
457
458 String s;
459 while((s = in.readLine()) != null) {
460 responseBody.append(s);
461 responseBody.append("\n");
462 }
463 String errorHeader = null;
464 // Look for a detailed error message from the server
465 if (activeConnection.getHeaderField("Error") != null) {
466 errorHeader = activeConnection.getHeaderField("Error");
467 System.err.println("Error header: " + errorHeader);
468 } else if (retCode != 200 && responseBody.length()>0) {
469 System.err.println("Error body: " + responseBody);
470 }
471 activeConnection.disconnect();
472
473 if (retCode != 200)
474 throw new OsmApiException(
475 retCode,
476 errorHeader == null? null : errorHeader.trim(),
477 responseBody == null ? null : responseBody.toString().trim()
478 );
479
480 return responseBody.toString();
481 } catch (UnknownHostException e) {
482 throw new OsmTransferException(e);
483 } catch (SocketTimeoutException e) {
484 if (retries-- > 0) {
485 continue;
486 }
487 throw new OsmTransferException(e);
488 } catch (ConnectException e) {
489 if (retries-- > 0) {
490 continue;
491 }
492 throw new OsmTransferException(e);
493 } catch (Exception e) {
494 if (e instanceof OsmTransferException) throw (OsmTransferException) e;
495 throw new OsmTransferException(e);
496 }
497 }
498 }
499
500 /**
501 * returns the API capabilities; null, if the API is not initialized yet
502 *
503 * @return the API capabilities
504 */
505 public Capabilities getCapabilities() {
506 return capabilities;
507 }
508
509 /**
510 * Replies the current changeset
511 *
512 * @return the current changeset
513 */
514 public Changeset getCurrentChangeset() {
515 return changeset;
516 }
517}
Note: See TracBrowser for help on using the repository browser.