Search in sources :

Example 51 with Attributes

use of javax.naming.directory.Attributes in project iaf by ibissource.

the class LdapSender method performOperationUpdate.

private String performOperationUpdate(String entryName, ParameterResolutionContext prc, Map paramValueMap, Attributes attrs) throws SenderException, ParameterException {
    String entryNameAfter = entryName;
    if (paramValueMap != null) {
        String newEntryName = (String) paramValueMap.get("newEntryName");
        if (newEntryName != null && StringUtils.isNotEmpty(newEntryName)) {
            if (log.isDebugEnabled())
                log.debug("newEntryName=[" + newEntryName + "]");
            DirContext dirContext = null;
            try {
                dirContext = getDirContext(paramValueMap);
                dirContext.rename(entryName, newEntryName);
                entryNameAfter = newEntryName;
            } catch (NamingException e) {
                String msg;
                // [LDAP: error code 32 - No Such Object...
                if (e.getMessage().startsWith("[LDAP: error code 32 - ")) {
                    msg = "Operation [" + getOperation() + "] failed - wrong entryName [" + entryName + "]";
                } else {
                    msg = "Exception in operation [" + getOperation() + "] entryName [" + entryName + "]";
                }
                storeLdapException(e, prc);
                throw new SenderException(msg, e);
            } finally {
                closeDirContext(dirContext);
            }
        }
    }
    if (manipulationSubject.equals(MANIPULATION_ATTRIBUTE)) {
        if (attrs == null && !entryNameAfter.equals(entryName)) {
            // it should be possible to only 'rename' the entry (without attribute change)
            return DEFAULT_RESULT;
        }
        NamingEnumeration na = attrs.getAll();
        while (na.hasMoreElements()) {
            Attribute a = (Attribute) na.nextElement();
            log.debug("Update attribute: " + a.getID());
            NamingEnumeration values;
            try {
                values = a.getAll();
            } catch (NamingException e1) {
                storeLdapException(e1, prc);
                throw new SenderException("cannot obtain values of Attribute [" + a.getID() + "]", e1);
            }
            while (values.hasMoreElements()) {
                Attributes partialAttrs = new BasicAttributes();
                Attribute singleValuedAttribute;
                String id = a.getID();
                Object value = values.nextElement();
                if (log.isDebugEnabled()) {
                    if (id.toLowerCase().contains("password") || id.toLowerCase().contains("pwd")) {
                        log.debug("Update value: ***");
                    } else {
                        log.debug("Update value: " + value);
                    }
                }
                if (unicodePwd && "unicodePwd".equalsIgnoreCase(id)) {
                    singleValuedAttribute = new BasicAttribute(id, encodeUnicodePwd(value));
                } else {
                    singleValuedAttribute = new BasicAttribute(id, value);
                }
                partialAttrs.put(singleValuedAttribute);
                DirContext dirContext = null;
                try {
                    dirContext = getDirContext(paramValueMap);
                    dirContext.modifyAttributes(entryNameAfter, DirContext.REPLACE_ATTRIBUTE, partialAttrs);
                } catch (NamingException e) {
                    String msg;
                    // [LDAP: error code 32 - No Such Object...
                    if (e.getMessage().startsWith("[LDAP: error code 32 - ")) {
                        msg = "Operation [" + getOperation() + "] failed - wrong entryName [" + entryNameAfter + "]";
                    } else {
                        msg = "Exception in operation [" + getOperation() + "] entryName [" + entryNameAfter + "]";
                    }
                    // result = DEFAULT_RESULT_UPDATE_NOK;
                    storeLdapException(e, prc);
                    throw new SenderException(msg, e);
                } finally {
                    closeDirContext(dirContext);
                }
            }
        }
        return DEFAULT_RESULT;
    } else {
        DirContext dirContext = null;
        try {
            dirContext = getDirContext(paramValueMap);
            // dirContext.rename(newEntryName, oldEntryName);
            // result = DEFAULT_RESULT;
            dirContext.rename(entryName, entryName);
            return "<LdapResult>Deze functionaliteit is nog niet beschikbaar - naam niet veranderd.</LdapResult>";
        } catch (NamingException e) {
            // [LDAP: error code 68 - Entry Already Exists]
            if (!e.getMessage().startsWith("[LDAP: error code 68 - ")) {
                storeLdapException(e, prc);
                throw new SenderException(e);
            }
            return DEFAULT_RESULT_CREATE_NOK;
        } finally {
            closeDirContext(dirContext);
        }
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) NamingEnumeration(javax.naming.NamingEnumeration) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) SenderException(nl.nn.adapterframework.core.SenderException)

