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

Last change on this file since 9519 was 9353, checked in by simon04, 8 years ago

Refactoring: introduce OsmApi#getServerUrl

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