1 19 package org.netbeans.modules.subversion.config; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import org.netbeans.modules.subversion.config.KVFile.Key; 24 import org.netbeans.modules.subversion.util.SvnUtils; 25 import org.tigris.subversion.svnclientadapter.SVNUrl; 26 27 32 public class PasswordFile extends SVNCredentialFile { 33 34 private static final Key PASSTYPE_KEY = new Key(0, "passtype"); private static final Key PASSWORD_KEY = new Key(1, "password"); private static final Key REALMSTRING_KEY = new Key(2, "svn:realmstring"); private static final Key USERNAME_KEY = new Key(3, "username"); 39 private final static String PASSTYPE_SIMPLE = "simple"; 41 public PasswordFile (String realmString) { 42 super(getFile(realmString)); 43 } 44 45 private PasswordFile (File file) { 46 super(file); 47 } 48 49 57 public static PasswordFile findFileForUrl(SVNUrl svnUrl) { 58 String urlString = SvnUtils.ripUserFromHost(svnUrl.getHost()); 60 String realmString = "<" + svnUrl.getProtocol() + "://" + urlString + ">"; PasswordFile nbPasswordFile = new PasswordFile(realmString); 62 63 if(!nbPasswordFile.getFile().exists()) { 64 65 File configDir = new File (SvnConfigFiles.getUserConfigPath() + "/auth/svn.simple"); File [] files = configDir.listFiles(); 67 if(files==null) { 68 return null; 69 } 70 for (int i = 0; i < files.length; i++) { 71 PasswordFile passwordFile = new PasswordFile(files[i]); 72 if(passwordFile.acceptSvnUrl(svnUrl) && 73 passwordFile.getPasstype().equals(PASSTYPE_SIMPLE)) { 75 passwordFile.setRealmString(realmString); 78 return passwordFile; 79 } 80 } 81 82 nbPasswordFile.setRealmString(realmString); 84 nbPasswordFile.setPasstype(PASSTYPE_SIMPLE); 85 nbPasswordFile.setPassword(""); nbPasswordFile.setUsername(""); return nbPasswordFile; 88 89 } else { 90 return nbPasswordFile; 91 } 92 } 93 94 public void store() throws IOException { 95 store(getFile(getRealmString())); 96 } 97 98 public String getPassword() { 99 return getStringValue(getPasswordKey()); 100 } 101 102 public String getUsername() { 103 return getStringValue(getUsernameKey()); 104 } 105 106 public void setPassword(String password) { 107 setValue(getPasswordKey(), password); 108 } 109 110 public void setUsername(String username) { 111 setValue(getUsernameKey(), username); 112 } 113 114 protected String getRealmString() { 115 return getStringValue(getRealmstringKey()); 116 } 117 118 protected void setRealmString(String realm) { 119 setValue(getRealmstringKey(), realm.getBytes()); 120 } 121 122 private void setPasstype(String passtype) { 123 setValue(getPasstypeKey(), passtype); 124 } 125 126 private String getPasstype() { 127 return getStringValue(getPasstypeKey()); 128 } 129 130 private boolean acceptSvnUrl(SVNUrl svnUrl) { 131 if(svnUrl==null) { 132 return false; 133 } 134 String realmStrig = getRealmString(); 135 if(realmStrig==null || realmStrig.length() < 6 ) { 136 return false; 138 } 139 String urlString = SvnUtils.ripUserFromHost(svnUrl.getHost()); 140 return realmStrig.substring(1).startsWith(svnUrl.getProtocol() + "://" + urlString); } 142 143 private static File getFile(String realmString) { 144 return new File (SvnConfigFiles.getNBConfigPath() + "auth/svn.simple/" + getFileName(realmString)); } 146 147 private Key getPasstypeKey() { 148 return getKey(PASSTYPE_KEY); 149 } 150 151 private Key getPasswordKey() { 152 return getKey(PASSWORD_KEY); 153 } 154 155 private Key getRealmstringKey() { 156 return getKey(REALMSTRING_KEY); 157 } 158 159 private Key getUsernameKey() { 160 return getKey(USERNAME_KEY); 161 } 162 163 } 164 | Popular Tags |