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.opends.server.schema.SchemaConstants.*;
020
021import java.util.List;
022
023import org.forgerock.i18n.LocalizableMessage;
024import org.forgerock.opendj.config.server.ConfigChangeResult;
025import org.forgerock.opendj.config.server.ConfigException;
026import org.forgerock.opendj.ldap.schema.Schema;
027import org.forgerock.opendj.ldap.schema.SchemaOptions;
028import org.forgerock.opendj.ldap.schema.Syntax;
029import org.forgerock.opendj.config.server.ConfigurationChangeListener;
030import org.forgerock.opendj.server.config.server.TelephoneNumberAttributeSyntaxCfg;
031import org.opends.server.api.AttributeSyntax;
032import org.opends.server.core.ServerContext;
033import org.opends.server.types.DirectoryException;
034
035/**
036 * This class implements the telephone number attribute syntax, which is defined
037 * in RFC 2252.  Note that this can have two modes of operation, depending on
038 * its configuration.  Most of the time, it will be very lenient when deciding
039 * what to accept, and will allow anything but only pay attention to the digits.
040 * However, it can also be configured in a "strict" mode, in which case it will
041 * only accept values in the E.123 international telephone number format.
042 */
043public class TelephoneNumberSyntax
044       extends AttributeSyntax<TelephoneNumberAttributeSyntaxCfg>
045       implements ConfigurationChangeListener<TelephoneNumberAttributeSyntaxCfg>
046{
047
048  /** Indicates whether this matching rule should operate in strict mode. */
049  private boolean strictMode;
050
051  /** The current configuration for this telephone number syntax. */
052  private TelephoneNumberAttributeSyntaxCfg currentConfig;
053
054  private ServerContext serverContext;
055
056  /**
057   * Creates a new instance of this syntax.  Note that the only thing that
058   * should be done here is to invoke the default constructor for the
059   * superclass.  All initialization should be performed in the
060   * <CODE>initializeSyntax</CODE> method.
061   */
062  public TelephoneNumberSyntax()
063  {
064    super();
065  }
066
067  @Override
068  public void initializeSyntax(TelephoneNumberAttributeSyntaxCfg configuration, ServerContext serverContext)
069      throws ConfigException, DirectoryException
070  {
071    this.serverContext = serverContext;
072
073    // We may or may not have access to the config entry.  If we do, then see if
074    // we should use the strict compliance mode. If not, just assume that we won't.
075    strictMode = false;
076    if (configuration != null)
077    {
078      currentConfig = configuration;
079      currentConfig.addTelephoneNumberChangeListener(this);
080      strictMode = currentConfig.isStrictFormat();
081      serverContext.getSchema().updateSchemaOption(SchemaOptions.ALLOW_NON_STANDARD_TELEPHONE_NUMBERS, !strictMode);
082    }
083  }
084
085  @Override
086  public Syntax getSDKSyntax(Schema schema)
087  {
088    return schema.getSyntax(SchemaConstants.SYNTAX_TELEPHONE_OID);
089  }
090
091  @Override
092  public void finalizeSyntax()
093  {
094    currentConfig.removeTelephoneNumberChangeListener(this);
095  }
096
097  @Override
098  public String getName()
099  {
100    return SYNTAX_TELEPHONE_NAME;
101  }
102
103  @Override
104  public String getOID()
105  {
106    return SYNTAX_TELEPHONE_OID;
107  }
108
109  @Override
110  public String getDescription()
111  {
112    return SYNTAX_TELEPHONE_DESCRIPTION;
113  }
114
115  @Override
116  public boolean isConfigurationChangeAcceptable(
117                      TelephoneNumberAttributeSyntaxCfg configuration,
118                      List<LocalizableMessage> unacceptableReasons)
119  {
120    // The configuration will always be acceptable.
121    return true;
122  }
123
124  @Override
125  public ConfigChangeResult applyConfigurationChange(
126              TelephoneNumberAttributeSyntaxCfg configuration)
127  {
128    currentConfig = configuration;
129    strictMode = configuration.isStrictFormat();
130    final ConfigChangeResult ccr = new ConfigChangeResult();
131    try
132    {
133      serverContext.getSchema().updateSchemaOption(SchemaOptions.ALLOW_NON_STANDARD_TELEPHONE_NUMBERS, !strictMode);
134    }
135    catch (DirectoryException e)
136    {
137      ccr.setResultCode(e.getResultCode());
138      ccr.addMessage(e.getMessageObject());
139    }
140    return ccr;
141  }
142}
143