Search in sources :

Example 21 with Attributes

use of javax.naming.directory.Attributes in project OpenAM by OpenRock.

the class AMSetupServlet method getLdapHostAndPort.

// Method to get hostname and port number with the
// provided Domain Name for Active Directory user data store.
private String[] getLdapHostAndPort(String domainName) throws NamingException, IOException {
    if (!domainName.endsWith(".")) {
        domainName += '.';
    }
    DirContext ictx;
    // The resource record type A is defined in RFC 1035.
    try {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        ictx = new InitialDirContext(env);
        Attributes attributes = ictx.getAttributes(domainName, new String[] { "A" });
        Attribute attrib = attributes.get("A");
        if (attrib == null) {
            throw new NamingException();
        }
    } catch (NamingException e) {
        // throw exception.
        throw e;
    }
    // then look for the LDAP server
    String serverHostName;
    String serverPortStr;
    String ldapServer = "_ldap._tcp." + domainName;
    try {
        // Attempting to resolve ldapServer to SRV record.
        // This is a mechanism defined in MSDN, querying
        // SRV records for _ldap._tcp.DOMAINNAME.
        // and get host and port from domain.
        Attributes attributes = ictx.getAttributes(ldapServer, new String[] { "SRV" });
        Attribute attr = attributes.get("SRV");
        if (attr == null) {
            throw new NamingException();
        }
        String[] srv = attr.get().toString().split(" ");
        String hostNam = srv[3];
        serverHostName = hostNam.substring(0, hostNam.length() - 1);
        serverPortStr = srv[2];
    } catch (NamingException e) {
        // throw exception.
        throw e;
    }
    // try to connect to LDAP port to make sure this machine
    // has LDAP service
    int serverPort = Integer.parseInt(serverPortStr);
    try {
        new Socket(serverHostName, serverPort).close();
    } catch (IOException e) {
        throw e;
    }
    String[] hostAndPort = new String[2];
    hostAndPort[0] = serverHostName;
    hostAndPort[1] = serverPortStr;
    return hostAndPort;
}
Also used : Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) IOException(java.io.IOException) Socket(java.net.Socket)

Example 22 with Attributes

use of javax.naming.directory.Attributes in project yyl_example by Relucent.

the class LdapDaoHelper method create.

/**
	 * 创建新条目
	 * @param entry 新条目
	 * @param ctx LDAP上下文连接
	 * @deprecated 该方法未经过严谨测试
	 */
public static void create(LdapEntry entry, LdapContext ctx) throws NamingException {
    try {
        String dn = entry.getDn();
        Attributes attrs = new BasicAttributes(true);
        if (entry != null && !entry.isEmpty()) {
            Iterator<String> iterator = entry.keySet().iterator();
            while (iterator.hasNext()) {
                String id = iterator.next();
                Attribute attr = new BasicAttribute(id);
                List<?> values = entry.getAll(id);
                if (values != null) {
                    for (Object value : values) {
                        attr.add(value);
                    }
                }
                attrs.put(attr);
            }
        }
        ctx.createSubcontext(dn, attrs);
    } catch (NamingException e) {
        throw e;
    }
}
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)

Example 23 with Attributes

use of javax.naming.directory.Attributes in project perun by CESNET.

the class LdapConnectorImpl method createResource.

