Changeset 195 in josm
- Timestamp:
- 2007-01-14T01:49:03+01:00 (18 years ago)
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/data/Preferences.java
r192 r195 55 55 * Map the property name to the property object. 56 56 */ 57 pr ivatefinal SortedMap<String, String> properties = new TreeMap<String, String>();57 protected final SortedMap<String, String> properties = new TreeMap<String, String>(); 58 58 59 59 /** -
src/org/openstreetmap/josm/data/ServerSidePreferences.java
r172 r195 1 1 package org.openstreetmap.josm.data; 2 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 5 import java.io.BufferedReader; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.io.OutputStreamWriter; 9 import java.io.PrintWriter; 10 import java.io.Reader; 11 import java.io.StringReader; 12 import java.net.HttpURLConnection; 13 import java.net.MalformedURLException; 3 14 import java.net.URL; 4 15 import java.util.Collection; 5 16 import java.util.Collections; 17 import java.util.Map.Entry; 18 19 import javax.swing.JOptionPane; 20 21 import org.openstreetmap.josm.Main; 22 import org.openstreetmap.josm.io.OsmConnection; 23 import org.openstreetmap.josm.io.XmlWriter; 24 import org.openstreetmap.josm.tools.Base64; 25 import org.openstreetmap.josm.tools.XmlObjectParser; 6 26 7 27 /** … … 13 33 public class ServerSidePreferences extends Preferences { 14 34 15 private final URL serverUrl; 16 private final String userName; 35 private final Connection connection; 17 36 18 public ServerSidePreferences(URL serverUrl, String userName) { 19 this.serverUrl = serverUrl; 20 this.userName = userName; 21 load(); 22 } 23 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 24 94 @Override public String getPreferencesDir() { 25 return serverUrl+"/user/"+userName+"/preferences";26 95 return connection.serverUrl.toString(); 96 } 27 97 28 @Override public void load() { 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) { 29 116 resetToDefault(); 30 } 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 } 31 130 32 @Override protected void save() { 33 } 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 } 34 149 35 150 @Override public Collection<Bookmark> loadBookmarks() { 36 151 return Collections.<Bookmark>emptyList(); 37 152 } 38 153 39 154 @Override public void saveBookmarks(Collection<Bookmark> bookmarks) { 40 155 } 41 156 } -
src/org/openstreetmap/josm/gui/MainApplet.java
r172 r195 4 4 5 5 import java.awt.GridBagLayout; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.KeyEvent; 6 8 import java.util.Arrays; 7 9 import java.util.Collection; … … 18 20 19 21 import org.openstreetmap.josm.Main; 22 import org.openstreetmap.josm.actions.JosmAction; 20 23 import org.openstreetmap.josm.data.ServerSidePreferences; 21 24 import org.openstreetmap.josm.tools.GBC; … … 23 26 public class MainApplet extends JApplet { 24 27 25 private final class MainCaller extends Main { 28 public static final class UploadPreferencesAction extends JosmAction { 29 public UploadPreferencesAction() { 30 super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"), KeyEvent.VK_U, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK, true); 31 } 32 public void actionPerformed(ActionEvent e) { 33 ((ServerSidePreferences)Main.pref).upload(); 34 } 35 } 36 37 private final class MainCaller extends Main { 26 38 private MainCaller() { 27 39 setContentPane(contentPane); … … 76 88 77 89 Main.applet = true; 78 Main.pref = new ServerSidePreferences(getCodeBase(), username); 90 Main.pref = new ServerSidePreferences(getCodeBase()); 91 ((ServerSidePreferences)Main.pref).download(username, password); 79 92 80 93 Main.preConstructorInit(args); … … 86 99 // remove offending stuff from JOSM (that would break the SecurityManager) 87 100 m.remove(m.fileMenu); 101 m.editMenu.add(new UploadPreferencesAction()); 88 102 m.open.setEnabled(false); 89 103 m.exit.setEnabled(false); -
src/org/openstreetmap/josm/io/OsmConnection.java
r172 r195 17 17 18 18 import org.openstreetmap.josm.Main; 19 import org.openstreetmap.josm.tools.Base64; 19 20 import org.openstreetmap.josm.tools.GBC; 20 21 … … 126 127 } 127 128 } 129 130 protected void addAuth(HttpURLConnection con) { 131 con.addRequestProperty("Authorization", "Basic "+Base64.encode(Main.pref.get("osm-server.username")+":"+Main.pref.get("osm-server.password"))); 132 } 128 133 } -
src/org/openstreetmap/josm/tools/XmlObjectParser.java
r193 r195 230 230 return XmlObjectParser.this.hasNext(); 231 231 } catch (SAXException e) { 232 e.printStackTrace(); 232 233 throw new RuntimeException(e); 233 234 } … … 237 238 return XmlObjectParser.this.next(); 238 239 } catch (SAXException e) { 240 e.printStackTrace(); 239 241 throw new RuntimeException(e); 240 242 }
Note:
See TracChangeset
for help on using the changeset viewer.