001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.InputStream; 007import java.net.HttpURLConnection; 008import java.net.MalformedURLException; 009import java.net.URL; 010import java.util.List; 011 012import org.openstreetmap.josm.Main; 013import org.openstreetmap.josm.data.gpx.GpxData; 014import org.openstreetmap.josm.data.notes.Note; 015import org.openstreetmap.josm.data.osm.DataSet; 016import org.openstreetmap.josm.gui.progress.ProgressMonitor; 017import org.openstreetmap.josm.tools.HttpClient; 018 019/** 020 * This DataReader reads directly from the REST API of the osm server. 021 * 022 * It supports plain text transfer as well as gzip or deflate encoded transfers; 023 * if compressed transfers are unwanted, set property osm-server.use-compression 024 * to false. 025 * 026 * @author imi 027 */ 028public abstract class OsmServerReader extends OsmConnection { 029 private final OsmApi api = OsmApi.getOsmApi(); 030 private boolean doAuthenticate; 031 protected boolean gpxParsedProperly; 032 033 /** 034 * Open a connection to the given url and return a reader on the input stream 035 * from that connection. In case of user cancel, return <code>null</code>. 036 * Relative URL's are directed to API base URL. 037 * @param urlStr The url to connect to. 038 * @param progressMonitor progress monitoring and abort handler 039 * @return A reader reading the input stream (servers answer) or <code>null</code>. 040 * @throws OsmTransferException if data transfer errors occur 041 */ 042 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException { 043 return getInputStream(urlStr, progressMonitor, null); 044 } 045 046 /** 047 * Open a connection to the given url and return a reader on the input stream 048 * from that connection. In case of user cancel, return <code>null</code>. 049 * Relative URL's are directed to API base URL. 050 * @param urlStr The url to connect to. 051 * @param progressMonitor progress monitoring and abort handler 052 * @param reason The reason to show on console. Can be {@code null} if no reason is given 053 * @return A reader reading the input stream (servers answer) or <code>null</code>. 054 * @throws OsmTransferException if data transfer errors occur 055 */ 056 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException { 057 try { 058 api.initialize(progressMonitor); 059 String url = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr); 060 return getInputStreamRaw(url, progressMonitor, reason); 061 } finally { 062 progressMonitor.invalidate(); 063 } 064 } 065 066 /** 067 * Return the base URL for relative URL requests 068 * @return base url of API 069 */ 070 protected String getBaseUrl() { 071 return api.getBaseUrl(); 072 } 073 074 /** 075 * Open a connection to the given url and return a reader on the input stream 076 * from that connection. In case of user cancel, return <code>null</code>. 077 * @param urlStr The exact url to connect to. 078 * @param progressMonitor progress monitoring and abort handler 079 * @return An reader reading the input stream (servers answer) or <code>null</code>. 080 * @throws OsmTransferException if data transfer errors occur 081 */ 082 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException { 083 return getInputStreamRaw(urlStr, progressMonitor, null); 084 } 085 086 /** 087 * Open a connection to the given url and return a reader on the input stream 088 * from that connection. In case of user cancel, return <code>null</code>. 089 * @param urlStr The exact url to connect to. 090 * @param progressMonitor progress monitoring and abort handler 091 * @param reason The reason to show on console. Can be {@code null} if no reason is given 092 * @return An reader reading the input stream (servers answer) or <code>null</code>. 093 * @throws OsmTransferException if data transfer errors occur 094 */ 095 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException { 096 return getInputStreamRaw(urlStr, progressMonitor, reason, false); 097 } 098 099 /** 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 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}