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

Last change on this file since 1388 was 1353, checked in by stoecker, 15 years ago

apply patch by xeen - #2114

  • Property svn:eol-style set to native
File size: 3.3 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.IOException;
7import java.io.InputStream;
8import java.net.HttpURLConnection;
9import java.net.URL;
10import java.util.zip.Inflater;
11import java.util.zip.InflaterInputStream;
12import java.util.zip.GZIPInputStream;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.gui.PleaseWaitDialog;
17import org.xml.sax.SAXException;
18
19/**
20 * This DataReader reads directly from the REST API of the osm server.
21 *
22 * It supports plain text transfer as well as gzip or deflate encoded transfers;
23 * if compressed transfers are unwanted, set property osm-server.use-compression
24 * to false.
25 *
26 * @author imi
27 */
28public abstract class OsmServerReader extends OsmConnection {
29 /**
30 * Open a connection to the given url and return a reader on the input stream
31 * from that connection. In case of user cancel, return <code>null</code>.
32 * @param urlStr The exact url to connect to.
33 * @param pleaseWaitDlg
34 * @return An reader reading the input stream (servers answer) or <code>null</code>.
35 */
36 protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
37 String version = Main.pref.get("osm-server.version", "0.5");
38 urlStr = Main.pref.get("osm-server.url")+"/"+version+"/" + urlStr;
39 return getInputStreamRaw(urlStr, pleaseWaitDlg);
40 }
41
42 protected InputStream getInputStreamRaw(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
43
44// System.out.println("download: "+urlStr);
45 initAuthentication();
46 URL url = new URL(urlStr);
47 activeConnection = (HttpURLConnection)url.openConnection();
48 if (cancel) {
49 activeConnection.disconnect();
50 return null;
51 }
52
53 if (Main.pref.getBoolean("osm-server.use-compression", true))
54 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
55
56 activeConnection.setConnectTimeout(15000);
57
58 try {
59 activeConnection.connect();
60 }
61 catch (Exception e) {
62 throw new IOException(tr("Couldn't connect to the osm server. Please check your internet connection."));
63 }
64
65 if (isAuthCancelled() && activeConnection.getResponseCode() == 401)
66 return null;
67 if (activeConnection.getResponseCode() == 500)
68 {
69 throw new IOException(tr("Server returned internal error. Try a reduced area or retry after waiting some time."));
70 }
71// System.out.println("got return: "+activeConnection.getResponseCode());
72
73 String encoding = activeConnection.getContentEncoding();
74 InputStream inputStream = new ProgressInputStream(activeConnection, pleaseWaitDlg);
75 if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
76 inputStream = new GZIPInputStream(inputStream);
77 }
78 else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
79 inputStream = new InflaterInputStream(inputStream, new Inflater(true));
80 }
81 return inputStream;
82 }
83
84 public abstract DataSet parseOsm() throws SAXException, IOException;
85
86}
Note: See TracBrowser for help on using the repository browser.