1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.io.InputStreamReader;
|
---|
10 | import java.net.HttpURLConnection;
|
---|
11 | import java.net.MalformedURLException;
|
---|
12 | import java.net.URL;
|
---|
13 | import java.util.zip.GZIPInputStream;
|
---|
14 | import java.util.zip.Inflater;
|
---|
15 | import java.util.zip.InflaterInputStream;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This DataReader reads directly from the REST API of the osm server.
|
---|
25 | *
|
---|
26 | * It supports plain text transfer as well as gzip or deflate encoded transfers;
|
---|
27 | * if compressed transfers are unwanted, set property osm-server.use-compression
|
---|
28 | * to false.
|
---|
29 | *
|
---|
30 | * @author imi
|
---|
31 | */
|
---|
32 | public abstract class OsmServerReader extends OsmConnection {
|
---|
33 | private OsmApi api = OsmApi.getOsmApi();
|
---|
34 | private boolean doAuthenticate = false;
|
---|
35 | protected boolean gpxParsedProperly;
|
---|
36 |
|
---|
37 | protected enum Compression {
|
---|
38 | NONE,
|
---|
39 | BZIP2,
|
---|
40 | GZIP
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Open a connection to the given url and return a reader on the input stream
|
---|
45 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
46 | * Relative URL's are directed to API base URL.
|
---|
47 | * @param urlStr The url to connect to.
|
---|
48 | * @param progressMonitor progress monitoring and abort handler
|
---|
49 | * @return A reader reading the input stream (servers answer) or <code>null</code>.
|
---|
50 | * @throws OsmTransferException thrown if data transfer errors occur
|
---|
51 | */
|
---|
52 | protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
53 | return getInputStream(urlStr, progressMonitor, null);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Open a connection to the given url and return a reader on the input stream
|
---|
58 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
59 | * Relative URL's are directed to API base URL.
|
---|
60 | * @param urlStr The url to connect to.
|
---|
61 | * @param progressMonitor progress monitoring and abort handler
|
---|
62 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
63 | * @return A reader reading the input stream (servers answer) or <code>null</code>.
|
---|
64 | * @throws OsmTransferException thrown if data transfer errors occur
|
---|
65 | */
|
---|
66 | protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
|
---|
67 | try {
|
---|
68 | api.initialize(progressMonitor);
|
---|
69 | String url = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr);
|
---|
70 | return getInputStreamRaw(url, progressMonitor, reason);
|
---|
71 | } finally {
|
---|
72 | progressMonitor.invalidate();
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Return the base URL for relative URL requests
|
---|
78 | * @return base url of API
|
---|
79 | */
|
---|
80 | protected String getBaseUrl() {
|
---|
81 | return api.getBaseUrl();
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Open a connection to the given url and return a reader on the input stream
|
---|
86 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
87 | * @param urlStr The exact url to connect to.
|
---|
88 | * @param progressMonitor progress monitoring and abort handler
|
---|
89 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
90 | * @throws OsmTransferException thrown if data transfer errors occur
|
---|
91 | */
|
---|
92 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
93 | return getInputStreamRaw(urlStr, progressMonitor, null);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Open a connection to the given url and return a reader on the input stream
|
---|
98 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
99 | * @param urlStr The exact url to connect to.
|
---|
100 | * @param progressMonitor progress monitoring and abort handler
|
---|
101 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
102 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
103 | * @throws OsmTransferException thrown if data transfer errors occur
|
---|
104 | */
|
---|
105 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
|
---|
106 | try {
|
---|
107 | URL url = null;
|
---|
108 | try {
|
---|
109 | url = new URL(urlStr.replace(" ", "%20"));
|
---|
110 | } catch(MalformedURLException e) {
|
---|
111 | throw new OsmTransferException(e);
|
---|
112 | }
|
---|
113 | try {
|
---|
114 | // fix #7640, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
|
---|
115 | activeConnection = Utils.openHttpConnection(url, false);
|
---|
116 | } catch(Exception e) {
|
---|
117 | throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e);
|
---|
118 | }
|
---|
119 | if (cancel) {
|
---|
120 | activeConnection.disconnect();
|
---|
121 | return null;
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (doAuthenticate) {
|
---|
125 | addAuth(activeConnection);
|
---|
126 | }
|
---|
127 | if (cancel)
|
---|
128 | throw new OsmTransferCanceledException();
|
---|
129 | if (Main.pref.getBoolean("osm-server.use-compression", true)) {
|
---|
130 | activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
|
---|
131 | }
|
---|
132 |
|
---|
133 | activeConnection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
|
---|
134 |
|
---|
135 | try {
|
---|
136 | if (reason != null && !reason.isEmpty()) {
|
---|
137 | Main.info("GET " + url + " (" + reason + ")");
|
---|
138 | } else {
|
---|
139 | Main.info("GET " + url);
|
---|
140 | }
|
---|
141 | activeConnection.connect();
|
---|
142 | } catch (Exception e) {
|
---|
143 | Main.error(e);
|
---|
144 | OsmTransferException ote = new OsmTransferException(tr("Could not connect to the OSM server. Please check your internet connection."), e);
|
---|
145 | ote.setUrl(url.toString());
|
---|
146 | throw ote;
|
---|
147 | }
|
---|
148 | try {
|
---|
149 | if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
|
---|
150 | throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
|
---|
151 |
|
---|
152 | if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
|
---|
153 | throw new OsmTransferCanceledException();
|
---|
154 |
|
---|
155 | String encoding = activeConnection.getContentEncoding();
|
---|
156 | if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
---|
157 | String errorHeader = activeConnection.getHeaderField("Error");
|
---|
158 | StringBuilder errorBody = new StringBuilder();
|
---|
159 | try
|
---|
160 | {
|
---|
161 | InputStream i = FixEncoding(activeConnection.getErrorStream(), encoding);
|
---|
162 | if (i != null) {
|
---|
163 | BufferedReader in = new BufferedReader(new InputStreamReader(i));
|
---|
164 | String s;
|
---|
165 | while((s = in.readLine()) != null) {
|
---|
166 | errorBody.append(s);
|
---|
167 | errorBody.append("\n");
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | catch(Exception e) {
|
---|
172 | errorBody.append(tr("Reading error text failed."));
|
---|
173 | }
|
---|
174 |
|
---|
175 | throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString(), url.toString());
|
---|
176 | }
|
---|
177 |
|
---|
178 | return FixEncoding(new ProgressInputStream(activeConnection, progressMonitor), encoding);
|
---|
179 | } catch (OsmTransferException e) {
|
---|
180 | throw e;
|
---|
181 | } catch (Exception e) {
|
---|
182 | throw new OsmTransferException(e);
|
---|
183 | }
|
---|
184 | } finally {
|
---|
185 | progressMonitor.invalidate();
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | private InputStream FixEncoding(InputStream stream, String encoding) throws IOException
|
---|
190 | {
|
---|
191 | if ("gzip".equalsIgnoreCase(encoding)) {
|
---|
192 | stream = new GZIPInputStream(stream);
|
---|
193 | }
|
---|
194 | else if ("deflate".equalsIgnoreCase(encoding)) {
|
---|
195 | stream = new InflaterInputStream(stream, new Inflater(true));
|
---|
196 | }
|
---|
197 | return stream;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Download OSM files from somewhere
|
---|
202 | * @param progressMonitor The progress monitor
|
---|
203 | * @return The corresponding dataset
|
---|
204 | * @throws OsmTransferException if any error occurs
|
---|
205 | */
|
---|
206 | public abstract DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException;
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Download OSM Change files from somewhere
|
---|
210 | * @param progressMonitor The progress monitor
|
---|
211 | * @return The corresponding dataset
|
---|
212 | * @throws OsmTransferException if any error occurs
|
---|
213 | */
|
---|
214 | public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
215 | return null;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Download BZip2-compressed OSM Change files from somewhere
|
---|
220 | * @param progressMonitor The progress monitor
|
---|
221 | * @return The corresponding dataset
|
---|
222 | * @throws OsmTransferException if any error occurs
|
---|
223 | */
|
---|
224 | public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
225 | return null;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Download GZip-compressed OSM Change files from somewhere
|
---|
230 | * @param progressMonitor The progress monitor
|
---|
231 | * @return The corresponding dataset
|
---|
232 | * @throws OsmTransferException if any error occurs
|
---|
233 | */
|
---|
234 | public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
235 | return null;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Retrieve raw gps waypoints from the server API.
|
---|
240 | * @param progressMonitor The progress monitor
|
---|
241 | * @return The corresponding GPX tracks
|
---|
242 | * @throws OsmTransferException if any error occurs
|
---|
243 | */
|
---|
244 | public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
245 | return null;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Retrieve BZip2-compressed GPX files from somewhere.
|
---|
250 | * @param progressMonitor The progress monitor
|
---|
251 | * @return The corresponding GPX tracks
|
---|
252 | * @throws OsmTransferException if any error occurs
|
---|
253 | * @since 6244
|
---|
254 | */
|
---|
255 | public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
256 | return null;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Download BZip2-compressed OSM files from somewhere
|
---|
261 | * @param progressMonitor The progress monitor
|
---|
262 | * @return The corresponding dataset
|
---|
263 | * @throws OsmTransferException if any error occurs
|
---|
264 | */
|
---|
265 | public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
266 | return null;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Download GZip-compressed OSM files from somewhere
|
---|
271 | * @param progressMonitor The progress monitor
|
---|
272 | * @return The corresponding dataset
|
---|
273 | * @throws OsmTransferException if any error occurs
|
---|
274 | */
|
---|
275 | public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
276 | return null;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Returns true if this reader is adding authentication credentials to the read
|
---|
281 | * request sent to the server.
|
---|
282 | *
|
---|
283 | * @return true if this reader is adding authentication credentials to the read
|
---|
284 | * request sent to the server
|
---|
285 | */
|
---|
286 | public boolean isDoAuthenticate() {
|
---|
287 | return doAuthenticate;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Sets whether this reader adds authentication credentials to the read
|
---|
292 | * request sent to the server.
|
---|
293 | *
|
---|
294 | * @param doAuthenticate true if this reader adds authentication credentials to the read
|
---|
295 | * request sent to the server
|
---|
296 | */
|
---|
297 | public void setDoAuthenticate(boolean doAuthenticate) {
|
---|
298 | this.doAuthenticate = doAuthenticate;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Determines if the GPX data has been parsed properly.
|
---|
303 | * @return true if the GPX data has been parsed properly, false otherwise
|
---|
304 | * @see GpxReader#parse
|
---|
305 | */
|
---|
306 | public final boolean isGpxParsedProperly() {
|
---|
307 | return gpxParsedProperly;
|
---|
308 | }
|
---|
309 | }
|
---|