mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
19.48.2016 3f803490a041ecc636da2e0a9ec978d108527b22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2009 Sun Microsystems, Inc.
 * Portions Copyright 2012-2016 ForgeRock AS.
 */
package org.opends.server.schema;
 
import static org.opends.messages.ConfigMessages.*;
import static org.opends.messages.SchemaMessages.*;
 
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.config.server.ConfigChangeResult;
import org.forgerock.opendj.config.server.ConfigException;
import org.forgerock.opendj.config.server.ConfigurationChangeListener;
import org.forgerock.opendj.ldap.schema.ConflictingSchemaElementException;
import org.forgerock.opendj.ldap.schema.CoreSchema;
import org.forgerock.opendj.ldap.schema.MatchingRule;
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldap.schema.SchemaBuilder;
import org.forgerock.opendj.server.config.server.CollationMatchingRuleCfg;
import org.opends.server.api.MatchingRuleFactory;
import org.opends.server.core.DirectoryServer;
import org.opends.server.schema.SchemaHandler.SchemaUpdater;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.InitializationException;
import org.opends.server.util.CollectionUtils;
 
/**
 * This class is a factory class for Collation matching rules. It
 * creates different matching rules based on the configuration entries.
 */
public final class CollationMatchingRuleFactory extends
    MatchingRuleFactory<CollationMatchingRuleCfg> implements
    ConfigurationChangeListener<CollationMatchingRuleCfg>
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  /** Stores the list of available locales on this JVM. */
  private static final Set<Locale> supportedLocales = CollectionUtils.newHashSet(Locale.getAvailableLocales());
 
  /** Current Configuration. */
  private CollationMatchingRuleCfg currentConfig;
  /** Map of OID and the Matching Rule. */
  private final Map<String, MatchingRule> matchingRules = new HashMap<>();
 
  /** Creates a new instance of CollationMatchingRuleFactory. */
  public CollationMatchingRuleFactory()
  {
    super();
  }
 
  @Override
  public final Collection<MatchingRule> getMatchingRules()
  {
    return Collections.unmodifiableCollection(matchingRules.values());
  }
 
  @Override
  public void initializeMatchingRule(CollationMatchingRuleCfg configuration)
      throws ConfigException, InitializationException
  {
    // The core schema contains all supported collation matching rules so read it for initialization.
    // The server's schemaNG may have different things configured slightly differently
    SchemaHandler schemaHandler = DirectoryServer.getInstance().getServerContext().getSchemaHandler();
    Schema coreSchema = CoreSchema.getInstance();
 
    // on startup, the SDK already has existing matching rules
    // remove them all before letting the server set them all up
    // according to what this factory decides must be setup
    final Set<MatchingRule> defaultMatchingRules = getCollationMatchingRules(coreSchema.getMatchingRules());
    unregisterMatchingRules(schemaHandler, defaultMatchingRules);
    matchingRules.putAll(collectConfiguredMatchingRules(configuration, coreSchema));
 
    // Save this configuration.
    currentConfig = configuration;
 
    // Register for change events.
    currentConfig.addCollationChangeListener(this);
  }
 
  private void unregisterMatchingRules(SchemaHandler schemaHandler, final Collection<MatchingRule> matchingRules)
      throws ConfigException
  {
    try
    {
      schemaHandler.updateSchema(new SchemaUpdater()
      {
        @Override
        public void update(SchemaBuilder builder)
        {
          for (final MatchingRule rule : matchingRules)
          {
            builder.removeMatchingRule(rule.getNameOrOID());
          }
        }
      });
    }
    catch (DirectoryException e)
    {
      throw new ConfigException(e.getMessageObject(), e);
    }
  }
 
  private Map<String, MatchingRule> collectConfiguredMatchingRules(CollationMatchingRuleCfg configuration,
      Schema coreSchema)
  {
    final Map<String, MatchingRule> results = new HashMap<>();
    for (String collation : configuration.getCollation())
    {
      CollationMapper mapper = new CollationMapper(collation);
 
      String nOID = mapper.getNumericOID();
      String languageTag = mapper.getLanguageTag();
      if (nOID == null || languageTag == null)
      {
        logger.error(WARN_ATTR_INVALID_COLLATION_MATCHING_RULE_FORMAT, collation);
        continue;
      }
      Locale locale = getLocale(languageTag);
      if (locale == null)
      {
        // This locale is not supported by JVM.
        logger.error(WARN_ATTR_INVALID_COLLATION_MATCHING_RULE_LOCALE, collation, configuration.dn(), languageTag);
        continue;
      }
 
      final int[] numericSuffixes = { 1, 2, 3, 4, 5, 6 };
      for (int suffix : numericSuffixes)
      {
        final String oid = nOID + "." + suffix;
        final MatchingRule matchingRule = coreSchema.getMatchingRule(oid);
        results.put(oid, matchingRule);
      }
      // the default (equality) matching rule
      final MatchingRule defaultEqualityMatchingRule = coreSchema.getMatchingRule(nOID);
      results.put(nOID, defaultEqualityMatchingRule);
    }
    return results;
  }
 
  private Set<MatchingRule> getCollationMatchingRules(Collection<MatchingRule> matchingRules)
  {
    final Set<MatchingRule> results = new HashSet<>();
    for (MatchingRule matchingRule : matchingRules)
    {
      if (matchingRule.getOID().startsWith("1.3.6.1.4.1.42.2.27.9.4."))
      {
        results.add(matchingRule);
      }
    }
    return results;
  }
 
  @Override
  public void finalizeMatchingRule()
  {
    // De-register the listener.
    currentConfig.removeCollationChangeListener(this);
  }
 
  @Override
  public ConfigChangeResult applyConfigurationChange(final CollationMatchingRuleCfg configuration)
  {
    // validation has already been performed in isConfigurationChangeAcceptable()
    final ConfigChangeResult ccr = new ConfigChangeResult();
 
    if (!configuration.isEnabled()
        || currentConfig.isEnabled() != configuration.isEnabled())
    {
      // Don't do anything if:
      // 1. The configuration is disabled.
      // 2. There is a change in the enable status
      // i.e. (disable->enable or enable->disable). In this case, the
      // ConfigManager will have already created the new Factory object.
      return ccr;
    }
 
    // Since we have come here it means that this Factory is enabled
    // and there is a change in the CollationMatchingRuleFactory's configuration.
    SchemaHandler schemaHandler = DirectoryServer.getInstance().getServerContext().getSchemaHandler();
    final Collection<MatchingRule> existingCollationRules =
        getCollationMatchingRules(schemaHandler.getSchema().getMatchingRules());
 
    matchingRules.clear();
    final Map<String, MatchingRule> configuredMatchingRules =
        collectConfiguredMatchingRules(configuration, CoreSchema.getInstance());
    matchingRules.putAll(configuredMatchingRules);
 
    for (Iterator<MatchingRule> it = existingCollationRules.iterator(); it.hasNext();)
    {
      String oid = it.next().getOID();
      if (configuredMatchingRules.remove(oid) != null)
      {
        // no change
        it.remove();
      }
    }
    try
    {
      schemaHandler.updateSchema(new SchemaUpdater()
      {
        @Override
        public void update(SchemaBuilder builder)
        {
          Collection<MatchingRule> defaultMatchingRules = CoreSchema.getInstance().getMatchingRules();
          for (MatchingRule rule : defaultMatchingRules)
          {
            if (configuredMatchingRules.containsKey(rule.getOID()))
            {
              try
              {
                // added
                builder.buildMatchingRule(rule).addToSchema();
              }
              catch (ConflictingSchemaElementException e)
              {
                ccr.setAdminActionRequired(true);
                ccr.addMessage(WARN_CONFIG_SCHEMA_MR_CONFLICTING_MR.get(configuration.dn(), e.getMessageObject()));
              }
            }
          }
          for (MatchingRule ruleToRemove : existingCollationRules)
          {
            // removed
            builder.removeMatchingRule(ruleToRemove.getOID());
          }
        }
      });
    }
    catch (DirectoryException e)
    {
      ccr.setResultCode(e.getResultCode());
      ccr.addMessage(e.getMessageObject());
    }
 
    currentConfig = configuration;
    return ccr;
  }
 
  @Override
  public boolean isConfigurationChangeAcceptable(
      CollationMatchingRuleCfg configuration,
      List<LocalizableMessage> unacceptableReasons)
  {
    boolean configAcceptable = true;
 
    // If the new configuration disables this factory, don't do anything.
    if (!configuration.isEnabled())
    {
      return configAcceptable;
    }
 
    // If it comes here we don't need to verify MatchingRuleType;
    // it should be okay as its syntax is verified by the admin framework.
    // Iterate over the collations and verify if the format is okay.
    // Also, verify if the locale is allowed by the JVM.
    for (String collation : configuration.getCollation())
    {
      CollationMapper mapper = new CollationMapper(collation);
 
      String nOID = mapper.getNumericOID();
      String languageTag = mapper.getLanguageTag();
      if (nOID == null || languageTag == null)
      {
        configAcceptable = false;
        unacceptableReasons.add(WARN_ATTR_INVALID_COLLATION_MATCHING_RULE_FORMAT.get(collation));
        continue;
      }
 
      Locale locale = getLocale(languageTag);
      if (locale == null)
      {
        configAcceptable = false;
        unacceptableReasons.add(WARN_ATTR_INVALID_COLLATION_MATCHING_RULE_LOCALE.get(
                collation, configuration.dn(), languageTag));
        continue;
      }
    }
    return configAcceptable;
  }
 
  /**
   * Verifies if the locale is supported by the JVM.
   *
   * @param lTag
   *          The language tag specified in the configuration.
   * @return Locale The locale corresponding to the languageTag.
   */
  private Locale getLocale(String lTag)
  {
    // Separates the language and the country from the locale.
    Locale locale;
 
    int countryIndex = lTag.indexOf("-");
    int variantIndex = lTag.lastIndexOf("-");
 
    if (countryIndex > 0)
    {
      String lang = lTag.substring(0, countryIndex);
      String country;
 
      if (variantIndex > countryIndex)
      {
        country = lTag.substring(countryIndex + 1, variantIndex);
        String variant = lTag.substring(variantIndex + 1, lTag.length());
        locale = new Locale(lang, country, variant);
      }
      else
      {
        country = lTag.substring(countryIndex + 1, lTag.length());
        locale = new Locale(lang, country);
      }
    }
    else
    {
      locale = new Locale(lTag);
    }
 
    if (!supportedLocales.contains(locale))
    {
      // This locale is not supported by this JVM.
      locale = null;
    }
    return locale;
  }
 
  /** A utility class for extracting the OID and Language Tag from the configuration entry. */
  private final class CollationMapper
  {
    /** OID of the collation rule. */
    private String oid;
 
    /** Language Tag. */
    private String lTag;
 
    /**
     * Creates a new instance of CollationMapper.
     *
     * @param collation
     *          The collation text in the LOCALE:OID format.
     */
    private CollationMapper(String collation)
    {
      int index = collation.indexOf(":");
      if (index > 0)
      {
        oid = collation.substring(index + 1, collation.length());
        lTag = collation.substring(0, index);
      }
    }
 
    /**
     * Returns the OID part of the collation text.
     *
     * @return OID part of the collation text.
     */
    private String getNumericOID()
    {
      return oid;
    }
 
    /**
     * Returns the language Tag of collation text.
     *
     * @return Language Tag part of the collation text.
     */
    private String getLanguageTag()
    {
      return lTag;
    }
  }
}