KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > activedirectory > ActiveDirectoryUser


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.activedirectory;
21
22 import java.util.Calendar JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import com.sslexplorer.realms.Realm;
26 import com.sslexplorer.security.DefaultUser;
27 import com.sslexplorer.security.User;
28
29 /**
30  * Implementation of {@link User} that uses an Active Directory Account for its
31  * attributes.
32  *
33  * @author Lee Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
34  * @author Brett Smith <a HREF="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a>
35  */

36 public final class ActiveDirectoryUser extends DefaultUser {
37     private static final long serialVersionUID = 2475206921518033947L;
38     private final String JavaDoc userPrincipalName;
39     private final String JavaDoc originalDn;
40     private final String JavaDoc escapedDn;
41     
42     /**
43      * Constructor
44      * @param username username
45      * @param userPrincipalName userPrincipalName
46      * @param email email address
47      * @param fullname full name
48      * @param lastPasswordChange date of last password change
49      * @param realm Realm
50      */

51     ActiveDirectoryUser(String JavaDoc username, String JavaDoc userPrincipalName, String JavaDoc email, String JavaDoc fullname, String JavaDoc dn, String JavaDoc escapedDn, Date JavaDoc lastPasswordChange, Realm realm) {
52         super(selectUsername(username, userPrincipalName), email, fullname, lastPasswordChange, realm);
53         this.userPrincipalName = selectUserPrincipalName(username, userPrincipalName);
54         this.originalDn = dn;
55         this.escapedDn = escapedDn;
56     }
57     
58     private static String JavaDoc selectUsername(String JavaDoc username, String JavaDoc userPrincipalName) {
59         return isEmpty(userPrincipalName) ? username : parseUsername(userPrincipalName);
60     }
61     
62     private static String JavaDoc parseUsername(String JavaDoc userPrincipalName) {
63         int indexOf = userPrincipalName.indexOf("@");
64         return indexOf == -1 ? userPrincipalName : userPrincipalName.substring(0, indexOf);
65     }
66     
67     private static String JavaDoc selectUserPrincipalName(String JavaDoc username, String JavaDoc userPrincipalName) {
68         return isEmpty(userPrincipalName) ? username : fixUserPrincipalName(userPrincipalName);
69     }
70     
71     /**
72      * For some reason the userPrincipalName's domain must be upper case. If not
73      * then the user can't login as the realm cannot be found.
74      * @param userPrincipalName
75      * @return String
76      */

77     private static String JavaDoc fixUserPrincipalName(String JavaDoc userPrincipalName) {
78         int indexOf = userPrincipalName.indexOf("@");
79         if (indexOf == -1) {
80             return userPrincipalName;
81         }
82         indexOf++; // need to parse after the @ symbol
83
String JavaDoc domain = userPrincipalName.substring(indexOf, userPrincipalName.length()).toUpperCase();
84         String JavaDoc fixedUserPrincipalName = userPrincipalName.substring(0, indexOf) + domain;
85         return fixedUserPrincipalName;
86     }
87     
88     private static boolean isEmpty(String JavaDoc value) {
89         return value == null || value.length() == 0;
90     }
91     
92     /**
93      * Get the userPrincipalName of the user
94      * @return String
95      */

96     public String JavaDoc getUserPrincipalName() {
97         return userPrincipalName;
98     }
99
100     /**
101      * Get the original un-escaped dn
102      * @return dn
103      */

104     public String JavaDoc getOriginalDn() {
105         return originalDn;
106     }
107     
108     /**
109      * Get the escaped dn
110      * @return
111      */

112     public String JavaDoc getDn() {
113         return escapedDn;
114     }
115     
116     /**
117      * Converts an Active Directory long value into a <code>java.util.Date</code>.
118      * @param timeStamp the time to convert
119      * @return the <code>java.util.Date</code> representing the long
120      */

121     static Date JavaDoc adTimeToJavaDate(long timeStamp) {
122         Calendar JavaDoc calendar = Calendar.getInstance();
123         calendar.clear();
124         calendar.set(1601, 0, 1, 0, 0);
125         timeStamp = timeStamp / 10000 + calendar.getTime().getTime();
126         return new Date JavaDoc(timeStamp);
127     }
128     
129     /*
130      * (non-Javadoc)
131      *
132      * @see com.sslexplorer.security.DefaultUser#toString()
133      */

134     public String JavaDoc toString() {
135         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(super.toString());
136         buffer.append("[");
137         buffer.append("userPrincipalName='").append(userPrincipalName).append("' ");
138         buffer.append("originalDn='").append(originalDn).append("' ");
139         buffer.append("dn='").append(escapedDn).append("'");
140         buffer.append("]");
141         return buffer.toString();
142     }
143 }
Popular Tags