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

Last change on this file since 9174 was 9174, checked in by simon04, 9 years ago

see #12231 - Update unit tests, fix regression

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