[1523] | 1 | //License: GPL. See README for details.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[2852] | 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
[1523] | 6 |
|
---|
| 7 | import java.io.BufferedReader;
|
---|
| 8 | import java.io.BufferedWriter;
|
---|
[1559] | 9 | import java.io.IOException;
|
---|
| 10 | import java.io.InputStream;
|
---|
[1523] | 11 | import java.io.InputStreamReader;
|
---|
| 12 | import java.io.OutputStream;
|
---|
| 13 | import java.io.OutputStreamWriter;
|
---|
| 14 | import java.io.PrintWriter;
|
---|
| 15 | import java.io.StringReader;
|
---|
[1525] | 16 | import java.io.StringWriter;
|
---|
[1523] | 17 | import java.net.ConnectException;
|
---|
| 18 | import java.net.HttpURLConnection;
|
---|
| 19 | import java.net.SocketTimeoutException;
|
---|
| 20 | import java.net.URL;
|
---|
| 21 | import java.net.UnknownHostException;
|
---|
| 22 | import java.util.Collection;
|
---|
[1879] | 23 | import java.util.Collections;
|
---|
[1664] | 24 | import java.util.HashMap;
|
---|
[2825] | 25 | import java.util.logging.Logger;
|
---|
[1523] | 26 |
|
---|
[2641] | 27 | import javax.xml.parsers.ParserConfigurationException;
|
---|
[1523] | 28 | import javax.xml.parsers.SAXParserFactory;
|
---|
| 29 |
|
---|
| 30 | import org.openstreetmap.josm.Main;
|
---|
| 31 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
[4100] | 32 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
[1670] | 33 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[3934] | 34 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 35 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
[2115] | 36 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
[1811] | 37 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[2852] | 38 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[1523] | 39 | import org.xml.sax.Attributes;
|
---|
| 40 | import org.xml.sax.InputSource;
|
---|
| 41 | import org.xml.sax.SAXException;
|
---|
| 42 | import org.xml.sax.helpers.DefaultHandler;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
[1581] | 45 | * Class that encapsulates the communications with the OSM API.
|
---|
[1523] | 46 | *
|
---|
[1581] | 47 | * All interaction with the server-side OSM API should go through this class.
|
---|
| 48 | *
|
---|
[1523] | 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.
|
---|
[1581] | 51 | *
|
---|
[1523] | 52 | */
|
---|
| 53 | public class OsmApi extends OsmConnection {
|
---|
[2990] | 54 | @SuppressWarnings("unused")
|
---|
[2825] | 55 | static private final Logger logger = Logger.getLogger(OsmApi.class.getName());
|
---|
[1663] | 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;
|
---|
[1523] | 58 |
|
---|
[1664] | 59 | /** the collection of instantiated OSM APIs */
|
---|
| 60 | private static HashMap<String, OsmApi> instances = new HashMap<String, OsmApi>();
|
---|
| 61 |
|
---|
[1523] | 62 | /**
|
---|
[1664] | 63 | * replies the {@see OsmApi} for a given server URL
|
---|
[1677] | 64 | *
|
---|
[1664] | 65 | * @param serverUrl the server URL
|
---|
| 66 | * @return the OsmApi
|
---|
| 67 | * @throws IllegalArgumentException thrown, if serverUrl is null
|
---|
[1677] | 68 | *
|
---|
[1664] | 69 | */
|
---|
| 70 | static public OsmApi getOsmApi(String serverUrl) {
|
---|
| 71 | OsmApi api = instances.get(serverUrl);
|
---|
| 72 | if (api == null) {
|
---|
| 73 | api = new OsmApi(serverUrl);
|
---|
[1670] | 74 | instances.put(serverUrl,api);
|
---|
[1664] | 75 | }
|
---|
| 76 | return api;
|
---|
| 77 | }
|
---|
| 78 | /**
|
---|
| 79 | * replies the {@see OsmApi} for the URL given by the preference <code>osm-server.url</code>
|
---|
[1677] | 80 | *
|
---|
[1664] | 81 | * @return the OsmApi
|
---|
| 82 | * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set
|
---|
[1677] | 83 | *
|
---|
[1664] | 84 | */
|
---|
| 85 | static public OsmApi getOsmApi() {
|
---|
[1688] | 86 | String serverUrl = Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api");
|
---|
[1664] | 87 | if (serverUrl == null)
|
---|
[2734] | 88 | throw new IllegalStateException(tr("Preference ''{0}'' missing. Cannot initialize OsmApi.", "osm-server.url"));
|
---|
[1664] | 89 | return getOsmApi(serverUrl);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /** the server URL */
|
---|
| 93 | private String serverUrl;
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
[1523] | 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 |
|
---|
[1664] | 105 | /** the api capabilities */
|
---|
| 106 | private Capabilities capabilities = new Capabilities();
|
---|
| 107 |
|
---|
[1523] | 108 | /**
|
---|
| 109 | * true if successfully initialized
|
---|
| 110 | */
|
---|
| 111 | private boolean initialized = false;
|
---|
[1581] | 112 |
|
---|
[1525] | 113 | private StringWriter swriter = new StringWriter();
|
---|
| 114 | private OsmWriter osmWriter = new OsmWriter(new PrintWriter(swriter), true, null);
|
---|
[1523] | 115 |
|
---|
| 116 | /**
|
---|
| 117 | * A parser for the "capabilities" response XML
|
---|
| 118 | */
|
---|
| 119 | private class CapabilitiesParser extends DefaultHandler {
|
---|
[1664] | 120 | @Override
|
---|
| 121 | public void startDocument() throws SAXException {
|
---|
| 122 | capabilities.clear();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[1523] | 125 | @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
[3212] | 126 | for (int i=0; i< atts.getLength(); i++) {
|
---|
[1664] | 127 | capabilities.put(qName, atts.getQName(i), atts.getValue(i));
|
---|
[1663] | 128 | }
|
---|
[1523] | 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
[1664] | 133 | * creates an OSM api for a specific server URL
|
---|
[1677] | 134 | *
|
---|
[1664] | 135 | * @param serverUrl the server URL. Must not be null
|
---|
| 136 | * @exception IllegalArgumentException thrown, if serverUrl is null
|
---|
| 137 | */
|
---|
| 138 | protected OsmApi(String serverUrl) {
|
---|
[2852] | 139 | CheckParameterUtil.ensureParameterNotNull(serverUrl, "serverUrl");
|
---|
[1664] | 140 | this.serverUrl = serverUrl;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
[1523] | 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 | }
|
---|
[1581] | 150 |
|
---|
[3993] | 151 | public void initialize(ProgressMonitor monitor) throws OsmApiInitializationException, OsmTransferCancelledException {
|
---|
| 152 | initialize(monitor, false);
|
---|
| 153 | }
|
---|
[1523] | 154 | /**
|
---|
| 155 | * Initializes this component by negotiating a protocol version with the server.
|
---|
[1670] | 156 | *
|
---|
[3993] | 157 | * @param monitor
|
---|
| 158 | * @param fastFail true to request quick initialisation with a small timeout (more likely to throw exception)
|
---|
[1670] | 159 | * @exception OsmApiInitializationException thrown, if an exception occurs
|
---|
[1523] | 160 | */
|
---|
[3993] | 161 | public void initialize(ProgressMonitor monitor, boolean fastFail) throws OsmApiInitializationException, OsmTransferCancelledException {
|
---|
[1664] | 162 | if (initialized)
|
---|
| 163 | return;
|
---|
[2037] | 164 | cancel = false;
|
---|
[1523] | 165 | try {
|
---|
[3993] | 166 | String s = sendRequest("GET", "capabilities", null, monitor, false, fastFail);
|
---|
[1664] | 167 | InputSource inputSource = new InputSource(new StringReader(s));
|
---|
[1523] | 168 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new CapabilitiesParser());
|
---|
[1664] | 169 | if (capabilities.supportsVersion("0.6")) {
|
---|
[1523] | 170 | version = "0.6";
|
---|
| 171 | } else {
|
---|
| 172 | System.err.println(tr("This version of JOSM is incompatible with the configured server."));
|
---|
[2569] | 173 | System.err.println(tr("It supports protocol version 0.6, while the server says it supports {0} to {1}.",
|
---|
[1664] | 174 | capabilities.get("version", "minimum"), capabilities.get("version", "maximum")));
|
---|
[1523] | 175 | initialized = false;
|
---|
| 176 | }
|
---|
[2181] | 177 | System.out.println(tr("Communications with {0} established using protocol version {1}.",
|
---|
[1664] | 178 | serverUrl,
|
---|
[1663] | 179 | version));
|
---|
[1523] | 180 | osmWriter.setVersion(version);
|
---|
[1664] | 181 | initialized = true;
|
---|
[3934] | 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 |
|
---|
[2641] | 209 | } catch(IOException e) {
|
---|
[1523] | 210 | initialized = false;
|
---|
[2641] | 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);
|
---|
[1523] | 223 | }
|
---|
| 224 | }
|
---|
[1581] | 225 |
|
---|
[1523] | 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 | */
|
---|
[4100] | 232 | private String toXml(IPrimitive o, boolean addBody) {
|
---|
[1525] | 233 | swriter.getBuffer().setLength(0);
|
---|
[1523] | 234 | osmWriter.setWithBody(addBody);
|
---|
[1578] | 235 | osmWriter.setChangeset(changeset);
|
---|
[1523] | 236 | osmWriter.header();
|
---|
| 237 | o.visit(osmWriter);
|
---|
| 238 | osmWriter.footer();
|
---|
| 239 | osmWriter.out.flush();
|
---|
[3366] | 240 | return swriter.toString();
|
---|
[1523] | 241 | }
|
---|
[1581] | 242 |
|
---|
[1523] | 243 | /**
|
---|
[2115] | 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();
|
---|
[4100] | 252 | osmWriter.visit(s);
|
---|
[2115] | 253 | osmWriter.footer();
|
---|
| 254 | osmWriter.out.flush();
|
---|
| 255 | return swriter.toString();
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /**
|
---|
[1523] | 259 | * Returns the base URL for API requests, including the negotiated version number.
|
---|
| 260 | * @return base URL string
|
---|
| 261 | */
|
---|
| 262 | public String getBaseUrl() {
|
---|
[1664] | 263 | StringBuffer rv = new StringBuffer(serverUrl);
|
---|
[1523] | 264 | if (version != null) {
|
---|
| 265 | rv.append("/");
|
---|
| 266 | rv.append(version);
|
---|
| 267 | }
|
---|
| 268 | rv.append("/");
|
---|
[1581] | 269 | // this works around a ruby (or lighttpd) bug where two consecutive slashes in
|
---|
[1523] | 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 |
|
---|
[1581] | 275 | /**
|
---|
[1523] | 276 | * Creates an OSM primitive on the server. The OsmPrimitive object passed in
|
---|
| 277 | * is modified by giving it the server-assigned id.
|
---|
[1581] | 278 | *
|
---|
[1523] | 279 | * @param osm the primitive
|
---|
| 280 | * @throws OsmTransferException if something goes wrong
|
---|
| 281 | */
|
---|
[4100] | 282 | public void createPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[1670] | 283 | String ret = "";
|
---|
| 284 | try {
|
---|
[2115] | 285 | ensureValidChangeset();
|
---|
| 286 | initialize(monitor);
|
---|
[2035] | 287 | ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/create", toXml(osm, true),monitor);
|
---|
[2070] | 288 | osm.setOsmId(Long.parseLong(ret.trim()), 1);
|
---|
[2604] | 289 | osm.setChangesetId(getChangeset().getId());
|
---|
[1670] | 290 | } catch(NumberFormatException e){
|
---|
[2181] | 291 | throw new OsmTransferException(tr("Unexpected format of ID replied by the server. Got ''{0}''.", ret));
|
---|
[1670] | 292 | }
|
---|
[1523] | 293 | }
|
---|
[1581] | 294 |
|
---|
[1523] | 295 | /**
|
---|
[2569] | 296 | * Modifies an OSM primitive on the server.
|
---|
[1581] | 297 | *
|
---|
[2569] | 298 | * @param osm the primitive. Must not be null.
|
---|
| 299 | * @param monitor the progress monitor
|
---|
[1523] | 300 | * @throws OsmTransferException if something goes wrong
|
---|
| 301 | */
|
---|
[4100] | 302 | public void modifyPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2115] | 303 | String ret = null;
|
---|
| 304 | try {
|
---|
| 305 | ensureValidChangeset();
|
---|
| 306 | initialize(monitor);
|
---|
[2569] | 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()));
|
---|
[2604] | 310 | osm.setChangesetId(getChangeset().getId());
|
---|
[3336] | 311 | osm.setVisible(true);
|
---|
[2115] | 312 | } catch(NumberFormatException e) {
|
---|
[2181] | 313 | throw new OsmTransferException(tr("Unexpected format of new version of modified primitive ''{0}''. Got ''{1}''.", osm.getId(), ret));
|
---|
[1523] | 314 | }
|
---|
[1581] | 315 | }
|
---|
| 316 |
|
---|
[1523] | 317 | /**
|
---|
| 318 | * Deletes an OSM primitive on the server.
|
---|
| 319 | * @param osm the primitive
|
---|
| 320 | * @throws OsmTransferException if something goes wrong
|
---|
| 321 | */
|
---|
[4100] | 322 | public void deletePrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2115] | 323 | ensureValidChangeset();
|
---|
[2035] | 324 | initialize(monitor);
|
---|
[1879] | 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));
|
---|
[1581] | 330 | }
|
---|
| 331 |
|
---|
[1523] | 332 | /**
|
---|
[2115] | 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
|
---|
[2512] | 336 | *
|
---|
[2115] | 337 | * The changeset must not be null, but its key/value-pairs may be empty.
|
---|
[2512] | 338 | *
|
---|
[2115] | 339 | * @param changeset the changeset toe be created. Must not be null.
|
---|
[2040] | 340 | * @param progressMonitor the progress monitor
|
---|
[1523] | 341 | * @throws OsmTransferException signifying a non-200 return code, or connection errors
|
---|
[2115] | 342 | * @throws IllegalArgumentException thrown if changeset is null
|
---|
[1523] | 343 | */
|
---|
[2115] | 344 | public void openChangeset(Changeset changeset, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[2852] | 345 | CheckParameterUtil.ensureParameterNotNull(changeset, "changeset");
|
---|
[1811] | 346 | try {
|
---|
[2040] | 347 | progressMonitor.beginTask((tr("Creating changeset...")));
|
---|
[2115] | 348 | initialize(progressMonitor);
|
---|
| 349 | String ret = "";
|
---|
| 350 | try {
|
---|
| 351 | ret = sendRequest("PUT", "changeset/create", toXml(changeset),progressMonitor);
|
---|
[2604] | 352 | changeset.setId(Integer.parseInt(ret.trim()));
|
---|
[2115] | 353 | changeset.setOpen(true);
|
---|
| 354 | } catch(NumberFormatException e){
|
---|
[2181] | 355 | throw new OsmTransferException(tr("Unexpected format of ID replied by the server. Got ''{0}''.", ret));
|
---|
[2115] | 356 | }
|
---|
| 357 | progressMonitor.setCustomText((tr("Successfully opened changeset {0}",changeset.getId())));
|
---|
[1811] | 358 | } finally {
|
---|
| 359 | progressMonitor.finishTask();
|
---|
| 360 | }
|
---|
[1523] | 361 | }
|
---|
| 362 |
|
---|
| 363 | /**
|
---|
[2115] | 364 | * Updates a changeset with the keys in <code>changesetUpdate</code>. The changeset must not
|
---|
| 365 | * be null and id > 0 must be true.
|
---|
[2061] | 366 | *
|
---|
[2115] | 367 | * @param changeset the changeset to update. Must not be null.
|
---|
| 368 | * @param monitor the progress monitor. If null, uses the {@see NullProgressMonitor#INSTANCE}.
|
---|
[2512] | 369 | *
|
---|
[2061] | 370 | * @throws OsmTransferException if something goes wrong.
|
---|
[2115] | 371 | * @throws IllegalArgumentException if changeset is null
|
---|
[2480] | 372 | * @throws IllegalArgumentException if changeset.getId() <= 0
|
---|
[2512] | 373 | *
|
---|
[2061] | 374 | */
|
---|
[2115] | 375 | public void updateChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2852] | 376 | CheckParameterUtil.ensureParameterNotNull(changeset, "changeset");
|
---|
[2115] | 377 | if (monitor == null) {
|
---|
| 378 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 379 | }
|
---|
| 380 | if (changeset.getId() <= 0)
|
---|
[2181] | 381 | throw new IllegalArgumentException(tr("Changeset ID > 0 expected. Got {0}.", changeset.getId()));
|
---|
[2061] | 382 | try {
|
---|
[2115] | 383 | monitor.beginTask(tr("Updating changeset..."));
|
---|
| 384 | initialize(monitor);
|
---|
| 385 | monitor.setCustomText(tr("Updating changeset {0}...", changeset.getId()));
|
---|
| 386 | sendRequest(
|
---|
| 387 | "PUT",
|
---|
[2135] | 388 | "changeset/" + changeset.getId(),
|
---|
| 389 | toXml(changeset),
|
---|
[2115] | 390 | monitor
|
---|
| 391 | );
|
---|
[2599] | 392 | } catch(ChangesetClosedException e) {
|
---|
| 393 | e.setSource(ChangesetClosedException.Source.UPDATE_CHANGESET);
|
---|
| 394 | throw e;
|
---|
[2480] | 395 | } catch(OsmApiException e) {
|
---|
[2569] | 396 | if (e.getResponseCode() == HttpURLConnection.HTTP_CONFLICT && ChangesetClosedException.errorHeaderMatchesPattern(e.getErrorHeader()))
|
---|
| 397 | throw new ChangesetClosedException(e.getErrorHeader(), ChangesetClosedException.Source.UPDATE_CHANGESET);
|
---|
[2480] | 398 | throw e;
|
---|
[2061] | 399 | } finally {
|
---|
[2115] | 400 | monitor.finishTask();
|
---|
[2061] | 401 | }
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | /**
|
---|
[2115] | 405 | * Closes a changeset on the server. Sets changeset.setOpen(false) if this operation
|
---|
| 406 | * succeeds.
|
---|
[1581] | 407 | *
|
---|
[2115] | 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}
|
---|
[2512] | 410 | *
|
---|
[1523] | 411 | * @throws OsmTransferException if something goes wrong.
|
---|
[2115] | 412 | * @throws IllegalArgumentException thrown if changeset is null
|
---|
| 413 | * @throws IllegalArgumentException thrown if changeset.getId() <= 0
|
---|
[1523] | 414 | */
|
---|
[2115] | 415 | public void closeChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2852] | 416 | CheckParameterUtil.ensureParameterNotNull(changeset, "changeset");
|
---|
[2115] | 417 | if (monitor == null) {
|
---|
| 418 | monitor = NullProgressMonitor.INSTANCE;
|
---|
[2040] | 419 | }
|
---|
[2115] | 420 | if (changeset.getId() <= 0)
|
---|
[2181] | 421 | throw new IllegalArgumentException(tr("Changeset ID > 0 expected. Got {0}.", changeset.getId()));
|
---|
[1811] | 422 | try {
|
---|
[2115] | 423 | monitor.beginTask(tr("Closing changeset..."));
|
---|
| 424 | initialize(monitor);
|
---|
[3566] | 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);
|
---|
[2115] | 428 | changeset.setOpen(false);
|
---|
[1811] | 429 | } finally {
|
---|
[2115] | 430 | monitor.finishTask();
|
---|
[1811] | 431 | }
|
---|
[1523] | 432 | }
|
---|
| 433 |
|
---|
| 434 | /**
|
---|
[1581] | 435 | * Uploads a list of changes in "diff" form to the server.
|
---|
| 436 | *
|
---|
[1523] | 437 | * @param list the list of changed OSM Primitives
|
---|
[2198] | 438 | * @param monitor the progress monitor
|
---|
[1523] | 439 | * @return list of processed primitives
|
---|
[1581] | 440 | * @throws OsmTransferException if something is wrong
|
---|
[1523] | 441 | */
|
---|
[4100] | 442 | public Collection<IPrimitive> uploadDiff(Collection<? extends IPrimitive> list, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[1811] | 443 | try {
|
---|
[2198] | 444 | monitor.beginTask("", list.size() * 2);
|
---|
[1811] | 445 | if (changeset == null)
|
---|
[2181] | 446 | throw new OsmTransferException(tr("No changeset present for diff upload."));
|
---|
[1581] | 447 |
|
---|
[2198] | 448 | initialize(monitor);
|
---|
[1581] | 449 |
|
---|
[2604] | 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();
|
---|
[1581] | 458 |
|
---|
[2604] | 459 | // Upload to the server
|
---|
| 460 | //
|
---|
[2852] | 461 | monitor.indeterminateSubTask(
|
---|
| 462 | trn("Uploading {0} object...", "Uploading {0} objects...", list.size(), list.size()));
|
---|
[2604] | 463 | String diffUploadResponse = sendRequest("POST", "changeset/" + changeset.getId() + "/upload", diffUploadRequest,monitor);
|
---|
[1581] | 464 |
|
---|
[2604] | 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 | );
|
---|
[2115] | 473 | } catch(OsmTransferException e) {
|
---|
| 474 | throw e;
|
---|
[2604] | 475 | } catch(OsmDataParsingException e) {
|
---|
[2115] | 476 | throw new OsmTransferException(e);
|
---|
[1690] | 477 | } finally {
|
---|
[2198] | 478 | monitor.finishTask();
|
---|
[1523] | 479 | }
|
---|
| 480 | }
|
---|
| 481 |
|
---|
[2035] | 482 | private void sleepAndListen(int retry, ProgressMonitor monitor) throws OsmTransferCancelledException {
|
---|
[1859] | 483 | System.out.print(tr("Waiting 10 seconds ... "));
|
---|
[1523] | 484 | for(int i=0; i < 10; i++) {
|
---|
[2035] | 485 | if (monitor != null) {
|
---|
| 486 | monitor.setCustomText(tr("Starting retry {0} of {1} in {2} seconds ...", getMaxRetries() - retry,getMaxRetries(), 10-i));
|
---|
| 487 | }
|
---|
[2641] | 488 | if (cancel)
|
---|
[1523] | 489 | throw new OsmTransferCancelledException();
|
---|
| 490 | try {
|
---|
| 491 | Thread.sleep(1000);
|
---|
| 492 | } catch (InterruptedException ex) {}
|
---|
| 493 | }
|
---|
[1859] | 494 | System.out.println(tr("OK - trying again."));
|
---|
[1523] | 495 | }
|
---|
| 496 |
|
---|
| 497 | /**
|
---|
[1859] | 498 | * Replies the max. number of retries in case of 5XX errors on the server
|
---|
[2512] | 499 | *
|
---|
[1859] | 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 |
|
---|
[2748] | 507 | protected boolean isUsingOAuth() {
|
---|
| 508 | String authMethod = Main.pref.get("osm-server.auth-method", "basic");
|
---|
| 509 | return authMethod.equals("oauth");
|
---|
| 510 | }
|
---|
| 511 |
|
---|
[2222] | 512 | private String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[3993] | 513 | return sendRequest(requestMethod, urlSuffix, requestBody, monitor, true, false);
|
---|
[2222] | 514 | }
|
---|
| 515 |
|
---|
[3993] | 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 |
|
---|
[1859] | 520 | /**
|
---|
[1523] | 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.
|
---|
[1581] | 525 | *
|
---|
[1523] | 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.
|
---|
[2478] | 530 | * @param monitor the progress monitor
|
---|
| 531 | * @param doAuthenticate set to true, if the request sent to the server shall include authentication
|
---|
| 532 | * credentials;
|
---|
[3993] | 533 | * @param fastFail true to request a short timeout
|
---|
[1581] | 534 | *
|
---|
[1523] | 535 | * @return the body of the HTTP response, if and only if the response code was "200 OK".
|
---|
[1581] | 536 | * @exception OsmTransferException if the HTTP return code was not 200 (and retries have
|
---|
| 537 | * been exhausted), or rewrapping a Java exception.
|
---|
[1523] | 538 | */
|
---|
[3993] | 539 | private String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor, boolean doAuthenticate, boolean fastFail) throws OsmTransferException {
|
---|
[1523] | 540 | StringBuffer responseBody = new StringBuffer();
|
---|
[1859] | 541 | int retries = getMaxRetries();
|
---|
[1581] | 542 |
|
---|
[1523] | 543 | while(true) { // the retry loop
|
---|
| 544 | try {
|
---|
[1879] | 545 | URL url = new URL(new URL(getBaseUrl()), urlSuffix);
|
---|
[1523] | 546 | System.out.print(requestMethod + " " + url + "... ");
|
---|
| 547 | activeConnection = (HttpURLConnection)url.openConnection();
|
---|
[4172] | 548 | activeConnection.setConnectTimeout(fastFail ? 1000 : Main.pref.getInteger("socket.timeout.connect",15)*1000);
|
---|
[1523] | 549 | activeConnection.setRequestMethod(requestMethod);
|
---|
[2222] | 550 | if (doAuthenticate) {
|
---|
| 551 | addAuth(activeConnection);
|
---|
| 552 | }
|
---|
[1523] | 553 |
|
---|
[1684] | 554 | if (requestMethod.equals("PUT") || requestMethod.equals("POST") || requestMethod.equals("DELETE")) {
|
---|
[1523] | 555 | activeConnection.setDoOutput(true);
|
---|
[1563] | 556 | activeConnection.setRequestProperty("Content-type", "text/xml");
|
---|
[1523] | 557 | OutputStream out = activeConnection.getOutputStream();
|
---|
[1563] | 558 |
|
---|
[1523] | 559 | // It seems that certain bits of the Ruby API are very unhappy upon
|
---|
[2478] | 560 | // receipt of a PUT/POST message without a Content-length header,
|
---|
[1523] | 561 | // even if the request has no payload.
|
---|
| 562 | // Since Java will not generate a Content-length header unless
|
---|
[1563] | 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 | }
|
---|
[1523] | 570 | out.close();
|
---|
| 571 | }
|
---|
[1581] | 572 |
|
---|
[1523] | 573 | activeConnection.connect();
|
---|
| 574 | System.out.println(activeConnection.getResponseMessage());
|
---|
| 575 | int retCode = activeConnection.getResponseCode();
|
---|
[1581] | 576 |
|
---|
[1523] | 577 | if (retCode >= 500) {
|
---|
| 578 | if (retries-- > 0) {
|
---|
[2035] | 579 | sleepAndListen(retries, monitor);
|
---|
| 580 | System.out.println(tr("Starting retry {0} of {1}.", getMaxRetries() - retries,getMaxRetries()));
|
---|
[1523] | 581 | continue;
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | // populate return fields.
|
---|
| 586 | responseBody.setLength(0);
|
---|
[1581] | 587 |
|
---|
[1559] | 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 | }
|
---|
[2478] | 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 | }
|
---|
[1523] | 607 | }
|
---|
[1663] | 608 | String errorHeader = null;
|
---|
[1523] | 609 | // Look for a detailed error message from the server
|
---|
| 610 | if (activeConnection.getHeaderField("Error") != null) {
|
---|
[1663] | 611 | errorHeader = activeConnection.getHeaderField("Error");
|
---|
| 612 | System.err.println("Error header: " + errorHeader);
|
---|
[1559] | 613 | } else if (retCode != 200 && responseBody.length()>0) {
|
---|
| 614 | System.err.println("Error body: " + responseBody);
|
---|
[1523] | 615 | }
|
---|
| 616 | activeConnection.disconnect();
|
---|
[1581] | 617 |
|
---|
[2198] | 618 | errorHeader = errorHeader == null? null : errorHeader.trim();
|
---|
[2478] | 619 | String errorBody = responseBody.length() == 0? null : responseBody.toString().trim();
|
---|
[2198] | 620 | switch(retCode) {
|
---|
[2273] | 621 | case HttpURLConnection.HTTP_OK:
|
---|
[2478] | 622 | return responseBody.toString();
|
---|
[2273] | 623 | case HttpURLConnection.HTTP_GONE:
|
---|
| 624 | throw new OsmApiPrimitiveGoneException(errorHeader, errorBody);
|
---|
[2569] | 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);
|
---|
[2748] | 630 | case HttpURLConnection.HTTP_FORBIDDEN:
|
---|
| 631 | OsmApiException e = new OsmApiException(retCode, errorHeader, errorBody);
|
---|
| 632 | e.setAccessedUrl(activeConnection.getURL().toString());
|
---|
| 633 | throw e;
|
---|
[2273] | 634 | default:
|
---|
| 635 | throw new OsmApiException(retCode, errorHeader, errorBody);
|
---|
[2198] | 636 | }
|
---|
[1523] | 637 | } catch (UnknownHostException e) {
|
---|
[1663] | 638 | throw new OsmTransferException(e);
|
---|
[1523] | 639 | } catch (SocketTimeoutException e) {
|
---|
[1663] | 640 | if (retries-- > 0) {
|
---|
[1523] | 641 | continue;
|
---|
[1663] | 642 | }
|
---|
| 643 | throw new OsmTransferException(e);
|
---|
[1523] | 644 | } catch (ConnectException e) {
|
---|
[1663] | 645 | if (retries-- > 0) {
|
---|
[1523] | 646 | continue;
|
---|
[1663] | 647 | }
|
---|
| 648 | throw new OsmTransferException(e);
|
---|
[2641] | 649 | } catch(IOException e){
|
---|
| 650 | throw new OsmTransferException(e);
|
---|
| 651 | } catch(OsmTransferCancelledException e){
|
---|
| 652 | throw e;
|
---|
[2115] | 653 | } catch(OsmTransferException e) {
|
---|
| 654 | throw e;
|
---|
[1523] | 655 | }
|
---|
| 656 | }
|
---|
| 657 | }
|
---|
[1670] | 658 |
|
---|
| 659 | /**
|
---|
[1811] | 660 | * returns the API capabilities; null, if the API is not initialized yet
|
---|
[1677] | 661 | *
|
---|
[1691] | 662 | * @return the API capabilities
|
---|
| 663 | */
|
---|
| 664 | public Capabilities getCapabilities() {
|
---|
| 665 | return capabilities;
|
---|
| 666 | }
|
---|
[1868] | 667 |
|
---|
[1894] | 668 | /**
|
---|
[2115] | 669 | * Ensures that the current changeset can be used for uploading data
|
---|
[2512] | 670 | *
|
---|
[2115] | 671 | * @throws OsmTransferException thrown if the current changeset can't be used for
|
---|
| 672 | * uploading data
|
---|
[1894] | 673 | */
|
---|
[2115] | 674 | protected void ensureValidChangeset() throws OsmTransferException {
|
---|
| 675 | if (changeset == null)
|
---|
[2734] | 676 | throw new OsmTransferException(tr("Current changeset is null. Cannot upload data."));
|
---|
[2115] | 677 | if (changeset.getId() <= 0)
|
---|
[2181] | 678 | throw new OsmTransferException(tr("ID of current changeset > 0 required. Current ID is {0}.", changeset.getId()));
|
---|
[2115] | 679 | }
|
---|
| 680 | /**
|
---|
| 681 | * Replies the changeset data uploads are currently directed to
|
---|
[2512] | 682 | *
|
---|
[2115] | 683 | * @return the changeset data uploads are currently directed to
|
---|
| 684 | */
|
---|
| 685 | public Changeset getChangeset() {
|
---|
[1894] | 686 | return changeset;
|
---|
| 687 | }
|
---|
[2115] | 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.
|
---|
[2512] | 693 | *
|
---|
[2115] | 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)
|
---|
[2181] | 704 | throw new IllegalArgumentException(tr("Changeset ID > 0 expected. Got {0}.", changeset.getId()));
|
---|
[2115] | 705 | if (!changeset.isOpen())
|
---|
[2181] | 706 | throw new IllegalArgumentException(tr("Open changeset expected. Got closed changeset with id {0}.", changeset.getId()));
|
---|
[2115] | 707 | this.changeset = changeset;
|
---|
| 708 | }
|
---|
[1523] | 709 | }
|
---|