001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS
026 */
027package org.opends.server.schema;
028
029import static org.opends.server.schema.SchemaConstants.*;
030
031import org.forgerock.i18n.LocalizableMessageBuilder;
032import org.forgerock.i18n.slf4j.LocalizedLogger;
033import org.forgerock.opendj.config.server.ConfigException;
034import org.forgerock.opendj.ldap.ByteSequence;
035import org.opends.server.admin.std.server.AttributeSyntaxCfg;
036import org.forgerock.opendj.ldap.schema.Schema;
037import org.forgerock.opendj.ldap.schema.SchemaBuilder;
038import org.forgerock.opendj.ldap.schema.Syntax;
039import org.opends.server.api.AttributeSyntax;
040import org.opends.server.core.ServerContext;
041import org.opends.server.types.DN;
042import org.opends.server.types.DirectoryException;
043import org.opends.server.types.SubtreeSpecification;
044
045
046/**
047 * This class defines the subtree specification attribute syntax,
048 * which is used to specify the scope of sub-entries (RFC 3672).
049 */
050public final class SubtreeSpecificationSyntax
051       extends AttributeSyntax<AttributeSyntaxCfg>
052{
053
054  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
055
056  /**
057   * Creates a new instance of this syntax. Note that the only thing
058   * that should be done here is to invoke the default constructor for
059   * the superclass. All initialization should be performed in the
060   * <CODE>initializeSyntax</CODE> method.
061   */
062  public SubtreeSpecificationSyntax() {
063    // No implementation required.
064  }
065
066  /** {@inheritDoc} */
067  @Override
068  public void initializeSyntax(AttributeSyntaxCfg configuration, ServerContext serverContext)
069      throws ConfigException
070  {
071    // Add the subtree specification syntax to the "new" schema
072    SchemaUpdater schemaUpdater = serverContext.getSchemaUpdater();
073    SchemaBuilder builder = schemaUpdater.getSchemaBuilder().buildSyntax(SYNTAX_SUBTREE_SPECIFICATION_OID)
074      .description(SYNTAX_SUBTREE_SPECIFICATION_DESCRIPTION)
075      .implementation(new SubtreeSpecificationSyntaxImpl())
076      .addToSchema();
077    schemaUpdater.updateSchema(builder.toSchema());
078  }
079
080  /** {@inheritDoc} */
081  @Override
082  public Syntax getSDKSyntax(Schema schema)
083  {
084    return schema.getSyntax(SchemaConstants.SYNTAX_SUBTREE_SPECIFICATION_OID);
085  }
086
087  /**
088   * Retrieves the common name for this attribute syntax.
089   *
090   * @return The common name for this attribute syntax.
091   */
092  @Override
093  public String getName() {
094
095    return SYNTAX_SUBTREE_SPECIFICATION_NAME;
096  }
097
098  /**
099   * Retrieves the OID for this attribute syntax.
100   *
101   * @return The OID for this attribute syntax.
102   */
103  @Override
104  public String getOID() {
105
106    return SYNTAX_SUBTREE_SPECIFICATION_OID;
107  }
108
109  /**
110   * Retrieves a description for this attribute syntax.
111   *
112   * @return A description for this attribute syntax.
113   */
114  @Override
115  public String getDescription() {
116
117    return SYNTAX_SUBTREE_SPECIFICATION_DESCRIPTION;
118  }
119
120  /**
121   * Indicates whether the provided value is acceptable for use in an
122   * attribute with this syntax. If it is not, then the reason may be
123   * appended to the provided buffer.
124   *
125   * @param value
126   *          The value for which to make the determination.
127   * @param invalidReason
128   *          The buffer to which the invalid reason should be appended.
129   * @return <CODE>true</CODE> if the provided value is acceptable for
130   *         use with this syntax, or <CODE>false</CODE> if not.
131   */
132  @Override
133  public boolean valueIsAcceptable(ByteSequence value,
134                                   LocalizableMessageBuilder invalidReason) {
135
136    // Use the subtree specification code to make this determination.
137    try {
138      SubtreeSpecification.valueOf(DN.rootDN(), value.toString());
139
140      return true;
141    } catch (DirectoryException e) {
142      logger.traceException(e);
143
144      invalidReason.append(e.getMessageObject());
145      return false;
146    }
147  }
148
149  /** {@inheritDoc} */
150  @Override
151  public boolean isBEREncodingRequired()
152  {
153    return false;
154  }
155
156  /** {@inheritDoc} */
157  @Override
158  public boolean isHumanReadable()
159  {
160    return true;
161  }
162}