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.MalformedURLException;
|
---|
15 | import java.net.URL;
|
---|
16 | import java.net.URLConnection;
|
---|
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.OsmTransferException;
|
---|
28 | import org.openstreetmap.josm.tools.Base64;
|
---|
29 | import org.xml.sax.SAXException;
|
---|
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 | public class MissingPassword extends Exception{
|
---|
39 | public String realm;
|
---|
40 | public MissingPassword(String r) {
|
---|
41 | realm = r;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private final Connection connection;
|
---|
46 |
|
---|
47 | private class Connection extends OsmConnection {
|
---|
48 | URL serverUrl;
|
---|
49 | public Connection(URL serverUrl) {
|
---|
50 | this.serverUrl = serverUrl;
|
---|
51 | }
|
---|
52 | public String download() throws MissingPassword {
|
---|
53 | try {
|
---|
54 | System.out.println("reading preferences from "+serverUrl);
|
---|
55 | URLConnection con = serverUrl.openConnection();
|
---|
56 | String username = get("applet.username");
|
---|
57 | String password = get("applet.password");
|
---|
58 | if(password.isEmpty() && username.isEmpty())
|
---|
59 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(username+":"+password));
|
---|
60 | con.connect();
|
---|
61 | if(username.isEmpty() && con instanceof HttpURLConnection
|
---|
62 | && ((HttpURLConnection) con).getResponseCode()
|
---|
63 | == HttpURLConnection.HTTP_UNAUTHORIZED) {
|
---|
64 | String t = ((HttpURLConnection) con).getHeaderField("WWW-Authenticate");
|
---|
65 | t = t.replace("Basic realm=\"","").replace("\"","");
|
---|
66 | throw new MissingPassword(t);
|
---|
67 | }
|
---|
68 | BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
---|
69 | StringBuilder b = new StringBuilder();
|
---|
70 | for (String line = reader.readLine(); line != null; line = reader.readLine()) {
|
---|
71 | b.append(line);
|
---|
72 | b.append("\n");
|
---|
73 | }
|
---|
74 | if (con instanceof HttpURLConnection) {
|
---|
75 | ((HttpURLConnection) con).disconnect();
|
---|
76 | }
|
---|
77 | return b.toString();
|
---|
78 | } catch (IOException e) {
|
---|
79 | e.printStackTrace();
|
---|
80 | }
|
---|
81 | return null;
|
---|
82 | }
|
---|
83 | public void upload(String s) {
|
---|
84 | try {
|
---|
85 | URL u = new URL(getPreferencesDir());
|
---|
86 | System.out.println("uploading preferences to "+u);
|
---|
87 | HttpURLConnection con = (HttpURLConnection)u.openConnection();
|
---|
88 | String username = get("applet.username");
|
---|
89 | String password = get("applet.password");
|
---|
90 | if(password.isEmpty() && username.isEmpty())
|
---|
91 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(username+":"+password));
|
---|
92 | con.setRequestMethod("POST");
|
---|
93 | con.setDoOutput(true);
|
---|
94 | con.connect();
|
---|
95 | PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream()));
|
---|
96 | out.println(s);
|
---|
97 | out.close();
|
---|
98 | con.getInputStream().close();
|
---|
99 | con.disconnect();
|
---|
100 | JOptionPane.showMessageDialog(
|
---|
101 | Main.parent,
|
---|
102 | tr("Preferences stored on {0}", u.getHost()),
|
---|
103 | tr("Information"),
|
---|
104 | JOptionPane.INFORMATION_MESSAGE
|
---|
105 | );
|
---|
106 | } catch (Exception e) {
|
---|
107 | e.printStackTrace();
|
---|
108 | JOptionPane.showMessageDialog(
|
---|
109 | Main.parent,
|
---|
110 | tr("Could not upload preferences. Reason: {0}", e.getMessage()),
|
---|
111 | tr("Error"),
|
---|
112 | JOptionPane.ERROR_MESSAGE
|
---|
113 | );
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | public ServerSidePreferences(URL serverUrl) {
|
---|
119 | Connection connection = null;
|
---|
120 | try {
|
---|
121 | connection = new Connection(new URL(serverUrl+"user/preferences"));
|
---|
122 | } catch (MalformedURLException e) {
|
---|
123 | e.printStackTrace();
|
---|
124 | JOptionPane.showMessageDialog(
|
---|
125 | Main.parent,
|
---|
126 | tr("Could not load preferences from server."),
|
---|
127 | tr("Error"),
|
---|
128 | JOptionPane.ERROR_MESSAGE
|
---|
129 | );
|
---|
130 | }
|
---|
131 | this.connection = connection;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override public String getPreferencesDir() {
|
---|
135 | return connection.serverUrl.toString();
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Do nothing on load. Preferences are loaded with download().
|
---|
140 | */
|
---|
141 | @Override public void load() {
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Do nothing on save. Preferences are uploaded using upload().
|
---|
146 | */
|
---|
147 | @Override public void save() {
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void download(String userName, String password) {
|
---|
151 | if (!properties.containsKey("applet.username") && userName != null) {
|
---|
152 | properties.put("applet.username", userName);
|
---|
153 | }
|
---|
154 | if (!properties.containsKey("applet.password") && password != null) {
|
---|
155 | properties.put("applet.password", password);
|
---|
156 | }
|
---|
157 | try {
|
---|
158 | download();
|
---|
159 | } catch (MissingPassword e) {
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | public boolean download() throws MissingPassword {
|
---|
164 | resetToDefault();
|
---|
165 | String cont = connection.download();
|
---|
166 | if (cont == null) return false;
|
---|
167 | Reader in = new StringReader(cont);
|
---|
168 | boolean res = false;
|
---|
169 | try {
|
---|
170 | fromXML(in);
|
---|
171 | } catch (RuntimeException e) {
|
---|
172 | e.printStackTrace();
|
---|
173 | } catch (SAXException e) {
|
---|
174 | e.printStackTrace();
|
---|
175 | }
|
---|
176 | return res;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Use this instead of save() for the ServerSidePreferences, since uploads
|
---|
181 | * are costly while save is called often.
|
---|
182 | *
|
---|
183 | * This is triggered by an explicit menu option.
|
---|
184 | */
|
---|
185 | public void upload() {
|
---|
186 | connection.upload(toXML(true));
|
---|
187 | }
|
---|
188 | }
|
---|