ldap.UserAccountImpl.java Source code

Java tutorial

Introduction

Here is the source code for ldap.UserAccountImpl.java

Source

/**
* Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org]
*
* This file is part of Redbasin OpenDocShare community project.
*
* Redbasin OpenDocShare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package ldap;

import ldap.UserAccount;
import util.LdapConstants;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingEnumeration;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UserAccountImpl extends BasicAttributes implements UserAccount {

    protected final Log logger = LogFactory.getLog(getClass());

    public String getUserID() {
        try {
            //return get(Config.USER_NAMING_ATT).get().toString();
            return get(LdapConstants.ldapDnAttrType).get().toString();
        } catch (NamingException e) {
            logger.info("Unexpected Internal error in UserAccountImple: error was: " + e.getMessage());
            return "Unexpected Internal error in UserAcountImple: error was" + e.getMessage();
        }
    }

    /*
        public String getUserDN() {
    //return Config.USER_NAMING_ATT + "=" + getUserID() + "," + Config.USER_BASE_DN;
    return LdapConstants.ldapDnAttrType + "=" + getUserID() + "," + LdapConstants.ldapUserBaseDn;
        }
    */

    public UserAccountImpl() {
        super();
    }

    public UserAccountImpl(Attributes atts) throws NamingException {
        NamingEnumeration attList = atts.getAll();
        while (attList.hasMore()) {
            Attribute att = (Attribute) attList.next();
            put(att);
        }
    }

    public String toString() {
        StringBuffer buffer = new StringBuffer();
        String name = null;
        try {
            NamingEnumeration attList = getAll();
            while (attList.hasMore()) {
                Attribute att = (Attribute) attList.next();
                //if (att.getID().equals(Config.USER_NAMING_ATT))
                if (att.getID().equals(LdapConstants.ldapAttrUid))
                    name = att.get().toString() + "\n";

                buffer.append("    ").append(att.getID()).append(": ");

                if (att.size() == 1)
                    buffer.append(att.get().toString()).append("\n");
                else {
                    NamingEnumeration values = att.getAll();
                    buffer.append("\n");
                    while (values.hasMore())
                        buffer.append("        ").append(values.next()).append("\n");
                }
            }
            if (name != null)
                buffer.insert(0, name);
        } catch (NamingException e) {
            return "Unexpected Internal Error dumping UserAccount to text.\nError was: " + e.getMessage();
        }
        return buffer.toString();
    }
}