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

Last change on this file since 2474 was 2181, checked in by stoecker, 15 years ago

lots of i18n fixes

  • Property svn:eol-style set to native
File size: 6.0 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.nio.charset.CharacterCodingException;
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
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 try {
71 if (doAuthenticate) {
72 addAuth(activeConnection);
73 }
74 } catch(CharacterCodingException e) {
75 System.err.println(tr("Error: failed to add authentication credentials to the connection."));
76 throw new OsmTransferException(e);
77 }
78 if (Main.pref.getBoolean("osm-server.use-compression", true)) {
79 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
80 }
81
82 activeConnection.setConnectTimeout(15000);
83
84 try {
85 System.out.println("GET " + url);
86 activeConnection.connect();
87 } catch (Exception e) {
88 throw new OsmTransferException(tr("Couldn't connect to the OSM server. Please check your internet connection."), e);
89 }
90 try {
91 if (isAuthCancelled() && activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
92 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
93
94 if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
95 String errorHeader = activeConnection.getHeaderField("Error");
96 InputStream i = null;
97 i = activeConnection.getErrorStream();
98 StringBuilder errorBody = new StringBuilder();
99 if (i != null) {
100 BufferedReader in = new BufferedReader(new InputStreamReader(i));
101 String s;
102 while((s = in.readLine()) != null) {
103 errorBody.append(s);
104 errorBody.append("\n");
105 }
106 }
107
108 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());
109 }
110
111 String encoding = activeConnection.getContentEncoding();
112 InputStream inputStream = new ProgressInputStream(activeConnection, progressMonitor);
113 if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
114 inputStream = new GZIPInputStream(inputStream);
115 }
116 else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
117 inputStream = new InflaterInputStream(inputStream, new Inflater(true));
118 }
119 return inputStream;
120 } catch(Exception e) {
121 if (e instanceof OsmTransferException)
122 throw (OsmTransferException)e;
123 else
124 throw new OsmTransferException(e);
125
126 }
127 } finally {
128 progressMonitor.invalidate();
129 }
130 }
131
132 public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException;
133
134 /**
135 * Returns true if this reader is adding authentication credentials to the read
136 * request sent to the server.
137 *
138 * @return true if this reader is adding authentication credentials to the read
139 * request sent to the server
140 */
141 public boolean isDoAuthenticate() {
142 return doAuthenticate;
143 }
144
145 /**
146 * Sets whether this reader adds authentication credentials to the read
147 * request sent to the server.
148 *
149 * @param doAuthenticate true if this reader adds authentication credentials to the read
150 * request sent to the server
151 */
152 public void setDoAuthenticate(boolean doAuthenticate) {
153 this.doAuthenticate = doAuthenticate;
154 }
155}
Note: See TracBrowser for help on using the repository browser.