1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
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
79 int sharpPos = nameAndUid.lastIndexOf( '#' );
80
81 if ( sharpPos != -1 )
82 {
83
84 if ( nameAndUid.indexOf( '#' ) != sharpPos )
85 {
86
87
88 return false;
89 }
90
91
92
93
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
113
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 }