source: josm/trunk/src/org/openstreetmap/josm/data/ServerSidePreferences.java@ 7004

Last change on this file since 7004 was 7004, checked in by Don-vip, 10 years ago

see #8465 - use multi-catch where applicable

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.io.OutputStreamWriter;
10import java.io.PrintWriter;
11import java.io.Reader;
12import java.io.StringReader;
13import java.net.HttpURLConnection;
14import java.net.MalformedURLException;
15import java.net.URL;
16import java.net.URLConnection;
17
18import javax.swing.JOptionPane;
19import javax.xml.stream.XMLStreamException;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.io.OsmConnection;
23import org.openstreetmap.josm.tools.Base64;
24import 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 */
32public class ServerSidePreferences extends Preferences {
33 public static class MissingPassword extends Exception{
34 public final 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 Main.info("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(), Utils.UTF_8));
65 StringBuilder b = new StringBuilder();
66 try {
67 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
68 b.append(line);
69 b.append("\n");
70 }
71 } finally {
72 reader.close();
73 }
74 if (con instanceof HttpURLConnection) {
75 ((HttpURLConnection) con).disconnect();
76 }
77 return b.toString();
78 } catch (IOException e) {
79 Main.error(e);
80 }
81 return null;
82 }
83 public void upload(String s) {
84 try {
85 URL u = new URL(getPreferencesDir());
86 Main.info("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 }
93 con.setRequestMethod("POST");
94 con.setDoOutput(true);
95 con.connect();
96 PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream(), Utils.UTF_8));
97 out.println(s);
98 Utils.close(out);
99 Utils.close(con.getInputStream());
100 con.disconnect();
101 JOptionPane.showMessageDialog(
102 Main.parent,
103 tr("Preferences stored on {0}", u.getHost()),
104 tr("Information"),
105 JOptionPane.INFORMATION_MESSAGE
106 );
107 } catch (Exception e) {
108 Main.error(e);
109 JOptionPane.showMessageDialog(
110 Main.parent,
111 tr("Could not upload preferences. Reason: {0}", e.getMessage()),
112 tr("Error"),
113 JOptionPane.ERROR_MESSAGE
114 );
115 }
116 }
117 }
118
119 /**
120 * Constructs a new {@code ServerSidePreferences}.
121 * @param serverUrl The server URL hosting the user preferences.
122 */
123 public ServerSidePreferences(URL serverUrl) {
124 Connection connection = null;
125 try {
126 connection = new Connection(new URL(serverUrl+"user/preferences"));
127 } catch (MalformedURLException e) {
128 Main.error(e);
129 JOptionPane.showMessageDialog(
130 Main.parent,
131 tr("Could not load preferences from server."),
132 tr("Error"),
133 JOptionPane.ERROR_MESSAGE
134 );
135 }
136 this.connection = connection;
137 }
138
139 @Override public String getPreferencesDir() {
140 return connection.serverUrl.toString();
141 }
142
143 /**
144 * Do nothing on load. Preferences are loaded with download().
145 */
146 @Override public void load() {
147 }
148
149 /**
150 * Do nothing on save. Preferences are uploaded using upload().
151 */
152 @Override public void save() {
153 }
154
155 public void download(String userName, String password) {
156 if (!settingsMap.containsKey("applet.username") && userName != null) {
157 settingsMap.put("applet.username", new StringSetting(userName));
158 }
159 if (!settingsMap.containsKey("applet.password") && password != null) {
160 settingsMap.put("applet.password", new StringSetting(password));
161 }
162 try {
163 download();
164 } catch (MissingPassword e) {
165 Main.warn(e);
166 }
167 }
168
169 public boolean download() throws MissingPassword {
170 resetToDefault();
171 String cont = connection.download();
172 if (cont == null) return false;
173 Reader in = new StringReader(cont);
174 boolean res = false;
175 try {
176 fromXML(in);
177 } catch (RuntimeException | XMLStreamException e) {
178 Main.error(e);
179 }
180 return res;
181 }
182
183 /**
184 * Use this instead of save() for the ServerSidePreferences, since uploads
185 * are costly while save is called often.
186 *
187 * This is triggered by an explicit menu option.
188 */
189 public void upload() {
190 connection.upload(toXML(true));
191 }
192}
Note: See TracBrowser for help on using the repository browser.