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

Last change on this file since 6362 was 6310, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Nested blocks of code should not be left empty

  • 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;
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 e.printStackTrace();
81 }
82 return null;
83 }
84 public void upload(String s) {
85 try {
86 URL u = new URL(getPreferencesDir());
87 Main.info("uploading preferences to "+u);
88 HttpURLConnection con = (HttpURLConnection)u.openConnection();
89 String username = get("applet.username");
90 String password = get("applet.password");
91 if(password.isEmpty() && username.isEmpty()) {
92 con.addRequestProperty("Authorization", "Basic "+Base64.encode(username+":"+password));
93 }
94 con.setRequestMethod("POST");
95 con.setDoOutput(true);
96 con.connect();
97 PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream()));
98 out.println(s);
99 Utils.close(out);
100 Utils.close(con.getInputStream());
101 con.disconnect();
102 JOptionPane.showMessageDialog(
103 Main.parent,
104 tr("Preferences stored on {0}", u.getHost()),
105 tr("Information"),
106 JOptionPane.INFORMATION_MESSAGE
107 );
108 } catch (Exception e) {
109 e.printStackTrace();
110 JOptionPane.showMessageDialog(
111 Main.parent,
112 tr("Could not upload preferences. Reason: {0}", e.getMessage()),
113 tr("Error"),
114 JOptionPane.ERROR_MESSAGE
115 );
116 }
117 }
118 }
119
120 public ServerSidePreferences(URL serverUrl) {
121 Connection connection = null;
122 try {
123 connection = new Connection(new URL(serverUrl+"user/preferences"));
124 } catch (MalformedURLException e) {
125 e.printStackTrace();
126 JOptionPane.showMessageDialog(
127 Main.parent,
128 tr("Could not load preferences from server."),
129 tr("Error"),
130 JOptionPane.ERROR_MESSAGE
131 );
132 }
133 this.connection = connection;
134 }
135
136 @Override public String getPreferencesDir() {
137 return connection.serverUrl.toString();
138 }
139
140 /**
141 * Do nothing on load. Preferences are loaded with download().
142 */
143 @Override public void load() {
144 }
145
146 /**
147 * Do nothing on save. Preferences are uploaded using upload().
148 */
149 @Override public void save() {
150 }
151
152 public void download(String userName, String password) {
153 if (!properties.containsKey("applet.username") && userName != null) {
154 properties.put("applet.username", userName);
155 }
156 if (!properties.containsKey("applet.password") && password != null) {
157 properties.put("applet.password", password);
158 }
159 try {
160 download();
161 } catch (MissingPassword e) {
162 Main.warn(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 (XMLStreamException 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.