View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.normalization;
18  
19  
20  import org.apache.ldap.server.interceptor.BaseInterceptor;
21  import org.apache.ldap.server.interceptor.InterceptorContext;
22  import org.apache.ldap.server.interceptor.NextInterceptor;
23  import org.apache.ldap.server.invocation.*;
24  import org.apache.ldap.server.schema.AttributeTypeRegistry;
25  import org.apache.ldap.common.name.NameComponentNormalizer;
26  import org.apache.ldap.common.name.DnParser;
27  import org.apache.ldap.common.schema.AttributeType;
28  
29  import javax.naming.NamingException;
30  
31  
32  /***
33   * A name normalization service.  This service makes sure all relative and distinuished
34   * names are normalized before calls are made against the respective interface methods
35   * on the root nexus.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$
39   */
40  public class NormalizationService extends BaseInterceptor
41  {
42      private DnParser parser;
43  
44  
45      public void init( InterceptorContext context ) throws NamingException
46      {
47          AttributeTypeRegistry attributeRegistry = context.getGlobalRegistries().getAttributeTypeRegistry();
48  
49          parser = new DnParser( new PerComponentNormalizer( attributeRegistry ) );
50      }
51  
52  
53      public void destroy()
54      {
55      }
56  
57  
58      // ------------------------------------------------------------------------
59      // Normalize all Name based arguments for BackingStore interface operations
60      // ------------------------------------------------------------------------
61  
62  
63      protected void process( NextInterceptor nextInterceptor, Add call ) throws NamingException
64      {
65          synchronized( parser )
66          {
67              call.setNormalizedName( parser.parse( call.getNormalizedName().toString() ) );
68          }
69  
70          super.process( nextInterceptor, call );
71      }
72  
73  
74      protected void process( NextInterceptor nextInterceptor, Delete call ) throws NamingException
75      {
76          synchronized( parser )
77          {
78              call.setName( parser.parse( call.getName().toString() ) );
79          }
80  
81          super.process( nextInterceptor, call );
82      }
83  
84  
85      protected void process( NextInterceptor nextInterceptor, Modify call ) throws NamingException
86      {
87          synchronized( parser )
88          {
89              call.setName( parser.parse( call.getName().toString() ) );
90          }
91  
92          super.process( nextInterceptor, call );
93      }
94  
95  
96      protected void process( NextInterceptor nextInterceptor, ModifyMany call ) throws NamingException
97      {
98          synchronized( parser )
99          {
100             call.setName( parser.parse( call.getName().toString() ) );
101         }
102 
103         super.process( nextInterceptor, call );
104     }
105 
106 
107     protected void process( NextInterceptor nextInterceptor, ModifyRN call ) throws NamingException
108     {
109         synchronized( parser )
110         {
111             call.setName( parser.parse( call.getName().toString() ) );
112         }
113 
114         super.process( nextInterceptor, call );
115     }
116 
117 
118     protected void process( NextInterceptor nextInterceptor, Move call ) throws NamingException
119     {
120         synchronized( parser )
121         {
122             call.setName( parser.parse( call.getName().toString() ) );
123 
124             call.setNewParentName( parser.parse( call.getNewParentName().toString() ) );
125         }
126 
127         super.process( nextInterceptor, call );
128     }
129 
130 
131     protected void process( NextInterceptor nextInterceptor, MoveAndModifyRN call ) throws NamingException
132     {
133         synchronized( parser )
134         {
135             call.setName( parser.parse( call.getName().toString() ) );
136 
137             call.setNewParentName( parser.parse( call.getNewParentName().toString() ) );
138         }
139 
140         super.process( nextInterceptor, call );
141     }
142 
143 
144     protected void process( NextInterceptor nextInterceptor, Search call ) throws NamingException
145     {
146         synchronized( parser )
147         {
148             call.setBaseName( parser.parse( call.getBaseName().toString() ) );
149         }
150 
151         super.process( nextInterceptor, call );
152     }
153 
154 
155     protected void process( NextInterceptor nextInterceptor, HasEntry call ) throws NamingException
156     {
157         synchronized( parser )
158         {
159             call.setName( parser.parse( call.getName().toString() ) );
160         }
161 
162         super.process( nextInterceptor, call );
163     }
164 
165 
166     protected void process( NextInterceptor nextInterceptor, IsSuffix call ) throws NamingException
167     {
168         synchronized( parser )
169         {
170             call.setName( parser.parse( call.getName().toString() ) );
171         }
172 
173         super.process( nextInterceptor, call );
174     }
175 
176 
177     protected void process( NextInterceptor nextInterceptor, List call ) throws NamingException
178     {
179         synchronized( parser )
180         {
181             call.setBaseName( parser.parse( call.getBaseName().toString() ) );
182         }
183 
184         super.process( nextInterceptor, call );
185     }
186 
187 
188     protected void process( NextInterceptor nextInterceptor, Lookup call ) throws NamingException
189     {
190         synchronized( parser )
191         {
192             call.setName( parser.parse( call.getName().toString() ) );
193         }
194 
195         super.process( nextInterceptor, call );
196     }
197 
198 
199     protected void process( NextInterceptor nextInterceptor, LookupWithAttrIds call ) throws NamingException
200     {
201         synchronized( parser )
202         {
203             call.setName( parser.parse( call.getName().toString() ) );
204         }
205 
206         super.process( nextInterceptor, call );
207     }
208 
209 
210     // ------------------------------------------------------------------------
211     // Normalize all Name based arguments for other interface operations
212     // ------------------------------------------------------------------------
213 
214 
215     protected void process( NextInterceptor nextInterceptor, GetMatchedDN call ) throws NamingException
216     {
217         synchronized( parser )
218         {
219             call.setName( parser.parse( call.getName().toString() ) );
220         }
221 
222         super.process( nextInterceptor, call );
223     }
224 
225 
226     protected void process( NextInterceptor nextInterceptor, GetSuffix call ) throws NamingException
227     {
228         synchronized( parser )
229         {
230             call.setName( parser.parse( call.getName().toString() ) );
231         }
232 
233         super.process( nextInterceptor, call );
234     }
235 
236 
237 
238     /***
239      * A normalizer that normalizes each name component specifically according to
240      * the attribute type of the name component.
241      */
242     class PerComponentNormalizer implements NameComponentNormalizer
243     {
244         /*** the attribute type registry we use to lookup component normalizers */
245         private final AttributeTypeRegistry registry;
246 
247 
248         /***
249          * Creates a name component normalizer that looks up normalizers using
250          * an AttributeTypeRegistry.
251          *
252          * @param registry the attribute type registry to get normalizers
253          */
254         public PerComponentNormalizer( AttributeTypeRegistry registry )
255         {
256             this.registry = registry;
257         }
258 
259 
260         public String normalizeByName( String name, String value ) throws NamingException
261         {
262             AttributeType type = registry.lookup( name );
263 
264             return ( String ) type.getEquality().getNormalizer().normalize( value );
265         }
266 
267 
268         public String normalizeByOid( String oid, String value ) throws NamingException
269         {
270             AttributeType type = registry.lookup( oid );
271 
272             return ( String ) type.getEquality().getNormalizer().normalize( value );
273         }
274     }
275 }