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

Last change on this file since 2734 was 2734, checked in by framm, 14 years ago

message fixes

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