source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerReader.java@ 16426

Last change on this file since 16426 was 16426, checked in by simon04, 4 years ago

see #18712 - Add NetworkManager.isOffline(String) to test offline status of given URL

Deprecates OnlineResource.checkOfflineAccess(String, String)

  • Property svn:eol-style set to native
File size: 17.8 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[18]2package org.openstreetmap.josm.io;
3
[916]4import static org.openstreetmap.josm.tools.I18n.tr;
5
[9732]6import java.io.IOException;
[103]7import java.io.InputStream;
[12992]8import java.net.Authenticator.RequestorType;
[18]9import java.net.HttpURLConnection;
[1670]10import java.net.MalformedURLException;
[18]11import java.net.URL;
[6803]12import java.util.List;
[18]13
[12510]14import javax.xml.parsers.ParserConfigurationException;
15
[4521]16import org.openstreetmap.josm.data.gpx.GpxData;
[7531]17import org.openstreetmap.josm.data.notes.Note;
[1146]18import org.openstreetmap.josm.data.osm.DataSet;
[1811]19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
[11194]20import org.openstreetmap.josm.io.auth.CredentialsAgentException;
21import org.openstreetmap.josm.io.auth.CredentialsManager;
[9172]22import org.openstreetmap.josm.tools.HttpClient;
[12620]23import org.openstreetmap.josm.tools.Logging;
[12510]24import org.openstreetmap.josm.tools.Utils;
25import org.openstreetmap.josm.tools.XmlParsingException;
[13901]26import org.openstreetmap.josm.tools.XmlUtils;
[12510]27import org.w3c.dom.Document;
28import org.w3c.dom.Node;
29import org.xml.sax.SAXException;
[18]30
31/**
[228]32 * This DataReader reads directly from the REST API of the osm server.
[1169]33 *
[228]34 * It supports plain text transfer as well as gzip or deflate encoded transfers;
35 * if compressed transfers are unwanted, set property osm-server.use-compression
36 * to false.
[153]37 *
[18]38 * @author imi
39 */
[1146]40public abstract class OsmServerReader extends OsmConnection {
[9078]41 private final OsmApi api = OsmApi.getOsmApi();
[11194]42 private boolean doAuthenticate;
[5494]43 protected boolean gpxParsedProperly;
[14216]44 protected String contentType;
[1664]45
[1169]46 /**
[11194]47 * Constructs a new {@code OsmServerReader}.
48 */
49 public OsmServerReader() {
50 try {
[16422]51 doAuthenticate = OsmApi.isUsingOAuth()
52 && CredentialsManager.getInstance().lookupOAuthAccessToken() != null
53 && OsmApi.USE_OAUTH_FOR_ALL_REQUESTS.get();
[11194]54 } catch (CredentialsAgentException e) {
[12620]55 Logging.warn(e);
[11194]56 }
57 }
58
59 /**
[1169]60 * Open a connection to the given url and return a reader on the input stream
61 * from that connection. In case of user cancel, return <code>null</code>.
[5863]62 * Relative URL's are directed to API base URL.
63 * @param urlStr The url to connect to.
64 * @param progressMonitor progress monitoring and abort handler
[6695]65 * @return A reader reading the input stream (servers answer) or <code>null</code>.
[8291]66 * @throws OsmTransferException if data transfer errors occur
[1169]67 */
[10378]68 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
[6695]69 return getInputStream(urlStr, progressMonitor, null);
70 }
71
72 /**
73 * Open a connection to the given url and return a reader on the input stream
74 * from that connection. In case of user cancel, return <code>null</code>.
75 * Relative URL's are directed to API base URL.
76 * @param urlStr The url to connect to.
77 * @param progressMonitor progress monitoring and abort handler
78 * @param reason The reason to show on console. Can be {@code null} if no reason is given
79 * @return A reader reading the input stream (servers answer) or <code>null</code>.
[8291]80 * @throws OsmTransferException if data transfer errors occur
[6695]81 */
[10378]82 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
[1875]83 try {
[2035]84 api.initialize(progressMonitor);
[6695]85 String url = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr);
86 return getInputStreamRaw(url, progressMonitor, reason);
[1875]87 } finally {
88 progressMonitor.invalidate();
89 }
[1670]90 }
[1664]91
[5863]92 /**
[6695]93 * Return the base URL for relative URL requests
[5863]94 * @return base url of API
95 */
[5097]96 protected String getBaseUrl() {
97 return api.getBaseUrl();
98 }
99
[5863]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 * @return An reader reading the input stream (servers answer) or <code>null</code>.
[8291]106 * @throws OsmTransferException if data transfer errors occur
[5863]107 */
[1811]108 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
[6695]109 return getInputStreamRaw(urlStr, progressMonitor, null);
110 }
[6787]111
[6695]112 /**
113 * Open a connection to the given url and return a reader on the input stream
114 * from that connection. In case of user cancel, return <code>null</code>.
115 * @param urlStr The exact url to connect to.
116 * @param progressMonitor progress monitoring and abort handler
117 * @param reason The reason to show on console. Can be {@code null} if no reason is given
118 * @return An reader reading the input stream (servers answer) or <code>null</code>.
[8291]119 * @throws OsmTransferException if data transfer errors occur
[6695]120 */
121 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
[6803]122 return getInputStreamRaw(urlStr, progressMonitor, reason, false);
123 }
124
125 /**
[12596]126 * Open a connection to the given url (if HTTP, trough a GET request) and return a reader on the input stream
[6803]127 * from that connection. In case of user cancel, return <code>null</code>.
128 * @param urlStr The exact url to connect to.
129 * @param progressMonitor progress monitoring and abort handler
130 * @param reason The reason to show on console. Can be {@code null} if no reason is given
131 * @param uncompressAccordingToContentDisposition Whether to inspect the HTTP header {@code Content-Disposition}
[13352]132 * for {@code filename} and uncompress a gzip/bzip2/xz/zip stream.
[6803]133 * @return An reader reading the input stream (servers answer) or <code>null</code>.
[8291]134 * @throws OsmTransferException if data transfer errors occur
[6803]135 */
[12596]136 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
137 boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
138 return getInputStreamRaw(urlStr, progressMonitor, reason, uncompressAccordingToContentDisposition, "GET", null);
139 }
140
141 /**
142 * Open a connection to the given url (if HTTP, with the specified method) and return a reader on the input stream
143 * from that connection. In case of user cancel, return <code>null</code>.
144 * @param urlStr The exact url to connect to.
145 * @param progressMonitor progress monitoring and abort handler
146 * @param reason The reason to show on console. Can be {@code null} if no reason is given
147 * @param uncompressAccordingToContentDisposition Whether to inspect the HTTP header {@code Content-Disposition}
[13352]148 * for {@code filename} and uncompress a gzip/bzip2/xz/zip stream.
[12596]149 * @param httpMethod HTTP method ("GET", "POST" or "PUT")
150 * @param requestBody HTTP request body (for "POST" and "PUT" methods only). Must be null for "GET" method.
151 * @return An reader reading the input stream (servers answer) or <code>null</code>.
152 * @throws OsmTransferException if data transfer errors occur
153 * @since 12596
154 */
[7033]155 @SuppressWarnings("resource")
[8509]156 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
[12596]157 boolean uncompressAccordingToContentDisposition, String httpMethod, byte[] requestBody) throws OsmTransferException {
[1608]158 try {
[16426]159 if (NetworkManager.isOffline(urlStr)) {
160 throw new OsmApiException(new OfflineAccessException(tr("{0} not available (offline mode)", urlStr)));
161 }
[7434]162
[1875]163 URL url = null;
164 try {
[3021]165 url = new URL(urlStr.replace(" ", "%20"));
[8510]166 } catch (MalformedURLException e) {
[1875]167 throw new OsmTransferException(e);
168 }
[228]169
[13356]170 String protocol = url.getProtocol();
171 if ("file".equals(protocol) || "jar".equals(protocol)) {
[9732]172 try {
[13356]173 return Utils.openStream(url);
[9732]174 } catch (IOException e) {
175 throw new OsmTransferException(e);
176 }
177 }
178
[12596]179 final HttpClient client = HttpClient.create(url, httpMethod)
[15969]180 .setAccept("application/xml, */*;q=0.8")
[12596]181 .setFinishOnCloseOutput(false)
182 .setReasonForRequest(reason)
[12711]183 .setOutputMessage(tr("Downloading data..."))
[12596]184 .setRequestBody(requestBody);
[9309]185 activeConnection = client;
[9308]186 adaptRequest(client);
[2641]187 if (doAuthenticate) {
[9172]188 addAuth(client);
[2124]189 }
[2641]190 if (cancel)
[8415]191 throw new OsmTransferCanceledException("Operation canceled");
[1169]192
[9309]193 final HttpClient.Response response;
[1875]194 try {
[9309]195 response = client.connect(progressMonitor);
[14216]196 contentType = response.getContentType();
[10212]197 } catch (IOException e) {
[12620]198 Logging.error(e);
[8509]199 OsmTransferException ote = new OsmTransferException(
200 tr("Could not connect to the OSM server. Please check your internet connection."), e);
[5575]201 ote.setUrl(url.toString());
202 throw ote;
[1875]203 }
204 try {
[12992]205 if (response.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
206 CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER);
[8510]207 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);
[12992]208 }
[1353]209
[9309]210 if (response.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
[8415]211 throw new OsmTransferCanceledException("Proxy Authentication Required");
[2641]212
[9309]213 if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
214 String errorHeader = response.getHeaderField("Error");
[10420]215 String errorBody = fetchResponseText(response);
[13499]216 throw new OsmApiException(response.getResponseCode(), errorHeader, errorBody, url.toString(), null,
[14216]217 contentType);
[1670]218 }
[1169]219
[9309]220 response.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition);
221 return response.getContent();
[6246]222 } catch (OsmTransferException e) {
223 throw e;
[10212]224 } catch (IOException e) {
[6246]225 throw new OsmTransferException(e);
[1670]226 }
[1875]227 } finally {
228 progressMonitor.invalidate();
[1169]229 }
230 }
231
[10420]232 private static String fetchResponseText(final HttpClient.Response response) {
233 try {
234 return response.fetchContent();
235 } catch (IOException e) {
[12620]236 Logging.error(e);
[10420]237 return tr("Reading error text failed.");
238 }
239 }
240
[6244]241 /**
[9308]242 * Allows subclasses to modify the request.
243 * @param request the prepared request
244 * @since 9308
245 */
246 protected void adaptRequest(HttpClient request) {
247 }
248
249 /**
[6244]250 * Download OSM files from somewhere
251 * @param progressMonitor The progress monitor
252 * @return The corresponding dataset
[6643]253 * @throws OsmTransferException if any error occurs
[6244]254 */
[11023]255 public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException;
[1169]256
[6244]257 /**
[13352]258 * Download compressed OSM files from somewhere
[6244]259 * @param progressMonitor The progress monitor
[13352]260 * @param compression compression to use
[6244]261 * @return The corresponding dataset
[6643]262 * @throws OsmTransferException if any error occurs
[13352]263 * @since 13352
[6244]264 */
[13352]265 public DataSet parseOsm(ProgressMonitor progressMonitor, Compression compression) throws OsmTransferException {
[15784]266 throw new UnsupportedOperationException();
[4530]267 }
[5361]268
[6244]269 /**
[13352]270 * Download OSM Change uncompressed files from somewhere
[6244]271 * @param progressMonitor The progress monitor
272 * @return The corresponding dataset
[6643]273 * @throws OsmTransferException if any error occurs
[6244]274 */
[13352]275 public DataSet parseOsmChange(ProgressMonitor progressMonitor) throws OsmTransferException {
[5361]276 return null;
277 }
278
[6244]279 /**
[13352]280 * Download OSM Change compressed files from somewhere
[6244]281 * @param progressMonitor The progress monitor
[13352]282 * @param compression compression to use
[6244]283 * @return The corresponding dataset
[6643]284 * @throws OsmTransferException if any error occurs
[13352]285 * @since 13352
[6244]286 */
[13352]287 public DataSet parseOsmChange(ProgressMonitor progressMonitor, Compression compression) throws OsmTransferException {
[15784]288 throw new UnsupportedOperationException();
[5361]289 }
290
[6244]291 /**
292 * Retrieve raw gps waypoints from the server API.
293 * @param progressMonitor The progress monitor
294 * @return The corresponding GPX tracks
295 * @throws OsmTransferException if any error occurs
296 */
[13352]297 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
[4521]298 return null;
299 }
[5317]300
[6244]301 /**
[13352]302 * Retrieve compressed GPX files from somewhere.
303 * @param progressMonitor The progress monitor
304 * @param compression compression to use
305 * @return The corresponding GPX tracks
306 * @throws OsmTransferException if any error occurs
307 * @since 13352
308 */
309 public GpxData parseRawGps(ProgressMonitor progressMonitor, Compression compression) throws OsmTransferException {
[15784]310 throw new UnsupportedOperationException();
[13352]311 }
312
313 /**
[2124]314 * Returns true if this reader is adding authentication credentials to the read
315 * request sent to the server.
[2512]316 *
[2124]317 * @return true if this reader is adding authentication credentials to the read
318 * request sent to the server
319 */
320 public boolean isDoAuthenticate() {
321 return doAuthenticate;
322 }
323
324 /**
325 * Sets whether this reader adds authentication credentials to the read
326 * request sent to the server.
[2512]327 *
[2124]328 * @param doAuthenticate true if this reader adds authentication credentials to the read
329 * request sent to the server
330 */
331 public void setDoAuthenticate(boolean doAuthenticate) {
332 this.doAuthenticate = doAuthenticate;
333 }
[6070]334
[5494]335 /**
336 * Determines if the GPX data has been parsed properly.
337 * @return true if the GPX data has been parsed properly, false otherwise
338 * @see GpxReader#parse
339 */
340 public final boolean isGpxParsedProperly() {
341 return gpxParsedProperly;
342 }
[7531]343
344 /**
345 * Downloads notes from the API, given API limit parameters
346 *
[8218]347 * @param noteLimit How many notes to download.
[7531]348 * @param daysClosed Return notes closed this many days in the past. -1 means all notes, ever. 0 means only unresolved notes.
349 * @param progressMonitor Progress monitor for user feedback
350 * @return List of notes returned by the API
351 * @throws OsmTransferException if any errors happen
352 */
[8218]353 public List<Note> parseNotes(int noteLimit, int daysClosed, ProgressMonitor progressMonitor) throws OsmTransferException {
[7531]354 return null;
355 }
356
357 /**
358 * Downloads notes from a given raw URL. The URL is assumed to be complete and no API limits are added
359 *
[8470]360 * @param progressMonitor progress monitor
[7531]361 * @return A list of notes parsed from the URL
[8470]362 * @throws OsmTransferException if any error occurs during dialog with OSM API
[7531]363 */
364 public List<Note> parseRawNotes(final ProgressMonitor progressMonitor) throws OsmTransferException {
365 return null;
366 }
367
368 /**
[13352]369 * Download notes from a URL that contains a compressed notes dump file
370 * @param progressMonitor progress monitor
371 * @param compression compression to use
372 * @return A list of notes parsed from the URL
373 * @throws OsmTransferException if any error occurs during dialog with OSM API
374 * @since 13352
375 */
376 public List<Note> parseRawNotes(ProgressMonitor progressMonitor, Compression compression) throws OsmTransferException {
[15784]377 throw new UnsupportedOperationException();
[13352]378 }
379
380 /**
[12510]381 * Returns an attribute from the given DOM node.
382 * @param node DOM node
383 * @param name attribute name
384 * @return attribute value for the given attribute
385 * @since 12510
386 */
387 protected static String getAttribute(Node node, String name) {
388 return node.getAttributes().getNamedItem(name).getNodeValue();
389 }
390
391 /**
392 * DOM document parser.
393 * @param <R> resulting type
394 * @since 12510
395 */
396 @FunctionalInterface
397 protected interface DomParser<R> {
398 /**
399 * Parses a given DOM document.
400 * @param doc DOM document
401 * @return parsed data
402 * @throws XmlParsingException if an XML parsing error occurs
403 */
404 R parse(Document doc) throws XmlParsingException;
405 }
406
407 /**
408 * Fetches generic data from the DOM document resulting an API call.
409 * @param api the OSM API call
410 * @param subtask the subtask translated message
411 * @param parser the parser converting the DOM document (OSM API result)
412 * @param <T> data type
413 * @param monitor The progress monitor
414 * @param reason The reason to show on console. Can be {@code null} if no reason is given
415 * @return The converted data
416 * @throws OsmTransferException if something goes wrong
417 * @since 12510
418 */
419 public <T> T fetchData(String api, String subtask, DomParser<T> parser, ProgressMonitor monitor, String reason)
420 throws OsmTransferException {
421 try {
422 monitor.beginTask("");
423 monitor.indeterminateSubTask(subtask);
424 try (InputStream in = getInputStream(api, monitor.createSubTaskMonitor(1, true), reason)) {
[13901]425 return parser.parse(XmlUtils.parseSafeDOM(in));
[12510]426 }
427 } catch (OsmTransferException e) {
428 throw e;
429 } catch (IOException | ParserConfigurationException | SAXException e) {
430 throw new OsmTransferException(e);
431 } finally {
432 monitor.finishTask();
433 }
434 }
[18]435}
Note: See TracBrowser for help on using the repository browser.