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

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

global replacement of e.printStackTrace() by Main.error(e)

  • Property svn:eol-style set to native
File size: 6.6 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 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()));
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()));
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 public ServerSidePreferences(URL serverUrl) {
120 Connection connection = null;
121 try {
122 connection = new Connection(new URL(serverUrl+"user/preferences"));
123 } catch (MalformedURLException e) {
124 Main.error(e);
125 JOptionPane.showMessageDialog(
126 Main.parent,
127 tr("Could not load preferences from server."),
128 tr("Error"),
129 JOptionPane.ERROR_MESSAGE
130 );
131 }
132 this.connection = connection;
133 }
134
135 @Override public String getPreferencesDir() {
136 return connection.serverUrl.toString();
137 }
138
139 /**
140 * Do nothing on load. Preferences are loaded with download().
141 */
142 @Override public void load() {
143 }
144
145 /**
146 * Do nothing on save. Preferences are uploaded using upload().
147 */
148 @Override public void save() {
149 }
150
151 public void download(String userName, String password) {
152 if (!settingsMap.containsKey("applet.username") && userName != null) {
153 settingsMap.put("applet.username", new StringSetting(userName));
154 }
155 if (!settingsMap.containsKey("applet.password") && password != null) {
156 settingsMap.put("applet.password", new StringSetting(password));
157 }
158 try {
159 download();
160 } catch (MissingPassword e) {
161 Main.warn(e);
162 }
163 }
164
165 public boolean download() throws MissingPassword {
166 resetToDefault();
167 String cont = connection.download();
168 if (cont == null) return false;
169 Reader in = new StringReader(cont);
170 boolean res = false;
171 try {
172 fromXML(in);
173 } catch (RuntimeException e) {
174 Main.error(e);
175 } catch (XMLStreamException e) {
176 Main.error(e);
177 }
178 return res;
179 }
180
181 /**
182 * Use this instead of save() for the ServerSidePreferences, since uploads
183 * are costly while save is called often.
184 *
185 * This is triggered by an explicit menu option.
186 */
187 public void upload() {
188 connection.upload(toXML(true));
189 }
190}
Note: See TracBrowser for help on using the repository browser.