[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[18] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
[916] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[18] | 6 | import java.io.IOException;
|
---|
[103] | 7 | import java.io.InputStream;
|
---|
[18] | 8 | import java.net.HttpURLConnection;
|
---|
| 9 | import java.net.URL;
|
---|
[228] | 10 | import java.util.zip.Inflater;
|
---|
| 11 | import java.util.zip.InflaterInputStream;
|
---|
| 12 | import java.util.zip.GZIPInputStream;
|
---|
[18] | 13 |
|
---|
[34] | 14 | import org.openstreetmap.josm.Main;
|
---|
[1146] | 15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[160] | 16 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
[1146] | 17 | import org.xml.sax.SAXException;
|
---|
[18] | 18 |
|
---|
| 19 | /**
|
---|
[228] | 20 | * This DataReader reads directly from the REST API of the osm server.
|
---|
[1169] | 21 | *
|
---|
[228] | 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.
|
---|
[153] | 25 | *
|
---|
[18] | 26 | * @author imi
|
---|
| 27 | */
|
---|
[1146] | 28 | public abstract class OsmServerReader extends OsmConnection {
|
---|
[1169] | 29 | /**
|
---|
| 30 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 31 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 32 | * @param urlStr The exact url to connect to.
|
---|
| 33 | * @param pleaseWaitDlg
|
---|
| 34 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
| 35 | */
|
---|
| 36 | protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
|
---|
| 37 | String version = Main.pref.get("osm-server.version", "0.5");
|
---|
| 38 | urlStr = Main.pref.get("osm-server.url")+"/"+version+"/" + urlStr;
|
---|
[1146] | 39 | return getInputStreamRaw(urlStr, pleaseWaitDlg);
|
---|
| 40 | }
|
---|
[1169] | 41 |
|
---|
[1146] | 42 | protected InputStream getInputStreamRaw(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
|
---|
[228] | 43 |
|
---|
[1353] | 44 | // System.out.println("download: "+urlStr);
|
---|
[1169] | 45 | initAuthentication();
|
---|
| 46 | URL url = new URL(urlStr);
|
---|
| 47 | activeConnection = (HttpURLConnection)url.openConnection();
|
---|
| 48 | if (cancel) {
|
---|
| 49 | activeConnection.disconnect();
|
---|
| 50 | return null;
|
---|
| 51 | }
|
---|
[228] | 52 |
|
---|
[1222] | 53 | if (Main.pref.getBoolean("osm-server.use-compression", true))
|
---|
[1169] | 54 | activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
|
---|
| 55 |
|
---|
| 56 | activeConnection.setConnectTimeout(15000);
|
---|
[1353] | 57 |
|
---|
[1238] | 58 | try {
|
---|
| 59 | activeConnection.connect();
|
---|
| 60 | }
|
---|
| 61 | catch (Exception e) {
|
---|
| 62 | throw new IOException(tr("Couldn't connect to the osm server. Please check your internet connection."));
|
---|
| 63 | }
|
---|
[1353] | 64 |
|
---|
[1169] | 65 | if (isAuthCancelled() && activeConnection.getResponseCode() == 401)
|
---|
| 66 | return null;
|
---|
| 67 | if (activeConnection.getResponseCode() == 500)
|
---|
| 68 | {
|
---|
| 69 | throw new IOException(tr("Server returned internal error. Try a reduced area or retry after waiting some time."));
|
---|
| 70 | }
|
---|
| 71 | // System.out.println("got return: "+activeConnection.getResponseCode());
|
---|
| 72 |
|
---|
| 73 | String encoding = activeConnection.getContentEncoding();
|
---|
| 74 | InputStream inputStream = new ProgressInputStream(activeConnection, pleaseWaitDlg);
|
---|
| 75 | if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
|
---|
| 76 | inputStream = new GZIPInputStream(inputStream);
|
---|
| 77 | }
|
---|
| 78 | else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
|
---|
| 79 | inputStream = new InflaterInputStream(inputStream, new Inflater(true));
|
---|
| 80 | }
|
---|
| 81 | return inputStream;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[1146] | 84 | public abstract DataSet parseOsm() throws SAXException, IOException;
|
---|
[1169] | 85 |
|
---|
[18] | 86 | }
|
---|