ldap.Entry.java Source code

Java tutorial

Introduction

Here is the source code for ldap.Entry.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 javax.naming.directory.*;
import javax.naming.ldap.LdapName;
import javax.naming.Name;
import javax.naming.InvalidNameException;
import javax.naming.NamingException;
import java.util.Enumeration;
import util.LdapConstants;

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

/**
 * Combines an Attributes object with a name to reflect a directory entry.
 * *
 * Provides a handful of utility methods for easy creation and testing
 */
public class Entry extends BasicAttributes implements Comparable {
    public static final String OBJECTCLASS = "objectClass";
    protected static final Log logger = LogFactory.getLog("ldap.SearchUtility");
    protected LdapName name;

    public Entry(SearchResult result) throws InvalidNameException {
        this(new LdapName(result.getNameInNamespace()), result.getAttributes());
    }

    /**
     *
     * @param entryName the unique distinguished name of the entry.  may be null.
     * @param entryAtts the attributes to add to this entry... may be null.
     */
    public Entry(LdapName entryName, Attributes entryAtts) {
        super(true);

        name = entryName;

        if (entryAtts != null) {
            Enumeration newAtts = entryAtts.getAll();
            while (newAtts.hasMoreElements())
                this.put((Attribute) newAtts.nextElement());
        }
    }

    public Entry(LdapName name, Attribute[] atts) {
        this(name, makeAtts(atts));
    }

    public Entry(Name name, Attribute[] atts) throws InvalidNameException {
        this(new LdapName(name.toString()), makeAtts(atts));
    }

    public Entry(Name name, Attributes atts) throws InvalidNameException {
        this(new LdapName(name.toString()), atts);
    }

    public Entry(String DN, Attribute[] atts) throws InvalidNameException {
        this(new LdapName(DN), makeAtts(atts));
    }

    public Entry(String DN) throws InvalidNameException {
        super(true);
        name = new LdapName(DN);
    }

    public Entry(LdapName name) {
        super(true);
        this.name = name;
    }

    public Entry() {
        super(true);
        name = null;
    }

    public LdapName getName() {
        return name;
    }

    /**
     * Note that this does not change the underlying name in the directory, and is not
     * a substitute for a 'rename' directory call.  This is only intended for messing around
     * with an entry *before* it is written to the directory.
     * @param newName
     */
    public void setName(LdapName newName) {
        name = newName;
    }

    public String toString() {
        try {
            StringBuffer result = new StringBuffer();
            result.append("name:").append(getStringName()).append("\n");
            Enumeration<String> attNames = getIDs();
            while (attNames.hasMoreElements()) {
                String id = attNames.nextElement();
                //result.append("\tatt ").append(id).append(": ");
                result.append(id).append(":");
                String[] vals = getValues(id);
                if (vals.length == 1)
                    result.append(vals[0]).append("\n");
                else {
                    for (String val : vals)
                        //result.append("\n\t").append(val);
                        result.append(val);
                    //result.append("\n");
                }
            }
            return result.toString();
        } catch (NamingException e) {
            return "unable to list attribute due to exception: " + e.getMessage(); // very rare
        }
    }

    public String getStringName() {
        return (name == null) ? "" : name.toString();
    }

    public Attribute getObjectClasses() {
        return get(OBJECTCLASS);
    }

    /**
     * Utility method to get as a String an attribute value.  Returns 'first'
     * attribute value if multi valued.
     * @param attributeName
     * @return null if attribute doesn't exist.
     * @throws NamingException
     */
    public String getValue(String attributeName) throws NamingException {
        Attribute att = get(attributeName);
        if (att == null)
            return null;
        if (att.size() == 0) // can this happen?
            return "";

        Object value = att.get();
        if (value == null)
            return null;

        return value.toString();
    }

    /**
     * Utility method to get as a String all the attribute values
     * of a particular attribute.
     * @param attributeName
     * @return null if attribute does not exist, an array of string values otherwise.
     * @throws NamingException
     */
    public String[] getValues(String attributeName) throws NamingException {
        Attribute att = get(attributeName);
        if (att == null)
            return null;

        String[] returnVals = new String[att.size()];

        for (int i = 0; i < att.size(); i++)
            returnVals[i] = att.get(i).toString();

        return returnVals;
    }

    /**
     * Utility method - useful for creating a multi valued attribute for the Entry constructor
     * @param ID
     * @param vals
     * @return a newly created multi valued attribute
     */
    public static BasicAttribute makeAtt(String ID, String[] vals) {
        BasicAttribute att = new BasicAttribute(ID);
        for (String val : vals)
            att.add(val);
        return att;
    }

    /**
     * Utility method - useful for creating a multi valued attributes for the Entry constructor,
     * esp when chained with a bunch of 'makeAtt()' calls.
     * @param vals
     * @return a newly created set of attributes
     */
    public static BasicAttributes makeAtts(Attribute[] vals) {
        BasicAttributes atts = new BasicAttributes();
        for (Attribute val : vals)
            atts.put(val);
        return atts;
    }

    public int compareTo(Object o) {
        Entry compareMe = (Entry) o;

        try {
            //String name1 = getValue(Config.USER_NAMING_ATT);
            //String name2 = compareMe.getValue(Config.USER_NAMING_ATT);
            String name1 = getValue(LdapConstants.ldapAttrUid);
            String name2 = compareMe.getValue(LdapConstants.ldapAttrUid);
            return name1.compareTo(name2);
        } catch (NamingException e) {
            logger.info("Unexpected Exception while sorting users: " + e.getMessage());
            return 0;
        }
    }
}