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 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017
018package org.opends.server.admin;
019
020import static org.forgerock.util.Reject.ifNull;
021
022import java.util.EnumSet;
023
024import org.forgerock.i18n.LocalizedIllegalArgumentException;
025import org.forgerock.opendj.ldap.DN;
026
027/**
028 * DN property definition.
029 */
030public final class DNPropertyDefinition extends PropertyDefinition<DN> {
031
032  /**
033   * Optional base DN which all valid values must be immediately
034   * subordinate to.
035   */
036  private final DN baseDN;
037
038
039
040  /**
041   * An interface for incrementally constructing DN property
042   * definitions.
043   */
044  public static class Builder extends
045      AbstractBuilder<DN, DNPropertyDefinition> {
046
047    /**
048     * Optional base DN which all valid values must be immediately
049     * subordinate to.
050     */
051    private DN baseDN;
052
053
054
055    /** Private constructor. */
056    private Builder(
057        AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
058      super(d, propertyName);
059    }
060
061
062
063    /**
064     * Set the base DN which all valid values must be immediately
065     * subordinate to. By default there is no based DN.
066     *
067     * @param baseDN
068     *          The string representation of the base DN.
069     * @throws IllegalArgumentException
070     *           If the provided string is not a valid DN string
071     *           representation.
072     */
073    public void setBaseDN(String baseDN)
074        throws IllegalArgumentException {
075      setBaseDN(baseDN != null ? DN.valueOf(baseDN) : null);
076    }
077
078
079
080    /**
081     * Set the base DN which all valid values must be immediately
082     * subordinate to. By default there is no based DN.
083     *
084     * @param baseDN
085     *          The base DN.
086     */
087    public void setBaseDN(DN baseDN) {
088      this.baseDN = baseDN;
089    }
090
091
092
093    /** {@inheritDoc} */
094    @Override
095    protected DNPropertyDefinition buildInstance(
096        AbstractManagedObjectDefinition<?, ?> d, String propertyName,
097        EnumSet<PropertyOption> options,
098        AdministratorAction adminAction,
099        DefaultBehaviorProvider<DN> defaultBehavior) {
100      return new DNPropertyDefinition(d, propertyName, options,
101          adminAction, defaultBehavior, baseDN);
102    }
103  }
104
105
106
107  /**
108   * Create a DN property definition builder.
109   *
110   * @param d
111   *          The managed object definition associated with this
112   *          property definition.
113   * @param propertyName
114   *          The property name.
115   * @return Returns the new boolean property definition builder.
116   */
117  public static Builder createBuilder(
118      AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
119    return new Builder(d, propertyName);
120  }
121
122
123
124  /** Private constructor. */
125  private DNPropertyDefinition(
126      AbstractManagedObjectDefinition<?, ?> d, String propertyName,
127      EnumSet<PropertyOption> options,
128      AdministratorAction adminAction,
129      DefaultBehaviorProvider<DN> defaultBehavior, DN baseDN) {
130    super(d, DN.class, propertyName, options, adminAction, defaultBehavior);
131    this.baseDN = baseDN;
132  }
133
134
135
136  /**
137   * Get the base DN which all valid values must be immediately
138   * subordinate to, or <code>null</code> if there is no based DN.
139   *
140   * @return Returns the base DN which all valid values must be
141   *         immediately subordinate to.
142   */
143  public DN getBaseDN() {
144    return baseDN;
145  }
146
147
148
149  /** {@inheritDoc} */
150  @Override
151  public void validateValue(DN value)
152      throws PropertyException {
153    ifNull(value);
154
155    if (baseDN != null) {
156      DN parent = value.parent();
157
158      if (parent == null) {
159        parent = DN.rootDN();
160      }
161
162      if (!parent.equals(baseDN)) {
163        throw PropertyException.illegalPropertyValueException(this, value);
164      }
165    }
166  }
167
168  @Override
169  public DN decodeValue(String value)
170      throws PropertyException {
171    ifNull(value);
172
173    try {
174      DN dn = DN.valueOf(value);
175      validateValue(dn);
176      return dn;
177    } catch (LocalizedIllegalArgumentException | PropertyException e) {
178      throw PropertyException.illegalPropertyValueException(this, value);
179    }
180  }
181
182  @Override
183  public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
184    return v.visitDN(this, p);
185  }
186
187
188
189  /** {@inheritDoc} */
190  @Override
191  public <R, P> R accept(PropertyValueVisitor<R, P> v, DN value, P p) {
192    return v.visitDN(this, value, p);
193  }
194
195
196
197  /** {@inheritDoc} */
198  @Override
199  public int compare(DN o1, DN o2) {
200    return o1.compareTo(o2);
201  }
202}