//------------------RESOURCE MODIFICATION METHODS----------------------------
public void createResource(Resource resource, String entityID) throws InternalErrorException {
    // Create a set of attributes
    Attributes attributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute objClasses = new BasicAttribute("objectClass");
    objClasses.add("top");
    objClasses.add("perunResource");
    // Add attributes
    attributes.put(objClasses);
    attributes.put("cn", resource.getName());
    attributes.put("perunResourceId", String.valueOf(resource.getId()));
    attributes.put("perunFacilityId", String.valueOf(resource.getFacilityId()));
    attributes.put("perunVoId", String.valueOf(resource.getVoId()));
    if (resource.getDescription() != null && !resource.getDescription().isEmpty())
        attributes.put("description", resource.getDescription());
    // get info about entityID attribute if exists
    if (entityID != null)
        attributes.put("entityID", entityID);
    // Create the entry
    try {
        ldapTemplate.bind(getResourceDN(String.valueOf(resource.getVoId()), String.valueOf(resource.getId())), null, attributes);
        log.debug("New entry created in LDAP: Resource {} in Vo with Id=" + resource.getVoId() + " and Facility with ID=" + resource.getFacilityId() + ".", resource);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 24 with Attributes

use of javax.naming.directory.Attributes in project perun by CESNET.

the class LdapConnectorImpl method addGroup.

//------------------GROUP MODIFICATION METHODS-------------------------------
public void addGroup(Group group) throws InternalErrorException {
    // Create a set of attributes
    Attributes attributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute objClasses = new BasicAttribute("objectClass");
    objClasses.add("top");
    objClasses.add("perunGroup");
    // Add attributes
    attributes.put(objClasses);
    attributes.put("cn", group.getName());
    attributes.put("perunGroupId", String.valueOf(group.getId()));
    attributes.put("perunUniqueGroupName", new String(this.getVoShortName(group.getVoId()) + ":" + group.getName()));
    attributes.put("perunVoId", String.valueOf(group.getVoId()));
    if (group.getDescription() != null && !group.getDescription().isEmpty())
        attributes.put("description", group.getDescription());
    if (group.getParentGroupId() != null) {
        attributes.put("perunParentGroup", "perunGroupId=" + group.getParentGroupId().toString() + ",perunVoId=" + group.getVoId() + "," + ldapProperties.getLdapBase());
        attributes.put("perunParentGroupId", group.getParentGroupId().toString());
    }
    // Create the entry
    try {
        ldapTemplate.bind(getGroupDN(String.valueOf(group.getVoId()), String.valueOf(group.getId())), null, attributes);
        log.debug("New entry created in LDAP: Group {} in Vo with Id=" + group.getVoId() + ".", group);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 25 with Attributes

use of javax.naming.directory.Attributes in project perun by CESNET.

the class LdapConnectorImpl method createVo.

//--------------------------VO MODIFICATION METHODS---------------------------
public void createVo(Vo vo) throws InternalErrorException {
    // Create a set of attributes for vo
    Attributes voAttributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute voObjClasses = new BasicAttribute("objectClass");
    voObjClasses.add("top");
    voObjClasses.add("organization");
    voObjClasses.add("perunVO");
    // Add attributes
    voAttributes.put(voObjClasses);
    voAttributes.put("o", vo.getShortName());
    voAttributes.put("description", vo.getName());
    voAttributes.put("perunVoId", String.valueOf(vo.getId()));
    // Create the entires
    try {
        ldapTemplate.bind(getVoDNByVoId(String.valueOf(vo.getId())), null, voAttributes);
        log.debug("New entry created in LDAP: Vo {}.", vo);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

Attributes (javax.naming.directory.Attributes)81 Attribute (javax.naming.directory.Attribute)57 SearchResult (javax.naming.directory.SearchResult)32 BasicAttributes (javax.naming.directory.BasicAttributes)31 NamingException (javax.naming.NamingException)26 BasicAttribute (javax.naming.directory.BasicAttribute)24 SearchControls (javax.naming.directory.SearchControls)23 NamingEnumeration (javax.naming.NamingEnumeration)19 DirContext (javax.naming.directory.DirContext)19 ArrayList (java.util.ArrayList)17 InitialDirContext (javax.naming.directory.InitialDirContext)12 IOException (java.io.IOException)10 Hashtable (java.util.Hashtable)9 HashSet (java.util.HashSet)8 LdapContext (javax.naming.ldap.LdapContext)8 File (java.io.File)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7 Test (org.junit.Test)6 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5