source: josm/trunk/src/org/openstreetmap/josm/data/ServerSidePreferences.java@ 2180

Last change on this file since 2180 was 2017, checked in by Gubaer, 15 years ago

removed OptionPaneUtil
cleanup of deprecated Layer API
cleanup of deprecated APIs in OsmPrimitive and Way
cleanup of imports

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