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

Last change on this file since 1102 was 1071, checked in by framm, 15 years ago

Support for changeset uploads in 0.6. Error handling is not fully done but basics are working. Full changeset upload is now the default if version is 0.6; set osm-server.atomic-upload=false in your configuration file to go back to individual object upload.

  • Property svn:eol-style set to native
File size: 2.5 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.gui.PleaseWaitDialog;
16
17/**
18 * This DataReader reads directly from the REST API of the osm server.
19 *
20 * It supports plain text transfer as well as gzip or deflate encoded transfers;
21 * if compressed transfers are unwanted, set property osm-server.use-compression
22 * to false.
23 *
24 * @author imi
25 */
26abstract class OsmServerReader extends OsmConnection {
27 /**
28 * Open a connection to the given url and return a reader on the input stream
29 * from that connection. In case of user cancel, return <code>null</code>.
30 * @param urlStr The exact url to connect to.
31 * @param pleaseWaitDlg
32 * @return An reader reading the input stream (servers answer) or <code>null</code>.
33 */
34 protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
35 String version = Main.pref.get("osm-server.version", "0.5");
36 urlStr = Main.pref.get("osm-server.url")+"/"+version+"/" + urlStr;
37 System.out.println("download: "+urlStr);
38 initAuthentication();
39 URL url = new URL(urlStr);
40 activeConnection = (HttpURLConnection)url.openConnection();
41 if (cancel) {
42 activeConnection.disconnect();
43 return null;
44 }
45
46 if (Boolean.parseBoolean(Main.pref.get("osm-server.use-compression", "true")))
47 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
48
49 activeConnection.setConnectTimeout(15000);
50 if (isAuthCancelled() && activeConnection.getResponseCode() == 401)
51 return null;
52 if( activeConnection.getResponseCode() == 500 )
53 {
54 throw new IOException(tr("Server returned internal error. Try a reduced area or retry after waiting some time."));
55 }
56// System.out.println("got return: "+activeConnection.getResponseCode());
57
58 String encoding = activeConnection.getContentEncoding();
59 InputStream inputStream = new ProgressInputStream(activeConnection, pleaseWaitDlg);
60 if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
61 inputStream = new GZIPInputStream(inputStream);
62 }
63 else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
64 inputStream = new InflaterInputStream(inputStream, new Inflater(true));
65 }
66 return inputStream;
67 }
68}
Note: See TracBrowser for help on using the repository browser.