Java Code Examples for javax.naming.directory.Attribute#getID()

The following examples show how to use javax.naming.directory.Attribute#getID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Rdn.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of {@code attrSet} cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 2
Source File: Rdn.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 3
Source File: NameAwareAttribute.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a new instance from the supplied Attribute.
 *
 * @param attribute the Attribute to copy.
 */
public NameAwareAttribute(Attribute attribute) {
    this(attribute.getID(), attribute.isOrdered());
    try {
        NamingEnumeration<?> incomingValues = attribute.getAll();
        while(incomingValues.hasMore()) {
            this.add(incomingValues.next());
        }
    } catch (NamingException e) {
        throw LdapUtils.convertLdapException(e);
    }

    if (attribute instanceof NameAwareAttribute) {
        NameAwareAttribute nameAwareAttribute = (NameAwareAttribute) attribute;
        populateValuesAsNames(nameAwareAttribute, this);
    }
}
 
Example 4
Source File: Rdn.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 5
Source File: Rdn.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 6
Source File: GUISSOLdapClient.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private List<String> formatUserEnName(SearchResult sResult) {

    if (null == sResult) {
        return Collections.emptyList();
    }

    List<String> result = new ArrayList<String>();
    try {
        String memberKey = ldapConfig.get("memberKey");
        NamingEnumeration namingEnumeration = sResult.getAttributes().getAll();
        while (namingEnumeration.hasMoreElements()) {
            Attribute attr = (Attribute) namingEnumeration.next();
            String attrId = attr.getID();
            if (memberKey.equals(attrId)) {
                List<String> userEnNames = formatUserEnName(attr);
                result.addAll(userEnNames);
            }
        }

    }
    catch (Exception e) {
        loggerError("formatUserEnName 619", "", e);
    }
    return result;
}
 
Example 7
Source File: Jetel2LdapData.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 
 */
@Override
public void setAttribute(Attribute attr, DataField df) throws BadDataFormatException {

	/*
	 * df is null in the DataRecord. It's a real problem, 
	 * if the value is null, df is not and reply true to isNull.			 
	 */
	if (df == null) {
		throw new NullPointerException("Field " + attr.getID() + " is null.");
	} else if (df.getType() != DataFieldMetadata.BYTE_FIELD
			&& df.getType() != DataFieldMetadata.BYTE_FIELD_COMPRESSED) {
		throw new BadDataFormatException("LDAP transformation exception : Field " + attr.getID() + " is not a Byte array.");
	} else if (df.isNull()) {
		// Set Ldap Attr value to null
		attr.clear();
	} else {
		Object[] values = getvalues(df);
		for(int i = 0; i < values.length; i++) {
			Object o = values[i];
			if (!attr.add(o)) {
				throw new BadDataFormatException("LDAP transformation exception : Field " + attr.getID() + " is not a Byte array.");
			}
		}
	}
}
 
Example 8
Source File: Rdn.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 9
Source File: Rdn.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 10
Source File: Rdn.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 11
Source File: Rdn.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 12
Source File: Rdn.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 13
Source File: Rdn.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 14
Source File: Rdn.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 15
Source File: Jetel2LdapData.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 
 */
@Override
public void setAttribute(Attribute attr, DataField df)
		throws BadDataFormatException {

	/*
	 * df is null in the DataRecord. It's a real problem, 
	 * if the value is null, df is not and reply true to isNull.			 
	 */
	if (df == null) {
		throw new NullPointerException("Field " + attr.getID()
				+ " is null.");
	} else if (df.getType() != DataFieldMetadata.STRING_FIELD) {
		throw new BadDataFormatException(
				"LDAP transformation exception : Field " + attr.getID()
						+ " is not a String.");
	} else if (df.isNull()) {
		// Set Ldap Attr value to null
		attr.clear();
	} else {
		Object[] values = getvalues(df);
		for(int i = 0; i < values.length; i++) {
			Object o = values[i];
			if (!attr.add(o)) {
				throw new BadDataFormatException(
						"LDAP transformation exception : Field "
								+ attr.getID() + " is not a String.");
			}
		}
	}
}
 
