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   *    http://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.server.schema.bootstrap;
21  
22  
23  import javax.naming.NamingException;
24  
25  import org.apache.directory.server.schema.registries.OidRegistry;
26  import org.apache.directory.server.schema.registries.Registries;
27  import org.apache.directory.shared.ldap.exception.LdapNamingException;
28  import org.apache.directory.shared.ldap.message.ResultCodeEnum;
29  import org.apache.directory.shared.ldap.schema.Normalizer;
30  import org.apache.directory.shared.ldap.schema.syntax.NumericOidSyntaxChecker;
31  import org.apache.directory.shared.ldap.util.StringTools;
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   * setRegistries(Registries) method is exposed.
38   * 
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev$
42   */
43  public class NameOrNumericIdNormalizer implements Normalizer
44  {
45      private static final long serialVersionUID = 1L;
46      private NumericOidSyntaxChecker checker = new NumericOidSyntaxChecker();
47      private transient OidRegistry registry;
48      
49      
50      public NameOrNumericIdNormalizer( OidRegistry registry )
51      {
52          this.registry = registry;
53      }
54      
55      
56      public NameOrNumericIdNormalizer()
57      {
58      }
59      
60      
61      /* (non-Javadoc)
62       * @see org.apache.directory.shared.ldap.schema.Normalizer#normalize(java.lang.Object)
63       */
64      public Object normalize( Object value ) throws NamingException
65      {
66          String strValue;
67  
68          if ( value == null )
69          {
70              return null;
71          }
72          
73          if ( value instanceof String )
74          {
75              strValue = ( String ) value;
76          }
77          else if ( value instanceof byte[] )
78          {
79              strValue = StringTools.utf8ToString( ( byte[] ) value ); 
80          }
81          else
82          {
83              strValue = value.toString();
84          }
85  
86          if ( strValue.length() == 0 )
87          {
88              return "";
89          }
90          
91          // if value is a numeric id then return it as is
92          if ( checker.isValidSyntax( strValue ) )
93          {
94              return value;
95          }
96          
97          // if it is a name we need to do a lookup
98          if ( registry.hasOid( strValue ) )
99          {
100             return registry.getOid( strValue );
101         }
102         
103         // if all else fails
104         throw new LdapNamingException( "Encountered name based id of " + value 
105             + " which was not found in the OID registry" , ResultCodeEnum.OTHER );
106     }
107     
108     
109     public void setRegistries( Registries registries )
110     {
111         this.registry = registries.getOidRegistry();
112     }
113 }