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

Last change on this file since 3747 was 3566, checked in by stoecker, 14 years ago

see #5404 - hopefully work around bugs in proxy software

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