1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.schema.normalizers;
21
22
23 import java.io.UnsupportedEncodingException;
24
25 import org.apache.directory.api.i18n.I18n;
26 import org.apache.directory.api.ldap.model.entry.BinaryValue;
27 import org.apache.directory.api.ldap.model.exception.LdapException;
28 import org.apache.directory.api.ldap.model.schema.AttributeType;
29 import org.apache.directory.api.ldap.model.schema.MatchingRule;
30 import org.apache.directory.api.ldap.model.schema.Normalizer;
31 import org.apache.directory.api.ldap.model.schema.SchemaManager;
32 import org.apache.directory.api.util.Hex;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43
44 public class ConcreteNameComponentNormalizer implements NameComponentNormalizer
45 {
46
47 private static final Logger LOG = LoggerFactory.getLogger( ConcreteNameComponentNormalizer.class );
48
49
50 private final SchemaManager schemaManager;
51
52
53
54
55
56
57
58
59
60 public ConcreteNameComponentNormalizer( SchemaManager schemaManager )
61 {
62 this.schemaManager = schemaManager;
63 }
64
65
66 private String unescape( String value )
67 {
68 char[] newVal = new char[value.length()];
69 int escaped = 0;
70 char high = 0;
71 char low = 0;
72 int pos = 0;
73
74 for ( char c : value.toCharArray() )
75 {
76 switch ( escaped )
77 {
78 case 0:
79 if ( c == '\\' )
80 {
81 escaped = 1;
82 }
83 else
84 {
85 newVal[pos++] = c;
86 }
87
88 break;
89
90 case 1:
91 escaped++;
92 high = c;
93 break;
94
95 case 2:
96 escaped = 0;
97 low = c;
98 newVal[pos++] = ( char ) Hex.getHexValue( high, low );
99
100 }
101 }
102
103 return new String( newVal, 0, pos );
104 }
105
106
107
108
109
110 public Object normalizeByName( String name, String value ) throws LdapException
111 {
112 AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( name );
113
114 if ( attributeType.getSyntax().isHumanReadable() )
115 {
116 return lookup( name ).normalize( value );
117 }
118 else
119 {
120 try
121 {
122 String unescaped = unescape( value );
123 byte[] valBytes = unescaped.getBytes( "UTF-8" );
124
125 return lookup( name ).normalize( new BinaryValue( valBytes ) );
126 }
127 catch ( UnsupportedEncodingException uee )
128 {
129 String message = I18n.err( I18n.ERR_04222 );
130 LOG.error( message );
131 throw new LdapException( message, uee );
132 }
133 }
134
135 }
136
137
138
139
140
141 public Object normalizeByName( String name, byte[] value ) throws LdapException
142 {
143 AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( name );
144
145 if ( !attributeType.getSyntax().isHumanReadable() )
146 {
147 return lookup( name ).normalize( new BinaryValue( value ) );
148 }
149 else
150 {
151 try
152 {
153 String valStr = new String( value, "UTF-8" );
154 return lookup( name ).normalize( valStr );
155 }
156 catch ( UnsupportedEncodingException uee )
157 {
158 String message = I18n.err( I18n.ERR_04223 );
159 LOG.error( message );
160 throw new LdapException( message, uee );
161 }
162 }
163 }
164
165
166
167
168
169 public Object normalizeByOid( String oid, String value ) throws LdapException
170 {
171 return lookup( oid ).normalize( value );
172 }
173
174
175
176
177
178 public Object normalizeByOid( String oid, byte[] value ) throws LdapException
179 {
180 return lookup( oid ).normalize( new BinaryValue( value ) );
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195 private Normalizer lookup( String id ) throws LdapException
196 {
197 AttributeType type = schemaManager.lookupAttributeTypeRegistry( id );
198 MatchingRule mrule = type.getEquality();
199
200 if ( mrule == null )
201 {
202 return new NoOpNormalizer( id );
203 }
204
205 return mrule.getNormalizer();
206 }
207
208
209
210
211
212 public boolean isDefined( String id )
213 {
214 return schemaManager.getAttributeTypeRegistry().contains( id );
215 }
216
217
218 public String normalizeName( String attributeName ) throws LdapException
219 {
220 return schemaManager.getAttributeTypeRegistry().getOidByName( attributeName );
221 }
222 }