Changeset 195 in josm for src/org/openstreetmap/josm/data
- Timestamp:
- 2007-01-14T01:49:03+01:00 (18 years ago)
- Location:
- src/org/openstreetmap/josm/data
- Files:
-
- 2 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 +"/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 }
Note:
See TracChangeset
for help on using the changeset viewer.