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