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.interceptor;
18  
19  
20  import org.apache.ldap.server.authn.LdapPrincipal;
21  import org.apache.ldap.server.invocation.*;
22  import org.apache.ldap.server.jndi.ServerContext;
23  
24  import javax.naming.NamingException;
25  
26  
27  /***
28   * A easy-to-use implementation of {@link Interceptor} that demultiplexes invocations
29   * using method signature overloading.
30   * <p/>
31   * This {@link Interceptor} forwards received process requests to an appropriate
32   * <code>process(NextInterceptor, <em>ConcreteInvocation</em>)</code> methods.  Users
33   * can override any <code>process(..)</code> methods that correspond to
34   * {@link Invocation} types that he or she wants to filter.
35   * <p/>
36   * For example, if user wants to filter {@link Add} invocation:
37   * <pre>
38   * public class MyInterceptor extends BaseInterceptor
39   * {
40   *     protected void process( NextInterceptor nextInterceptor, Add invocation )
41   *     {
42   *         nextInterceptor.process( invocation );
43   *         System.out.println( "Item added!" );
44   *     }
45   * }
46   * </pre>
47   * <code>BaseInterceptor</code> handles all long and tedious if-elseif blocks behind the
48   * scenes.
49   *
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   * @version $Rev: 159316 $, $Date: 2005-03-28 17:20:10 -0500 (Mon, 28 Mar 2005) $
52   */
53  public abstract class BaseInterceptor implements Interceptor
54  {
55      /***
56       * Gets the call's current context's Principal.
57       *
58       * @return the principal making the call
59       */
60      public static LdapPrincipal getPrincipal( Invocation call )
61      {
62          ServerContext ctx = ( ServerContext ) call.getContextStack().peek();
63          return ctx.getPrincipal();
64      }
65  
66  
67      protected BaseInterceptor()
68      {
69      }
70  
71  
72      // ------------------------------------------------------------------------
73      // Interceptor's Invoke Method
74      // ------------------------------------------------------------------------
75  
76      /***
77       * Uses a switch on the invocation method type to call the respective member
78       * analog method that does the work of the Interceptor for that Invocation method.
79       */
80      public void process( NextInterceptor nextInterceptor, Invocation call )
81              throws NamingException
82      {
83          if ( call instanceof Add )
84          {
85              process( nextInterceptor, ( Add ) call );
86          }
87          else if ( call instanceof Delete )
88          {
89              process( nextInterceptor, ( Delete ) call );
90          }
91          else if ( call instanceof GetMatchedDN )
92          {
93              process( nextInterceptor, ( GetMatchedDN ) call );
94          }
95          else if ( call instanceof GetSuffix )
96          {
97              process( nextInterceptor, ( GetSuffix ) call );
98          }
99          else if ( call instanceof HasEntry )
100         {
101             process( nextInterceptor, ( HasEntry ) call );
102         }
103         else if ( call instanceof IsSuffix )
104         {
105             process( nextInterceptor, ( IsSuffix ) call );
106         }
107         else if ( call instanceof List )
108         {
109             process( nextInterceptor, ( List ) call );
110         }
111         else if ( call instanceof ListSuffixes )
112         {
113             process( nextInterceptor, ( ListSuffixes ) call );
114         }
115         else if ( call instanceof Lookup )
116         {
117             process( nextInterceptor, ( Lookup ) call );
118         }
119         else if ( call instanceof LookupWithAttrIds )
120         {
121             process( nextInterceptor, ( LookupWithAttrIds ) call );
122         }
123         else if ( call instanceof Modify )
124         {
125             process( nextInterceptor, ( Modify ) call );
126         }
127         else if ( call instanceof ModifyMany )
128         {
129             process( nextInterceptor, ( ModifyMany ) call );
130         }
131         else if ( call instanceof ModifyRN )
132         {
133             process( nextInterceptor, ( ModifyRN ) call );
134         }
135         else if ( call instanceof Move )
136         {
137             process( nextInterceptor, ( Move ) call );
138         }
139         else if ( call instanceof MoveAndModifyRN )
140         {
141             process( nextInterceptor, ( MoveAndModifyRN ) call );
142         }
143         else if ( call instanceof Search )
144         {
145             process( nextInterceptor, ( Search ) call );
146         }
147         else
148         {
149             throw new IllegalArgumentException( "Unknown call type: " + call.getClass() );
150         }
151     }
152 
153 
154     // ------------------------------------------------------------------------
155     // Invocation Analogs
156     // ------------------------------------------------------------------------
157 
158 
159     protected void process( NextInterceptor nextInterceptor, Add call ) throws NamingException
160     {
161         nextInterceptor.process( call );
162     }
163 
164 
165     protected void process( NextInterceptor nextInterceptor, Delete call ) throws NamingException
166     {
167         nextInterceptor.process( call );
168     }
169 
170 
171     protected void process( NextInterceptor nextInterceptor, GetMatchedDN call ) throws NamingException
172     {
173         nextInterceptor.process( call );
174     }
175 
176 
177     protected void process( NextInterceptor nextInterceptor, GetSuffix call ) throws NamingException
178     {
179         nextInterceptor.process( call );
180     }
181 
182 
183     protected void process( NextInterceptor nextInterceptor, HasEntry call ) throws NamingException
184     {
185         nextInterceptor.process( call );
186     }
187 
188 
189     protected void process( NextInterceptor nextInterceptor, IsSuffix call ) throws NamingException
190     {
191         nextInterceptor.process( call );
192     }
193 
194 
195     protected void process( NextInterceptor nextInterceptor, List call ) throws NamingException
196     {
197         nextInterceptor.process( call );
198     }
199 
200 
201     protected void process( NextInterceptor nextInterceptor, ListSuffixes call ) throws NamingException
202     {
203         nextInterceptor.process( call );
204     }
205 
206 
207     protected void process( NextInterceptor nextInterceptor, Lookup call ) throws NamingException
208     {
209         nextInterceptor.process( call );
210     }
211 
212 
213     protected void process( NextInterceptor nextInterceptor, LookupWithAttrIds call ) throws NamingException
214     {
215         nextInterceptor.process( call );
216     }
217 
218 
219     protected void process( NextInterceptor nextInterceptor, Modify call ) throws NamingException
220     {
221         nextInterceptor.process( call );
222     }
223 
224 
225     protected void process( NextInterceptor nextInterceptor, ModifyMany call ) throws NamingException
226     {
227         nextInterceptor.process( call );
228     }
229 
230 
231     protected void process( NextInterceptor nextInterceptor, ModifyRN call ) throws NamingException
232     {
233         nextInterceptor.process( call );
234     }
235 
236 
237     protected void process( NextInterceptor nextInterceptor, Move call ) throws NamingException
238     {
239         nextInterceptor.process( call );
240     }
241 
242 
243     protected void process( NextInterceptor nextInterceptor, MoveAndModifyRN call ) throws NamingException
244     {
245         nextInterceptor.process( call );
246     }
247 
248 
249     protected void process( NextInterceptor nextInterceptor, Search call ) throws NamingException
250     {
251         nextInterceptor.process( call );
252     }
253 }