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;
21  
22  
23  import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
24  import org.apache.directory.server.schema.registries.Registries;
25  import org.apache.directory.shared.ldap.name.LdapDN;
26  import org.apache.directory.shared.ldap.schema.Normalizer;
27  import org.apache.directory.shared.ldap.util.StringTools;
28  
29  import javax.naming.NamingException;
30  
31  
32  /**
33   * A noirmalizer for UniqueMember
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev$
37   */
38  public class NameAndOptionalUIDNormalizer implements Normalizer
39  {
40      private static final long serialVersionUID = 1L;
41  
42      private AttributeTypeRegistry attrRegistry;
43      
44      
45      public NameAndOptionalUIDNormalizer( AttributeTypeRegistry attrRegistry )
46      {
47          this.attrRegistry = attrRegistry;
48      }
49      
50      
51      public NameAndOptionalUIDNormalizer()
52      {
53      }
54   
55      
56      public void setRegistries( Registries registries )
57      {
58          this.attrRegistry = registries.getAttributeTypeRegistry();
59      }
60      
61  
62      public Object normalize( Object value ) throws NamingException
63      {
64          if ( value instanceof byte[] )
65          {
66              value = StringTools.utf8ToString( ( byte[] ) value );
67          }
68  
69          if ( value instanceof String )
70          {
71              String nameAndUid = (String)value;
72              
73              if ( nameAndUid.length() == 0 )
74              {
75                  return false;
76              }
77              
78              // Let's see if we have an UID part
79              int sharpPos = nameAndUid.lastIndexOf( '#' );
80              
81              if ( sharpPos != -1 )
82              {
83                  // Now, check that we don't have another '#'
84                  if ( nameAndUid.indexOf( '#' ) != sharpPos )
85                  {
86                      // Yes, we have one : this is not allowed, it should have been
87                      // escaped.
88                      return false;
89                  }
90                  
91                  // This is an UID if the '#' is immediatly
92                  // followed by a BitString, except if the '#' is
93                  // on the last position
94                  String uid = nameAndUid.substring( sharpPos + 1 );
95                  
96                  if ( sharpPos > 0 )
97                  {
98                      LdapDN dn = new LdapDN( nameAndUid.substring( 0, sharpPos ) );
99                      
100                     dn.normalize( attrRegistry.getNormalizerMapping() );
101                     
102                     return dn.getNormName() + '#' + uid;
103                 }
104                 else
105                 {
106                     throw new IllegalStateException( "I do not know how to handle NameAndOptionalUID normalization with objects of class: " 
107                         + (value == null ? null : value.getClass() ) );
108                 }
109             }
110             else
111             {
112                 // No UID, the strValue is a DN
113                 // Return the normalized DN
114                 return new LdapDN( nameAndUid ).getNormName();
115             }
116         }
117         else
118         {
119             throw new IllegalStateException( "I do not know how to handle NameAndOptionalUID normalization with objects of class: " 
120                 + (value == null ? null : value.getClass() ) );
121         }
122     }
123 }