mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
08.23.2013 33255bfee39fb3cb3813894831a76bc7c014c13a
opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/Config.java
@@ -15,165 +15,50 @@
 */
package org.forgerock.opendj.rest2ldap;
import static org.forgerock.opendj.rest2ldap.Config.ReadOnUpdatePolicy.USE_READ_ENTRY_CONTROLS;
import static org.forgerock.opendj.rest2ldap.Utils.ensureNotNull;
import org.forgerock.opendj.ldap.DecodeOptions;
import org.forgerock.opendj.ldap.Filter;
import org.forgerock.opendj.ldap.schema.Schema;
/**
 * Common configuration options.
 */
public final class Config {
    /**
     * An interface for incrementally constructing common configuration options.
     */
    public static final class Builder {
        private Filter falseFilter;
        private ReadOnUpdatePolicy readOnUpdatePolicy;
        private Filter trueFilter;
        private Builder() {
            // Nothing to do.
        }
        /**
         * Returns a new configuration based on the current state of this
         * builder.
         *
         * @return A new configuration based on the current state of this
         *         builder.
         */
        public Config build() {
            return new Config(trueFilter, falseFilter, readOnUpdatePolicy);
        }
        /**
         * Sets the absolute false filter which should be used when querying the
         * LDAP server.
         *
         * @param filter
         *            The absolute false filter.
         * @return A reference to this builder.
         */
        public Builder falseFilter(final Filter filter) {
            this.trueFilter = ensureNotNull(filter);
            return this;
        }
        /**
         * Sets the policy which should be used in order to read an entry before
         * it is deleted, or after it is added or modified.
         *
         * @param policy
         *            The policy which should be used in order to read an entry
         *            before it is deleted, or after it is added or modified.
         * @return A reference to this builder.
         */
        public Builder readOnUpdatePolicy(final ReadOnUpdatePolicy policy) {
            this.readOnUpdatePolicy = ensureNotNull(policy);
            return this;
        }
        /**
         * Sets the absolute true filter which should be used when querying the
         * LDAP server.
         *
         * @param filter
         *            The absolute true filter.
         * @return A reference to this builder.
         */
        public Builder trueFilter(final Filter filter) {
            this.trueFilter = ensureNotNull(filter);
            return this;
        }
    };
    /**
     * The policy which should be used in order to read an entry before it is
     * deleted, or after it is added or modified.
     */
    public static enum ReadOnUpdatePolicy {
        /**
         * The LDAP entry will not be read when an update is performed. More
         * specifically, the REST resource will not be returned as part of a
         * create, delete, patch, or update request.
         */
        DISABLED,
        /**
         * The LDAP entry will be read atomically using the RFC 4527 read-entry
         * controls. More specifically, the REST resource will be returned as
         * part of a create, delete, patch, or update request, and it will
         * reflect the state of the resource at the time the update was
         * performed. This policy requires that the LDAP server supports RFC
         * 4527.
         */
        USE_READ_ENTRY_CONTROLS,
        /**
         * The LDAP entry will be read non-atomically using an LDAP search when
         * an update is performed. More specifically, the REST resource will be
         * returned as part of a create, delete, patch, or update request, but
         * it may not reflect the state of the resource at the time the update
         * was performed.
         */
        USE_SEARCH;
    }
    private static final Config DEFAULT = new Builder().trueFilter(Filter.objectClassPresent())
            .falseFilter(Filter.present("1.1")).readOnUpdatePolicy(USE_READ_ENTRY_CONTROLS).build();
    /**
     * Returns a new builder which can be used for incrementally constructing
     * common configuration options. The builder will initially have
     * {@link #defaultConfig() default} settings.
     *
     * @return The new builder.
     */
    public static Builder builder() {
        return builder(DEFAULT);
    }
    /**
     * Returns a new builder which can be used for incrementally constructing
     * common configuration options. The builder will initially have the same
     * settings as the provided configuration.
     *
     * @param config
     *            The initial settings.
     * @return The new builder.
     */
    public static Builder builder(final Config config) {
        return new Builder().trueFilter(config.trueFilter()).falseFilter(config.falseFilter())
                .readOnUpdatePolicy(config.readOnUpdatePolicy());
    }
    /**
     * Returns the default configuration having the following settings:
     * <ul>
     * <li>the absolute true filter {@code (objectClass=*)}
     * <li>the absolute false filter {@code (1.1=*)}
     * <li>the read on update policy
     * {@link ReadOnUpdatePolicy#USE_READ_ENTRY_CONTROLS}.
     * </ul>
     *
     * @return The default configuration.
     */
    public static Config defaultConfig() {
        return DEFAULT;
    }
final class Config {
    private final Filter falseFilter;
    private final Schema schema;
    private final ReadOnUpdatePolicy readOnUpdatePolicy;
    private final Filter trueFilter;
    private final DecodeOptions options;
    private Config(final Filter trueFilter, final Filter falseFilter,
            final ReadOnUpdatePolicy readOnUpdatePolicy) {
    Config(final Filter trueFilter, final Filter falseFilter,
            final ReadOnUpdatePolicy readOnUpdatePolicy, final Schema schema) {
        this.trueFilter = trueFilter;
        this.falseFilter = falseFilter;
        this.readOnUpdatePolicy = readOnUpdatePolicy;
        this.schema = schema;
        this.options = new DecodeOptions().setSchema(schema);
    }
    /**
     * Returns the schema which should be used when attribute types and
     * controls.
     *
     * @return The schema which should be used when attribute types and
     *         controls.
     */
    public Schema schema() {
        return schema;
    }
    /**
     * Returns the decoding options which should be used when decoding controls
     * in responses.
     *
     * @return The decoding options which should be used when decoding controls
     *         in responses.
     */
    public DecodeOptions decodeOptions() {
        return options;
    }
    /**