JResourceLDAP.java

00001 
00027 package org.objectweb.jonas.security.realm.factory;
00028 
00029 import java.security.NoSuchAlgorithmException;
00030 import java.text.MessageFormat;
00031 import java.util.ArrayList;
00032 import java.util.Enumeration;
00033 import java.util.Hashtable;
00034 import java.util.Iterator;
00035 import java.util.Vector;
00036 
00037 import javax.naming.AuthenticationException;
00038 import javax.naming.Context;
00039 import javax.naming.Name;
00040 import javax.naming.NameParser;
00041 import javax.naming.NamingEnumeration;
00042 import javax.naming.NamingException;
00043 import javax.naming.Reference;
00044 import javax.naming.StringRefAddr;
00045 import javax.naming.directory.Attribute;
00046 import javax.naming.directory.Attributes;
00047 import javax.naming.directory.DirContext;
00048 import javax.naming.directory.InitialDirContext;
00049 import javax.naming.directory.SearchControls;
00050 import javax.naming.directory.SearchResult;
00051 
00052 import org.objectweb.util.monolog.api.BasicLevel;
00053 
00054 import org.objectweb.jonas.security.realm.lib.HashHelper;
00055 import org.objectweb.jonas.security.realm.principals.LDAPUser;
00056 import org.objectweb.jonas.security.realm.principals.User;
00057 
00063 public class JResourceLDAP extends JResource implements JResourceLDAPMBean {
00064 
00068     private static final String FACTORY_TYPE = "org.objectweb.jonas.security.realm.factory.JResourceLDAP";
00069 
00073     private static final String FACTORY_NAME = "org.objectweb.jonas.security.realm.factory.JResourceLDAPFactory";
00074 
00079     private static final String BIND_AUTHENTICATION_MODE = "bind";
00080 
00086     private static final String COMPARE_AUTHENTICATION_MODE = "compare";
00087 
00091     private String initialContextFactory = null;
00092 
00096     private String providerUrl = null;
00097 
00102     private String securityAuthentication = null;
00103 
00108     private String securityPrincipal = null;
00109 
00113     private String securityCredentials = null;
00114 
00119     private String securityProtocol = null;
00120 
00125     private String language = null;
00126 
00131     private String referral = null;
00132 
00137     private String stateFactories = null;
00138 
00143     private String authenticationMode = null;
00144 
00148     private String userPasswordAttribute = null;
00149 
00153     private String userRolesAttribute = null;
00154 
00158     private String roleNameAttribute = null;
00159 
00163     private String baseDN = null;
00164 
00168     private String userDN = null;
00169 
00173     private String userSearchFilter = null;
00174 
00178     private String roleDN = null;
00179 
00183     private String roleSearchFilter = null;
00184 
00188     private String algorithm = null;
00189 
00194     public JResourceLDAP() throws Exception {
00195         super();
00196     }
00197 
00204     public User findUser(String username) throws JResourceException {
00205 
00206         // No username. failed
00207         if (username == null) {
00208             return null;
00209         }
00210 
00211         // User to return
00212         LDAPUser user = new LDAPUser();
00213         user.setName(username);
00214         try {
00215 
00216             // Attributes for performing the search
00217             DirContext dirContext = getDirContext();
00218             SearchControls sctls = new SearchControls();
00219 
00220             String[] attributes = null;
00221             if (authenticationMode.equals(COMPARE_AUTHENTICATION_MODE)) {
00222                 attributes = new String[] {userPasswordAttribute, userRolesAttribute};
00223             } else {
00224                 attributes = new String[0];
00225             }
00226             sctls.setReturningAttributes(attributes);
00227             sctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
00228 
00229             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00230                 getLogger().log(BasicLevel.DEBUG, "userDN = '" + userDN + "'");
00231                 getLogger().log(BasicLevel.DEBUG, "baseDN = '" + baseDN + "'");
00232             }
00233 
00234             String lookupUserDN = null;
00235             // DN of the user. If no specific user DN, use the baseDN
00236             if (userDN != null && !userDN.equals("")) {
00237                 lookupUserDN = userDN.concat(",").concat(baseDN);
00238             } else {
00239                 lookupUserDN = baseDN;
00240             }
00241             Object[] arguments = {username};
00242             lookupUserDN = MessageFormat.format(lookupUserDN, arguments);
00243             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00244                 getLogger().log(BasicLevel.DEBUG, "lookupUserDN = '" + lookupUserDN + "'");
00245             }
00246 
00247             // Search the DN with the user filter with the specific argument and
00248             // specific search controls
00249             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00250                 getLogger().log(BasicLevel.DEBUG, "search : lookupUserDN = '" + lookupUserDN + "', searchFilter = '"
00251                         + userSearchFilter + "', username = '" + username + "'");
00252             }
00253             NamingEnumeration answer = dirContext
00254                     .search(lookupUserDN, userSearchFilter, new Object[] {username}, sctls);
00255             // No answer
00256             if (answer == null || !answer.hasMore()) {
00257                 if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00258                     if (answer == null) {
00259                         getLogger().log(BasicLevel.DEBUG, "answer is null");
00260                     } else {
00261                         getLogger().log(BasicLevel.DEBUG, "no anwser");
00262                     }
00263                 }
00264                 return null;
00265             }
00266 
00267             // Get first answer
00268             SearchResult firstAnswer = (SearchResult) answer.next();
00269             // More than one answer. Not so good.
00270             if (answer.hasMore()) {
00271                 return null;
00272             }
00273 
00274             // Build full User DN
00275             NameParser nameParser = dirContext.getNameParser("");
00276             Name contextName = nameParser.parse(dirContext.getNameInNamespace());
00277 
00278             // Add the lookupUserDN to the context name -> baseDN
00279             Name baseDNName = contextName.addAll(nameParser.parse(lookupUserDN));
00280 
00281             // Add the uid retrieve from the search to the baseDN --> user DN
00282             Name userFullDN = baseDNName.addAll(nameParser.parse(firstAnswer.getName()));
00283             user.setDN(userFullDN.toString());
00284 
00285             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00286                 getLogger().log(BasicLevel.DEBUG, "DN found : '" + userFullDN + "'");
00287             }
00288 
00289             // Get the user attributes
00290             Attributes userAttributes = firstAnswer.getAttributes();
00291             if (userAttributes == null) {
00292                 if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00293                     getLogger().log(BasicLevel.DEBUG, "No user attributes found");
00294                 }
00295                 return null;
00296             }
00297 
00298             String password = null;
00299             // Retrieve password if in compare mode
00300             if (authenticationMode.equals(COMPARE_AUTHENTICATION_MODE)) {
00301                 password = readValueFromAttribute(userPasswordAttribute, userAttributes);
00302                 if (password != null) {
00303                     user.setPassword(password);
00304                 }
00305             }
00306 
00307             // Add the roles which are found from the user
00308             String roles = readValuesFromAttribute(userRolesAttribute, userAttributes);
00309             user.setRoles(roles);
00310 
00311         } catch (javax.naming.NamingException ne) {
00312             throw new JResourceException("Could not find user :" + ne.getMessage());
00313         }
00314         return user;
00315     }
00316 
00323     public boolean isValidUser(User user, String credentials) {
00324 
00325         // null password ... can't authenticate
00326         if (credentials == null || user == null) {
00327             return false;
00328         }
00329 
00330         // Choose the right method
00331         if (authenticationMode.equals(COMPARE_AUTHENTICATION_MODE)) {
00332             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00333                 getLogger().log(BasicLevel.DEBUG, "Compare mode");
00334             }
00335             return isValidUserCompare(user, credentials);
00336         } else if (authenticationMode.equals(BIND_AUTHENTICATION_MODE)) {
00337             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00338                 getLogger().log(BasicLevel.DEBUG, "Bind mode");
00339             }
00340             return isValidUserBind(user, credentials);
00341         } else {
00342             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00343                 getLogger().log(BasicLevel.DEBUG, "No authentication mode found, return false");
00344             }
00345             return false;
00346         }
00347 
00348     }
00349 
00358     public boolean isValidUserBind(User user, String credentials) {
00359 
00360         // Get the DN of the ldap user
00361         if (!(user instanceof LDAPUser)) {
00362             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00363                 getLogger().log(BasicLevel.DEBUG, "Not instance of LDAPUser");
00364             }
00365             return false;
00366         }
00367 
00368         String dn = ((LDAPUser) user).getDN();
00369         if (dn == null) {
00370             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00371                 getLogger().log(BasicLevel.DEBUG, "No DN found in User");
00372             }
00373             return false;
00374         }
00375 
00376         //Make our env
00377         Hashtable env = getEnvInitialDirContext();
00378         env.put(Context.SECURITY_PRINCIPAL, dn);
00379         env.put(Context.SECURITY_CREDENTIALS, credentials);
00380 
00381         boolean validated = false;
00382         try {
00383             DirContext dirContext = new InitialDirContext(env);
00384             validated = true;
00385             dirContext.close();
00386         } catch (AuthenticationException e) {
00387             // not validated
00388             getLogger().log(BasicLevel.ERROR, "Can't make an initial dir context : " + e.getMessage());
00389         } catch (NamingException ne) {
00390             // not validated
00391             getLogger().log(BasicLevel.ERROR, "Naming exception " + ne.getMessage());
00392         }
00393         return validated;
00394     }
00395 
00404     public boolean isValidUserCompare(User user, String credentials) {
00405 
00406         boolean validated = false;
00407 
00408         //Get algorithm and hashpassword
00409         String pass = user.getHashPassword().getPassword();
00410         String algo = user.getHashPassword().getAlgorithm();
00411 
00412         // Crypt password ?
00413         if (algo != null && pass != null) {
00414             try {
00415                 validated = HashHelper.hashPassword(credentials, algo).equalsIgnoreCase(pass);
00416             } catch (NoSuchAlgorithmException nsae) {
00417                 getLogger().log(BasicLevel.ERROR, "Can't make a password with the algorithm " + algo + ". "
00418                         + nsae.getMessage());
00419             }
00420         } else if ((algorithm != null) && (!algorithm.equals(""))) {
00421             // Encode password with the specified algorithm (no clear)
00422             try {
00423                 validated = HashHelper.hashPassword(credentials, algorithm).equalsIgnoreCase(pass);
00424             } catch (NoSuchAlgorithmException nsae) {
00425                 getLogger().log(BasicLevel.ERROR, "Can't make a password with the algorithm " + algorithm + ". "
00426                         + nsae.getMessage());
00427             }
00428         } else {
00429             // clear
00430             validated = credentials.equals(pass);
00431         }
00432         return validated;
00433     }
00434 
00441     public ArrayList getArrayListCombinedRoles(User user) throws JResourceException {
00442 
00443         ArrayList allCombinedRoles = new ArrayList();
00444         // Return empty array if user null
00445         if (user == null) {
00446             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00447                 getLogger().log(BasicLevel.DEBUG, "User is empty, return empty array of roles");
00448             }
00449             return allCombinedRoles;
00450         }
00451 
00452         // Add all user roles found with the user ldap entry
00453         String[] userRoles = user.getArrayRoles();
00454         for (int r = 0; r < userRoles.length; r++) {
00455             String roleName = userRoles[r];
00456             if (!allCombinedRoles.contains(roleName)) {
00457                 allCombinedRoles.add(roleName);
00458             }
00459         }
00460 
00461         // Add roles found in ldap
00462         if (!(user instanceof LDAPUser)) {
00463             return allCombinedRoles;
00464         }
00465         String dn = ((LDAPUser) user).getDN();
00466 
00467         // No valid info
00468         if (dn == null) {
00469             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00470                 getLogger().log(BasicLevel.DEBUG, "DN of user is empty, return empty array of roles");
00471             }
00472             return allCombinedRoles;
00473         }
00474 
00475         try {
00476             // Attributes for performing the search
00477             DirContext dirContext = getDirContext();
00478             SearchControls sctls = new SearchControls();
00479 
00480             String[] attributes = new String[] {roleNameAttribute};
00481             sctls.setReturningAttributes(attributes);
00482             sctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
00483             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00484                 getLogger().log(BasicLevel.DEBUG, "roleDN = '" + roleDN + "'");
00485                 getLogger().log(BasicLevel.DEBUG, "baseDN = '" + baseDN + "'");
00486             }
00487 
00488             // DN of the role. If no specific role DN, use the baseDN
00489             String lookupRoleDN = null;
00490             if ((roleDN != null) && (!roleDN.equals(""))) {
00491                 lookupRoleDN = roleDN.concat(",").concat(baseDN);
00492             } else {
00493                 lookupRoleDN = baseDN;
00494             }
00495 
00496             // Search the DN with the role filter with the specific argument and
00497             // specific search controls
00498             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00499                 getLogger().log(BasicLevel.DEBUG, "search with lookupRoleDN = '" + lookupRoleDN + "', rolesearchFilter = '"
00500                         + roleSearchFilter + "', dn = '" + dn + "'");
00501             }
00502 
00503             NamingEnumeration answers = dirContext.search(lookupRoleDN, roleSearchFilter, new Object[] {dn}, sctls);
00504 
00505             if (answers == null) {
00506                 if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00507                     getLogger().log(BasicLevel.DEBUG, "answer is null");
00508                 }
00509                 return allCombinedRoles;
00510             }
00511 
00512             Vector vRoles = new Vector();
00513             while (answers.hasMore()) {
00514                 SearchResult answer = (SearchResult) answers.next();
00515                 Attributes roleAttributes = answer.getAttributes();
00516                 if (roleAttributes == null) {
00517                     continue;
00518                 }
00519                 addValueFromAttributeToVector(roleNameAttribute, roleAttributes, vRoles);
00520             }
00521 
00522             for (Enumeration e = vRoles.elements(); e.hasMoreElements();) {
00523                 String roleName = (String) e.nextElement();
00524                 if (!allCombinedRoles.contains(roleName)) {
00525                     allCombinedRoles.add(roleName);
00526                 }
00527             }
00528         } catch (javax.naming.NamingException ne) {
00529             throw new JResourceException("Could not find roles from the user :" + ne.getMessage());
00530         }
00531         user.setCombinedRoles(allCombinedRoles);
00532         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
00533             StringBuffer rolesStr = new StringBuffer();
00534             for (Iterator it = allCombinedRoles.iterator(); it.hasNext();) {
00535                 rolesStr.append((String) it.next());
00536                 rolesStr.append(",");
00537             }
00538             getLogger().log(BasicLevel.DEBUG, "Roles are : " + rolesStr + " for user '" + user.getName() + "'");
00539         }
00540 
00541         return allCombinedRoles;
00542     }
00543 
00548     public String toXML() {
00549 
00550         // Required values
00551 
00552         StringBuffer xml = new StringBuffer("    <ldaprealm name=\"");
00553         xml.append(getName());
00554 
00555         xml.append("\"\n               baseDN=\"");
00556         if (baseDN != null) {
00557             xml.append(baseDN);
00558         }
00559 
00560         if ((initialContextFactory != null) && (!initialContextFactory.equals(""))) {
00561             xml.append("\"\n               initialContextFactory=\"");
00562             xml.append(initialContextFactory);
00563         }
00564 
00565         if ((providerUrl != null) && (!providerUrl.equals(""))) {
00566             xml.append("\"\n               providerUrl=\"");
00567             xml.append(providerUrl);
00568         }
00569 
00570         if ((securityAuthentication != null) && (!securityAuthentication.equals(""))) {
00571             xml.append("\"\n               securityAuthentication=\"");
00572             xml.append(securityAuthentication);
00573         }
00574 
00575         if ((securityPrincipal != null) && (!securityPrincipal.equals(""))) {
00576             xml.append("\"\n               securityPrincipal=\"");
00577             xml.append(securityPrincipal);
00578         }
00579 
00580         if ((securityCredentials != null) && (!securityCredentials.equals(""))) {
00581             xml.append("\"\n               securityCredentials=\"");
00582             xml.append(securityCredentials);
00583         }
00584 
00585         if ((authenticationMode != null) && (!authenticationMode.equals(""))) {
00586             xml.append("\"\n               authenticationMode=\"");
00587             xml.append(authenticationMode);
00588         }
00589 
00590         if ((userPasswordAttribute != null) && (!userPasswordAttribute.equals(""))) {
00591             xml.append("\"\n               userPasswordAttribute=\"");
00592             xml.append(userPasswordAttribute);
00593         }
00594 
00595         if ((userRolesAttribute != null) && (!userRolesAttribute.equals(""))) {
00596             xml.append("\"\n               userRolesAttribute=\"");
00597             xml.append(userRolesAttribute);
00598         }
00599 
00600         if ((roleNameAttribute != null) && (!roleNameAttribute.equals(""))) {
00601             xml.append("\"\n               roleNameAttribute=\"");
00602             xml.append(roleNameAttribute);
00603         }
00604 
00605         if ((userDN != null) && (!userDN.equals(""))) {
00606             xml.append("\"\n               userDN=\"");
00607             xml.append(userDN);
00608         }
00609 
00610         if ((userSearchFilter != null) && (!userSearchFilter.equals(""))) {
00611             xml.append("\"\n               userSearchFilter=\"");
00612             xml.append(userSearchFilter);
00613         }
00614 
00615         if ((roleDN != null) && (!roleDN.equals(""))) {
00616             xml.append("\"\n               roleDN=\"");
00617             xml.append(roleDN);
00618         }
00619 
00620         if ((roleSearchFilter != null) && (!roleSearchFilter.equals(""))) {
00621             xml.append("\"\n               roleSearchFilter=\"");
00622             xml.append(roleSearchFilter);
00623         }
00624 
00625         // Optional values
00626         if ((securityProtocol != null) && (!securityProtocol.equals(""))) {
00627             xml.append("\"\n               securityProtocol=\"");
00628             xml.append(securityProtocol);
00629         }
00630 
00631         if ((language != null) && (!language.equals(""))) {
00632             xml.append("\"\n               language=\"");
00633             xml.append(language);
00634         }
00635 
00636         if ((referral != null) && (!referral.equals(""))) {
00637             xml.append("\"\n               referral=\"");
00638             xml.append(referral);
00639         }
00640 
00641         if ((stateFactories != null) && (!stateFactories.equals(""))) {
00642             xml.append("\"\n               stateFactories=\"");
00643             xml.append(stateFactories);
00644         }
00645 
00646         if ((algorithm != null) && (!algorithm.equals(""))) {
00647             xml.append("\"\n               algorithm=\"");
00648             xml.append(algorithm);
00649         }
00650 
00651         xml.append("\" />");
00652 
00653         return xml.toString();
00654     }
00655 
00660     public String toString() {
00661         return this.toXML();
00662     }
00663 
00672     public Reference getReference() throws NamingException {
00673 
00674         // Build the reference to the factory FACTORY_TYPE
00675         Reference reference = new Reference(FACTORY_TYPE, FACTORY_NAME, null);
00676 
00677         // Add ref addr
00678         reference.add(new StringRefAddr("name", getName()));
00679         reference.add(new StringRefAddr("initialContextFactory", getInitialContextFactory()));
00680         reference.add(new StringRefAddr("providerUrl", getProviderUrl()));
00681         reference.add(new StringRefAddr("securityAuthentication", getSecurityAuthentication()));
00682         reference.add(new StringRefAddr("securityPrincipal", getSecurityPrincipal()));
00683         reference.add(new StringRefAddr("securityCredentials", getSecurityCredentials()));
00684         reference.add(new StringRefAddr("securityProtocol", getSecurityProtocol()));
00685         reference.add(new StringRefAddr("language", getLanguage()));
00686         reference.add(new StringRefAddr("referral", getReferral()));
00687         reference.add(new StringRefAddr("stateFactories", getStateFactories()));
00688         reference.add(new StringRefAddr("authenticationMode", getAuthenticationMode()));
00689         reference.add(new StringRefAddr("userPasswordAttribute", getUserPasswordAttribute()));
00690         reference.add(new StringRefAddr("userRolesAttribute", getUserRolesAttribute()));
00691         reference.add(new StringRefAddr("roleNameAttribute", getRoleNameAttribute()));
00692         reference.add(new StringRefAddr("baseDN", getBaseDN()));
00693         reference.add(new StringRefAddr("userDN", getUserDN()));
00694         reference.add(new StringRefAddr("userSearchFilter", getUserSearchFilter()));
00695         reference.add(new StringRefAddr("roleDN", getRoleDN()));
00696         reference.add(new StringRefAddr("roleSearchFilter", getRoleSearchFilter()));
00697         reference.add(new StringRefAddr("algorithm", algorithm));
00698 
00699         return reference;
00700     }
00701 
00702 
00707     public void setInitialContextFactory(String initialContextFactory) {
00708         this.initialContextFactory = initialContextFactory;
00709     }
00710 
00715     public void setProviderUrl(String providerUrl) {
00716         this.providerUrl = providerUrl;
00717     }
00718 
00725     public void setSecurityAuthentication(String securityAuthentication) {
00726         this.securityAuthentication = securityAuthentication;
00727     }
00728 
00733     public void setSecurityPrincipal(String securityPrincipal) {
00734         this.securityPrincipal = securityPrincipal;
00735     }
00736 
00741     public void setSecurityCredentials(String securityCredentials) {
00742         this.securityCredentials = securityCredentials;
00743     }
00744 
00749     public void setSecurityProtocol(String securityProtocol) {
00750         this.securityProtocol = securityProtocol;
00751     }
00752 
00757     public void setLanguage(String language) {
00758         this.language = language;
00759     }
00760 
00766     public void setReferral(String referral) {
00767         this.referral = referral;
00768     }
00769 
00774     public void setStateFactories(String stateFactories) {
00775         this.stateFactories = stateFactories;
00776     }
00777 
00783     public void setAuthenticationMode(String authenticationMode) {
00784         this.authenticationMode = authenticationMode;
00785     }
00786 
00792     public void setUserPasswordAttribute(String userPasswordAttribute) {
00793         this.userPasswordAttribute = userPasswordAttribute;
00794     }
00795 
00801     public void setUserRolesAttribute(String userRolesAttribute) {
00802         this.userRolesAttribute = userRolesAttribute;
00803     }
00804 
00809     public void setRoleNameAttribute(String roleNameAttribute) {
00810         this.roleNameAttribute = roleNameAttribute;
00811     }
00812 
00817     public void setBaseDN(String baseDN) {
00818         // empty value can't be set
00819         if ((baseDN != null) && (!baseDN.equals(""))) {
00820             this.baseDN = baseDN;
00821         }
00822     }
00823 
00829     public void setUserDN(String userDN) {
00830         if ((userDN != null) && (!userDN.equals(""))) {
00831             this.userDN = userDN;
00832         }
00833     }
00834 
00839     public void setUserSearchFilter(String userSearchFilter) {
00840         this.userSearchFilter = userSearchFilter;
00841     }
00842 
00849     public void setRoleDN(String roleDN) {
00850         this.roleDN = roleDN;
00851     }
00852 
00857     public void setRoleSearchFilter(String roleSearchFilter) {
00858         this.roleSearchFilter = roleSearchFilter;
00859     }
00860 
00865     public void setAlgorithm(String algorithm) {
00866         this.algorithm = algorithm;
00867     }
00868 
00873     public String getInitialContextFactory() {
00874         return initialContextFactory;
00875     }
00876 
00881     public String getProviderUrl() {
00882         return providerUrl;
00883     }
00884 
00890     public String getSecurityAuthentication() {
00891         return securityAuthentication;
00892     }
00893 
00898     public String getSecurityPrincipal() {
00899         return securityPrincipal;
00900     }
00901 
00906     public String getSecurityCredentials() {
00907         return securityCredentials;
00908     }
00909 
00914     public String getSecurityProtocol() {
00915         return securityProtocol;
00916     }
00917 
00922     public String getLanguage() {
00923         return language;
00924     }
00925 
00931     public String getReferral() {
00932         return referral;
00933     }
00934 
00939     public String getStateFactories() {
00940         return stateFactories;
00941     }
00942 
00947     public String getAuthenticationMode() {
00948         return authenticationMode;
00949     }
00950 
00955     public String getUserPasswordAttribute() {
00956         return userPasswordAttribute;
00957     }
00958 
00963     public String getUserRolesAttribute() {
00964         return userRolesAttribute;
00965     }
00966 
00971     public String getRoleNameAttribute() {
00972         return roleNameAttribute;
00973     }
00974 
00979     public String getBaseDN() {
00980         return baseDN;
00981     }
00982 
00988     public String getUserDN() {
00989         return userDN;
00990     }
00991 
00996     public String getUserSearchFilter() {
00997         return userSearchFilter;
00998     }
00999 
01006     public String getRoleDN() {
01007         return roleDN;
01008     }
01009 
01014     public String getRoleSearchFilter() {
01015         return roleSearchFilter;
01016     }
01017 
01022     public String getAlgorithm() {
01023         return algorithm;
01024     }
01025 
01031     protected DirContext getDirContext() throws NamingException {
01032         DirContext dirContext = null;
01033         // Get the InitialDirContext with our env
01034         dirContext = new InitialDirContext(getEnvInitialDirContext());
01035 
01036         return dirContext;
01037     }
01038 
01043     private Hashtable getEnvInitialDirContext() {
01044 
01045         // The environment is a hashtable
01046         Hashtable env = new Hashtable();
01047         env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
01048         env.put(Context.PROVIDER_URL, providerUrl);
01049         env.put(Context.SECURITY_AUTHENTICATION, securityAuthentication);
01050         if ((securityPrincipal != null) && (!securityPrincipal.equals(""))) {
01051             env.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
01052         }
01053         if ((securityCredentials != null) && (!securityCredentials.equals(""))) {
01054             env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
01055         }
01056         if ((language != null) && (!language.equals(""))) {
01057             env.put(Context.LANGUAGE, language);
01058         }
01059         if ((referral != null) && (!referral.equals(""))) {
01060             env.put(Context.REFERRAL, referral);
01061         }
01062         if ((securityProtocol != null) && (!securityProtocol.equals(""))) {
01063             env.put(Context.SECURITY_PROTOCOL, securityProtocol);
01064         }
01065         if ((stateFactories != null) && (!stateFactories.equals(""))) {
01066             env.put(Context.STATE_FACTORIES, stateFactories);
01067         }
01068         return env;
01069     }
01070 
01080     private String readValueFromAttribute(String attrID, Attributes attributes) throws NamingException {
01081 
01082         // No attributes or the attrID is null (must be a non null attribute)
01083         if (attributes == null || attrID == null) {
01084             return null;
01085         }
01086 
01087         Attribute attribute = attributes.get(attrID);
01088 
01089         // Attribute not found
01090         if (attribute == null) {
01091             return null;
01092         }
01093 
01094         Object o = attribute.get();
01095         String value = null;
01096 
01097         if (o instanceof byte[]) {
01098             value = new String((byte[]) o);
01099         } else {
01100             value = o.toString();
01101         }
01102 
01103         return value;
01104     }
01105 
01115     private String readValuesFromAttribute(String attrID, Attributes attributes) throws NamingException {
01116 
01117         // No attributes or the attrID is null (must be a non null attribute)
01118         if (attributes == null || attrID == null) {
01119             return null;
01120         }
01121 
01122         Attribute attribute = attributes.get(attrID);
01123 
01124         // Attribute not found
01125         if (attribute == null) {
01126             return null;
01127         }
01128 
01129         String value = null;
01130         NamingEnumeration e = attribute.getAll();
01131         while (e.hasMore()) {
01132             String s = (String) e.next();
01133             if (value == null) {
01134                 value = s;
01135             } else {
01136                 value = value + "," + s;
01137             }
01138         }
01139         return value;
01140     }
01141 
01150     private void addValueFromAttributeToVector(String attrID, Attributes attributes, Vector v) throws NamingException {
01151 
01152         // No attributes or the attrID is null (must be a non null attribute)
01153         if (attributes == null || attrID == null) {
01154             return;
01155         }
01156 
01157         Attribute attribute = attributes.get(attrID);
01158 
01159         // Attribute not found
01160         if (attribute == null) {
01161             return;
01162         }
01163 
01164         NamingEnumeration e = attribute.getAll();
01165         while (e.hasMore()) {
01166             v.add(e.next());
01167         }
01168 
01169     }
01170 
01175     public void removeMBeans() throws JResourceException {
01176         //no MBeans
01177     }
01178 
01179 }

Generated on Tue Feb 15 15:05:22 2005 for JOnAS by  doxygen 1.3.9.1