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

Last change on this file since 729 was 655, checked in by ramack, 16 years ago

patch by bruce89, closes #812; thanks bruce

  • Property svn:eol-style set to native
File size: 2.3 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import java.io.IOException;
5import java.io.InputStream;
6import java.net.HttpURLConnection;
7import java.net.URL;
8import java.util.zip.Inflater;
9import java.util.zip.InflaterInputStream;
10import java.util.zip.GZIPInputStream;
11
12import org.openstreetmap.josm.Main;
13import 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 */
24abstract 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}
Note: See TracBrowser for help on using the repository browser.