⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ldaputil.java

📁 OPIAM stands for Open Identity and Access Management. This Suite will provide modules for user & rig
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.persistence;

import opiam.admin.faare.MessageUtil;
import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.config.javabeans.JBClassDescriptor;
import opiam.admin.faare.config.javabeans.JBFieldDescriptor;
import opiam.admin.faare.config.javabeans.JBLdapConfig;
import opiam.admin.faare.exception.PersistenceException;
import opiam.admin.faare.persistence.javabeans.JBTop;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.utils.StringUtil;

import netscape.ldap.LDAPAttribute;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPEntry;
import netscape.ldap.LDAPException;

import netscape.ldap.util.DN;
import netscape.ldap.util.RDN;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;

import org.apache.log4j.Logger;

import java.lang.reflect.InvocationTargetException;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;


/**
 * Utility classes with static methods only.
 * Enlighten the PersistenceLDAP class.
 */
public final class LdapUtil
{
    /** Utility class. */
    private LdapUtil()
    {
    }

    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(LdapUtil.class);

    /** Applicative connection. */
    private static LDAPConnection appliLd = null;

    /**
     * Replaces token with a property got from current user.
     * @param buf Filter with current user token
     * @param userContext user context
     *
     * @return Filter with replaced value.
     */
    private static String createObjectFilter(StringBuffer buf,
        UserContext userContext)
    {
        _logger.debug("createObjectFilter buf : " + buf);

        String result = null;

        try
        {
            String temp = buf.toString();
            int dot = temp.indexOf(".");
            _logger.debug("createObjectFilter dot : " + dot);

            String var = temp.substring(0, dot);
            _logger.debug("createObjectFilter var : " + var);

            String att = temp.substring(dot + 1);
            _logger.debug("createObjectFilter att : " + att);

            if (var.compareToIgnoreCase(LdapObjectFilter.VAR_CURRENT_USER) == 0)
            {
                Object o = PropertyUtils.getProperty(userContext.getJbUser(),
                        att);
                _logger.debug("createObjectFilter o : " + o);
                result = (String) o;
                _logger.debug("createObjectFilter result : " + result);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * Creates object filter, replacing a property got from current user.
     * Called by LogicalTreeView.
     * @param objectFilter Object filter with current user token
     * @param userContext user context
     *
     * @return Filter with replaced value.
     */
    public static String getObjectFilter(String objectFilter,
        UserContext userContext)
    {
        _logger.debug("getObjectFilter buf : " + objectFilter);

        String result = null;

        try
        {
            if (objectFilter.indexOf(LdapObjectFilter.VAR_CURRENT_USER) != -1)
            {
                int startDol = objectFilter.indexOf("$");
                _logger.debug("getObjectFilter dol : " + startDol);

                StringBuffer temp = new StringBuffer();

                temp.append(objectFilter.substring(startDol + 1));
                _logger.debug("getObjectFilter temp : " + temp);

                int endDol = temp.toString().indexOf("$");
                _logger.debug("getObjectFilter dol : " + startDol);

                StringBuffer temp2 = new StringBuffer();
                temp2.append(temp.substring(0, endDol));
                _logger.debug("getObjectFilter temp2 : " + temp2);

                String temp3 = createObjectFilter(temp2, userContext);
                _logger.debug("getObjectFilter temp3 : " + temp3);

                objectFilter = StringUtil.replace(objectFilter,
                        "$" + temp2 + "$", temp3);
                _logger.debug("getObjectFilter objectFilter : " + objectFilter);

                return objectFilter;
            }
            else
            {
                return objectFilter;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * Gets last modification date as string from an LDAP entry.
     *
     * @param entry LDAP entry
     *
     * @return date or null if last modification date attribute does not exist
     *
     * @throws PersistenceException if entry is null
     */
    public static String getModifyFromLDAPEntry(LDAPEntry entry)
        throws PersistenceException
    {
        if (entry == null)
        {
            throw new PersistenceException(MessageUtil.formatMessage(
                    "MSG_ARGUMENT_NULL", "entry", "getModifyFromLDAPEntry",
                    "PersistenceLDAP"));
        }

        LDAPAttribute att = entry.getAttribute("modifytimestamp");

        if (att == null)
        {
            return null;
        }
        else
        {
            return (att.getStringValueArray()[0]);
        }
    }

    /**
     * Formats a DName.
     * @param dn dname to format
     * @return Formatted DName
     */
    public static String formatDN(String dn)
    {
        DN ldapDN = new DN(dn);
        Vector mRdns = ldapDN.getRDNs();

        String returnedDN = "";

        for (int i = 0; i < mRdns.size(); i++)
        {
            if (i != 0)
            {
                returnedDN += ",";
            }

            returnedDN = returnedDN +
                ((RDN) mRdns.elementAt(i)).toString().trim();
        }

        return returnedDN.toLowerCase();
    }

    /**
     * Gets last modification date as Timestamp object from an LDAP entry.
     *
     * @param entry LDAP entry
     *
     * @return timestamp or null if last modification date attribute does not exist.
     *
     * @throws PersistenceException if entry is null.
     */
    public static Timestamp getTimestampFromLDAPEntry(LDAPEntry entry)
        throws PersistenceException
    {
        if (entry == null)
        {
            throw new PersistenceException(MessageUtil.formatMessage(
                    "MSG_ARGUMENT_NULL", "entry", "getTimestampFromLDAPEntry",
                    "PersistenceLDAP"));
        }

        String modify = ((String) (entry.getAttribute("modifytimestamp")
                                        .getStringValueArray()[0]));
        GregorianCalendar gc = new GregorianCalendar((new Integer(
                    modify.substring(0, 4))).intValue(),
                (new Integer(modify.substring(5, 6))).intValue(),
                (new Integer(modify.substring(7, 8))).intValue(),
                (new Integer(modify.substring(9, 10))).intValue(),
                (new Integer(modify.substring(11, 12))).intValue(),
                (new Integer(modify.substring(13, 14))).intValue());

        return new Timestamp(gc.getTime().getTime());
    }

    /**
     * Gets a text error message corresponding to an LDAP error code.
     * @param le LDAP exception.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -