1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.net.HttpURLConnection;
|
---|
7 | import java.net.URL;
|
---|
8 | import java.util.zip.Inflater;
|
---|
9 | import java.util.zip.InflaterInputStream;
|
---|
10 | import java.util.zip.GZIPInputStream;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This DataReader reads directly from the REST API of the osm server.
|
---|
17 | *
|
---|
18 | * It supports plain text transfer as well as gzip or deflate encoded transfers;
|
---|
19 | * if compressed transfers are unwanted, set property osm-server.use-compression
|
---|
20 | * to false.
|
---|
21 | *
|
---|
22 | * @author imi
|
---|
23 | */
|
---|
24 | abstract class OsmServerReader extends OsmConnection {
|
---|
25 | /**
|
---|
26 | * Open a connection to the given url and return a reader on the input stream
|
---|
27 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
28 | * @param urlStr The exact url to connect to.
|
---|
29 | * @param pleaseWaitDlg
|
---|
30 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
31 | */
|
---|
32 | protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
|
---|
33 | String version = Main.pref.get("osm-server.version", "0.5");
|
---|
34 | urlStr = Main.pref.get("osm-server.url")+"/"+version+"/" + urlStr;
|
---|
35 | System.out.println("download: "+urlStr);
|
---|
36 | initAuthentication();
|
---|
37 | URL url = new URL(urlStr);
|
---|
38 | activeConnection = (HttpURLConnection)url.openConnection();
|
---|
39 | if (cancel) {
|
---|
40 | activeConnection.disconnect();
|
---|
41 | return null;
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (Boolean.parseBoolean(Main.pref.get("osm-server.use-compression", "true")))
|
---|
45 | activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
|
---|
46 |
|
---|
47 | System.out.println("got return: "+activeConnection.getResponseCode());
|
---|
48 | activeConnection.setConnectTimeout(15000);
|
---|
49 | if (isAuthCancelled() && activeConnection.getResponseCode() == 401)
|
---|
50 | return null;
|
---|
51 |
|
---|
52 | String encoding = activeConnection.getContentEncoding();
|
---|
53 | InputStream inputStream = new ProgressInputStream(activeConnection, pleaseWaitDlg);
|
---|
54 | if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
|
---|
55 | inputStream = new GZIPInputStream(inputStream);
|
---|
56 | }
|
---|
57 | else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
|
---|
58 | inputStream = new InflaterInputStream(inputStream, new Inflater(true));
|
---|
59 | }
|
---|
60 | return inputStream;
|
---|
61 | }
|
---|
62 | }
|
---|