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 java.util.Map;
21  
22  import javax.naming.Name;
23  import javax.naming.NamingEnumeration;
24  import javax.naming.NamingException;
25  import javax.naming.directory.Attributes;
26  import javax.naming.directory.ModificationItem;
27  import javax.naming.directory.SearchControls;
28  
29  import org.apache.ldap.common.filter.ExprNode;
30  import org.apache.ldap.common.name.DnParser;
31  import org.apache.ldap.common.name.NameComponentNormalizer;
32  import org.apache.ldap.common.schema.AttributeType;
33  import org.apache.ldap.server.configuration.InterceptorConfiguration;
34  import org.apache.ldap.server.interceptor.BaseInterceptor;
35  import org.apache.ldap.server.interceptor.NextInterceptor;
36  import org.apache.ldap.server.jndi.ContextFactoryConfiguration;
37  import org.apache.ldap.server.partition.ContextPartitionNexus;
38  import org.apache.ldap.server.schema.AttributeTypeRegistry;
39  
40  
41  /***
42   * A name normalization service.  This service makes sure all relative and distinuished
43   * names are normalized before calls are made against the respective interface methods
44   * on {@link ContextPartitionNexus}.
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   * @version $Rev: 264732 $
48   */
49  public class NormalizationService extends BaseInterceptor
50  {
51      private DnParser parser;
52  
53  
54      public void init( ContextFactoryConfiguration factoryCfg, InterceptorConfiguration cfg ) throws NamingException
55      {
56          AttributeTypeRegistry attributeRegistry = factoryCfg.getGlobalRegistries().getAttributeTypeRegistry();
57          parser = new DnParser( new PerComponentNormalizer( attributeRegistry ) );
58      }
59  
60  
61      public void destroy()
62      {
63      }
64  
65  
66      // ------------------------------------------------------------------------
67      // Normalize all Name based arguments for ContextPartition interface operations
68      // ------------------------------------------------------------------------
69  
70  
71      public void add( NextInterceptor nextInterceptor, String upName, Name normName, Attributes attrs ) throws NamingException
72      {
73          synchronized( parser )
74          {
75              normName = parser.parse( normName.toString() );
76          }
77  
78          nextInterceptor.add( upName, normName, attrs );
79      }
80  
81  
82      public void delete( NextInterceptor nextInterceptor, Name name ) throws NamingException
83      {
84          synchronized( parser )
85          {
86              name = parser.parse( name.toString() );
87          }
88  
89          nextInterceptor.delete( name );
90      }
91  
92  
93      public void modify( NextInterceptor nextInterceptor, Name name, int modOp, Attributes attrs ) throws NamingException
94      {
95          synchronized( parser )
96          {
97              name = parser.parse( name.toString() );
98          }
99  
100         nextInterceptor.modify( name, modOp, attrs );
101     }
102 
103 
104     public void modify( NextInterceptor nextInterceptor, Name name, ModificationItem[] items ) throws NamingException
105     {
106         synchronized( parser )
107         {
108             name = parser.parse( name.toString() );
109         }
110 
111         nextInterceptor.modify( name, items );
112     }
113 
114 
115     public void modifyRn( NextInterceptor nextInterceptor, Name name, String newRn, boolean deleteOldRn ) throws NamingException
116     {
117         synchronized( parser )
118         {
119             name = parser.parse( name.toString() );
120         }
121 
122         nextInterceptor.modifyRn( name, newRn, deleteOldRn );
123     }
124 
125 
126     public void move( NextInterceptor nextInterceptor, Name name, Name newParentName ) throws NamingException
127     {
128         synchronized( parser )
129         {
130             name = parser.parse( name.toString() );
131             newParentName = parser.parse( newParentName.toString() );
132         }
133 
134         nextInterceptor.move( name, newParentName );
135     }
136 
137 
138     public void move( NextInterceptor nextInterceptor, Name name, Name newParentName, String newRn, boolean deleteOldRn ) throws NamingException
139     {
140         synchronized( parser )
141         {
142             name = parser.parse( name.toString() );
143 
144             newParentName = parser.parse( newParentName.toString() );
145         }
146 
147         nextInterceptor.move( name, newParentName, newRn, deleteOldRn );
148     }
149 
150 
151     public NamingEnumeration search( NextInterceptor nextInterceptor,
152             Name base, Map env, ExprNode filter,
153             SearchControls searchCtls ) throws NamingException
154     {
155         synchronized( parser )
156         {
157             base = parser.parse( base.toString() );
158         }
159 
160         return nextInterceptor.search( base, env, filter, searchCtls );
161     }
162 
163 
164     public boolean hasEntry( NextInterceptor nextInterceptor, Name name ) throws NamingException
165     {
166         synchronized( parser )
167         {
168             name = parser.parse( name.toString() );
169         }
170 
171         return nextInterceptor.hasEntry( name );
172     }
173 
174 
175     public boolean isSuffix( NextInterceptor nextInterceptor, Name name ) throws NamingException
176     {
177         synchronized( parser )
178         {
179             name = parser.parse( name.toString() );
180         }
181 
182         return nextInterceptor.isSuffix( name );
183     }
184 
185 
186     public NamingEnumeration list( NextInterceptor nextInterceptor, Name base ) throws NamingException
187     {
188         synchronized( parser )
189         {
190             base = parser.parse( base.toString() );
191         }
192 
193         return nextInterceptor.list( base );
194     }
195 
196 
197     public Attributes lookup( NextInterceptor nextInterceptor, Name name ) throws NamingException
198     {
199         synchronized( parser )
200         {
201             name = parser.parse( name.toString() );
202         }
203 
204         return nextInterceptor.lookup( name );
205     }
206 
207 
208     public Attributes lookup( NextInterceptor nextInterceptor, Name name, String[] attrIds ) throws NamingException
209     {
210         synchronized( parser )
211         {
212             name = parser.parse( name.toString() );
213         }
214 
215         return nextInterceptor.lookup( name, attrIds );
216     }
217 
218 
219     // ------------------------------------------------------------------------
220     // Normalize all Name based arguments for other interface operations
221     // ------------------------------------------------------------------------
222 
223 
224     public Name getMatchedName( NextInterceptor nextInterceptor, Name name, boolean normalized ) throws NamingException
225     {
226         synchronized( parser )
227         {
228             name = parser.parse( name.toString() );
229         }
230 
231         return nextInterceptor.getMatchedName( name, normalized );
232     }
233 
234 
235     public Name getSuffix( NextInterceptor nextInterceptor, Name name, boolean normalized ) throws NamingException
236     {
237         synchronized( parser )
238         {
239             name = parser.parse( name.toString() );
240         }
241 
242         return nextInterceptor.getSuffix( name, normalized );
243     }
244 
245 
246 
247     /***
248      * A normalizer that normalizes each name component specifically according to
249      * the attribute type of the name component.
250      */
251     private class PerComponentNormalizer implements NameComponentNormalizer
252     {
253         /*** the attribute type registry we use to lookup component normalizers */
254         private final AttributeTypeRegistry registry;
255 
256 
257         /***
258          * Creates a name component normalizer that looks up normalizers using
259          * an AttributeTypeRegistry.
260          *
261          * @param registry the attribute type registry to get normalizers
262          */
263         public PerComponentNormalizer( AttributeTypeRegistry registry )
264         {
265             this.registry = registry;
266         }
267 
268 
269         public String normalizeByName( String name, String value ) throws NamingException
270         {
271             AttributeType type = registry.lookup( name );
272 
273             return ( String ) type.getEquality().getNormalizer().normalize( value );
274         }
275 
276 
277         public String normalizeByOid( String oid, String value ) throws NamingException
278         {
279             AttributeType type = registry.lookup( oid );
280 
281             return ( String ) type.getEquality().getNormalizer().normalize( value );
282         }
283     }
284 }