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

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

avoid code duplication

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