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

Last change on this file since 5508 was 5494, checked in by Don-vip, 12 years ago

fix #8006 - Download remote marker layer

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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;
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 */
31public abstract class OsmServerReader extends OsmConnection {
32 private OsmApi api = OsmApi.getOsmApi();
33 private boolean doAuthenticate = false;
34 protected boolean gpxParsedProperly;
35
36 /**
37 * Open a connection to the given url and return a reader on the input stream
38 * from that connection. In case of user cancel, return <code>null</code>.
39 * @param urlStr The exact url to connect to.
40 * @param pleaseWaitDlg
41 * @return An reader reading the input stream (servers answer) or <code>null</code>.
42 */
43 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
44 try {
45 api.initialize(progressMonitor);
46 urlStr = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr);
47 return getInputStreamRaw(urlStr, progressMonitor);
48 } finally {
49 progressMonitor.invalidate();
50 }
51 }
52
53 protected String getBaseUrl() {
54 return api.getBaseUrl();
55 }
56
57 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
58 try {
59 URL url = null;
60 try {
61 url = new URL(urlStr.replace(" ", "%20"));
62 } catch(MalformedURLException e) {
63 throw new OsmTransferException(e);
64 }
65 try {
66 activeConnection = (HttpURLConnection)url.openConnection();
67 // fix #7640, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
68 activeConnection.setRequestProperty("Connection", "close");
69 } catch(Exception e) {
70 throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e);
71 }
72 if (cancel) {
73 activeConnection.disconnect();
74 return null;
75 }
76
77 if (doAuthenticate) {
78 addAuth(activeConnection);
79 }
80 if (cancel)
81 throw new OsmTransferCanceledException();
82 if (Main.pref.getBoolean("osm-server.use-compression", true)) {
83 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
84 }
85
86 activeConnection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
87
88 try {
89 System.out.println("GET " + url);
90 activeConnection.connect();
91 } catch (Exception e) {
92 e.printStackTrace();
93 throw new OsmTransferException(tr("Could not connect to the OSM server. Please check your internet connection."), e);
94 }
95 try {
96 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
97 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
98
99 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
100 throw new OsmTransferCanceledException();
101
102 String encoding = activeConnection.getContentEncoding();
103 if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
104 String errorHeader = activeConnection.getHeaderField("Error");
105 StringBuilder errorBody = new StringBuilder();
106 try
107 {
108 InputStream i = FixEncoding(activeConnection.getErrorStream(), encoding);
109 if (i != null) {
110 BufferedReader in = new BufferedReader(new InputStreamReader(i));
111 String s;
112 while((s = in.readLine()) != null) {
113 errorBody.append(s);
114 errorBody.append("\n");
115 }
116 }
117 }
118 catch(Exception e) {
119 errorBody.append(tr("Reading error text failed."));
120 }
121
122 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());
123 }
124
125 return FixEncoding(new ProgressInputStream(activeConnection, progressMonitor), encoding);
126 } catch(Exception e) {
127 if (e instanceof OsmTransferException)
128 throw (OsmTransferException)e;
129 else
130 throw new OsmTransferException(e);
131
132 }
133 } finally {
134 progressMonitor.invalidate();
135 }
136 }
137
138 private InputStream FixEncoding(InputStream stream, String encoding) throws IOException
139 {
140 if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
141 stream = new GZIPInputStream(stream);
142 }
143 else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
144 stream = new InflaterInputStream(stream, new Inflater(true));
145 }
146 return stream;
147 }
148
149 public abstract DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException;
150
151 public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
152 return null;
153 }
154
155 public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
156 return null;
157 }
158
159 public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
160 return null;
161 }
162
163 public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
164 return null;
165 }
166
167 public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
168 return null;
169 }
170
171 public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
172 return null;
173 }
174
175 /**
176 * Returns true if this reader is adding authentication credentials to the read
177 * request sent to the server.
178 *
179 * @return true if this reader is adding authentication credentials to the read
180 * request sent to the server
181 */
182 public boolean isDoAuthenticate() {
183 return doAuthenticate;
184 }
185
186 /**
187 * Sets whether this reader adds authentication credentials to the read
188 * request sent to the server.
189 *
190 * @param doAuthenticate true if this reader adds authentication credentials to the read
191 * request sent to the server
192 */
193 public void setDoAuthenticate(boolean doAuthenticate) {
194 this.doAuthenticate = doAuthenticate;
195 }
196
197 /**
198 * Determines if the GPX data has been parsed properly.
199 * @return true if the GPX data has been parsed properly, false otherwise
200 * @see GpxReader#parse
201 */
202 public final boolean isGpxParsedProperly() {
203 return gpxParsedProperly;
204 }
205}
Note: See TracBrowser for help on using the repository browser.