[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[18] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
[916] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[103] | 6 | import java.io.InputStream;
|
---|
[18] | 7 | import java.net.HttpURLConnection;
|
---|
[1670] | 8 | import java.net.MalformedURLException;
|
---|
[18] | 9 | import java.net.URL;
|
---|
[6803] | 10 | import java.util.List;
|
---|
[18] | 11 |
|
---|
[34] | 12 | import org.openstreetmap.josm.Main;
|
---|
[4521] | 13 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
[7531] | 14 | import org.openstreetmap.josm.data.notes.Note;
|
---|
[1146] | 15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[1811] | 16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[9172] | 17 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
[18] | 18 |
|
---|
| 19 | /**
|
---|
[228] | 20 | * This DataReader reads directly from the REST API of the osm server.
|
---|
[1169] | 21 | *
|
---|
[228] | 22 | * It supports plain text transfer as well as gzip or deflate encoded transfers;
|
---|
| 23 | * if compressed transfers are unwanted, set property osm-server.use-compression
|
---|
| 24 | * to false.
|
---|
[153] | 25 | *
|
---|
[18] | 26 | * @author imi
|
---|
| 27 | */
|
---|
[1146] | 28 | public abstract class OsmServerReader extends OsmConnection {
|
---|
[9078] | 29 | private final OsmApi api = OsmApi.getOsmApi();
|
---|
[8840] | 30 | private boolean doAuthenticate;
|
---|
[5494] | 31 | protected boolean gpxParsedProperly;
|
---|
[1664] | 32 |
|
---|
[1169] | 33 | /**
|
---|
| 34 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 35 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
[5863] | 36 | * Relative URL's are directed to API base URL.
|
---|
| 37 | * @param urlStr The url to connect to.
|
---|
| 38 | * @param progressMonitor progress monitoring and abort handler
|
---|
[6695] | 39 | * @return A reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 40 | * @throws OsmTransferException if data transfer errors occur
|
---|
[1169] | 41 | */
|
---|
[1811] | 42 | protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[6695] | 43 | return getInputStream(urlStr, progressMonitor, null);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 48 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 49 | * Relative URL's are directed to API base URL.
|
---|
| 50 | * @param urlStr The url to connect to.
|
---|
| 51 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 52 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
| 53 | * @return A reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 54 | * @throws OsmTransferException if data transfer errors occur
|
---|
[6695] | 55 | */
|
---|
| 56 | protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
|
---|
[1875] | 57 | try {
|
---|
[2035] | 58 | api.initialize(progressMonitor);
|
---|
[6695] | 59 | String url = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr);
|
---|
| 60 | return getInputStreamRaw(url, progressMonitor, reason);
|
---|
[1875] | 61 | } finally {
|
---|
| 62 | progressMonitor.invalidate();
|
---|
| 63 | }
|
---|
[1670] | 64 | }
|
---|
[1664] | 65 |
|
---|
[5863] | 66 | /**
|
---|
[6695] | 67 | * Return the base URL for relative URL requests
|
---|
[5863] | 68 | * @return base url of API
|
---|
| 69 | */
|
---|
[5097] | 70 | protected String getBaseUrl() {
|
---|
| 71 | return api.getBaseUrl();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[5863] | 74 | /**
|
---|
| 75 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 76 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 77 | * @param urlStr The exact url to connect to.
|
---|
| 78 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 79 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 80 | * @throws OsmTransferException if data transfer errors occur
|
---|
[5863] | 81 | */
|
---|
[1811] | 82 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[6695] | 83 | return getInputStreamRaw(urlStr, progressMonitor, null);
|
---|
| 84 | }
|
---|
[6787] | 85 |
|
---|
[6695] | 86 | /**
|
---|
| 87 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 88 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 89 | * @param urlStr The exact url to connect to.
|
---|
| 90 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 91 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
| 92 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 93 | * @throws OsmTransferException if data transfer errors occur
|
---|
[6695] | 94 | */
|
---|
| 95 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
|
---|
[6803] | 96 | return getInputStreamRaw(urlStr, progressMonitor, reason, false);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 101 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 102 | * @param urlStr The exact url to connect to.
|
---|
| 103 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 104 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
| 105 | * @param uncompressAccordingToContentDisposition Whether to inspect the HTTP header {@code Content-Disposition}
|
---|
| 106 | * for {@code filename} and uncompress a gzip/bzip2 stream.
|
---|
| 107 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 108 | * @throws OsmTransferException if data transfer errors occur
|
---|
[6803] | 109 | */
|
---|
[7033] | 110 | @SuppressWarnings("resource")
|
---|
[8509] | 111 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
|
---|
| 112 | boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
|
---|
[1608] | 113 | try {
|
---|
[7434] | 114 | OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlStr, Main.getJOSMWebsite());
|
---|
| 115 | OnlineResource.OSM_API.checkOfflineAccess(urlStr, Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL));
|
---|
| 116 |
|
---|
[1875] | 117 | URL url = null;
|
---|
| 118 | try {
|
---|
[3021] | 119 | url = new URL(urlStr.replace(" ", "%20"));
|
---|
[8510] | 120 | } catch (MalformedURLException e) {
|
---|
[1875] | 121 | throw new OsmTransferException(e);
|
---|
| 122 | }
|
---|
[228] | 123 |
|
---|
[9172] | 124 | final HttpClient client = HttpClient.create(url);
|
---|
| 125 | client.setReasonForRequest(reason);
|
---|
[2641] | 126 | if (doAuthenticate) {
|
---|
[9172] | 127 | addAuth(client);
|
---|
[2124] | 128 | }
|
---|
[2641] | 129 | if (cancel)
|
---|
[8415] | 130 | throw new OsmTransferCanceledException("Operation canceled");
|
---|
[1169] | 131 |
|
---|
[1875] | 132 | try {
|
---|
[9172] | 133 | activeConnection = client.connect();
|
---|
[1875] | 134 | } catch (Exception e) {
|
---|
[6643] | 135 | Main.error(e);
|
---|
[8509] | 136 | OsmTransferException ote = new OsmTransferException(
|
---|
| 137 | tr("Could not connect to the OSM server. Please check your internet connection."), e);
|
---|
[5575] | 138 | ote.setUrl(url.toString());
|
---|
| 139 | throw ote;
|
---|
[1875] | 140 | }
|
---|
| 141 | try {
|
---|
[2641] | 142 | if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
|
---|
[8510] | 143 | throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);
|
---|
[1353] | 144 |
|
---|
[2641] | 145 | if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
|
---|
[8415] | 146 | throw new OsmTransferCanceledException("Proxy Authentication Required");
|
---|
[2641] | 147 |
|
---|
[1875] | 148 | if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
---|
| 149 | String errorHeader = activeConnection.getHeaderField("Error");
|
---|
[9184] | 150 | String errorBody;
|
---|
| 151 | try {
|
---|
| 152 | errorBody = activeConnection.fetchContent();
|
---|
| 153 | } catch (Exception e) {
|
---|
| 154 | errorBody = tr("Reading error text failed.");
|
---|
| 155 | }
|
---|
[9172] | 156 | throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody, url.toString());
|
---|
[1670] | 157 | }
|
---|
[1169] | 158 |
|
---|
[9174] | 159 | activeConnection.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition);
|
---|
[6803] | 160 | InputStream in = new ProgressInputStream(activeConnection, progressMonitor);
|
---|
[9172] | 161 | return in;
|
---|
[6246] | 162 | } catch (OsmTransferException e) {
|
---|
| 163 | throw e;
|
---|
| 164 | } catch (Exception e) {
|
---|
| 165 | throw new OsmTransferException(e);
|
---|
[1670] | 166 | }
|
---|
[1875] | 167 | } finally {
|
---|
| 168 | progressMonitor.invalidate();
|
---|
[1169] | 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[6244] | 172 | /**
|
---|
| 173 | * Download OSM files from somewhere
|
---|
| 174 | * @param progressMonitor The progress monitor
|
---|
| 175 | * @return The corresponding dataset
|
---|
[6643] | 176 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 177 | */
|
---|
[5317] | 178 | public abstract DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException;
|
---|
[1169] | 179 |
|
---|
[6244] | 180 | /**
|
---|
| 181 | * Download OSM Change files from somewhere
|
---|
| 182 | * @param progressMonitor The progress monitor
|
---|
| 183 | * @return The corresponding dataset
|
---|
[6643] | 184 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 185 | */
|
---|
[5317] | 186 | public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[4530] | 187 | return null;
|
---|
| 188 | }
|
---|
[5361] | 189 |
|
---|
[6244] | 190 | /**
|
---|
| 191 | * Download BZip2-compressed OSM Change files from somewhere
|
---|
| 192 | * @param progressMonitor The progress monitor
|
---|
| 193 | * @return The corresponding dataset
|
---|
[6643] | 194 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 195 | */
|
---|
[5361] | 196 | public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 197 | return null;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[6244] | 200 | /**
|
---|
| 201 | * Download GZip-compressed OSM Change files from somewhere
|
---|
| 202 | * @param progressMonitor The progress monitor
|
---|
| 203 | * @return The corresponding dataset
|
---|
[6643] | 204 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 205 | */
|
---|
[5361] | 206 | public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 207 | return null;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[6244] | 210 | /**
|
---|
| 211 | * Retrieve raw gps waypoints from the server API.
|
---|
| 212 | * @param progressMonitor The progress monitor
|
---|
| 213 | * @return The corresponding GPX tracks
|
---|
| 214 | * @throws OsmTransferException if any error occurs
|
---|
| 215 | */
|
---|
[5317] | 216 | public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[4521] | 217 | return null;
|
---|
| 218 | }
|
---|
[5317] | 219 |
|
---|
[6244] | 220 | /**
|
---|
| 221 | * Retrieve BZip2-compressed GPX files from somewhere.
|
---|
| 222 | * @param progressMonitor The progress monitor
|
---|
| 223 | * @return The corresponding GPX tracks
|
---|
| 224 | * @throws OsmTransferException if any error occurs
|
---|
| 225 | * @since 6244
|
---|
| 226 | */
|
---|
| 227 | public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 228 | return null;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * Download BZip2-compressed OSM files from somewhere
|
---|
| 233 | * @param progressMonitor The progress monitor
|
---|
| 234 | * @return The corresponding dataset
|
---|
[6643] | 235 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 236 | */
|
---|
[5317] | 237 | public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 238 | return null;
|
---|
| 239 | }
|
---|
[5361] | 240 |
|
---|
[6244] | 241 | /**
|
---|
| 242 | * Download GZip-compressed OSM files from somewhere
|
---|
| 243 | * @param progressMonitor The progress monitor
|
---|
| 244 | * @return The corresponding dataset
|
---|
[6643] | 245 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 246 | */
|
---|
[5361] | 247 | public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 248 | return null;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[2124] | 251 | /**
|
---|
[6882] | 252 | * Download Zip-compressed OSM files from somewhere
|
---|
| 253 | * @param progressMonitor The progress monitor
|
---|
| 254 | * @return The corresponding dataset
|
---|
| 255 | * @throws OsmTransferException if any error occurs
|
---|
| 256 | * @since 6882
|
---|
| 257 | */
|
---|
| 258 | public DataSet parseOsmZip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 259 | return null;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /**
|
---|
[2124] | 263 | * Returns true if this reader is adding authentication credentials to the read
|
---|
| 264 | * request sent to the server.
|
---|
[2512] | 265 | *
|
---|
[2124] | 266 | * @return true if this reader is adding authentication credentials to the read
|
---|
| 267 | * request sent to the server
|
---|
| 268 | */
|
---|
| 269 | public boolean isDoAuthenticate() {
|
---|
| 270 | return doAuthenticate;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /**
|
---|
| 274 | * Sets whether this reader adds authentication credentials to the read
|
---|
| 275 | * request sent to the server.
|
---|
[2512] | 276 | *
|
---|
[2124] | 277 | * @param doAuthenticate true if this reader adds authentication credentials to the read
|
---|
| 278 | * request sent to the server
|
---|
| 279 | */
|
---|
| 280 | public void setDoAuthenticate(boolean doAuthenticate) {
|
---|
| 281 | this.doAuthenticate = doAuthenticate;
|
---|
| 282 | }
|
---|
[6070] | 283 |
|
---|
[5494] | 284 | /**
|
---|
| 285 | * Determines if the GPX data has been parsed properly.
|
---|
| 286 | * @return true if the GPX data has been parsed properly, false otherwise
|
---|
| 287 | * @see GpxReader#parse
|
---|
| 288 | */
|
---|
| 289 | public final boolean isGpxParsedProperly() {
|
---|
| 290 | return gpxParsedProperly;
|
---|
| 291 | }
|
---|
[7531] | 292 |
|
---|
| 293 | /**
|
---|
| 294 | * Downloads notes from the API, given API limit parameters
|
---|
| 295 | *
|
---|
[8218] | 296 | * @param noteLimit How many notes to download.
|
---|
[7531] | 297 | * @param daysClosed Return notes closed this many days in the past. -1 means all notes, ever. 0 means only unresolved notes.
|
---|
| 298 | * @param progressMonitor Progress monitor for user feedback
|
---|
| 299 | * @return List of notes returned by the API
|
---|
| 300 | * @throws OsmTransferException if any errors happen
|
---|
| 301 | */
|
---|
[8218] | 302 | public List<Note> parseNotes(int noteLimit, int daysClosed, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[7531] | 303 | return null;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | /**
|
---|
| 307 | * Downloads notes from a given raw URL. The URL is assumed to be complete and no API limits are added
|
---|
| 308 | *
|
---|
[8470] | 309 | * @param progressMonitor progress monitor
|
---|
[7531] | 310 | * @return A list of notes parsed from the URL
|
---|
[8470] | 311 | * @throws OsmTransferException if any error occurs during dialog with OSM API
|
---|
[7531] | 312 | */
|
---|
| 313 | public List<Note> parseRawNotes(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 314 | return null;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /**
|
---|
| 318 | * Download notes from a URL that contains a bzip2 compressed notes dump file
|
---|
[8470] | 319 | * @param progressMonitor progress monitor
|
---|
[7531] | 320 | * @return A list of notes parsed from the URL
|
---|
[8470] | 321 | * @throws OsmTransferException if any error occurs during dialog with OSM API
|
---|
[7531] | 322 | */
|
---|
| 323 | public List<Note> parseRawNotesBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 324 | return null;
|
---|
| 325 | }
|
---|
[18] | 326 | }
|
---|