Changeset 1169 in josm for trunk/src/org/openstreetmap/josm/io/OsmConnection.java
- Timestamp:
- 2008-12-23T15:07:05+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r763 r1169 34 34 public class OsmConnection { 35 35 36 37 38 39 40 41 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 42 43 44 43 protected boolean cancel = false; 44 protected HttpURLConnection activeConnection; 45 45 46 47 48 49 50 51 52 53 54 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 55 } catch (SecurityException e) { 56 56 } 57 57 } 58 58 59 60 61 62 63 64 65 66 67 68 69 70 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 88 89 90 89 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals("")); 90 p.add(savePassword, GBC.eop()); 91 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 110 111 112 113 114 115 116 117 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 118 119 120 121 122 123 124 119 /** 120 * @return Whether the connection was cancelled. 121 */ 122 protected final boolean isAuthCancelled() { 123 return authentication.authCancelled; 124 } 125 125 126 127 128 129 130 131 132 133 134 135 136 137 126 public void cancel() { 127 Main.pleaseWaitDlg.currentAction.setText(tr("Aborting...")); 128 cancel = true; 129 if (activeConnection != null) { 130 activeConnection.setConnectTimeout(100); 131 activeConnection.setReadTimeout(100); 132 try { 133 Thread.sleep(100); 134 } catch (InterruptedException ex) {} 135 activeConnection.disconnect(); 136 } 137 } 138 138 139 140 141 142 143 144 139 protected void addAuth(HttpURLConnection con) throws CharacterCodingException { 140 CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); 141 String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password"); 142 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth)); 143 con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes)); 144 } 145 145 }
Note:
See TracChangeset
for help on using the changeset viewer.