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

Last change on this file since 10063 was 9732, checked in by Don-vip, 8 years ago

remote control: add more unit tests, robustness

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