001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009 Sun Microsystems, Inc.
015 * Portions copyright 2011-2016 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.ldap;
019
020import org.forgerock.opendj.ldap.schema.Schema;
021
022import org.forgerock.util.Reject;
023
024/**
025 * Decode options allow applications to control how requests and responses are
026 * decoded. In particular:
027 * <ul>
028 * <li>The strategy for selecting which {@code Schema} should be used for
029 * decoding distinguished names, attribute descriptions, and other objects which
030 * require a schema in order to be decoded.
031 * <li>The {@code Attribute} implementation which should be used when decoding
032 * attributes.
033 * <li>The {@code Entry} implementation which should be used when decoding
034 * entries or entry like objects.
035 * </ul>
036 */
037public final class DecodeOptions {
038    private static final class FixedSchemaResolver implements SchemaResolver {
039        private final Schema schema;
040
041        private FixedSchemaResolver(final Schema schema) {
042            this.schema = schema;
043        }
044
045        @Override
046        public Schema resolveSchema(final String dn) {
047            return schema;
048        }
049    }
050
051    private SchemaResolver schemaResolver;
052    private EntryFactory entryFactory;
053    private AttributeFactory attributeFactory;
054
055    /**
056     * Creates a new set of decode options which will always use the default
057     * schema returned by {@link Schema#getDefaultSchema()},
058     * {@link LinkedAttribute}, and {@link LinkedHashMapEntry}.
059     */
060    public DecodeOptions() {
061        this.attributeFactory = LinkedAttribute.FACTORY;
062        this.entryFactory = LinkedHashMapEntry.FACTORY;
063        this.schemaResolver = SchemaResolver.DEFAULT;
064    }
065
066    /**
067     * Creates a new set of decode options having the same initial set of
068     * options as the provided set of decode options.
069     *
070     * @param options
071     *            The set of decode options to be copied.
072     */
073    public DecodeOptions(final DecodeOptions options) {
074        this.attributeFactory = options.attributeFactory;
075        this.entryFactory = options.entryFactory;
076        this.schemaResolver = options.schemaResolver;
077    }
078
079    /**
080     * Returns the {@code AttributeFactory} which will be used for creating new
081     * {@code Attribute} instances when decoding attributes.
082     *
083     * @return The {@code AttributeFactory} which will be used for creating new
084     *         {@code Attribute} instances when decoding attributes.
085     */
086    public final AttributeFactory getAttributeFactory() {
087        return attributeFactory;
088    }
089
090    /**
091     * Returns the {@code EntryFactory} which will be used for creating new
092     * {@code Entry} instances when decoding entries.
093     *
094     * @return The {@code EntryFactory} which will be used for creating new
095     *         {@code Entry} instances when decoding entries.
096     */
097    public final EntryFactory getEntryFactory() {
098        return entryFactory;
099    }
100
101    /**
102     * Returns the strategy for selecting which {@code Schema} should be used
103     * for decoding distinguished names, attribute descriptions, and other
104     * objects which require a {@code Schema} in order to be decoded.
105     *
106     * @return The schema resolver which will be used for decoding.
107     */
108    public final SchemaResolver getSchemaResolver() {
109        return schemaResolver;
110    }
111
112    /**
113     * Sets the {@code AttributeFactory} which will be used for creating new
114     * {@code Attribute} instances when decoding attributes.
115     *
116     * @param factory
117     *            The {@code AttributeFactory} which will be used for creating
118     *            new {@code Attribute} instances when decoding attributes.
119     * @return A reference to this set of decode options.
120     * @throws NullPointerException
121     *             If {@code factory} was {@code null}.
122     */
123    public final DecodeOptions setAttributeFactory(final AttributeFactory factory) {
124        Reject.ifNull(factory);
125        this.attributeFactory = factory;
126        return this;
127    }
128
129    /**
130     * Sets the {@code EntryFactory} which will be used for creating new
131     * {@code Entry} instances when decoding entries.
132     *
133     * @param factory
134     *            The {@code EntryFactory} which will be used for creating new
135     *            {@code Entry} instances when decoding entries.
136     * @return A reference to this set of decode options.
137     * @throws NullPointerException
138     *             If {@code factory} was {@code null}.
139     */
140    public final DecodeOptions setEntryFactory(final EntryFactory factory) {
141        Reject.ifNull(factory);
142        this.entryFactory = factory;
143        return this;
144    }
145
146    /**
147     * Sets the {@code Schema} which will be used for decoding distinguished
148     * names, attribute descriptions, and other objects which require a schema
149     * in order to be decoded. This setting overrides the currently active
150     * schema resolver set using {@link #setSchemaResolver}.
151     *
152     * @param schema
153     *            The {@code Schema} which will be used for decoding.
154     * @return A reference to this set of decode options.
155     * @throws NullPointerException
156     *             If {@code schema} was {@code null}.
157     */
158    public final DecodeOptions setSchema(final Schema schema) {
159        Reject.ifNull(schema);
160        this.schemaResolver = new FixedSchemaResolver(schema);
161        return this;
162    }
163
164    /**
165     * Sets the strategy for selecting which {@code Schema} should be used for
166     * decoding distinguished names, attribute descriptions, and other objects
167     * which require a {@code Schema} in order to be decoded. This setting
168     * overrides the currently active schema set using {@link #setSchema}.
169     *
170     * @param resolver
171     *            The {@code SchemaResolver} which will be used for decoding.
172     * @return A reference to this set of decode options.
173     * @throws NullPointerException
174     *             If {@code resolver} was {@code null}.
175     */
176    public final DecodeOptions setSchemaResolver(final SchemaResolver resolver) {
177        Reject.ifNull(resolver);
178        this.schemaResolver = resolver;
179        return this;
180    }
181}