View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.normalizers;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.exception.LdapException;
26  import org.apache.directory.api.ldap.model.exception.LdapOtherException;
27  import org.apache.directory.api.ldap.model.schema.Normalizer;
28  import org.apache.directory.api.ldap.model.schema.PrepareString;
29  import org.apache.directory.api.ldap.model.schema.SchemaManager;
30  import org.apache.directory.api.ldap.model.schema.syntaxCheckers.NumericOidSyntaxChecker;
31  import org.apache.directory.api.util.Strings;
32  
33  
34  /**
35   * A name or numeric id normalizer.  Needs an OID registry to operate properly.
36   * The OID registry is injected into this class after instantiation if a 
37   * setSchemaManager(SchemaManager) method is exposed.
38   * 
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  @SuppressWarnings("serial")
43  public class NameOrNumericIdNormalizer extends Normalizer
44  {
45      private NumericOidSyntaxChecker checker = NumericOidSyntaxChecker.INSTANCE;
46  
47      /** A reference to the schema manager used to normalize the Name */
48      private transient SchemaManager schemaManager;
49  
50      /** A static instance of this normalizer */
51      public static final NameOrNumericIdNormalizer INSTANCE = new NameOrNumericIdNormalizer();
52  
53  
54      /**
55       * Creates a new instance of GeneralizedTimeNormalizer.
56       */
57      public NameOrNumericIdNormalizer()
58      {
59          super( SchemaConstants.NAME_OR_NUMERIC_ID_MATCH_OID );
60      }
61  
62  
63      /**
64       * {@inheritDoc} 
65       */
66      @Override
67      public String normalize( String value ) throws LdapException
68      {
69          return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
70      }
71  
72  
73      /**
74       * {@inheritDoc} 
75       */
76      @Override
77      public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
78      {
79          if ( Strings.isEmpty( value ) )
80          {
81              return value;
82          }
83  
84          // if value is a numeric id then return it as is
85          if ( checker.isValidSyntax( value ) )
86          {
87              return value;
88          }
89  
90          // if it is a name we need to do a lookup
91          String oid = schemaManager.getRegistries().getOid( value );
92  
93          if ( oid != null )
94          {
95              return oid;
96          }
97  
98          // if all else fails and the schema is not in relaxed mode, throw an exception
99          if ( schemaManager.isStrict() )
100         {
101             throw new LdapOtherException( I18n.err( I18n.ERR_13725_CANNOT_HANDLE_NAME_AND_OPTIONAL_UID_NORM, value ) );
102         }
103         else
104         {
105             return value;
106         }
107     }
108 
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public void setSchemaManager( SchemaManager schemaManager )
115     {
116         this.schemaManager = schemaManager;
117     }
118 }