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

Last change on this file since 3217 was 3021, checked in by mjulius, 14 years ago

escape space characters in URLs to download

  • Property svn:eol-style set to native
File size: 6.1 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.InputStream;
8import java.io.InputStreamReader;
9import java.net.HttpURLConnection;
10import java.net.MalformedURLException;
11import java.net.URL;
12import java.util.logging.Logger;
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.osm.DataSet;
19import 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 */
30public abstract class OsmServerReader extends OsmConnection {
31 @SuppressWarnings("unused")
32 static private final Logger logger = Logger.getLogger(OsmServerReader.class.getName());
33 private OsmApi api = OsmApi.getOsmApi();
34 private boolean doAuthenticate = false;
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 = api.getBaseUrl() + urlStr;
47 return getInputStreamRaw(urlStr, progressMonitor);
48 } finally {
49 progressMonitor.invalidate();
50 }
51 }
52
53 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
54 try {
55 URL url = null;
56 try {
57 url = new URL(urlStr.replace(" ", "%20"));
58 } catch(MalformedURLException e) {
59 throw new OsmTransferException(e);
60 }
61 try {
62 activeConnection = (HttpURLConnection)url.openConnection();
63 } catch(Exception e) {
64 throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e);
65 }
66 if (cancel) {
67 activeConnection.disconnect();
68 return null;
69 }
70
71 if (doAuthenticate) {
72 addAuth(activeConnection);
73 }
74 if (cancel)
75 throw new OsmTransferCancelledException();
76 if (Main.pref.getBoolean("osm-server.use-compression", true)) {
77 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
78 }
79
80 activeConnection.setConnectTimeout(15000);
81
82 try {
83 System.out.println("GET " + url);
84 activeConnection.connect();
85 } catch (Exception e) {
86 e.printStackTrace();
87 throw new OsmTransferException(tr("Could not connect to the OSM server. Please check your internet connection."), e);
88 }
89 try {
90 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
91 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
92
93 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
94 throw new OsmTransferCancelledException();
95
96 if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
97 String errorHeader = activeConnection.getHeaderField("Error");
98 InputStream i = null;
99 i = activeConnection.getErrorStream();
100 StringBuilder errorBody = new StringBuilder();
101 if (i != null) {
102 BufferedReader in = new BufferedReader(new InputStreamReader(i));
103 String s;
104 while((s = in.readLine()) != null) {
105 errorBody.append(s);
106 errorBody.append("\n");
107 }
108 }
109
110 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());
111 }
112
113 String encoding = activeConnection.getContentEncoding();
114 InputStream inputStream = new ProgressInputStream(activeConnection, progressMonitor);
115 if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
116 inputStream = new GZIPInputStream(inputStream);
117 }
118 else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
119 inputStream = new InflaterInputStream(inputStream, new Inflater(true));
120 }
121 return inputStream;
122 } catch(Exception e) {
123 if (e instanceof OsmTransferException)
124 throw (OsmTransferException)e;
125 else
126 throw new OsmTransferException(e);
127
128 }
129 } finally {
130 progressMonitor.invalidate();
131 }
132 }
133
134 public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException;
135
136 /**
137 * Returns true if this reader is adding authentication credentials to the read
138 * request sent to the server.
139 *
140 * @return true if this reader is adding authentication credentials to the read
141 * request sent to the server
142 */
143 public boolean isDoAuthenticate() {
144 return doAuthenticate;
145 }
146
147 /**
148 * Sets whether this reader adds authentication credentials to the read
149 * request sent to the server.
150 *
151 * @param doAuthenticate true if this reader adds authentication credentials to the read
152 * request sent to the server
153 */
154 public void setDoAuthenticate(boolean doAuthenticate) {
155 this.doAuthenticate = doAuthenticate;
156 }
157}
Note: See TracBrowser for help on using the repository browser.