Search in sources :

Example 96 with DirContext

use of javax.naming.directory.DirContext in project fess by codelibs.

the class LdapManager method login.

public OptionalEntity<FessUser> login(final String username, final String password) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    if (StringUtil.isBlank(fessConfig.getLdapProviderUrl())) {
        return OptionalEntity.empty();
    }
    if (!validate()) {
        return OptionalEntity.empty();
    }
    final Hashtable<String, String> env = createSearchEnv(username, password);
    try (DirContextHolder holder = getDirContext(() -> env)) {
        final DirContext context = holder.get();
        if (logger.isDebugEnabled()) {
            logger.debug("Logged in.", context);
        }
        return OptionalEntity.of(createLdapUser(username, env));
    } catch (final Exception e) {
        logger.debug("Login failed.", e);
    }
    return OptionalEntity.empty();
}
Also used : InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) NamingException(javax.naming.NamingException) LdapOperationException(org.codelibs.fess.exception.LdapOperationException)

Example 97 with DirContext

use of javax.naming.directory.DirContext in project Lucee by lucee.

the class LDAPClient method modifydn.

/**
 *  modifies distinguished name attribute for LDAP entries on LDAP server
 * @param dn
 * @param attributes
 * @throws NamingException
 */
public void modifydn(String dn, String attributes) throws NamingException {
    DirContext ctx = new InitialDirContext(env);
    ctx.rename(dn, attributes);
    ctx.close();
}
Also used : InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 98 with DirContext

use of javax.naming.directory.DirContext in project Lucee by lucee.

the class LDAPClient method add.

/**
 * adds LDAP entries to LDAP server
 * @param dn
 * @param attributes
 * @param delimiter
 * @throws NamingException
 * @throws PageException
 */
public void add(String dn, String attributes, String delimiter, String seperator) throws NamingException, PageException {
    DirContext ctx = new InitialDirContext(env);
    ctx.createSubcontext(dn, toAttributes(attributes, delimiter, seperator));
    ctx.close();
}
Also used : InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 99 with DirContext

use of javax.naming.directory.DirContext in project Lucee by lucee.

the class LDAPClient method modify.

public void modify(String dn, int modifytype, String strAttributes, String delimiter, String separator) throws NamingException, PageException {
    DirContext context = new InitialDirContext(env);
    String[] strArrAttributes = toStringAttributes(strAttributes, delimiter);
    int count = 0;
    for (int i = 0; i < strArrAttributes.length; i++) {
        String[] attributesValues = getAttributesValues(strArrAttributes[i], separator);
        if (attributesValues == null)
            count++;
        else
            count += attributesValues.length;
    }
    ModificationItem[] modItems = new ModificationItem[count];
    BasicAttribute basicAttr = null;
    int k = 0;
    for (int i = 0; i < strArrAttributes.length; i++) {
        String attribute = strArrAttributes[i];
        String type = getAttrValueType(attribute);
        String[] values = getAttributesValues(attribute, separator);
        if (modifytype == DirContext.REPLACE_ATTRIBUTE) {
            if (values == null)
                basicAttr = new BasicAttribute(type);
            else
                basicAttr = new BasicAttribute(type, values[0]);
            modItems[k] = new ModificationItem(modifytype, basicAttr);
            k++;
            if (values != null && values.length > 1) {
                for (int j = 1; j < values.length; j++) {
                    basicAttr = new BasicAttribute(type, values[j]);
                    modItems[k] = new ModificationItem(DirContext.ADD_ATTRIBUTE, basicAttr);
                    k++;
                }
            }
        } else {
            for (int j = 0; j < values.length; j++) {
                if (type != null || modifytype == DirContext.ADD_ATTRIBUTE)
                    basicAttr = new BasicAttribute(type, values[j]);
                else
                    basicAttr = new BasicAttribute(values[j]);
                modItems[k] = new ModificationItem(modifytype, basicAttr);
                k++;
            }
        }
    }
    context.modifyAttributes(dn, modItems);
    context.close();
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 100 with DirContext

use of javax.naming.directory.DirContext in project Payara by payara.

the class ContainerBase method setResources.

/**
 * Set the resources DirContext object with which this Container is
 * associated.
 *
 * @param resources The newly associated DirContext
 * @throws Exception
 */
// XXX: Is exception ever thrown?
@Override
public void setResources(DirContext resources) throws Exception {
    // Called from StandardContext.setResources()
    // <- StandardContext.start()
    // <- ContainerBase.addChildInternal()
    // Change components if necessary
    DirContext oldResources;
    try {
        writeLock.lock();
        oldResources = this.resources;
        if (oldResources == resources)
            return;
        Hashtable<String, String> env = new Hashtable<String, String>();
        if (getParent() != null)
            env.put(ProxyDirContext.HOST, getParent().getName());
        env.put(ProxyDirContext.CONTEXT, getName());
        this.resources = new ProxyDirContext(env, resources);
    // Report this property change to interested listeners
    } finally {
        writeLock.unlock();
    }
    support.firePropertyChange("resources", oldResources, this.resources);
}
Also used : ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext)

Aggregations

DirContext (javax.naming.directory.DirContext)111 NamingException (javax.naming.NamingException)51 InitialDirContext (javax.naming.directory.InitialDirContext)43 SearchResult (javax.naming.directory.SearchResult)27 SearchControls (javax.naming.directory.SearchControls)24 Attributes (javax.naming.directory.Attributes)21 Attribute (javax.naming.directory.Attribute)17 IOException (java.io.IOException)16 NamingEnumeration (javax.naming.NamingEnumeration)16 Hashtable (java.util.Hashtable)14 Test (org.junit.Test)14 DistinguishedName (org.springframework.ldap.core.DistinguishedName)11 ProxyDirContext (org.apache.naming.resources.ProxyDirContext)10 WebDirContext (org.apache.naming.resources.WebDirContext)9 ArrayList (java.util.ArrayList)8 BaseDirContext (org.apache.naming.resources.BaseDirContext)8 FileDirContext (org.apache.naming.resources.FileDirContext)8 WARDirContext (org.apache.naming.resources.WARDirContext)8 Name (javax.naming.Name)7 BasicAttribute (javax.naming.directory.BasicAttribute)7