1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.io.OutputStreamWriter;
|
---|
10 | import java.io.PrintWriter;
|
---|
11 | import java.io.Reader;
|
---|
12 | import java.io.StringReader;
|
---|
13 | import java.net.HttpURLConnection;
|
---|
14 | import java.net.MalformedURLException;
|
---|
15 | import java.net.URL;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.Collections;
|
---|
18 | import java.util.LinkedList;
|
---|
19 | import java.util.StringTokenizer;
|
---|
20 | import java.util.Map.Entry;
|
---|
21 |
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.io.OsmConnection;
|
---|
26 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
27 | import org.openstreetmap.josm.tools.Base64;
|
---|
28 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * This class tweak the Preferences class to provide server side preference settings, as example
|
---|
32 | * used in the applet version.
|
---|
33 | *
|
---|
34 | * @author Imi
|
---|
35 | */
|
---|
36 | public class ServerSidePreferences extends Preferences {
|
---|
37 |
|
---|
38 | private final Connection connection;
|
---|
39 |
|
---|
40 | private class Connection extends OsmConnection {
|
---|
41 | URL serverUrl;
|
---|
42 | public Connection(URL serverUrl) {
|
---|
43 | this.serverUrl = serverUrl;
|
---|
44 | }
|
---|
45 | public String download() {
|
---|
46 | try {
|
---|
47 | System.out.println("reading preferences from "+serverUrl);
|
---|
48 | HttpURLConnection con = (HttpURLConnection)serverUrl.openConnection();
|
---|
49 | addAuth(con);
|
---|
50 | con.connect();
|
---|
51 | BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
---|
52 | StringBuilder b = new StringBuilder();
|
---|
53 | for (String line = reader.readLine(); line != null; line = reader.readLine()) {
|
---|
54 | b.append(line);
|
---|
55 | b.append("\n");
|
---|
56 | }
|
---|
57 | con.disconnect();
|
---|
58 | return b.toString();
|
---|
59 | } catch (IOException e) {
|
---|
60 | e.printStackTrace();
|
---|
61 | }
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 | public void upload(String s) {
|
---|
65 | try {
|
---|
66 | URL u = new URL(getPreferencesDir());
|
---|
67 | System.out.println("uplaoding preferences to "+u);
|
---|
68 | HttpURLConnection con = (HttpURLConnection)u.openConnection();
|
---|
69 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(get("osm-server.username")+":"+get("osm-server.password")));
|
---|
70 | con.setRequestMethod("POST");
|
---|
71 | con.setDoOutput(true);
|
---|
72 | con.connect();
|
---|
73 | PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream()));
|
---|
74 | out.println(s);
|
---|
75 | out.close();
|
---|
76 | con.getInputStream().close();
|
---|
77 | con.disconnect();
|
---|
78 | JOptionPane.showMessageDialog(Main.parent, tr("Preferences stored on {0}", u.getHost()));
|
---|
79 | } catch (Exception e) {
|
---|
80 | e.printStackTrace();
|
---|
81 | JOptionPane.showMessageDialog(Main.parent, tr("Could not upload preferences. Reason: {0}", e.getMessage()));
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public ServerSidePreferences(URL serverUrl) {
|
---|
87 | Connection connection = null;
|
---|
88 | try {
|
---|
89 | connection = new Connection(new URL(serverUrl+"user/preferences"));
|
---|
90 | } catch (MalformedURLException e) {
|
---|
91 | e.printStackTrace();
|
---|
92 | JOptionPane.showMessageDialog(Main.parent, tr("Could not load preferences from server."));
|
---|
93 | }
|
---|
94 | this.connection = connection;
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override public String getPreferencesDir() {
|
---|
98 | return connection.serverUrl.toString();
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Do nothing on load. Preferences are loaded with download().
|
---|
103 | */
|
---|
104 | @Override public void load() {
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Do nothing on save. Preferences are uploaded using upload().
|
---|
109 | */
|
---|
110 | @Override protected void save() {
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static class Prop {
|
---|
114 | public String key;
|
---|
115 | public String value;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void download(String userName, String password) {
|
---|
119 | resetToDefault();
|
---|
120 | if (!properties.containsKey("osm-server.username") && userName != null)
|
---|
121 | properties.put("osm-server.username", userName);
|
---|
122 | if (!properties.containsKey("osm-server.password") && password != null)
|
---|
123 | properties.put("osm-server.password", password);
|
---|
124 | Reader in = new StringReader(connection.download());
|
---|
125 | try {
|
---|
126 | XmlObjectParser.Uniform<Prop> parser = new XmlObjectParser.Uniform<Prop>(in, "tag", Prop.class);
|
---|
127 | for (Prop p : parser)
|
---|
128 | properties.put(p.key, p.value);
|
---|
129 | } catch (RuntimeException e) {
|
---|
130 | e.printStackTrace();
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Use this instead of save() for the ServerSidePreferences, since uploads
|
---|
136 | * are costly while save is called often.
|
---|
137 | *
|
---|
138 | * This is triggered by an explicit menu option.
|
---|
139 | */
|
---|
140 | public void upload() {
|
---|
141 | StringBuilder b = new StringBuilder("<preferences>\n");
|
---|
142 | for (Entry<String, String> p : properties.entrySet()) {
|
---|
143 | if (p.getKey().equals("osm-server.password"))
|
---|
144 | continue; // do not upload password. It would get stored in plain!
|
---|
145 | b.append("<tag key='");
|
---|
146 | b.append(XmlWriter.encode(p.getKey()));
|
---|
147 | b.append("' value='");
|
---|
148 | b.append(XmlWriter.encode(p.getValue()));
|
---|
149 | b.append("' />\n");
|
---|
150 | }
|
---|
151 | b.append("</preferences>");
|
---|
152 | connection.upload(b.toString());
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override public Collection<Bookmark> loadBookmarks() {
|
---|
156 | try {
|
---|
157 | Collection<Bookmark> bookmarks;
|
---|
158 | BufferedReader in = new BufferedReader(new InputStreamReader(new URL("http://"+connection.serverUrl.getHost()+"/josm/bookmarks").openStream()));
|
---|
159 | bookmarks = new LinkedList<Bookmark>();
|
---|
160 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
161 | StringTokenizer st = new StringTokenizer(line, ",");
|
---|
162 | if (st.countTokens() < 5)
|
---|
163 | continue;
|
---|
164 | Bookmark b = new Bookmark();
|
---|
165 | b.name = st.nextToken();
|
---|
166 | try {
|
---|
167 | for (int i = 0; i < b.latlon.length; ++i)
|
---|
168 | b.latlon[i] = Double.parseDouble(st.nextToken());
|
---|
169 | bookmarks.add(b);
|
---|
170 | } catch (NumberFormatException x) {
|
---|
171 | // line not parsed
|
---|
172 | }
|
---|
173 | }
|
---|
174 | in.close();
|
---|
175 | return bookmarks;
|
---|
176 | } catch (MalformedURLException e) {
|
---|
177 | e.printStackTrace();
|
---|
178 | } catch (IOException e) {
|
---|
179 | e.printStackTrace();
|
---|
180 | }
|
---|
181 | return Collections.emptyList();
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override public void saveBookmarks(Collection<Bookmark> bookmarks) {
|
---|
185 | }
|
---|
186 | }
|
---|