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.net.URLConnection;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.Collections;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.StringTokenizer;
|
---|
21 | import java.util.Map.Entry;
|
---|
22 | //import java.util.logging.Logger;
|
---|
23 |
|
---|
24 | import javax.swing.JOptionPane;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.io.OsmConnection;
|
---|
28 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
29 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
30 | import org.openstreetmap.josm.tools.Base64;
|
---|
31 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * This class tweak the Preferences class to provide server side preference settings, as example
|
---|
35 | * used in the applet version.
|
---|
36 | *
|
---|
37 | * @author Imi
|
---|
38 | */
|
---|
39 | public class ServerSidePreferences extends Preferences {
|
---|
40 | //static private final Logger logger = Logger.getLogger(ServerSidePreferences.class.getName());
|
---|
41 |
|
---|
42 | public class MissingPassword extends Exception{
|
---|
43 | public String realm;
|
---|
44 | public MissingPassword(String r) {
|
---|
45 | realm = r;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private final Connection connection;
|
---|
50 |
|
---|
51 | private class Connection extends OsmConnection {
|
---|
52 | URL serverUrl;
|
---|
53 | public Connection(URL serverUrl) {
|
---|
54 | this.serverUrl = serverUrl;
|
---|
55 | }
|
---|
56 | public String download() throws MissingPassword {
|
---|
57 | try {
|
---|
58 | System.out.println("reading preferences from "+serverUrl);
|
---|
59 | URLConnection con = serverUrl.openConnection();
|
---|
60 | String username = get("applet.username");
|
---|
61 | String password = get("applet.password");
|
---|
62 | if(password.isEmpty() && username.isEmpty())
|
---|
63 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(username+":"+password));
|
---|
64 | con.connect();
|
---|
65 | if(username.isEmpty() && con instanceof HttpURLConnection
|
---|
66 | && ((HttpURLConnection) con).getResponseCode()
|
---|
67 | == HttpURLConnection.HTTP_UNAUTHORIZED) {
|
---|
68 | String t = ((HttpURLConnection) con).getHeaderField("WWW-Authenticate");
|
---|
69 | t = t.replace("Basic realm=\"","").replace("\"","");
|
---|
70 | throw new MissingPassword(t);
|
---|
71 | }
|
---|
72 | BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
---|
73 | StringBuilder b = new StringBuilder();
|
---|
74 | for (String line = reader.readLine(); line != null; line = reader.readLine()) {
|
---|
75 | b.append(line);
|
---|
76 | b.append("\n");
|
---|
77 | }
|
---|
78 | if (con instanceof HttpURLConnection) {
|
---|
79 | ((HttpURLConnection) con).disconnect();
|
---|
80 | }
|
---|
81 | return b.toString();
|
---|
82 | } catch (IOException e) {
|
---|
83 | e.printStackTrace();
|
---|
84 | }
|
---|
85 | return null;
|
---|
86 | }
|
---|
87 | public void upload(String s) {
|
---|
88 | try {
|
---|
89 | URL u = new URL(getPreferencesDir());
|
---|
90 | System.out.println("uploading preferences to "+u);
|
---|
91 | HttpURLConnection con = (HttpURLConnection)u.openConnection();
|
---|
92 | String username = get("applet.username");
|
---|
93 | String password = get("applet.password");
|
---|
94 | if(password.isEmpty() && username.isEmpty())
|
---|
95 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(username+":"+password));
|
---|
96 | con.setRequestMethod("POST");
|
---|
97 | con.setDoOutput(true);
|
---|
98 | con.connect();
|
---|
99 | PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream()));
|
---|
100 | out.println(s);
|
---|
101 | out.close();
|
---|
102 | con.getInputStream().close();
|
---|
103 | con.disconnect();
|
---|
104 | JOptionPane.showMessageDialog(
|
---|
105 | Main.parent,
|
---|
106 | tr("Preferences stored on {0}", u.getHost()),
|
---|
107 | tr("Information"),
|
---|
108 | JOptionPane.INFORMATION_MESSAGE
|
---|
109 | );
|
---|
110 | } catch (Exception e) {
|
---|
111 | e.printStackTrace();
|
---|
112 | JOptionPane.showMessageDialog(
|
---|
113 | Main.parent,
|
---|
114 | tr("Could not upload preferences. Reason: {0}", e.getMessage()),
|
---|
115 | tr("Error"),
|
---|
116 | JOptionPane.ERROR_MESSAGE
|
---|
117 | );
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public ServerSidePreferences(URL serverUrl) {
|
---|
123 | Connection connection = null;
|
---|
124 | try {
|
---|
125 | connection = new Connection(new URL(serverUrl+"user/preferences"));
|
---|
126 | } catch (MalformedURLException e) {
|
---|
127 | e.printStackTrace();
|
---|
128 | JOptionPane.showMessageDialog(
|
---|
129 | Main.parent,
|
---|
130 | tr("Could not load preferences from server."),
|
---|
131 | tr("Error"),
|
---|
132 | JOptionPane.ERROR_MESSAGE
|
---|
133 | );
|
---|
134 | }
|
---|
135 | this.connection = connection;
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override public String getPreferencesDir() {
|
---|
139 | return connection.serverUrl.toString();
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Do nothing on load. Preferences are loaded with download().
|
---|
144 | */
|
---|
145 | @Override public void load() {
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Do nothing on save. Preferences are uploaded using upload().
|
---|
150 | */
|
---|
151 | @Override public void save() {
|
---|
152 | }
|
---|
153 |
|
---|
154 | public static class Prop {
|
---|
155 | public String key;
|
---|
156 | public String value;
|
---|
157 | }
|
---|
158 |
|
---|
159 | public void download(String userName, String password) {
|
---|
160 | if (!properties.containsKey("applet.username") && userName != null) {
|
---|
161 | properties.put("applet.username", userName);
|
---|
162 | }
|
---|
163 | if (!properties.containsKey("applet.password") && password != null) {
|
---|
164 | properties.put("applet.password", password);
|
---|
165 | }
|
---|
166 | try {
|
---|
167 | download();
|
---|
168 | } catch (MissingPassword e) {
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | public boolean download() throws MissingPassword {
|
---|
173 | resetToDefault();
|
---|
174 | String cont = connection.download();
|
---|
175 | if (cont == null) return false;
|
---|
176 | Reader in = new StringReader(cont);
|
---|
177 | boolean res = false;
|
---|
178 | try {
|
---|
179 | /* TODO: parse collection! */
|
---|
180 | XmlObjectParser.Uniform<Prop> parser = new XmlObjectParser.Uniform<Prop>(in, "tag", Prop.class);
|
---|
181 | for (Prop p : parser) {
|
---|
182 | res = true;
|
---|
183 | properties.put(p.key, p.value);
|
---|
184 | }
|
---|
185 | } catch (RuntimeException e) {
|
---|
186 | e.printStackTrace();
|
---|
187 | }
|
---|
188 | return res;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Use this instead of save() for the ServerSidePreferences, since uploads
|
---|
193 | * are costly while save is called often.
|
---|
194 | *
|
---|
195 | * This is triggered by an explicit menu option.
|
---|
196 | */
|
---|
197 | public void upload() {
|
---|
198 | StringBuilder b = new StringBuilder("<preferences>\n");
|
---|
199 | for (Entry<String, String> p : properties.entrySet()) {
|
---|
200 | if (p.getKey().equals("osm-server.password")) {
|
---|
201 | continue; // do not upload password. It would get stored in plain!
|
---|
202 | }
|
---|
203 | String r = p.getValue();
|
---|
204 | if(r.contains("\u001e"))
|
---|
205 | {
|
---|
206 | b.append("<collection key='");
|
---|
207 | b.append(XmlWriter.encode(p.getKey()));
|
---|
208 | b.append(">\n");
|
---|
209 | for (String val : r.split("\u001e", -1))
|
---|
210 | {
|
---|
211 | b.append(" <entry value='");
|
---|
212 | b.append(XmlWriter.encode(val));
|
---|
213 | b.append("' />\n");
|
---|
214 | }
|
---|
215 | }
|
---|
216 | else
|
---|
217 | {
|
---|
218 | b.append("<tag key='");
|
---|
219 | b.append(XmlWriter.encode(p.getKey()));
|
---|
220 | b.append("' value='");
|
---|
221 | b.append(XmlWriter.encode(p.getValue()));
|
---|
222 | b.append("' />\n");
|
---|
223 | }
|
---|
224 | }
|
---|
225 | b.append("</preferences>");
|
---|
226 | connection.upload(b.toString());
|
---|
227 | }
|
---|
228 | }
|
---|