1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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.InputStream;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.net.HttpURLConnection;
|
---|
10 | import java.net.MalformedURLException;
|
---|
11 | import java.net.URL;
|
---|
12 | import java.util.logging.Logger;
|
---|
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.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * This DataReader reads directly from the REST API of the osm server.
|
---|
23 | *
|
---|
24 | * It supports plain text transfer as well as gzip or deflate encoded transfers;
|
---|
25 | * if compressed transfers are unwanted, set property osm-server.use-compression
|
---|
26 | * to false.
|
---|
27 | *
|
---|
28 | * @author imi
|
---|
29 | */
|
---|
30 | public abstract class OsmServerReader extends OsmConnection {
|
---|
31 | static private final Logger logger = Logger.getLogger(OsmServerReader.class.getName());
|
---|
32 | private OsmApi api = OsmApi.getOsmApi();
|
---|
33 | private boolean doAuthenticate = false;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Open a connection to the given url and return a reader on the input stream
|
---|
37 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
38 | * @param urlStr The exact url to connect to.
|
---|
39 | * @param pleaseWaitDlg
|
---|
40 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
41 | */
|
---|
42 | protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
43 | try {
|
---|
44 | api.initialize(progressMonitor);
|
---|
45 | urlStr = api.getBaseUrl() + urlStr;
|
---|
46 | return getInputStreamRaw(urlStr, progressMonitor);
|
---|
47 | } finally {
|
---|
48 | progressMonitor.invalidate();
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
53 | try {
|
---|
54 | URL url = null;
|
---|
55 | try {
|
---|
56 | url = new URL(urlStr);
|
---|
57 | } catch(MalformedURLException e) {
|
---|
58 | throw new OsmTransferException(e);
|
---|
59 | }
|
---|
60 | try {
|
---|
61 | activeConnection = (HttpURLConnection)url.openConnection();
|
---|
62 | } catch(Exception e) {
|
---|
63 | throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e);
|
---|
64 | }
|
---|
65 | if (cancel) {
|
---|
66 | activeConnection.disconnect();
|
---|
67 | return null;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (doAuthenticate) {
|
---|
71 | addAuth(activeConnection);
|
---|
72 | }
|
---|
73 | if (cancel)
|
---|
74 | throw new OsmTransferCancelledException();
|
---|
75 | if (Main.pref.getBoolean("osm-server.use-compression", true)) {
|
---|
76 | activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
|
---|
77 | }
|
---|
78 |
|
---|
79 | activeConnection.setConnectTimeout(15000);
|
---|
80 |
|
---|
81 | try {
|
---|
82 | System.out.println("GET " + url);
|
---|
83 | activeConnection.connect();
|
---|
84 | } catch (Exception e) {
|
---|
85 | e.printStackTrace();
|
---|
86 | throw new OsmTransferException(tr("Couldn't connect to the OSM server. Please check your internet connection."), e);
|
---|
87 | }
|
---|
88 | try {
|
---|
89 | if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
|
---|
90 | throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
|
---|
91 |
|
---|
92 | if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
|
---|
93 | throw new OsmTransferCancelledException();
|
---|
94 |
|
---|
95 | if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
---|
96 | String errorHeader = activeConnection.getHeaderField("Error");
|
---|
97 | InputStream i = null;
|
---|
98 | i = activeConnection.getErrorStream();
|
---|
99 | StringBuilder errorBody = new StringBuilder();
|
---|
100 | if (i != null) {
|
---|
101 | BufferedReader in = new BufferedReader(new InputStreamReader(i));
|
---|
102 | String s;
|
---|
103 | while((s = in.readLine()) != null) {
|
---|
104 | errorBody.append(s);
|
---|
105 | errorBody.append("\n");
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());
|
---|
110 | }
|
---|
111 |
|
---|
112 | String encoding = activeConnection.getContentEncoding();
|
---|
113 | InputStream inputStream = new ProgressInputStream(activeConnection, progressMonitor);
|
---|
114 | if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
|
---|
115 | inputStream = new GZIPInputStream(inputStream);
|
---|
116 | }
|
---|
117 | else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
|
---|
118 | inputStream = new InflaterInputStream(inputStream, new Inflater(true));
|
---|
119 | }
|
---|
120 | return inputStream;
|
---|
121 | } catch(Exception e) {
|
---|
122 | if (e instanceof OsmTransferException)
|
---|
123 | throw (OsmTransferException)e;
|
---|
124 | else
|
---|
125 | throw new OsmTransferException(e);
|
---|
126 |
|
---|
127 | }
|
---|
128 | } finally {
|
---|
129 | progressMonitor.invalidate();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException;
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Returns true if this reader is adding authentication credentials to the read
|
---|
137 | * request sent to the server.
|
---|
138 | *
|
---|
139 | * @return true if this reader is adding authentication credentials to the read
|
---|
140 | * request sent to the server
|
---|
141 | */
|
---|
142 | public boolean isDoAuthenticate() {
|
---|
143 | return doAuthenticate;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Sets whether this reader adds authentication credentials to the read
|
---|
148 | * request sent to the server.
|
---|
149 | *
|
---|
150 | * @param doAuthenticate true if this reader adds authentication credentials to the read
|
---|
151 | * request sent to the server
|
---|
152 | */
|
---|
153 | public void setDoAuthenticate(boolean doAuthenticate) {
|
---|
154 | this.doAuthenticate = doAuthenticate;
|
---|
155 | }
|
---|
156 | }
|
---|