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

Last change on this file since 4067 was 3938, checked in by stoecker, 13 years ago

fix applet preferences, move XML code to Preferences, as we may want to use it as standard format soon

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