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 | private final Connection connection;
|
---|
43 |
|
---|
44 | private class Connection extends OsmConnection {
|
---|
45 | URL serverUrl;
|
---|
46 | public Connection(URL serverUrl) {
|
---|
47 | this.serverUrl = serverUrl;
|
---|
48 | }
|
---|
49 | public String download() {
|
---|
50 | try {
|
---|
51 | System.out.println("reading preferences from "+serverUrl);
|
---|
52 | URLConnection con = serverUrl.openConnection();
|
---|
53 | if (con instanceof HttpURLConnection) {
|
---|
54 | addAuth((HttpURLConnection) con);
|
---|
55 | }
|
---|
56 | con.connect();
|
---|
57 | BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
---|
58 | StringBuilder b = new StringBuilder();
|
---|
59 | for (String line = reader.readLine(); line != null; line = reader.readLine()) {
|
---|
60 | b.append(line);
|
---|
61 | b.append("\n");
|
---|
62 | }
|
---|
63 | if (con instanceof HttpURLConnection) {
|
---|
64 | ((HttpURLConnection) con).disconnect();
|
---|
65 | }
|
---|
66 | return b.toString();
|
---|
67 | } catch (IOException e) {
|
---|
68 | e.printStackTrace();
|
---|
69 | } catch(OsmTransferException e) {
|
---|
70 | e.printStackTrace();
|
---|
71 | }
|
---|
72 | return null;
|
---|
73 | }
|
---|
74 | public void upload(String s) {
|
---|
75 | try {
|
---|
76 | URL u = new URL(getPreferencesDir());
|
---|
77 | System.out.println("uploading preferences to "+u);
|
---|
78 | HttpURLConnection con = (HttpURLConnection)u.openConnection();
|
---|
79 | // FIXME:
|
---|
80 | // - doesn't work if CredentialManager isn't JosmPreferencesCredentialManager
|
---|
81 | // - doesn't work for OAuth
|
---|
82 |
|
---|
83 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(get("osm-server.username")+":"+get("osm-server.password")));
|
---|
84 | con.setRequestMethod("POST");
|
---|
85 | con.setDoOutput(true);
|
---|
86 | con.connect();
|
---|
87 | PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream()));
|
---|
88 | out.println(s);
|
---|
89 | out.close();
|
---|
90 | con.getInputStream().close();
|
---|
91 | con.disconnect();
|
---|
92 | JOptionPane.showMessageDialog(
|
---|
93 | Main.parent,
|
---|
94 | tr("Preferences stored on {0}", u.getHost()),
|
---|
95 | tr("Information"),
|
---|
96 | JOptionPane.INFORMATION_MESSAGE
|
---|
97 | );
|
---|
98 | } catch (Exception e) {
|
---|
99 | e.printStackTrace();
|
---|
100 | JOptionPane.showMessageDialog(
|
---|
101 | Main.parent,
|
---|
102 | tr("Could not upload preferences. Reason: {0}", e.getMessage()),
|
---|
103 | tr("Error"),
|
---|
104 | JOptionPane.ERROR_MESSAGE
|
---|
105 | );
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public ServerSidePreferences(URL serverUrl) {
|
---|
111 | Connection connection = null;
|
---|
112 | try {
|
---|
113 | connection = new Connection(new URL(serverUrl+"user/preferences"));
|
---|
114 | } catch (MalformedURLException e) {
|
---|
115 | e.printStackTrace();
|
---|
116 | JOptionPane.showMessageDialog(
|
---|
117 | Main.parent,
|
---|
118 | tr("Could not load preferences from server."),
|
---|
119 | tr("Error"),
|
---|
120 | JOptionPane.ERROR_MESSAGE
|
---|
121 | );
|
---|
122 | }
|
---|
123 | this.connection = connection;
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override public String getPreferencesDir() {
|
---|
127 | return connection.serverUrl.toString();
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Do nothing on load. Preferences are loaded with download().
|
---|
132 | */
|
---|
133 | @Override public void load() {
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Do nothing on save. Preferences are uploaded using upload().
|
---|
138 | */
|
---|
139 | @Override public void save() {
|
---|
140 | }
|
---|
141 |
|
---|
142 | public static class Prop {
|
---|
143 | public String key;
|
---|
144 | public String value;
|
---|
145 | }
|
---|
146 |
|
---|
147 | public void download(String userName, String password) {
|
---|
148 | if (!properties.containsKey("osm-server.username") && userName != null) {
|
---|
149 | properties.put("osm-server.username", userName);
|
---|
150 | }
|
---|
151 | if (!properties.containsKey("osm-server.password") && password != null) {
|
---|
152 | properties.put("osm-server.password", password);
|
---|
153 | }
|
---|
154 | download();
|
---|
155 | }
|
---|
156 |
|
---|
157 | public boolean download() {
|
---|
158 | resetToDefault();
|
---|
159 | String cont = connection.download();
|
---|
160 | if (cont == null) return false;
|
---|
161 | Reader in = new StringReader(cont);
|
---|
162 | boolean res = false;
|
---|
163 | try {
|
---|
164 | XmlObjectParser.Uniform<Prop> parser = new XmlObjectParser.Uniform<Prop>(in, "tag", Prop.class);
|
---|
165 | for (Prop p : parser) {
|
---|
166 | res = true;
|
---|
167 | properties.put(p.key, p.value);
|
---|
168 | }
|
---|
169 | } catch (RuntimeException e) {
|
---|
170 | e.printStackTrace();
|
---|
171 | }
|
---|
172 | return res;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Use this instead of save() for the ServerSidePreferences, since uploads
|
---|
177 | * are costly while save is called often.
|
---|
178 | *
|
---|
179 | * This is triggered by an explicit menu option.
|
---|
180 | */
|
---|
181 | public void upload() {
|
---|
182 | StringBuilder b = new StringBuilder("<preferences>\n");
|
---|
183 | for (Entry<String, String> p : properties.entrySet()) {
|
---|
184 | if (p.getKey().equals("osm-server.password")) {
|
---|
185 | continue; // do not upload password. It would get stored in plain!
|
---|
186 | }
|
---|
187 | b.append("<tag key='");
|
---|
188 | b.append(XmlWriter.encode(p.getKey()));
|
---|
189 | b.append("' value='");
|
---|
190 | b.append(XmlWriter.encode(p.getValue()));
|
---|
191 | b.append("' />\n");
|
---|
192 | }
|
---|
193 | b.append("</preferences>");
|
---|
194 | connection.upload(b.toString());
|
---|
195 | }
|
---|
196 | }
|
---|