KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > config > PasswordFile


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.config;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
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 /**
28  * Represents a file holding the username and password credentials for a realmstring.
29  *
30  * @author Tomas Stupka
31  */

32 public class PasswordFile extends SVNCredentialFile {
33
34     private static final Key PASSTYPE_KEY = new Key(0, "passtype"); // NOI18N
35
private static final Key PASSWORD_KEY = new Key(1, "password"); // NOI18N
36
private static final Key REALMSTRING_KEY = new Key(2, "svn:realmstring"); // NOI18N
37
private static final Key USERNAME_KEY = new Key(3, "username"); // NOI18N
38

39     private final static String JavaDoc PASSTYPE_SIMPLE = "simple"; // NOI18N
40

41     public PasswordFile (String JavaDoc realmString) {
42         super(getFile(realmString));
43     }
44
45     private PasswordFile (File JavaDoc file) {
46         super(file);
47     }
48
49     /**
50      * Goes through the Netbeans Subversion modules configuration directory and looks
51      * for a file holding the username and password for the givenurl.
52      *
53      * @param svnUrl the url
54      * @return the file holding the username and password for the givenurl or null
55      * if nothing was found
56      */

57     public static PasswordFile findFileForUrl(SVNUrl svnUrl) {
58         // create our own realmstring -
59
String JavaDoc urlString = SvnUtils.ripUserFromHost(svnUrl.getHost());
60         String JavaDoc realmString = "<" + svnUrl.getProtocol() + "://" + urlString + ">"; // NOI18N
61
PasswordFile nbPasswordFile = new PasswordFile(realmString);
62         
63         if(!nbPasswordFile.getFile().exists()) {
64
65             File JavaDoc configDir = new File JavaDoc(SvnConfigFiles.getUserConfigPath() + "/auth/svn.simple"); // NOI18N
66
File JavaDoc[] 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)) // windows likes to use wincryp, but we can accept only plain text
74
{
75                     // overwrites the value given by svn with our own, but there is no chance to get
76
// the realm string as svn does.
77
passwordFile.setRealmString(realmString);
78                     return passwordFile;
79                 }
80             }
81             
82             // no password file - let's create an empty one then...
83
nbPasswordFile.setRealmString(realmString);
84             nbPasswordFile.setPasstype(PASSTYPE_SIMPLE);
85             nbPasswordFile.setPassword(""); // NOI18N
86
nbPasswordFile.setUsername(""); // NOI18N
87
return nbPasswordFile;
88             
89         } else {
90             return nbPasswordFile;
91         }
92     }
93
94     public void store() throws IOException JavaDoc {
95         store(getFile(getRealmString()));
96     }
97
98     public String JavaDoc getPassword() {
99         return getStringValue(getPasswordKey());
100     }
101
102     public String JavaDoc getUsername() {
103         return getStringValue(getUsernameKey());
104     }
105
106     public void setPassword(String JavaDoc password) {
107         setValue(getPasswordKey(), password);
108     }
109
110     public void setUsername(String JavaDoc username) {
111         setValue(getUsernameKey(), username);
112     }
113     
114     protected String JavaDoc getRealmString() {
115         return getStringValue(getRealmstringKey());
116     }
117
118     protected void setRealmString(String JavaDoc realm) {
119         setValue(getRealmstringKey(), realm.getBytes());
120     }
121
122     private void setPasstype(String JavaDoc passtype) {
123         setValue(getPasstypeKey(), passtype);
124     }
125
126     private String JavaDoc getPasstype() {
127         return getStringValue(getPasstypeKey());
128     }
129
130     private boolean acceptSvnUrl(SVNUrl svnUrl) {
131         if(svnUrl==null) {
132             return false;
133         }
134         String JavaDoc realmStrig = getRealmString();
135         if(realmStrig==null || realmStrig.length() < 6 ) {
136             // at least 'svn://'
137
return false;
138         }
139         String JavaDoc urlString = SvnUtils.ripUserFromHost(svnUrl.getHost());
140         return realmStrig.substring(1).startsWith(svnUrl.getProtocol() + "://" + urlString); // NOI18N
141
}
142     
143     private static File JavaDoc getFile(String JavaDoc realmString) {
144         return new File JavaDoc(SvnConfigFiles.getNBConfigPath() + "auth/svn.simple/" + getFileName(realmString)); // NOI18N
145
}
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