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

Last change on this file since 13486 was 13356, checked in by Don-vip, 6 years ago

fix #15662 - allow JOSM to open JAR URLs containing exclamation marks (workaround to https://bugs.openjdk.java.net/browse/JDK-4523159)

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