Changeset 9172 in josm for trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
- Timestamp:
- 2015-12-26T23:42:03+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r9078 r9172 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.BufferedReader;7 import java.io.IOException;8 6 import java.io.InputStream; 9 import java.io.InputStreamReader;10 7 import java.net.HttpURLConnection; 11 8 import java.net.MalformedURLException; 12 9 import java.net.URL; 13 import java.nio.charset.StandardCharsets;14 10 import java.util.List; 15 import java.util.Map;16 import java.util.zip.GZIPInputStream;17 import java.util.zip.Inflater;18 import java.util.zip.InflaterInputStream;19 11 20 12 import org.openstreetmap.josm.Main; … … 23 15 import org.openstreetmap.josm.data.osm.DataSet; 24 16 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 25 import org.openstreetmap.josm.tools. Utils;17 import org.openstreetmap.josm.tools.HttpClient; 26 18 27 19 /** … … 129 121 throw new OsmTransferException(e); 130 122 } 131 try { 132 // fix #7640, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive 133 activeConnection = Utils.openHttpConnection(url, false); 134 } catch (Exception e) { 135 throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e); 136 } 137 Utils.setupURLConnection(activeConnection); 138 if (cancel) { 139 activeConnection.disconnect(); 140 return null; 141 } 142 123 124 final HttpClient client = HttpClient.create(url); 125 client.setReasonForRequest(reason); 143 126 if (doAuthenticate) { 144 addAuth( activeConnection);127 addAuth(client); 145 128 } 146 129 if (cancel) 147 130 throw new OsmTransferCanceledException("Operation canceled"); 148 if (Main.pref.getBoolean("osm-server.use-compression", true)) {149 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");150 }151 131 152 132 try { 153 if (reason != null && !reason.isEmpty()) { 154 Main.info("GET " + url + " (" + reason + ')'); 155 } else { 156 Main.info("GET " + url); 157 } 158 activeConnection.connect(); 133 activeConnection = client.connect(); 159 134 } catch (Exception e) { 160 135 Main.error(e); … … 165 140 } 166 141 try { 167 if (Main.isDebugEnabled()) {168 Main.debug("RESPONSE: "+activeConnection.getHeaderFields());169 }170 142 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 171 143 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null); … … 174 146 throw new OsmTransferCanceledException("Proxy Authentication Required"); 175 147 176 String encoding = activeConnection.getContentEncoding();177 148 if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { 178 149 String errorHeader = activeConnection.getHeaderField("Error"); 179 StringBuilder errorBody = new StringBuilder(); 180 try { 181 InputStream i = fixEncoding(activeConnection.getErrorStream(), encoding); 182 if (i != null) { 183 BufferedReader in = new BufferedReader(new InputStreamReader(i, StandardCharsets.UTF_8)); 184 String s; 185 while ((s = in.readLine()) != null) { 186 errorBody.append(s); 187 errorBody.append('\n'); 188 } 189 } 190 } catch (Exception e) { 191 errorBody.append(tr("Reading error text failed.")); 192 } 193 194 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString(), url.toString()); 150 final String errorBody = activeConnection.fetchContent(); 151 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody, url.toString()); 195 152 } 196 153 197 154 InputStream in = new ProgressInputStream(activeConnection, progressMonitor); 198 155 if (uncompressAccordingToContentDisposition) { 199 in = uncompressAccordingToContentDisposition(in, activeConnection.getHeaderFields());156 activeConnection.uncompressAccordingToContentDisposition(true); 200 157 } 201 return fixEncoding(in, encoding);158 return in; 202 159 } catch (OsmTransferException e) { 203 160 throw e; … … 210 167 } 211 168 212 private static InputStream fixEncoding(InputStream stream, String encoding) throws IOException {213 if ("gzip".equalsIgnoreCase(encoding)) {214 stream = new GZIPInputStream(stream);215 } else if ("deflate".equalsIgnoreCase(encoding)) {216 stream = new InflaterInputStream(stream, new Inflater(true));217 }218 return stream;219 }220 221 private InputStream uncompressAccordingToContentDisposition(InputStream stream, Map<String, List<String>> headerFields) throws IOException {222 List<String> field = headerFields.get("Content-Disposition");223 if (field != null && field.toString().contains(".gz\"")) {224 return Compression.GZIP.getUncompressedInputStream(stream);225 } else if (field != null && field.toString().contains(".bz2\"")) {226 return Compression.BZIP2.getUncompressedInputStream(stream);227 } else {228 return stream;229 }230 }231 232 169 /** 233 170 * Download OSM files from somewhere
Note:
See TracChangeset
for help on using the changeset viewer.