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

Last change on this file since 9242 was 9185, checked in by simon04, 8 years ago

see #12231 - HttpClient now reports status to ProgressMonitor

  • Property svn:eol-style set to native
File size: 13.2 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, Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL));
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 client.setReasonForRequest(reason);
126 if (doAuthenticate) {
127 addAuth(client);
128 }
129 if (cancel)
130 throw new OsmTransferCanceledException("Operation canceled");
131
132 try {
133 activeConnection = client.connect(progressMonitor);
134 } catch (Exception e) {
135 Main.error(e);
136 OsmTransferException ote = new OsmTransferException(
137 tr("Could not connect to the OSM server. Please check your internet connection."), e);
138 ote.setUrl(url.toString());
139 throw ote;
140 }
141 try {
142 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
143 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);
144
145 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
146 throw new OsmTransferCanceledException("Proxy Authentication Required");
147
148 if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
149 String errorHeader = activeConnection.getHeaderField("Error");
150 String errorBody;
151 try {
152 errorBody = activeConnection.fetchContent();
153 } catch (Exception e) {
154 errorBody = tr("Reading error text failed.");
155 }
156 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody, url.toString());
157 }
158
159 activeConnection.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition);
160 return activeConnection.getContent();
161 } catch (OsmTransferException e) {
162 throw e;
163 } catch (Exception e) {
164 throw new OsmTransferException(e);
165 }
166 } finally {
167 progressMonitor.invalidate();
168 }
169 }
170
171 /**
172 * Download OSM files from somewhere
173 * @param progressMonitor The progress monitor
174 * @return The corresponding dataset
175 * @throws OsmTransferException if any error occurs
176 */
177 public abstract DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException;
178
179 /**
180 * Download OSM Change files from somewhere
181 * @param progressMonitor The progress monitor
182 * @return The corresponding dataset
183 * @throws OsmTransferException if any error occurs
184 */
185 public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
186 return null;
187 }
188
189 /**
190 * Download BZip2-compressed OSM Change files from somewhere
191 * @param progressMonitor The progress monitor
192 * @return The corresponding dataset
193 * @throws OsmTransferException if any error occurs
194 */
195 public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
196 return null;
197 }
198
199 /**
200 * Download GZip-compressed 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 parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
206 return null;
207 }
208
209 /**
210 * Retrieve raw gps waypoints from the server API.
211 * @param progressMonitor The progress monitor
212 * @return The corresponding GPX tracks
213 * @throws OsmTransferException if any error occurs
214 */
215 public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
216 return null;
217 }
218
219 /**
220 * Retrieve BZip2-compressed GPX files from somewhere.
221 * @param progressMonitor The progress monitor
222 * @return The corresponding GPX tracks
223 * @throws OsmTransferException if any error occurs
224 * @since 6244
225 */
226 public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
227 return null;
228 }
229
230 /**
231 * Download BZip2-compressed OSM files from somewhere
232 * @param progressMonitor The progress monitor
233 * @return The corresponding dataset
234 * @throws OsmTransferException if any error occurs
235 */
236 public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
237 return null;
238 }
239
240 /**
241 * Download GZip-compressed OSM files from somewhere
242 * @param progressMonitor The progress monitor
243 * @return The corresponding dataset
244 * @throws OsmTransferException if any error occurs
245 */
246 public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
247 return null;
248 }
249
250 /**
251 * Download Zip-compressed OSM files from somewhere
252 * @param progressMonitor The progress monitor
253 * @return The corresponding dataset
254 * @throws OsmTransferException if any error occurs
255 * @since 6882
256 */
257 public DataSet parseOsmZip(final ProgressMonitor progressMonitor) throws OsmTransferException {
258 return null;
259 }
260
261 /**
262 * Returns true if this reader is adding authentication credentials to the read
263 * request sent to the server.
264 *
265 * @return true if this reader is adding authentication credentials to the read
266 * request sent to the server
267 */
268 public boolean isDoAuthenticate() {
269 return doAuthenticate;
270 }
271
272 /**
273 * Sets whether this reader adds authentication credentials to the read
274 * request sent to the server.
275 *
276 * @param doAuthenticate true if this reader adds authentication credentials to the read
277 * request sent to the server
278 */
279 public void setDoAuthenticate(boolean doAuthenticate) {
280 this.doAuthenticate = doAuthenticate;
281 }
282
283 /**
284 * Determines if the GPX data has been parsed properly.
285 * @return true if the GPX data has been parsed properly, false otherwise
286 * @see GpxReader#parse
287 */
288 public final boolean isGpxParsedProperly() {
289 return gpxParsedProperly;
290 }
291
292 /**
293 * Downloads notes from the API, given API limit parameters
294 *
295 * @param noteLimit How many notes to download.
296 * @param daysClosed Return notes closed this many days in the past. -1 means all notes, ever. 0 means only unresolved notes.
297 * @param progressMonitor Progress monitor for user feedback
298 * @return List of notes returned by the API
299 * @throws OsmTransferException if any errors happen
300 */
301 public List<Note> parseNotes(int noteLimit, int daysClosed, ProgressMonitor progressMonitor) throws OsmTransferException {
302 return null;
303 }
304
305 /**
306 * Downloads notes from a given raw URL. The URL is assumed to be complete and no API limits are added
307 *
308 * @param progressMonitor progress monitor
309 * @return A list of notes parsed from the URL
310 * @throws OsmTransferException if any error occurs during dialog with OSM API
311 */
312 public List<Note> parseRawNotes(final ProgressMonitor progressMonitor) throws OsmTransferException {
313 return null;
314 }
315
316 /**
317 * Download notes from a URL that contains a bzip2 compressed notes dump file
318 * @param progressMonitor progress monitor
319 * @return A list of notes parsed from the URL
320 * @throws OsmTransferException if any error occurs during dialog with OSM API
321 */
322 public List<Note> parseRawNotesBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
323 return null;
324 }
325}
Note: See TracBrowser for help on using the repository browser.