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

Last change on this file since 662 was 662, checked in by framm, 16 years ago
  • patch for usernames with international characters. please revert this immediately if it causes any trouble. submitted by danilo@….
  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Font;
7import java.awt.GridBagLayout;
8import java.net.Authenticator;
9import java.net.HttpURLConnection;
10import java.net.PasswordAuthentication;
11import java.nio.ByteBuffer;
12import java.nio.CharBuffer;
13import java.nio.charset.Charset;
14import java.nio.charset.CharsetEncoder;
15import java.nio.charset.CharacterCodingException;
16
17import javax.swing.JCheckBox;
18import javax.swing.JLabel;
19import javax.swing.JOptionPane;
20import javax.swing.JPanel;
21import javax.swing.JPasswordField;
22import javax.swing.JTextField;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.tools.Base64;
26import org.openstreetmap.josm.tools.GBC;
27
28/**
29 * Base class that handles common things like authentication for the reader and writer
30 * to the osm server.
31 *
32 * @author imi
33 */
34public class OsmConnection {
35
36 public static class OsmParseException extends Exception {
37 public OsmParseException() {super();}
38 public OsmParseException(String message, Throwable cause) {super(message, cause);}
39 public OsmParseException(String message) {super(message);}
40 public OsmParseException(Throwable cause) {super(cause);}
41 }
42
43 protected boolean cancel = false;
44 protected HttpURLConnection activeConnection;
45
46 private static OsmAuth authentication = new OsmAuth();
47 /**
48 * Initialize the http defaults and the authenticator.
49 */
50 static {
51 //TODO: refactor this crap (maybe just insert the damn auth http-header by yourself)
52 try {
53 HttpURLConnection.setFollowRedirects(true);
54 Authenticator.setDefault(authentication);
55 } catch (SecurityException e) {
56 }
57 }
58
59 /**
60 * The authentication class handling the login requests.
61 */
62 private static class OsmAuth extends Authenticator {
63 /**
64 * Set to true, when the autenticator tried the password once.
65 */
66 boolean passwordtried = false;
67 /**
68 * Whether the user cancelled the password dialog
69 */
70 boolean authCancelled = false;
71
72 @Override protected PasswordAuthentication getPasswordAuthentication() {
73 String username = Main.pref.get("osm-server.username");
74 String password = Main.pref.get("osm-server.password");
75 if (passwordtried || username.equals("") || password.equals("")) {
76 JPanel p = new JPanel(new GridBagLayout());
77 if (!username.equals("") && !password.equals(""))
78 p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop());
79 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0));
80 JTextField usernameField = new JTextField(username, 20);
81 p.add(usernameField, GBC.eol());
82 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0));
83 JPasswordField passwordField = new JPasswordField(password, 20);
84 p.add(passwordField, GBC.eol());
85 JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted."));
86 warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
87 p.add(warning, GBC.eop());
88
89 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals(""));
90 p.add(savePassword, GBC.eop());
91
92 int choice = JOptionPane.showConfirmDialog(Main.parent, p, tr("Enter Password"), JOptionPane.OK_CANCEL_OPTION);
93 if (choice == JOptionPane.CANCEL_OPTION) {
94 authCancelled = true;
95 return null;
96 }
97 username = usernameField.getText();
98 password = String.valueOf(passwordField.getPassword());
99 if (savePassword.isSelected()) {
100 Main.pref.put("osm-server.username", username);
101 Main.pref.put("osm-server.password", password);
102 }
103 if (username.equals(""))
104 return null;
105 }
106 passwordtried = true;
107 return new PasswordAuthentication(username, password.toCharArray());
108 }
109 }
110
111 /**
112 * Must be called before each connection attemp to initialize the authentication.
113 */
114 protected final void initAuthentication() {
115 authentication.authCancelled = false;
116 authentication.passwordtried = false;
117 }
118
119 /**
120 * @return Whether the connection was cancelled.
121 */
122 protected final boolean isAuthCancelled() {
123 return authentication.authCancelled;
124 }
125
126 public void cancel() {
127 Main.pleaseWaitDlg.currentAction.setText(tr("Aborting..."));
128 cancel = true;
129 if (activeConnection != null) {
130 activeConnection.setConnectTimeout(1);
131 activeConnection.setReadTimeout(1);
132 activeConnection.disconnect();
133 }
134 }
135
136 protected void addAuth(HttpURLConnection con) throws CharacterCodingException {
137 CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
138 String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password");
139 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth));
140 con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
141 }
142}
Note: See TracBrowser for help on using the repository browser.