Changeset 195 in josm for src


Ignore:
Timestamp:
2007-01-14T01:49:03+01:00 (17 years ago)
Author:
imi
Message:
  • changed ServerSidePreferences to actually upload / download something to server
Location:
src/org/openstreetmap/josm
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/data/Preferences.java

    r192 r195  
    5555         * Map the property name to the property object.
    5656         */
    57         private final SortedMap<String, String> properties = new TreeMap<String, String>();
     57        protected final SortedMap<String, String> properties = new TreeMap<String, String>();
    5858
    5959        /**
  • src/org/openstreetmap/josm/data/ServerSidePreferences.java

    r172 r195  
    11package org.openstreetmap.josm.data;
    22
     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;
    314import java.net.URL;
    415import java.util.Collection;
    516import 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;
    626
    727/**
     
    1333public class ServerSidePreferences extends Preferences {
    1434
    15         private final URL serverUrl;
    16         private final String userName;
     35        private final Connection connection;
    1736
    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
    2494        @Override public String getPreferencesDir() {
    25             return serverUrl+"/user/"+userName+"/preferences";
    26     }
     95                return connection.serverUrl.toString();
     96        }
    2797
    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) {
    29116                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        }
    31130
    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        }
    34149
    35150        @Override public Collection<Bookmark> loadBookmarks() {
    36151                return Collections.<Bookmark>emptyList();
    37     }
     152        }
    38153
    39154        @Override public void saveBookmarks(Collection<Bookmark> bookmarks) {
    40     }
     155        }
    41156}
  • src/org/openstreetmap/josm/gui/MainApplet.java

    r172 r195  
    44
    55import java.awt.GridBagLayout;
     6import java.awt.event.ActionEvent;
     7import java.awt.event.KeyEvent;
    68import java.util.Arrays;
    79import java.util.Collection;
     
    1820
    1921import org.openstreetmap.josm.Main;
     22import org.openstreetmap.josm.actions.JosmAction;
    2023import org.openstreetmap.josm.data.ServerSidePreferences;
    2124import org.openstreetmap.josm.tools.GBC;
     
    2326public class MainApplet extends JApplet {
    2427
    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 {
    2638                private MainCaller() {
    2739                        setContentPane(contentPane);
     
    7688
    7789                Main.applet = true;
    78                 Main.pref = new ServerSidePreferences(getCodeBase(), username);
     90                Main.pref = new ServerSidePreferences(getCodeBase());
     91                ((ServerSidePreferences)Main.pref).download(username, password);
    7992
    8093                Main.preConstructorInit(args);
     
    8699                // remove offending stuff from JOSM (that would break the SecurityManager)
    87100                m.remove(m.fileMenu);
     101                m.editMenu.add(new UploadPreferencesAction());
    88102                m.open.setEnabled(false);
    89103                m.exit.setEnabled(false);
  • src/org/openstreetmap/josm/io/OsmConnection.java

    r172 r195  
    1717
    1818import org.openstreetmap.josm.Main;
     19import org.openstreetmap.josm.tools.Base64;
    1920import org.openstreetmap.josm.tools.GBC;
    2021
     
    126127                }
    127128        }
     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    }
    128133}
  • src/org/openstreetmap/josm/tools/XmlObjectParser.java

    r193 r195  
    230230                                        return XmlObjectParser.this.hasNext();
    231231                                } catch (SAXException e) {
     232                                        e.printStackTrace();
    232233                                        throw new RuntimeException(e);
    233234                                }
     
    237238                                        return XmlObjectParser.this.next();
    238239                                } catch (SAXException e) {
     240                                        e.printStackTrace();
    239241                                        throw new RuntimeException(e);
    240242                                }
Note: See TracChangeset for help on using the changeset viewer.