source: josm/src/org/openstreetmap/josm/io/OsmConnection.java@ 98

Last change on this file since 98 was 98, checked in by imi, 18 years ago
  • added Applet version of JOSM (unfinished)
  • fixed display bug if --no-fullscreen and --geometry was specified
File size: 3.5 KB
Line 
1package org.openstreetmap.josm.io;
2
3import java.awt.Font;
4import java.awt.GridBagLayout;
5import java.net.Authenticator;
6import java.net.HttpURLConnection;
7import java.net.PasswordAuthentication;
8
9import javax.swing.BoundedRangeModel;
10import javax.swing.JLabel;
11import javax.swing.JOptionPane;
12import javax.swing.JPanel;
13import javax.swing.JPasswordField;
14import javax.swing.JTextField;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.tools.GBC;
18
19/**
20 * Base class that handles common things like authentication for the reader and writer
21 * to the osm server.
22 *
23 * @author imi
24 */
25public class OsmConnection {
26
27 protected boolean cancel = false;
28 protected HttpURLConnection activeConnection;
29 protected JLabel currentAction;
30 protected BoundedRangeModel progress;
31
32 private static OsmAuth authentication;
33 /**
34 * Initialize the http defaults and the authenticator.
35 */
36 static {
37 HttpURLConnection.setFollowRedirects(true);
38 Authenticator.setDefault(authentication = new OsmAuth());
39 }
40
41 /**
42 * The authentication class handling the login requests.
43 */
44 private static class OsmAuth extends Authenticator {
45 /**
46 * Set to true, when the autenticator tried the password once.
47 */
48 boolean passwordtried = false;
49 /**
50 * Whether the user cancelled the password dialog
51 */
52 boolean authCancelled = false;
53
54 @Override protected PasswordAuthentication getPasswordAuthentication() {
55 String username = Main.pref.get("osm-server.username");
56 String password = Main.pref.get("osm-server.password");
57 if (passwordtried || username.equals("") || password.equals("")) {
58 JPanel p = new JPanel(new GridBagLayout());
59 p.add(new JLabel("Username"), GBC.std().insets(0,0,10,0));
60 JTextField usernameField = new JTextField(username, 20);
61 p.add(usernameField, GBC.eol());
62 p.add(new JLabel("Password"), GBC.std().insets(0,0,10,0));
63 JPasswordField passwordField = new JPasswordField(password, 20);
64 p.add(passwordField, GBC.eol());
65 JLabel warning = new JLabel("Warning: The password is transferred unencrypted.");
66 warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
67 p.add(warning, GBC.eol());
68 int choice = JOptionPane.showConfirmDialog(Main.parent, p, "Enter Password", JOptionPane.OK_CANCEL_OPTION);
69 if (choice == JOptionPane.CANCEL_OPTION) {
70 authCancelled = true;
71 return null;
72 }
73 username = usernameField.getText();
74 password = String.valueOf(passwordField.getPassword());
75 if (username.equals(""))
76 return null;
77 }
78 passwordtried = true;
79 return new PasswordAuthentication(username, password.toCharArray());
80 }
81 }
82
83 /**
84 * Must be called before each connection attemp to initialize the authentication.
85 */
86 protected final void initAuthentication() {
87 authentication.authCancelled = false;
88 authentication.passwordtried = false;
89 }
90
91 /**
92 * @return Whether the connection was cancelled.
93 */
94 protected final boolean isAuthCancelled() {
95 return authentication.authCancelled;
96 }
97
98 public void setProgressInformation(JLabel currentAction, BoundedRangeModel progress) {
99 this.currentAction = currentAction;
100 this.progress = progress;
101 }
102
103 public void cancel() {
104 currentAction.setText("Aborting...");
105 cancel = true;
106 if (activeConnection != null) {
107 activeConnection.setConnectTimeout(1);
108 activeConnection.setReadTimeout(1);
109 activeConnection.disconnect();
110 }
111 }
112}
Note: See TracBrowser for help on using the repository browser.