1 package fr.jayasoft.ivy.util; 2 3 import java.awt.GridBagConstraints ; 4 import java.awt.GridBagLayout ; 5 import java.awt.Insets ; 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.util.Properties ; 11 12 import javax.swing.ImageIcon ; 13 import javax.swing.JCheckBox ; 14 import javax.swing.JLabel ; 15 import javax.swing.JOptionPane ; 16 import javax.swing.JPanel ; 17 import javax.swing.JPasswordField ; 18 import javax.swing.JTextField ; 19 20 import fr.jayasoft.ivy.Ivy; 21 22 public class CredentialsUtil { 23 24 25 26 private static final class CredentialPanel extends JPanel { 27 JTextField userNameField = new JTextField (20); 28 29 JTextField passwordField = new JPasswordField (20); 30 31 JCheckBox rememberDataCB = new JCheckBox ("remember my information"); 32 33 CredentialPanel(Credentials credentials, File passfile) { 34 GridBagLayout layout = new GridBagLayout (); 35 setLayout(layout); 36 GridBagConstraints c = new GridBagConstraints (); 37 c.insets = new Insets (2,2,2,2); 38 39 c.gridx = 1; 40 c.gridheight = 1; 41 c.gridwidth = 2; 42 String prompt = credentials.getRealm() != null ? 43 "Enter username and password for \""+credentials.getRealm()+"\" at "+credentials.getHost() : 44 "Enter username and password for "+credentials.getHost(); 45 add(new JLabel (prompt), c); 46 47 c.gridy = 1; 48 c.gridwidth = 1; 49 50 add(new JLabel ("username: "), c); 51 c.gridx = 2; 52 add(userNameField, c); 53 c.gridx = 1; 54 c.gridy++; 55 56 if (credentials.getUserName() != null) { 57 userNameField.setText(credentials.getUserName()); 58 } 59 60 if (credentials.getPasswd() == null) { 61 add(new JLabel ("passwd: "), c); 62 c.gridx = 2; 63 add(passwordField, c); 64 c.gridx = 1; 65 c.gridy++; 66 } else { 67 passwordField.setText(credentials.getPasswd()); 68 } 69 70 if (passfile != null) { 71 c.gridwidth = 2; 72 add(rememberDataCB, c); 73 c.gridy++; 74 } 75 c.gridwidth = 2; 76 add(new JLabel (), c); 78 } 79 } 80 81 82 public static Credentials promptCredentials(Credentials c, File passfile) { 83 c = loadPassfile(c, passfile); 84 if (c.getUserName() != null && c.getPasswd() != null) { 85 return c; 86 } 87 CredentialPanel credentialPanel = new CredentialPanel(c, passfile); 88 if (JOptionPane.showOptionDialog(null, credentialPanel, c.getHost()+" credentials", JOptionPane.OK_CANCEL_OPTION, 0, new ImageIcon (Ivy.class.getResource("logo.png")), null, new Integer (JOptionPane.OK_OPTION)) == JOptionPane.OK_OPTION) { 89 String username=credentialPanel.userNameField.getText(); 90 String passwd=credentialPanel.passwordField.getText(); 91 if (credentialPanel.rememberDataCB.isSelected()) { 92 Properties props = new EncrytedProperties(); 93 props.setProperty("username", username); 94 props.setProperty("passwd", passwd); 95 FileOutputStream fos = null; 96 try { 97 fos = new FileOutputStream (passfile); 98 props.store(fos, ""); 99 } catch (Exception e) { 100 Message.warn("error occured while saving password file "+passfile+": "+ e); 101 } finally { 102 if (fos != null) {try {fos.close();} catch (Exception e) {}} 103 } 104 } 105 c = new Credentials(c.getRealm(), c.getHost(), username, passwd); 106 } 107 return c; 108 } 109 110 111 public static Credentials loadPassfile(Credentials c, File passfile) { 112 if (passfile != null && passfile.exists()) { 113 Properties props = new EncrytedProperties(); 114 FileInputStream fis = null; 115 try { 116 fis = new FileInputStream (passfile); 117 props.load(fis); 118 String username = c.getUserName(); 119 String passwd = c.getPasswd(); 120 if (username == null) { 121 username = props.getProperty("username"); 122 } 123 if (passwd == null) { 124 passwd = props.getProperty("passwd"); 125 } 126 return new Credentials(c.getRealm(), c.getHost(), username, passwd); 127 } catch (IOException e) { 128 Message.warn("error occured while loading password file "+passfile+": "+ e); 129 } finally { 130 if (fis != null) { 131 try { 132 fis.close(); 133 } catch (IOException e) { 134 } 135 } 136 } 137 } 138 return c; 139 } 140 141 142 } 143 | Popular Tags |