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 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2012-2016 ForgeRock AS.
016 */
017package org.opends.server.schema;
018
019import static org.forgerock.opendj.ldap.schema.SchemaOptions.*;
020import static org.opends.server.schema.SchemaConstants.*;
021
022import java.util.List;
023
024import org.forgerock.i18n.LocalizableMessage;
025import org.forgerock.opendj.config.server.ConfigChangeResult;
026import org.forgerock.opendj.config.server.ConfigException;
027import org.forgerock.opendj.ldap.schema.Schema;
028import org.forgerock.opendj.ldap.schema.Syntax;
029import org.forgerock.opendj.config.server.ConfigurationChangeListener;
030import org.forgerock.opendj.server.config.server.DirectoryStringAttributeSyntaxCfg;
031import org.opends.server.api.AttributeSyntax;
032import org.opends.server.core.ServerContext;
033import org.opends.server.types.DirectoryException;
034
035
036/**
037 * This class defines the directory string attribute syntax, which is simply a
038 * set of UTF-8 characters.  By default, they will be treated in a
039 * case-insensitive manner, and equality, ordering, substring, and approximate
040 * matching will be allowed.
041 */
042public class DirectoryStringSyntax
043       extends AttributeSyntax<DirectoryStringAttributeSyntaxCfg>
044       implements ConfigurationChangeListener<DirectoryStringAttributeSyntaxCfg>
045{
046
047  /** Indicates whether we will allow zero-length values. */
048  private boolean allowZeroLengthValues;
049
050  /** The reference to the configuration for this directory string syntax. */
051  private DirectoryStringAttributeSyntaxCfg currentConfig;
052
053  private ServerContext serverContext;
054
055  /**
056   * Creates a new instance of this syntax.  Note that the only thing that
057   * should be done here is to invoke the default constructor for the
058   * superclass.  All initialization should be performed in the
059   * <CODE>initializeSyntax</CODE> method.
060   */
061  public DirectoryStringSyntax()
062  {
063    super();
064  }
065
066  @Override
067  public void initializeSyntax(DirectoryStringAttributeSyntaxCfg configuration, ServerContext serverContext)
068      throws ConfigException, DirectoryException
069  {
070    this.serverContext = serverContext;
071
072    // This syntax is one of the Directory Server's core syntaxes and therefore
073    // it may be instantiated at times without a configuration entry.  If that
074    // is the case, then we'll exit now before doing anything that could require
075    // access to that entry.
076    if (configuration == null)
077    {
078      return;
079    }
080
081    currentConfig = configuration;
082    currentConfig.addDirectoryStringChangeListener(this);
083    allowZeroLengthValues = currentConfig.isAllowZeroLengthValues();
084    serverContext.getSchema().updateSchemaOption(ALLOW_ZERO_LENGTH_DIRECTORY_STRINGS, allowZeroLengthValues);
085  }
086
087  @Override
088  public Syntax getSDKSyntax(Schema schema)
089  {
090    return schema.getSyntax(SchemaConstants.SYNTAX_DIRECTORY_STRING_OID);
091  }
092
093  @Override
094  public void finalizeSyntax()
095  {
096    currentConfig.removeDirectoryStringChangeListener(this);
097  }
098
099  @Override
100  public String getName()
101  {
102    return SYNTAX_DIRECTORY_STRING_NAME;
103  }
104
105  @Override
106  public String getOID()
107  {
108    return SYNTAX_DIRECTORY_STRING_OID;
109  }
110
111  @Override
112  public String getDescription()
113  {
114    return SYNTAX_DIRECTORY_STRING_DESCRIPTION;
115  }
116
117  /**
118   * Indicates whether zero-length values will be allowed.  This is technically
119   * forbidden by the LDAP specification, but it was allowed in earlier versions
120   * of the server, and the discussion of the directory string syntax in RFC
121   * 2252 does not explicitly state that they are not allowed.
122   *
123   * @return  <CODE>true</CODE> if zero-length values should be allowed for
124   *          attributes with a directory string syntax, or <CODE>false</CODE>
125   *          if not.
126   */
127  public boolean allowZeroLengthValues()
128  {
129    return allowZeroLengthValues;
130  }
131
132  @Override
133  public boolean isConfigurationChangeAcceptable(
134                      DirectoryStringAttributeSyntaxCfg configuration,
135                      List<LocalizableMessage> unacceptableReasons)
136  {
137    // The configuration will always be acceptable.
138    return true;
139  }
140
141  @Override
142  public ConfigChangeResult applyConfigurationChange(
143              DirectoryStringAttributeSyntaxCfg configuration)
144  {
145    currentConfig = configuration;
146    allowZeroLengthValues = configuration.isAllowZeroLengthValues();
147    final ConfigChangeResult ccr = new ConfigChangeResult();
148    try
149    {
150      serverContext.getSchema().updateSchemaOption(ALLOW_ZERO_LENGTH_DIRECTORY_STRINGS, allowZeroLengthValues);
151    }
152    catch (DirectoryException e)
153    {
154      ccr.setResultCode(e.getResultCode());
155      ccr.addMessage(e.getMessageObject());
156    }
157    return ccr;
158  }
159}