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