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

Revision 5084, 30.8 KB checked in by simon04, 2 months ago (diff)

hopefully fix #4216 - API calls blocking, cancel not working, JOSM hangs

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