Example 16
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Map<String, List<String>> parseAttributeValues(
        final NamingEnumeration attributeEnum,
        final boolean returnAllValues
)
        throws NamingException
{
    final Map<String, List<String>> attrValues = new HashMap<String, List<String>>();
    if ( attributeEnum != null && attributeEnum.hasMore() )
    {
        while ( attributeEnum.hasMore() )
        {
            final Attribute loopAttribute = ( Attribute ) attributeEnum.next();
            final String attrName = loopAttribute.getID();
            final List<String> valueList = new ArrayList<String>();
            for ( NamingEnumeration attrValueEnum = loopAttribute.getAll(); attrValueEnum.hasMore(); )
            {
                final Object value = attrValueEnum.next();
                valueList.add( value.toString() );
                if ( !returnAllValues )
                {
                    attrValueEnum.close();
                    break;
                }
            }
            attrValues.put( attrName, Collections.unmodifiableList( valueList ) );
        }
    }
    return Collections.unmodifiableMap( attrValues );
}
 
Example 17
Source File: GUISSOLdapClient.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private String formatEmailInfo(Attribute attr) {

    if (null == attr) {
        return "";
    }

    StringBuilder values = new StringBuilder();
    boolean isFormat = false;
    try {

        String formatS = "=";
        String attrId = attr.getID();
        String groupKey = ldapConfig.get("groupKey");
        String groupTag = ldapConfig.get("groupTag");
        for (NamingEnumeration vals = attr.getAll(); vals.hasMore();) {
            String strValue = vals.next().toString();
            if (groupKey.equals(attrId) && strValue.indexOf(groupTag) >= 0) {

                values.append(",");
                isFormat = true;

                if (strValue.indexOf(formatS) == -1) {
                    values.append(strValue);
                    continue;
                }

                int begin = strValue.indexOf(formatS) + formatS.length();
                int end = strValue.indexOf(",");
                values.append(strValue.substring(begin, end));

            }
        }
    }
    catch (Exception e) {
        loggerError("formatEmailInfo 555", "", e);
        throw new ApphubException(e);
    }
    /**
     * 去除第一个逗号
     */
    String result = "";
    if (isFormat) {
        result = values.toString().substring(1);
    }
    return result;
}
 
Example 18
Source File: ModifyAttributesOperationRecorder.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
/**
 * Get a ModificationItem to use for rollback of the supplied modification.
 * 
 * @param originalAttributes
 *            All Attributes of the target DN that are affected of any of
 *            the ModificationItems.
 * @param modificationItem
 *            the ModificationItem to create a rollback item for.
 * @return A ModificationItem to use for rollback of the supplied
 *         ModificationItem.
 */
protected ModificationItem getCompensatingModificationItem(
        Attributes originalAttributes, ModificationItem modificationItem) {
    Attribute modificationAttribute = modificationItem.getAttribute();
    Attribute originalAttribute = originalAttributes
            .get(modificationAttribute.getID());

    if (modificationItem.getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        if (modificationAttribute.size() == 0) {
            // If the modification attribute size it means that the
            // Attribute should be removed entirely - we should store a
            // ModificationItem to restore all present values for rollback.
            return new ModificationItem(DirContext.ADD_ATTRIBUTE,
                    (Attribute) originalAttribute.clone());
        } else {
            // The rollback modification will be to re-add the removed
            // attribute values.
            return new ModificationItem(DirContext.ADD_ATTRIBUTE,
                    (Attribute) modificationAttribute.clone());
        }
    } else if (modificationItem.getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        if (originalAttribute != null) {
            return new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    (Attribute) originalAttribute.clone());
        } else {
            // The attribute doesn't previously exist - the rollback
            // operation will be to remove the attribute.
            return new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
                    new BasicAttribute(modificationAttribute.getID()));
        }
    } else {
        // An ADD_ATTRIBUTE operation
        if (originalAttribute == null) {
            // The attribute doesn't previously exist - the rollback
            // operation will be to remove the attribute.
            return new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
                    new BasicAttribute(modificationAttribute.getID()));
        } else {
            // The attribute does exist before - we should store the
            // previous value and it should be used for replacing in
            // rollback.
            return new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    (Attribute) originalAttribute.clone());
        }
    }
}
 
