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

Last change on this file since 4067 was 3993, checked in by framm, 13 years ago

use short timeout for initial API connection, related to #6037

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