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

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