Example 19
Source File: LdapUtil.java    From jeecg with Apache License 2.0 4 votes vote down vote up
/**
 * @param base
 *            :根节点(在这里是"dc=example,dc=com")
 * @param scope
 *            :搜索范围,分为"base"(本节点),"one"(单层),""(遍历)
 * @param filter
 *            :指定子节点(格式为"(objectclass=*)",*是指全部,你也可以指定某一特定类型的树节点)
 */
public void searchInformation(String base, String scope, String filter,
		DirContext dc) {
	SearchControls sc = new SearchControls();
	if (scope.equals("base")) {
		sc.setSearchScope(SearchControls.OBJECT_SCOPE);
	} else if (scope.equals("one")) {
		sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
	} else {
		sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
	}
	NamingEnumeration<?> ne = null;
	try {
		ne = dc.search(base, filter, sc);
		// Use the NamingEnumeration object to cycle through
		// the result set.
		while (ne.hasMore()) {
			//System.out.println();
			SearchResult sr = (SearchResult) ne.next();
			String name = sr.getName();
			if (base != null && !base.equals("")) {
				LogUtil.info("entry: " + name + "," + base);
			} else {
				LogUtil.info("entry: " + name);
			}

			Attributes at = sr.getAttributes();
			NamingEnumeration<?> ane = at.getAll();
			while (ane.hasMore()) {
				Attribute attr = (Attribute) ane.next();
				String attrType = attr.getID();
				NamingEnumeration<?> values = attr.getAll();
				// Another NamingEnumeration object, this time
				// to iterate through attribute values.
				while (values.hasMore()) {
					Object oneVal = values.nextElement();
					if (oneVal instanceof String) {
						LogUtil.info(attrType + ": "+ (String) oneVal);
					} else {
						LogUtil.info(attrType + ": "+ new String((byte[]) oneVal));
					}
				}
			}
		}
	} catch (Exception nex) {
		System.err.println("Error: " + nex.getMessage());
		nex.printStackTrace();
	}
}
 
Example 20
Source File: LdapConnection.java    From hop with Apache License 2.0 4 votes vote down vote up
public RowMeta getFields( String searchBase ) throws HopException {
  RowMeta fields = new RowMeta();
  List<String> fieldsl = new ArrayList<>();
  try {
    search( searchBase, null, 0, null, SEARCH_SCOPE_SUBTREE_SCOPE );
    Attributes attributes = null;
    fieldsl = new ArrayList<>();
    while ( ( attributes = getAttributes() ) != null ) {

      NamingEnumeration<? extends Attribute> ne = attributes.getAll();

      while ( ne.hasMore() ) {
        Attribute attr = ne.next();
        String fieldName = attr.getID();
        if ( !fieldsl.contains( fieldName ) ) {
          fieldsl.add( fieldName );

          String attributeValue = attr.get().toString();
          int valueType;

          // Try to determine the data type
          //
          if ( IsDate( attributeValue ) ) {
            valueType = IValueMeta.TYPE_DATE;
          } else if ( IsInteger( attributeValue ) ) {
            valueType = IValueMeta.TYPE_INTEGER;
          } else if ( IsNumber( attributeValue ) ) {
            valueType = IValueMeta.TYPE_NUMBER;
          } else {
            valueType = IValueMeta.TYPE_STRING;
          }

          IValueMeta value = ValueMetaFactory.createValueMeta( fieldName, valueType );
          fields.addValueMeta( value );
        }
      }
    }
    return fields;
  } catch ( Exception e ) {
    throw new HopException( BaseMessages.getString( PKG, "LdapConnection.Error.RetrievingFields" ) );
  } finally {
    fieldsl = null;
  }
}