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

Last change on this file since 6643 was 6643, checked in by Don-vip, 10 years ago

global replacement of e.printStackTrace() by Main.error(e)

  • Property svn:eol-style set to native
File size: 10.3 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.BufferedReader;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.InputStreamReader;
10import java.net.HttpURLConnection;
11import java.net.MalformedURLException;
12import java.net.URL;
13import java.util.zip.GZIPInputStream;
14import java.util.zip.Inflater;
15import java.util.zip.InflaterInputStream;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.gpx.GpxData;
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21import 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 */
32public 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 An 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 try {
54 api.initialize(progressMonitor);
55 urlStr = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr);
56 return getInputStreamRaw(urlStr, progressMonitor);
57 } finally {
58 progressMonitor.invalidate();
59 }
60 }
61
62 /**
63 * Retrun the base URL for relative URL requests
64 * @return base url of API
65 */
66 protected String getBaseUrl() {
67 return api.getBaseUrl();
68 }
69
70 /**
71 * Open a connection to the given url and return a reader on the input stream
72 * from that connection. In case of user cancel, return <code>null</code>.
73 * @param urlStr The exact url to connect to.
74 * @param progressMonitor progress monitoring and abort handler
75 * @return An reader reading the input stream (servers answer) or <code>null</code>.
76 * @throws OsmTransferException thrown if data transfer errors occur
77 */
78 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
79 try {
80 URL url = null;
81 try {
82 url = new URL(urlStr.replace(" ", "%20"));
83 } catch(MalformedURLException e) {
84 throw new OsmTransferException(e);
85 }
86 try {
87 // fix #7640, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
88 activeConnection = Utils.openHttpConnection(url, false);
89 } catch(Exception e) {
90 throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e);
91 }
92 if (cancel) {
93 activeConnection.disconnect();
94 return null;
95 }
96
97 if (doAuthenticate) {
98 addAuth(activeConnection);
99 }
100 if (cancel)
101 throw new OsmTransferCanceledException();
102 if (Main.pref.getBoolean("osm-server.use-compression", true)) {
103 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
104 }
105
106 activeConnection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
107
108 try {
109 Main.info("GET " + url);
110 activeConnection.connect();
111 } catch (Exception e) {
112 Main.error(e);
113 OsmTransferException ote = new OsmTransferException(tr("Could not connect to the OSM server. Please check your internet connection."), e);
114 ote.setUrl(url.toString());
115 throw ote;
116 }
117 try {
118 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
119 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
120
121 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
122 throw new OsmTransferCanceledException();
123
124 String encoding = activeConnection.getContentEncoding();
125 if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
126 String errorHeader = activeConnection.getHeaderField("Error");
127 StringBuilder errorBody = new StringBuilder();
128 try
129 {
130 InputStream i = FixEncoding(activeConnection.getErrorStream(), encoding);
131 if (i != null) {
132 BufferedReader in = new BufferedReader(new InputStreamReader(i));
133 String s;
134 while((s = in.readLine()) != null) {
135 errorBody.append(s);
136 errorBody.append("\n");
137 }
138 }
139 }
140 catch(Exception e) {
141 errorBody.append(tr("Reading error text failed."));
142 }
143
144 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString(), url.toString());
145 }
146
147 return FixEncoding(new ProgressInputStream(activeConnection, progressMonitor), encoding);
148 } catch (OsmTransferException e) {
149 throw e;
150 } catch (Exception e) {
151 throw new OsmTransferException(e);
152 }
153 } finally {
154 progressMonitor.invalidate();
155 }
156 }
157
158 private InputStream FixEncoding(InputStream stream, String encoding) throws IOException
159 {
160 if ("gzip".equalsIgnoreCase(encoding)) {
161 stream = new GZIPInputStream(stream);
162 }
163 else if ("deflate".equalsIgnoreCase(encoding)) {
164 stream = new InflaterInputStream(stream, new Inflater(true));
165 }
166 return stream;
167 }
168
169 /**
170 * Download OSM files from somewhere
171 * @param progressMonitor The progress monitor
172 * @return The corresponding dataset
173 * @throws OsmTransferException if any error occurs
174 */
175 public abstract DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException;
176
177 /**
178 * Download OSM Change files from somewhere
179 * @param progressMonitor The progress monitor
180 * @return The corresponding dataset
181 * @throws OsmTransferException if any error occurs
182 */
183 public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
184 return null;
185 }
186
187 /**
188 * Download BZip2-compressed OSM Change files from somewhere
189 * @param progressMonitor The progress monitor
190 * @return The corresponding dataset
191 * @throws OsmTransferException if any error occurs
192 */
193 public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
194 return null;
195 }
196
197 /**
198 * Download GZip-compressed OSM Change files from somewhere
199 * @param progressMonitor The progress monitor
200 * @return The corresponding dataset
201 * @throws OsmTransferException if any error occurs
202 */
203 public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
204 return null;
205 }
206
207 /**
208 * Retrieve raw gps waypoints from the server API.
209 * @param progressMonitor The progress monitor
210 * @return The corresponding GPX tracks
211 * @throws OsmTransferException if any error occurs
212 */
213 public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
214 return null;
215 }
216
217 /**
218 * Retrieve BZip2-compressed GPX files from somewhere.
219 * @param progressMonitor The progress monitor
220 * @return The corresponding GPX tracks
221 * @throws OsmTransferException if any error occurs
222 * @since 6244
223 */
224 public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
225 return null;
226 }
227
228 /**
229 * Download BZip2-compressed OSM 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 parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
235 return null;
236 }
237
238 /**
239 * Download GZip-compressed OSM files from somewhere
240 * @param progressMonitor The progress monitor
241 * @return The corresponding dataset
242 * @throws OsmTransferException if any error occurs
243 */
244 public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
245 return null;
246 }
247
248 /**
249 * Returns true if this reader is adding authentication credentials to the read
250 * request sent to the server.
251 *
252 * @return true if this reader is adding authentication credentials to the read
253 * request sent to the server
254 */
255 public boolean isDoAuthenticate() {
256 return doAuthenticate;
257 }
258
259 /**
260 * Sets whether this reader adds authentication credentials to the read
261 * request sent to the server.
262 *
263 * @param doAuthenticate true if this reader adds authentication credentials to the read
264 * request sent to the server
265 */
266 public void setDoAuthenticate(boolean doAuthenticate) {
267 this.doAuthenticate = doAuthenticate;
268 }
269
270 /**
271 * Determines if the GPX data has been parsed properly.
272 * @return true if the GPX data has been parsed properly, false otherwise
273 * @see GpxReader#parse
274 */
275 public final boolean isGpxParsedProperly() {
276 return gpxParsedProperly;
277 }
278}
Note: See TracBrowser for help on using the repository browser.