1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.net.HttpURLConnection;
|
---|
10 | import java.net.MalformedURLException;
|
---|
11 | import java.net.URL;
|
---|
12 | import java.util.zip.GZIPInputStream;
|
---|
13 | import java.util.zip.Inflater;
|
---|
14 | import java.util.zip.InflaterInputStream;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * This DataReader reads directly from the REST API of the osm server.
|
---|
22 | *
|
---|
23 | * It supports plain text transfer as well as gzip or deflate encoded transfers;
|
---|
24 | * if compressed transfers are unwanted, set property osm-server.use-compression
|
---|
25 | * to false.
|
---|
26 | *
|
---|
27 | * @author imi
|
---|
28 | */
|
---|
29 | public abstract class OsmServerReader extends OsmConnection {
|
---|
30 |
|
---|
31 | private OsmApi api = OsmApi.getOsmApi();
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Open a connection to the given url and return a reader on the input stream
|
---|
35 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
36 | * @param urlStr The exact url to connect to.
|
---|
37 | * @param pleaseWaitDlg
|
---|
38 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
39 | */
|
---|
40 | protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
41 | api.initialize();
|
---|
42 | urlStr = api.getBaseUrl() + urlStr;
|
---|
43 | return getInputStreamRaw(urlStr, progressMonitor);
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
47 | URL url = null;
|
---|
48 | try {
|
---|
49 | url = new URL(urlStr);
|
---|
50 | } catch(MalformedURLException e) {
|
---|
51 | throw new OsmTransferException(e);
|
---|
52 | }
|
---|
53 | try {
|
---|
54 | activeConnection = (HttpURLConnection)url.openConnection();
|
---|
55 | } catch(Exception e) {
|
---|
56 | throw new OsmTransferException(tr("Failed to open connection to API {0}", url.toExternalForm()), e);
|
---|
57 | }
|
---|
58 | if (cancel) {
|
---|
59 | activeConnection.disconnect();
|
---|
60 | return null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (Main.pref.getBoolean("osm-server.use-compression", true)) {
|
---|
64 | activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
|
---|
65 | }
|
---|
66 |
|
---|
67 | activeConnection.setConnectTimeout(15000);
|
---|
68 |
|
---|
69 | try {
|
---|
70 | System.out.println("GET " + url);
|
---|
71 | activeConnection.connect();
|
---|
72 | } catch (Exception e) {
|
---|
73 | throw new OsmTransferException(tr("Couldn't connect to the osm server. Please check your internet connection."), e);
|
---|
74 | }
|
---|
75 | try {
|
---|
76 | if (isAuthCancelled() && activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
|
---|
77 | throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
|
---|
78 |
|
---|
79 | if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
---|
80 | String errorHeader = activeConnection.getHeaderField("Error");
|
---|
81 | InputStream i = null;
|
---|
82 | i = activeConnection.getErrorStream();
|
---|
83 | StringBuilder errorBody = new StringBuilder();
|
---|
84 | if (i != null) {
|
---|
85 | BufferedReader in = new BufferedReader(new InputStreamReader(i));
|
---|
86 | String s;
|
---|
87 | while((s = in.readLine()) != null) {
|
---|
88 | errorBody.append(s);
|
---|
89 | errorBody.append("\n");
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());
|
---|
94 | }
|
---|
95 |
|
---|
96 | String encoding = activeConnection.getContentEncoding();
|
---|
97 | InputStream inputStream = new ProgressInputStream(activeConnection, progressMonitor);
|
---|
98 | if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
|
---|
99 | inputStream = new GZIPInputStream(inputStream);
|
---|
100 | }
|
---|
101 | else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
|
---|
102 | inputStream = new InflaterInputStream(inputStream, new Inflater(true));
|
---|
103 | }
|
---|
104 | return inputStream;
|
---|
105 | } catch(Exception e) {
|
---|
106 | if (e instanceof OsmTransferException)
|
---|
107 | throw (OsmTransferException)e;
|
---|
108 | else
|
---|
109 | throw new OsmTransferException(e);
|
---|
110 |
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException;
|
---|
115 |
|
---|
116 | }
|
---|