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

Last change on this file since 7448 was 7434, checked in by Don-vip, 10 years ago

fix #8885 (see #4614) - add offline mode with new command line argument --offline which can take one of several of these values (comma separated):

  • josm_website: to disable all accesses to JOSM website (when not cached, disables Getting Started page, help, plugin list, styles, imagery, presets, rules)
  • osm_api: to disable all accesses to OSM API (disables download, upload, changeset queries, history, user message notification)
  • all: alias to disable all values. Currently equivalent to "josm_website,osm_api"

Plus improved javadoc, fixed EDT violations, and fixed a bug with HTTP redirection sent without "Location" header

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