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

Last change on this file since 195 was 195, checked in by imi, 17 years ago
  • changed ServerSidePreferences to actually upload / download something to server
File size: 4.8 KB
Line 
1package org.openstreetmap.josm.data;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.io.BufferedReader;
6import java.io.IOException;
7import java.io.InputStreamReader;
8import java.io.OutputStreamWriter;
9import java.io.PrintWriter;
10import java.io.Reader;
11import java.io.StringReader;
12import java.net.HttpURLConnection;
13import java.net.MalformedURLException;
14import java.net.URL;
15import java.util.Collection;
16import java.util.Collections;
17import java.util.Map.Entry;
18
19import javax.swing.JOptionPane;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.io.OsmConnection;
23import org.openstreetmap.josm.io.XmlWriter;
24import org.openstreetmap.josm.tools.Base64;
25import org.openstreetmap.josm.tools.XmlObjectParser;
26
27/**
28 * This class tweak the Preferences class to provide server side preference settings, as example
29 * used in the applet version.
30 *
31 * @author Imi
32 */
33public class ServerSidePreferences extends Preferences {
34
35 private final Connection connection;
36
37 private class Connection extends OsmConnection {
38 URL serverUrl;
39 public Connection(URL serverUrl) {
40 this.serverUrl = serverUrl;
41 }
42 public String download() {
43 try {
44 System.out.println("reading preferenced from "+serverUrl);
45 HttpURLConnection con = (HttpURLConnection)serverUrl.openConnection();
46 addAuth(con);
47 con.connect();
48 BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
49 StringBuilder b = new StringBuilder();
50 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
51 b.append(line);
52 b.append("\n");
53 }
54 con.disconnect();
55 return b.toString();
56 } catch (IOException e) {
57 e.printStackTrace();
58 }
59 return null;
60 }
61 public void upload(String s) {
62 try {
63 URL u = new URL(getPreferencesDir());
64 System.out.println("uplaoding preferences to "+u);
65 HttpURLConnection con = (HttpURLConnection)u.openConnection();
66 con.addRequestProperty("Authorization", "Basic "+Base64.encode(get("osm-server.username")+":"+get("osm-server.password")));
67 con.setRequestMethod("POST");
68 con.setDoOutput(true);
69 con.connect();
70 PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream()));
71 out.println(s);
72 out.close();
73 con.getInputStream().close();
74 con.disconnect();
75 JOptionPane.showMessageDialog(Main.parent, tr("Preferences stored on {0}", u.getHost()));
76 } catch (Exception e) {
77 e.printStackTrace();
78 JOptionPane.showMessageDialog(Main.parent, tr("Could not upload preferences. Reason: {0}", e.getMessage()));
79 }
80 }
81 }
82
83 public ServerSidePreferences(URL serverUrl) {
84 Connection connection = null;
85 try {
86 connection = new Connection(new URL(serverUrl+"/user/preferences"));
87 } catch (MalformedURLException e) {
88 e.printStackTrace();
89 JOptionPane.showMessageDialog(Main.parent, tr("Could not load preferenced from server."));
90 }
91 this.connection = connection;
92 }
93
94 @Override public String getPreferencesDir() {
95 return connection.serverUrl.toString();
96 }
97
98 /**
99 * Do nothing on load. Preferences are loaded with download().
100 */
101 @Override public void load() throws IOException {
102 }
103
104 /**
105 * Do nothing on save. Preferences are uploaded using upload().
106 */
107 @Override protected void save() {
108 }
109
110 public static class Prop {
111 public String key;
112 public String value;
113 }
114
115 public void download(String userName, String password) {
116 resetToDefault();
117 if (!properties.containsKey("osm-server.username") && userName != null)
118 properties.put("osm-server.username", userName);
119 if (!properties.containsKey("osm-server.password") && password != null)
120 properties.put("osm-server.password", password);
121 Reader in = new StringReader(connection.download());
122 try {
123 XmlObjectParser.Uniform<Prop> parser = new XmlObjectParser.Uniform<Prop>(in, "tag", Prop.class);
124 for (Prop p : parser)
125 properties.put(p.key, p.value);
126 } catch (RuntimeException e) {
127 e.printStackTrace();
128 }
129 }
130
131 /**
132 * Use this instead of save() for the ServerSidePreferences, since uploads
133 * are costly while save is called often.
134 *
135 * This is triggered by an explicit menu option.
136 */
137 public void upload() {
138 StringBuilder b = new StringBuilder("<preferences>\n");
139 for (Entry<String, String> p : properties.entrySet()) {
140 b.append("<tag key='");
141 b.append(XmlWriter.encode(p.getKey()));
142 b.append("' value='");
143 b.append(XmlWriter.encode(p.getValue()));
144 b.append("' />\n");
145 }
146 b.append("</preferences>");
147 connection.upload(b.toString());
148 }
149
150 @Override public Collection<Bookmark> loadBookmarks() {
151 return Collections.<Bookmark>emptyList();
152 }
153
154 @Override public void saveBookmarks(Collection<Bookmark> bookmarks) {
155 }
156}
Note: See TracBrowser for help on using the repository browser.