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