| | |
| | | * |
| | | * |
| | | * Copyright 2008-2009 Sun Microsystems, Inc. |
| | | * Portions Copyright 2013 ForgeRock, AS. |
| | | * Portions Copyright 2013-2014 ForgeRock, AS. |
| | | */ |
| | | package org.forgerock.opendj.config.client.ldap; |
| | | |
| | |
| | | import org.forgerock.opendj.config.client.spi.PropertySet; |
| | | import org.forgerock.opendj.ldap.Attribute; |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.Connection; |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.EntryNotFoundException; |
| | | import org.forgerock.opendj.ldap.ErrorResultException; |
| | | import org.forgerock.opendj.ldap.ErrorResultIOException; |
| | | import org.forgerock.opendj.ldap.Filter; |
| | | import org.forgerock.opendj.ldap.ResultCode; |
| | | import org.forgerock.opendj.ldap.SearchResultReferenceIOException; |
| | | import org.forgerock.opendj.ldap.SearchScope; |
| | | import org.forgerock.opendj.ldap.responses.SearchResultEntry; |
| | | import org.forgerock.opendj.ldif.ConnectionEntryReader; |
| | | |
| | | /** |
| | | * The LDAP management context driver implementation. |
| | |
| | | |
| | | private LDAPManagementContext context; |
| | | |
| | | private final LDAPConnection connection; |
| | | private final Connection connection; |
| | | |
| | | // The LDAP profile which should be used to construct LDAP |
| | | // requests and decode LDAP responses. |
| | |
| | | * @param profile |
| | | * The LDAP profile. |
| | | */ |
| | | public LDAPDriver(LDAPConnection connection, LDAPProfile profile) { |
| | | LDAPDriver(Connection connection, LDAPProfile profile) { |
| | | this.connection = connection; |
| | | this.profile = profile; |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public void close() { |
| | | connection.unbind(); |
| | | connection.close(); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | try { |
| | | // Read the entry associated with the managed object. |
| | | DN dn = LDAPNameBuilder.create(path, profile); |
| | | DN dn = DNBuilder.create(path, profile); |
| | | AbstractManagedObjectDefinition<C, S> d = path.getManagedObjectDefinition(); |
| | | ManagedObjectDefinition<? extends C, ? extends S> mod = getEntryDefinition(d, dn); |
| | | |
| | |
| | | String attrId = profile.getAttributeName(mod, pd); |
| | | attrIds.add(attrId); |
| | | } |
| | | |
| | | SearchResultEntry searchResultEntry = connection.readEntry(dn, attrIds); |
| | | SearchResultEntry searchResultEntry = |
| | | connection.readEntry(dn, attrIds.toArray(new String[0])); |
| | | |
| | | // Build the managed object's properties. |
| | | List<PropertyException> exceptions = new LinkedList<PropertyException>(); |
| | |
| | | |
| | | try { |
| | | // Read the entry associated with the managed object. |
| | | DN dn = LDAPNameBuilder.create(path, profile); |
| | | DN dn = DNBuilder.create(path, profile); |
| | | ManagedObjectDefinition<? extends C, ? extends S> objectDef = getEntryDefinition(d, dn); |
| | | |
| | | // Make sure we use the correct property definition, the |
| | |
| | | propertyDef = (PropertyDefinition<P>) objectDef.getPropertyDefinition(propertyDef.getName()); |
| | | |
| | | String attrID = profile.getAttributeName(objectDef, propertyDef); |
| | | SearchResultEntry resultEntry = connection.readEntry(dn, Collections.singleton(attrID)); |
| | | SearchResultEntry resultEntry = connection.readEntry(dn, attrID); |
| | | Attribute attribute = resultEntry.getAttribute(attrID); |
| | | |
| | | // Decode the values. |
| | |
| | | } |
| | | |
| | | // Get the search base DN. |
| | | DN dn = LDAPNameBuilder.create(parent, rd, profile); |
| | | DN dn = DNBuilder.create(parent, rd, profile); |
| | | |
| | | // Retrieve only those entries which are sub-types of the |
| | | // specified definition. |
| | | StringBuilder builder = new StringBuilder(); |
| | | builder.append("(objectclass="); |
| | | builder.append(profile.getObjectClass(d)); |
| | | builder.append(')'); |
| | | String filter = builder.toString(); |
| | | |
| | | Filter filter = Filter.equality("objectClass", profile.getObjectClass(d)); |
| | | List<String> children = new ArrayList<String>(); |
| | | try { |
| | | for (DN child : connection.listEntries(dn, filter)) { |
| | | for (DN child : listEntries(dn, filter)) { |
| | | children.add(child.rdn().getFirstAVA().getAttributeValue().toString()); |
| | | } |
| | | } catch (ErrorResultException e) { |
| | |
| | | } |
| | | |
| | | // Get the search base DN. |
| | | DN dn = LDAPNameBuilder.create(parent, rd, profile); |
| | | DN dn = DNBuilder.create(parent, rd, profile); |
| | | |
| | | // Retrieve only those entries which are sub-types of the |
| | | // specified definition. |
| | | StringBuilder builder = new StringBuilder(); |
| | | builder.append("(objectclass="); |
| | | builder.append(profile.getObjectClass(d)); |
| | | builder.append(')'); |
| | | String filter = builder.toString(); |
| | | |
| | | Filter filter = Filter.equality("objectClass", profile.getObjectClass(d)); |
| | | List<String> children = new ArrayList<String>(); |
| | | try { |
| | | for (DN child : connection.listEntries(dn, filter)) { |
| | | for (DN child : listEntries(dn, filter)) { |
| | | children.add(child.rdn().getFirstAVA().getAttributeValue().toString()); |
| | | } |
| | | } catch (ErrorResultException e) { |
| | |
| | | } |
| | | |
| | | ManagedObjectPath<?, ?> parent = path.parent(); |
| | | DN dn = LDAPNameBuilder.create(parent, profile); |
| | | DN dn = DNBuilder.create(parent, profile); |
| | | if (!entryExists(dn)) { |
| | | throw new ManagedObjectNotFoundException(); |
| | | } |
| | | |
| | | dn = LDAPNameBuilder.create(path, profile); |
| | | dn = DNBuilder.create(path, profile); |
| | | return entryExists(dn); |
| | | } |
| | | |
| | |
| | | protected <C extends ConfigurationClient, S extends Configuration> void deleteManagedObject( |
| | | ManagedObjectPath<C, S> path) throws OperationRejectedException, ErrorResultException { |
| | | // Delete the entry and any subordinate entries. |
| | | DN dn = LDAPNameBuilder.create(path, profile); |
| | | DN dn = DNBuilder.create(path, profile); |
| | | try { |
| | | connection.deleteSubtree(dn); |
| | | connection.deleteSubtree(dn.toString()); |
| | | } catch (ErrorResultException e) { |
| | | if (e.getResult().getResultCode() == ResultCode.UNWILLING_TO_PERFORM) { |
| | | AbstractManagedObjectDefinition<?, ?> d = path.getManagedObjectDefinition(); |
| | |
| | | * if a problem occurs. |
| | | */ |
| | | boolean entryExists(DN dn) throws ErrorResultException { |
| | | return connection.entryExists(dn); |
| | | try { |
| | | connection.readEntry(dn, "1.1"); |
| | | return true; |
| | | } catch (EntryNotFoundException e) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @return Returns the LDAP connection used for interacting with the server. |
| | | */ |
| | | LDAPConnection getLDAPConnection() { |
| | | Connection getLDAPConnection() { |
| | | return connection; |
| | | } |
| | | |
| | |
| | | getEntryDefinition(AbstractManagedObjectDefinition<C, S> d, DN dn) throws ErrorResultException, |
| | | DefinitionDecodingException { |
| | | // @Checkstyle:on |
| | | SearchResultEntry searchResultEntry = connection.readEntry(dn, Collections.singleton("objectclass")); |
| | | SearchResultEntry searchResultEntry = connection.readEntry(dn, "objectclass"); |
| | | Attribute objectClassAttr = searchResultEntry.getAttribute("objectclass"); |
| | | |
| | | if (objectClassAttr == null) { |
| | |
| | | |
| | | return d.resolveManagedObjectDefinition(resolver); |
| | | } |
| | | |
| | | private Collection<DN> listEntries(DN dn, Filter filter) throws ErrorResultException { |
| | | List<DN> names = new LinkedList<DN>(); |
| | | ConnectionEntryReader reader = |
| | | connection.search(dn.toString(), SearchScope.SINGLE_LEVEL, filter.toString()); |
| | | try { |
| | | while (reader.hasNext()) { |
| | | names.add(reader.readEntry().getName()); |
| | | } |
| | | } catch (ErrorResultIOException e) { |
| | | throw e.getCause(); |
| | | } catch (SearchResultReferenceIOException e) { |
| | | // Ignore. |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | return names; |
| | | } |
| | | } |