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

Last change on this file since 13254 was 12992, checked in by Don-vip, 7 years ago

fix #15435 - do not cache incorrect login credentials when using basic auth

  • Property svn:eol-style set to native
File size: 18.1 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 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 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 if ("file".equals(url.getProtocol())) {
167 try {
168 return url.openStream();
169 } catch (IOException e) {
170 throw new OsmTransferException(e);
171 }
172 }
173
174 final HttpClient client = HttpClient.create(url, httpMethod)
175 .setFinishOnCloseOutput(false)
176 .setReasonForRequest(reason)
177 .setOutputMessage(tr("Downloading data..."))
178 .setRequestBody(requestBody);
179 activeConnection = client;
180 adaptRequest(client);
181 if (doAuthenticate) {
182 addAuth(client);
183 }
184 if (cancel)
185 throw new OsmTransferCanceledException("Operation canceled");
186
187 final HttpClient.Response response;
188 try {
189 response = client.connect(progressMonitor);
190 } catch (IOException e) {
191 Logging.error(e);
192 OsmTransferException ote = new OsmTransferException(
193 tr("Could not connect to the OSM server. Please check your internet connection."), e);
194 ote.setUrl(url.toString());
195 throw ote;
196 }
197 try {
198 if (response.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
199 CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER);
200 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);
201 }
202
203 if (response.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
204 throw new OsmTransferCanceledException("Proxy Authentication Required");
205
206 if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
207 String errorHeader = response.getHeaderField("Error");
208 String errorBody = fetchResponseText(response);
209 throw new OsmApiException(response.getResponseCode(), errorHeader, errorBody, url.toString());
210 }
211
212 response.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition);
213 return response.getContent();
214 } catch (OsmTransferException e) {
215 throw e;
216 } catch (IOException e) {
217 throw new OsmTransferException(e);
218 }
219 } finally {
220 progressMonitor.invalidate();
221 }
222 }
223
224 private static String fetchResponseText(final HttpClient.Response response) {
225 try {
226 return response.fetchContent();
227 } catch (IOException e) {
228 Logging.error(e);
229 return tr("Reading error text failed.");
230 }
231 }
232
233 /**
234 * Allows subclasses to modify the request.
235 * @param request the prepared request
236 * @since 9308
237 */
238 protected void adaptRequest(HttpClient request) {
239 }
240
241 /**
242 * Download OSM files from somewhere
243 * @param progressMonitor The progress monitor
244 * @return The corresponding dataset
245 * @throws OsmTransferException if any error occurs
246 */
247 public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException;
248
249 /**
250 * Download OSM Change files from somewhere
251 * @param progressMonitor The progress monitor
252 * @return The corresponding dataset
253 * @throws OsmTransferException if any error occurs
254 */
255 public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
256 return null;
257 }
258
259 /**
260 * Download BZip2-compressed OSM Change files from somewhere
261 * @param progressMonitor The progress monitor
262 * @return The corresponding dataset
263 * @throws OsmTransferException if any error occurs
264 */
265 public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
266 return null;
267 }
268
269 /**
270 * Download GZip-compressed OSM Change files from somewhere
271 * @param progressMonitor The progress monitor
272 * @return The corresponding dataset
273 * @throws OsmTransferException if any error occurs
274 */
275 public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
276 return null;
277 }
278
279 /**
280 * Retrieve raw gps waypoints from the server API.
281 * @param progressMonitor The progress monitor
282 * @return The corresponding GPX tracks
283 * @throws OsmTransferException if any error occurs
284 */
285 public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
286 return null;
287 }
288
289 /**
290 * Retrieve BZip2-compressed GPX files from somewhere.
291 * @param progressMonitor The progress monitor
292 * @return The corresponding GPX tracks
293 * @throws OsmTransferException if any error occurs
294 * @since 6244
295 */
296 public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
297 return null;
298 }
299
300 /**
301 * Download BZip2-compressed OSM files from somewhere
302 * @param progressMonitor The progress monitor
303 * @return The corresponding dataset
304 * @throws OsmTransferException if any error occurs
305 */
306 public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
307 return null;
308 }
309
310 /**
311 * Download GZip-compressed OSM files from somewhere
312 * @param progressMonitor The progress monitor
313 * @return The corresponding dataset
314 * @throws OsmTransferException if any error occurs
315 */
316 public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
317 return null;
318 }
319
320 /**
321 * Download Zip-compressed OSM files from somewhere
322 * @param progressMonitor The progress monitor
323 * @return The corresponding dataset
324 * @throws OsmTransferException if any error occurs
325 * @since 6882
326 */
327 public DataSet parseOsmZip(final ProgressMonitor progressMonitor) throws OsmTransferException {
328 return null;
329 }
330
331 /**
332 * Returns true if this reader is adding authentication credentials to the read
333 * request sent to the server.
334 *
335 * @return true if this reader is adding authentication credentials to the read
336 * request sent to the server
337 */
338 public boolean isDoAuthenticate() {
339 return doAuthenticate;
340 }
341
342 /**
343 * Sets whether this reader adds authentication credentials to the read
344 * request sent to the server.
345 *
346 * @param doAuthenticate true if this reader adds authentication credentials to the read
347 * request sent to the server
348 */
349 public void setDoAuthenticate(boolean doAuthenticate) {
350 this.doAuthenticate = doAuthenticate;
351 }
352
353 /**
354 * Determines if the GPX data has been parsed properly.
355 * @return true if the GPX data has been parsed properly, false otherwise
356 * @see GpxReader#parse
357 */
358 public final boolean isGpxParsedProperly() {
359 return gpxParsedProperly;
360 }
361
362 /**
363 * Downloads notes from the API, given API limit parameters
364 *
365 * @param noteLimit How many notes to download.
366 * @param daysClosed Return notes closed this many days in the past. -1 means all notes, ever. 0 means only unresolved notes.
367 * @param progressMonitor Progress monitor for user feedback
368 * @return List of notes returned by the API
369 * @throws OsmTransferException if any errors happen
370 */
371 public List<Note> parseNotes(int noteLimit, int daysClosed, ProgressMonitor progressMonitor) throws OsmTransferException {
372 return null;
373 }
374
375 /**
376 * Downloads notes from a given raw URL. The URL is assumed to be complete and no API limits are added
377 *
378 * @param progressMonitor progress monitor
379 * @return A list of notes parsed from the URL
380 * @throws OsmTransferException if any error occurs during dialog with OSM API
381 */
382 public List<Note> parseRawNotes(final ProgressMonitor progressMonitor) throws OsmTransferException {
383 return null;
384 }
385
386 /**
387 * Download notes from a URL that contains a bzip2 compressed notes dump file
388 * @param progressMonitor progress monitor
389 * @return A list of notes parsed from the URL
390 * @throws OsmTransferException if any error occurs during dialog with OSM API
391 */
392 public List<Note> parseRawNotesBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
393 return null;
394 }
395
396 /**
397 * Returns an attribute from the given DOM node.
398 * @param node DOM node
399 * @param name attribute name
400 * @return attribute value for the given attribute
401 * @since 12510
402 */
403 protected static String getAttribute(Node node, String name) {
404 return node.getAttributes().getNamedItem(name).getNodeValue();
405 }
406
407 /**
408 * DOM document parser.
409 * @param <R> resulting type
410 * @since 12510
411 */
412 @FunctionalInterface
413 protected interface DomParser<R> {
414 /**
415 * Parses a given DOM document.
416 * @param doc DOM document
417 * @return parsed data
418 * @throws XmlParsingException if an XML parsing error occurs
419 */
420 R parse(Document doc) throws XmlParsingException;
421 }
422
423 /**
424 * Fetches generic data from the DOM document resulting an API call.
425 * @param api the OSM API call
426 * @param subtask the subtask translated message
427 * @param parser the parser converting the DOM document (OSM API result)
428 * @param <T> data type
429 * @param monitor The progress monitor
430 * @param reason The reason to show on console. Can be {@code null} if no reason is given
431 * @return The converted data
432 * @throws OsmTransferException if something goes wrong
433 * @since 12510
434 */
435 public <T> T fetchData(String api, String subtask, DomParser<T> parser, ProgressMonitor monitor, String reason)
436 throws OsmTransferException {
437 try {
438 monitor.beginTask("");
439 monitor.indeterminateSubTask(subtask);
440 try (InputStream in = getInputStream(api, monitor.createSubTaskMonitor(1, true), reason)) {
441 return parser.parse(Utils.parseSafeDOM(in));
442 }
443 } catch (OsmTransferException e) {
444 throw e;
445 } catch (IOException | ParserConfigurationException | SAXException e) {
446 throw new OsmTransferException(e);
447 } finally {
448 monitor.finishTask();
449 }
450 }
451}
Note: See TracBrowser for help on using the repository browser.