Example 52 with Attributes

use of javax.naming.directory.Attributes in project iaf by ibissource.

the class LdapSender method removeValuesFromAttributes.

// protected Attributes getAttributesFromParameters(ParameterResolutionContext prc) throws ParameterException {
// Parameter2AttributeHelper helper = new Parameter2AttributeHelper();
// prc.forAllParameters(paramList, helper);
// Attributes result = helper.result;
// 
// log.debug("LDAP STEP:	applyParameters(String message, ParameterResolutionContext prc)");
// log.debug("collected LDAP Attributes from parameters ["+result.toString()+"]");
// return result;
// }
// 
// private class Parameter2AttributeHelper implements IParameterHandler {
// private Attributes result = new BasicAttributes(true); // ignore attribute name case
// 
// public void handleParam(String paramName, Object value) throws ParameterException {
// 
// if (result.get(paramName) == null)
// result.put(new BasicAttribute(paramName, value));
// else
// result.get(paramName).add(value);
// 
// log.debug("LDAP STEP:	(Parameter2 ATTRIBUTE Helper)handleParam(String paramName, Object value) - result = [" + result.toString() +"]");
// }
// }
/**
 *Strips all the values from the attributes in <code>input</code>. This is performed to be able to delete
 *the attributes without having to match the values. If values exist they must be exactly matched too in
 *order to delete the attribute.
 */
protected Attributes removeValuesFromAttributes(Attributes input) {
    Attributes result = new BasicAttributes(true);
    // ignore attribute name case
    NamingEnumeration enumeration = input.getIDs();
    while (enumeration.hasMoreElements()) {
        String attrId = (String) enumeration.nextElement();
        result.put(new BasicAttribute(attrId));
    }
    return result;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration)

Example 53 with Attributes

use of javax.naming.directory.Attributes in project Spark by igniterealtime.

the class LoginDialog method getDnsKdc.

/**
 * Use DNS to lookup a KDC
 * @param realm The realm to look up
 * @return the KDC hostname
 */
private String getDnsKdc(String realm) {
    // _kerberos._udp.$realm
    try {
        Hashtable<String, String> env = new Hashtable<>();
        env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
        DirContext context = new InitialDirContext(env);
        Attributes dnsLookup = context.getAttributes("_kerberos._udp." + realm, new String[] { "SRV" });
        ArrayList<Integer> priorities = new ArrayList<>();
        HashMap<Integer, List<String>> records = new HashMap<>();
        for (Enumeration<?> e = dnsLookup.getAll(); e.hasMoreElements(); ) {
            Attribute record = (Attribute) e.nextElement();
            for (Enumeration<?> e2 = record.getAll(); e2.hasMoreElements(); ) {
                String sRecord = (String) e2.nextElement();
                String[] sRecParts = sRecord.split(" ");
                Integer pri = Integer.valueOf(sRecParts[0]);
                if (priorities.contains(pri)) {
                    List<String> recs = records.get(pri);
                    if (recs == null)
                        recs = new ArrayList<>();
                    recs.add(sRecord);
                } else {
                    priorities.add(pri);
                    List<String> recs = new ArrayList<>();
                    recs.add(sRecord);
                    records.put(pri, recs);
                }
            }
        }
        Collections.sort(priorities);
        List<String> l = records.get(priorities.get(0));
        String toprec = l.get(0);
        String[] sRecParts = toprec.split(" ");
        return sRecParts[3];
    } catch (NamingException e) {
        return "";
    }
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) List(java.util.List) NamingException(javax.naming.NamingException)

Example 54 with Attributes

use of javax.naming.directory.Attributes in project ldapchai by ldapchai.

the class JNDIProviderImpl method readStringAttributes.

