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.asn1.util.Oid;
24  import org.apache.directory.api.i18n.I18n;
25  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
26  import org.apache.directory.api.ldap.model.exception.LdapException;
27  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
28  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
29  import org.apache.directory.api.ldap.model.schema.Normalizer;
30  import org.apache.directory.api.ldap.model.schema.PrepareString;
31  import org.apache.directory.api.ldap.model.schema.SchemaManager;
32  import org.apache.directory.api.util.Strings;
33  
34  
35  /**
36   * A normalizer for the objectIdentifierMatch matching rule.
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  @SuppressWarnings("serial")
41  public class ObjectIdentifierNormalizer extends Normalizer
42  {
43      /** A reference to the schema manager used to normalize the Name */
44      private transient SchemaManager schemaManager;
45  
46      /**
47       * Creates a new instance of ObjectIdentifierNormalizer.
48       */
49      public ObjectIdentifierNormalizer()
50      {
51          super( SchemaConstants.OBJECT_IDENTIFIER_MATCH_MR_OID );
52      }
53  
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public void setSchemaManager( SchemaManager schemaManager )
60      {
61          this.schemaManager = schemaManager;
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public String normalize( String value ) throws LdapException
70      {
71          if ( Strings.isEmpty( value ) )
72          {
73              return "";
74          }
75          
76          String trimmedValue = value.trim();
77          
78          if ( Strings.isEmpty( trimmedValue ) )
79          {
80              return "";
81          }
82  
83          String oid = schemaManager.getRegistries().getOid( trimmedValue );
84          
85          if ( oid == null )
86          {
87              // Not found in the schemaManager : keep it as is
88              if ( Oid.isOid( trimmedValue ) )
89              {
90                  // It's an numericOid
91                  oid = trimmedValue;
92              }
93              else
94              {
95                  // It's a descr : ALPHA ( ALPHA | DIGIT | '-' )*
96                  for ( int i = 0; i < trimmedValue.length(); i++ )
97                  {
98                      char c = trimmedValue.charAt( i );
99                      
100                     if ( i == 0 )
101                     {
102                         if ( !Character.isLetter( c ) )
103                         {
104                             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
105                                 I18n.ERR_13724_INVALID_VALUE, value ) );
106                         }
107                     }
108                     else
109                     {
110                         if ( !( Character.isDigit( c ) || Character.isLetter( c ) || ( c == '-'  ) || ( c == '_' ) ) )
111                             {
112                             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
113                                 I18n.ERR_13724_INVALID_VALUE, value ) );
114                             }
115                     }
116                 }
117                 
118                 oid = trimmedValue;
119             }
120         }
121         
122         return oid;
123     }
124 
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
131     {
132         return normalize( value );
133     }
134 }