| File was renamed from opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/Rest2LDAP.java |
| | |
| | | import org.forgerock.util.time.Duration; |
| | | |
| | | /** Provides core factory methods and builders for constructing LDAP resource collections. */ |
| | | public final class Rest2LDAP { |
| | | public final class Rest2Ldap { |
| | | /** Indicates whether LDAP client connections should use SSL or StartTLS. */ |
| | | private enum ConnectionSecurity { |
| | | NONE, SSL, STARTTLS |
| | |
| | | private final List<Attribute> additionalLDAPAttributes = new LinkedList<>(); |
| | | private DN baseDN; // TODO: support template variables. |
| | | private AttributeDescription etagAttribute; |
| | | private NameStrategy nameStrategy; |
| | | private NamingStrategy namingStrategy; |
| | | private ReadOnUpdatePolicy readOnUpdatePolicy = CONTROLS; |
| | | private AttributeMapper rootMapper; |
| | | private PropertyMapper rootMapper; |
| | | private Schema schema = Schema.getDefaultSchema(); |
| | | private boolean usePermissiveModify; |
| | | private boolean useSubtreeDelete; |
| | |
| | | if (rootMapper == null) { |
| | | throw new IllegalStateException(ERR_CONFIG_NO_MAPPINGS_PROVIDED.get().toString()); |
| | | } |
| | | return new LDAPCollectionResourceProvider(baseDN, rootMapper, nameStrategy, etagAttribute, |
| | | new Config(readOnUpdatePolicy, useSubtreeDelete, usePermissiveModify, schema), |
| | | additionalLDAPAttributes); |
| | | return new SubResourceImpl(baseDN, rootMapper, namingStrategy, etagAttribute, |
| | | new Config(readOnUpdatePolicy, useSubtreeDelete, usePermissiveModify, schema), |
| | | additionalLDAPAttributes); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * Sets the attribute mapper which should be used for mapping JSON |
| | | * Sets the property mapper which should be used for mapping JSON |
| | | * resources to and from LDAP entries. |
| | | * |
| | | * @param mapper |
| | | * The attribute mapper. |
| | | * The property mapper. |
| | | * @return A reference to this LDAP resource collection builder. |
| | | */ |
| | | public Builder mapper(final AttributeMapper mapper) { |
| | | public Builder mapper(final PropertyMapper mapper) { |
| | | this.rootMapper = mapper; |
| | | return this; |
| | | } |
| | |
| | | * @return A reference to this LDAP resource collection builder. |
| | | */ |
| | | public Builder useClientDNNaming(final AttributeType attribute) { |
| | | this.nameStrategy = new DNNameStrategy(attribute); |
| | | this.namingStrategy = new DNNamingStrategy(attribute); |
| | | return this; |
| | | } |
| | | |
| | |
| | | */ |
| | | public Builder useClientNaming(final AttributeType dnAttribute, |
| | | final AttributeDescription idAttribute) { |
| | | this.nameStrategy = new AttributeNameStrategy(dnAttribute, idAttribute, false); |
| | | this.namingStrategy = new AttributeNamingStrategy(dnAttribute, idAttribute, false); |
| | | return this; |
| | | } |
| | | |
| | |
| | | */ |
| | | public Builder useServerNaming(final AttributeType dnAttribute, |
| | | final AttributeDescription idAttribute) { |
| | | this.nameStrategy = new AttributeNameStrategy(dnAttribute, idAttribute, true); |
| | | this.namingStrategy = new AttributeNamingStrategy(dnAttribute, idAttribute, true); |
| | | return this; |
| | | } |
| | | |
| | |
| | | return schema.getAttributeType(attribute); |
| | | } |
| | | |
| | | private AttributeMapper configureMapper(final JsonValue mapper) { |
| | | private PropertyMapper configureMapper(final JsonValue mapper) { |
| | | if (mapper.isDefined("constant")) { |
| | | return constant(mapper.get("constant").getObject()); |
| | | } else if (mapper.isDefined("simple")) { |
| | | final JsonValue config = mapper.get("simple"); |
| | | final SimpleAttributeMapper s = |
| | | final SimplePropertyMapper s = |
| | | simple(ad(config.get("ldapAttribute").required().asString())); |
| | | if (config.isDefined("defaultJSONValue")) { |
| | | s.defaultJSONValue(config.get("defaultJSONValue").getObject()); |
| | | s.defaultJsonValue(config.get("defaultJSONValue").getObject()); |
| | | } |
| | | if (config.get("isBinary").defaultTo(false).asBoolean()) { |
| | | s.isBinary(); |
| | |
| | | final DN baseDN = DN.valueOf(config.get("baseDN").required().asString(), schema); |
| | | final AttributeDescription primaryKey = |
| | | ad(config.get("primaryKey").required().asString()); |
| | | final AttributeMapper m = configureMapper(config.get("mapper").required()); |
| | | final ReferenceAttributeMapper r = reference(ldapAttribute, baseDN, primaryKey, m); |
| | | final PropertyMapper m = configureMapper(config.get("mapper").required()); |
| | | final ReferencePropertyMapper r = reference(ldapAttribute, baseDN, primaryKey, m); |
| | | if (config.get("isRequired").defaultTo(false).asBoolean()) { |
| | | r.isRequired(); |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private ObjectAttributeMapper configureObjectMapper(final JsonValue mapper) { |
| | | final ObjectAttributeMapper object = object(); |
| | | private ObjectPropertyMapper configureObjectMapper(final JsonValue mapper) { |
| | | final ObjectPropertyMapper object = object(); |
| | | for (final String attribute : mapper.keys()) { |
| | | object.attribute(attribute, configureMapper(mapper.get(attribute))); |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private static final class AttributeNameStrategy extends NameStrategy { |
| | | private static final class AttributeNamingStrategy extends NamingStrategy { |
| | | private final AttributeDescription dnAttribute; |
| | | private final AttributeDescription idAttribute; |
| | | private final boolean isServerProvided; |
| | | |
| | | private AttributeNameStrategy(final AttributeType dnAttribute, |
| | | final AttributeDescription idAttribute, final boolean isServerProvided) { |
| | | private AttributeNamingStrategy(final AttributeType dnAttribute, |
| | | final AttributeDescription idAttribute, final boolean isServerProvided) { |
| | | this.dnAttribute = AttributeDescription.create(dnAttribute); |
| | | if (this.dnAttribute.equals(idAttribute)) { |
| | | throw newLocalizedIllegalArgumentException(ERR_CONFIG_NAMING_STRATEGY_DN_AND_ID_NOT_DIFFERENT.get()); |
| | |
| | | } |
| | | |
| | | @Override |
| | | void getLDAPAttributes(final Connection connection, final Set<String> ldapAttributes) { |
| | | void getLdapAttributes(final Connection connection, final Set<String> ldapAttributes) { |
| | | ldapAttributes.add(idAttribute.toString()); |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private static final class DNNameStrategy extends NameStrategy { |
| | | private static final class DNNamingStrategy extends NamingStrategy { |
| | | private final AttributeDescription attribute; |
| | | |
| | | private DNNameStrategy(final AttributeType attribute) { |
| | | private DNNamingStrategy(final AttributeType attribute) { |
| | | this.attribute = AttributeDescription.create(attribute); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | void getLDAPAttributes(final Connection connection, final Set<String> ldapAttributes) { |
| | | void getLdapAttributes(final Connection connection, final Set<String> ldapAttributes) { |
| | | ldapAttributes.add(attribute.toString()); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns an attribute mapper which maps a single JSON attribute to a JSON |
| | | * Returns an property mapper which maps a single JSON attribute to a JSON |
| | | * constant. |
| | | * |
| | | * @param value |
| | | * The constant JSON value (a Boolean, Number, String, Map, or |
| | | * List). |
| | | * @return The attribute mapper. |
| | | * @return The property mapper. |
| | | */ |
| | | public static AttributeMapper constant(final Object value) { |
| | | return new JSONConstantAttributeMapper(value); |
| | | public static PropertyMapper constant(final Object value) { |
| | | return new JsonConstantPropertyMapper(value); |
| | | } |
| | | |
| | | /** |
| | | * Returns an attribute mapper which maps JSON objects to LDAP attributes. |
| | | * Returns an property mapper which maps JSON objects to LDAP attributes. |
| | | * |
| | | * @return The attribute mapper. |
| | | * @return The property mapper. |
| | | */ |
| | | public static ObjectAttributeMapper object() { |
| | | return new ObjectAttributeMapper(); |
| | | public static ObjectPropertyMapper object() { |
| | | return new ObjectPropertyMapper(); |
| | | } |
| | | |
| | | /** |
| | | * Returns an attribute mapper which provides a mapping from a JSON value to |
| | | * Returns an property mapper which provides a mapping from a JSON value to |
| | | * a single DN valued LDAP attribute. |
| | | * |
| | | * @param attribute |
| | |
| | | * The search primary key LDAP attribute to use for performing |
| | | * reverse lookups. |
| | | * @param mapper |
| | | * An attribute mapper which will be used to map LDAP attributes |
| | | * An property mapper which will be used to map LDAP attributes |
| | | * in the referenced entry. |
| | | * @return The attribute mapper. |
| | | * @return The property mapper. |
| | | */ |
| | | public static ReferenceAttributeMapper reference(final AttributeDescription attribute, |
| | | final DN baseDN, final AttributeDescription primaryKey, final AttributeMapper mapper) { |
| | | return new ReferenceAttributeMapper(Schema.getDefaultSchema(), attribute, baseDN, primaryKey, mapper); |
| | | public static ReferencePropertyMapper reference(final AttributeDescription attribute, |
| | | final DN baseDN, final AttributeDescription primaryKey, |
| | | final PropertyMapper mapper) { |
| | | return new ReferencePropertyMapper(Schema.getDefaultSchema(), attribute, baseDN, primaryKey, mapper); |
| | | } |
| | | |
| | | /** |
| | | * Returns an attribute mapper which provides a mapping from a JSON value to |
| | | * Returns an property mapper which provides a mapping from a JSON value to |
| | | * a single DN valued LDAP attribute. |
| | | * |
| | | * @param attribute |
| | |
| | | * The search primary key LDAP attribute to use for performing |
| | | * reverse lookups. |
| | | * @param mapper |
| | | * An attribute mapper which will be used to map LDAP attributes |
| | | * An property mapper which will be used to map LDAP attributes |
| | | * in the referenced entry. |
| | | * @return The attribute mapper. |
| | | * @return The property mapper. |
| | | */ |
| | | public static ReferenceAttributeMapper reference(final String attribute, final String baseDN, |
| | | final String primaryKey, final AttributeMapper mapper) { |
| | | public static ReferencePropertyMapper reference(final String attribute, final String baseDN, |
| | | final String primaryKey, final PropertyMapper mapper) { |
| | | return reference(AttributeDescription.valueOf(attribute), DN.valueOf(baseDN), |
| | | AttributeDescription.valueOf(primaryKey), mapper); |
| | | } |
| | | |
| | | /** |
| | | * Returns an attribute mapper which provides a simple mapping from a JSON |
| | | * Returns an property mapper which provides a simple mapping from a JSON |
| | | * value to a single LDAP attribute. |
| | | * |
| | | * @param attribute |
| | | * The LDAP attribute to be mapped. |
| | | * @return The attribute mapper. |
| | | * @return The property mapper. |
| | | */ |
| | | public static SimpleAttributeMapper simple(final AttributeDescription attribute) { |
| | | return new SimpleAttributeMapper(attribute); |
| | | public static SimplePropertyMapper simple(final AttributeDescription attribute) { |
| | | return new SimplePropertyMapper(attribute); |
| | | } |
| | | |
| | | /** |
| | | * Returns an attribute mapper which provides a simple mapping from a JSON |
| | | * Returns an property mapper which provides a simple mapping from a JSON |
| | | * value to a single LDAP attribute. |
| | | * |
| | | * @param attribute |
| | | * The LDAP attribute to be mapped. |
| | | * @return The attribute mapper. |
| | | * @return The property mapper. |
| | | */ |
| | | public static SimpleAttributeMapper simple(final String attribute) { |
| | | public static SimplePropertyMapper simple(final String attribute) { |
| | | return simple(AttributeDescription.valueOf(attribute)); |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private Rest2LDAP() { |
| | | private Rest2Ldap() { |
| | | // Prevent instantiation. |
| | | } |
| | | } |