@LdapOperation
public final Map<String, String> readStringAttributes(final String entryDN, final Set<String> attributes) throws ChaiUnavailableException, ChaiOperationException {
    activityPreCheck();
    getInputValidator().readStringAttributes(entryDN, attributes);
    // Allocate a return object
    final Map<String, String> returnObj = new LinkedHashMap<>();
    // get ldap connection
    final LdapContext ldapConnection = getLdapConnection();
    // Get only the Attribute that is passed in.
    final Attributes returnedAttribs;
    NamingEnumeration attrEnumeration = null;
    try {
        if (attributes == null || attributes.isEmpty()) {
            returnedAttribs = ldapConnection.getAttributes(addJndiEscape(entryDN), null);
            attrEnumeration = returnedAttribs.getAll();
            while (attrEnumeration.hasMoreElements()) {
                final Attribute attribute = (Attribute) attrEnumeration.nextElement();
                // Put an entry in the map, if there are no values insert null, otherwise, insert the first value
                if (attribute != null) {
                    returnObj.put(attribute.getID(), attribute.get().toString());
                }
            }
        } else {
            // Loop through each requested attribute
            returnedAttribs = ldapConnection.getAttributes(addJndiEscape(entryDN), attributes.toArray(new String[attributes.size()]));
            for (final String loopAttr : attributes) {
                // Ask JNDI for the attribute (which actually includes all the values)
                final Attribute attribute = returnedAttribs.get(loopAttr);
                // Put an entry in the map, if there are no values insert null, otherwise, insert the first value
                if (attribute != null) {
                    returnObj.put(loopAttr, attribute.get().toString());
                }
            }
        }
    } catch (NamingException e) {
        convertNamingException(e);
        return null;
    } finally {
        if (attrEnumeration != null) {
            try {
                attrEnumeration.close();
            } catch (NamingException e) {
            // nothing to do
            }
        }
    }
    return returnObj;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) NamingException(javax.naming.NamingException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext) LinkedHashMap(java.util.LinkedHashMap)

Example 55 with Attributes

use of javax.naming.directory.Attributes in project ldapchai by ldapchai.

the class JNDIProviderImpl method createEntry.

@LdapOperation
@ModifyOperation
public final void createEntry(final String entryDN, final Set<String> baseObjectClasses, final Map<String, String> stringAttributes) throws ChaiOperationException, ChaiUnavailableException {
    activityPreCheck();
    getInputValidator().createEntry(entryDN, baseObjectClasses, stringAttributes);
    final Attributes attrs = new BasicAttributes();
    // Put in the base object class an attribute
    final BasicAttribute objectClassAttr = new BasicAttribute(ChaiConstant.ATTR_LDAP_OBJECTCLASS);
    for (final String loopClass : baseObjectClasses) {
        objectClassAttr.add(loopClass);
    }
    attrs.put(objectClassAttr);
    // Add each of the attributes required.
    for (final Map.Entry<String, String> entry : stringAttributes.entrySet()) {
        attrs.put(entry.getKey(), entry.getValue());
    }
    // Create the object.
    final DirContext ldapConnection = getLdapConnection();
    try {
        ldapConnection.createSubcontext(addJndiEscape(entryDN), attrs);
    } catch (NamingException e) {
        convertNamingException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Attributes (javax.naming.directory.Attributes)252 Attribute (javax.naming.directory.Attribute)135 SearchResult (javax.naming.directory.SearchResult)87 NamingException (javax.naming.NamingException)84 BasicAttributes (javax.naming.directory.BasicAttributes)72 ArrayList (java.util.ArrayList)61 BasicAttribute (javax.naming.directory.BasicAttribute)56 SearchControls (javax.naming.directory.SearchControls)55 DirContext (javax.naming.directory.DirContext)49 NamingEnumeration (javax.naming.NamingEnumeration)44 Test (org.junit.Test)34 InitialDirContext (javax.naming.directory.InitialDirContext)32 LdapContext (javax.naming.ldap.LdapContext)29 HashMap (java.util.HashMap)25 InitialLdapContext (javax.naming.ldap.InitialLdapContext)24 Hashtable (java.util.Hashtable)20 HashSet (java.util.HashSet)18 Map (java.util.Map)17 IOException (java.io.IOException)16 Identity (org.olat.core.id.Identity)16