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