Changeset 33969 in osm for applications/editors/josm/plugins
- Timestamp:
- 2017-12-31T14:04:30+01:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/sds/src/org/openstreetmap/hot/sds/SdsApi.java
r33870 r33969 13 13 import java.net.ConnectException; 14 14 import java.net.HttpURLConnection; 15 import java.net.MalformedURLException;16 15 import java.net.SocketTimeoutException; 17 16 import java.net.URL; … … 20 19 import java.util.HashMap; 21 20 import java.util.List; 22 import java.util.zip.GZIPInputStream;23 import java.util.zip.Inflater;24 import java.util.zip.InflaterInputStream;25 21 26 22 import org.openstreetmap.josm.Main; 27 23 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 28 import org.openstreetmap.josm.io.OsmApiException;29 import org.openstreetmap.josm.io.OsmTransferCanceledException;30 import org.openstreetmap.josm.io.ProgressInputStream;31 24 import org.openstreetmap.josm.tools.CheckParameterUtil; 32 25 import org.openstreetmap.josm.tools.Logging; … … 447 440 } 448 441 } 449 450 protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws SdsTransferException {451 urlStr = getBaseUrl() + urlStr;452 try {453 URL url = null;454 try {455 url = new URL(urlStr.replace(" ", "%20"));456 } catch (MalformedURLException e) {457 throw new SdsTransferException(e);458 }459 try {460 activeConnection = (HttpURLConnection) url.openConnection();461 } catch (Exception e) {462 throw new SdsTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e);463 }464 if (cancel) {465 activeConnection.disconnect();466 return null;467 }468 469 addAuth(activeConnection);470 471 if (cancel)472 throw new SdsTransferException();473 if (Main.pref.getBoolean("osm-server.use-compression", true)) {474 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");475 }476 477 activeConnection.setConnectTimeout(Main.pref.getInt("socket.timeout.connect", 15)*1000);478 479 try {480 System.out.println("GET " + url);481 activeConnection.connect();482 } catch (Exception e) {483 e.printStackTrace();484 throw new SdsTransferException(tr("Could not connect to the OSM server. Please check your internet connection."), e);485 }486 try {487 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)488 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);489 490 if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)491 throw new OsmTransferCanceledException(tr("Proxy Authentication Required"));492 493 String encoding = activeConnection.getContentEncoding();494 if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {495 String errorHeader = activeConnection.getHeaderField("Error");496 StringBuilder errorBody = new StringBuilder();497 try {498 InputStream i = FixEncoding(activeConnection.getErrorStream(), encoding);499 if (i != null) {500 BufferedReader in = new BufferedReader(new InputStreamReader(i, StandardCharsets.UTF_8));501 String s;502 while ((s = in.readLine()) != null) {503 errorBody.append(s);504 errorBody.append("\n");505 }506 }507 } catch (Exception e) {508 errorBody.append(tr("Reading error text failed."));509 }510 511 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());512 }513 514 return FixEncoding(new ProgressInputStream(activeConnection, progressMonitor), encoding);515 } catch (Exception e) {516 if (e instanceof SdsTransferException)517 throw (SdsTransferException) e;518 else519 throw new SdsTransferException(e);520 521 }522 } finally {523 progressMonitor.invalidate();524 }525 }526 527 private InputStream FixEncoding(InputStream stream, String encoding) throws IOException {528 if (encoding != null && encoding.equalsIgnoreCase("gzip")) {529 stream = new GZIPInputStream(stream);530 } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {531 stream = new InflaterInputStream(stream, new Inflater(true));532 }533 return stream;534 }535 442 }
Note:
See TracChangeset
for help on using the changeset viewer.