001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core.interceptor;
021    
022    
023    import java.util.Set;
024    
025    import javax.naming.Context;
026    
027    import org.apache.directory.server.core.DirectoryService;
028    import org.apache.directory.server.core.LdapPrincipal;
029    import org.apache.directory.server.core.entry.ClonedServerEntry;
030    import org.apache.directory.server.core.filtering.EntryFilteringCursor;
031    import org.apache.directory.server.core.interceptor.context.AddContextPartitionOperationContext;
032    import org.apache.directory.server.core.interceptor.context.AddOperationContext;
033    import org.apache.directory.server.core.interceptor.context.BindOperationContext;
034    import org.apache.directory.server.core.interceptor.context.CompareOperationContext;
035    import org.apache.directory.server.core.interceptor.context.DeleteOperationContext;
036    import org.apache.directory.server.core.interceptor.context.EntryOperationContext;
037    import org.apache.directory.server.core.interceptor.context.GetMatchedNameOperationContext;
038    import org.apache.directory.server.core.interceptor.context.GetRootDSEOperationContext;
039    import org.apache.directory.server.core.interceptor.context.GetSuffixOperationContext;
040    import org.apache.directory.server.core.interceptor.context.ListOperationContext;
041    import org.apache.directory.server.core.interceptor.context.ListSuffixOperationContext;
042    import org.apache.directory.server.core.interceptor.context.LookupOperationContext;
043    import org.apache.directory.server.core.interceptor.context.ModifyOperationContext;
044    import org.apache.directory.server.core.interceptor.context.MoveAndRenameOperationContext;
045    import org.apache.directory.server.core.interceptor.context.MoveOperationContext;
046    import org.apache.directory.server.core.interceptor.context.OperationContext;
047    import org.apache.directory.server.core.interceptor.context.RemoveContextPartitionOperationContext;
048    import org.apache.directory.server.core.interceptor.context.RenameOperationContext;
049    import org.apache.directory.server.core.interceptor.context.SearchOperationContext;
050    import org.apache.directory.server.core.interceptor.context.UnbindOperationContext;
051    import org.apache.directory.server.core.invocation.InvocationStack;
052    import org.apache.directory.shared.ldap.name.DN;
053    
054    
055    /**
056     * A easy-to-use implementation of {@link Interceptor}.  All methods are
057     * implemented to pass the flow of control to next interceptor by defaults.
058     * Please override the methods you have concern in.
059     *
060     * @org.apache.xbean.XBean
061     *
062     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
063     * @version $Rev: 918766 $, $Date: 2010-03-04 00:25:11 +0100 (Thu, 04 Mar 2010) $
064     */
065    public abstract class BaseInterceptor implements Interceptor
066    {
067        /**
068         * default interceptor name is its class, preventing accidental duplication of interceptors by naming
069         * instances differently
070         * @return (default, class name) interceptor name
071         */
072        public String getName()
073        {
074            return getClass().getName();
075        }
076        
077        /**
078         * TODO delete this since it uses static access
079         * Returns {@link LdapPrincipal} of current context.
080         * @return the authenticated principal
081         */
082        public static LdapPrincipal getPrincipal()
083        {
084            return getContext().getSession().getEffectivePrincipal();
085        }
086    
087    
088        /**
089         * TODO delete this since it uses static access
090         * Returns the current JNDI {@link Context}.
091         * @return the context on the invocation stack
092         */
093        public static OperationContext getContext()
094        {
095            return InvocationStack.getInstance().peek();
096        }
097    
098    
099        /**
100         * Creates a new instance.
101         */
102        protected BaseInterceptor()
103        {
104        }
105    
106    
107        /**
108         * This method does nothing by default.
109         * @throws Exception 
110         */
111        public void init( DirectoryService directoryService ) throws Exception
112        {
113        }
114    
115    
116        /**
117         * This method does nothing by default.
118         */
119        public void destroy()
120        {
121        }
122    
123    
124        // ------------------------------------------------------------------------
125        // Interceptor's Invoke Method
126        // ------------------------------------------------------------------------
127    
128        public void add( NextInterceptor next, AddOperationContext opContext ) throws Exception
129        {
130            next.add( opContext );
131        }
132    
133    
134        public void delete( NextInterceptor next, DeleteOperationContext opContext ) throws Exception
135        {
136            next.delete( opContext );
137        }
138    
139    
140        public DN getMatchedName ( NextInterceptor next, GetMatchedNameOperationContext opContext ) throws Exception
141        {
142            return next.getMatchedName( opContext );
143        }
144    
145    
146        public ClonedServerEntry getRootDSE( NextInterceptor next, GetRootDSEOperationContext opContext ) throws Exception
147        {
148            return next.getRootDSE( opContext );
149        }
150    
151    
152        public DN getSuffix( NextInterceptor next, GetSuffixOperationContext opContext ) throws Exception
153        {
154            return next.getSuffix( opContext );
155        }
156    
157    
158        public boolean hasEntry( NextInterceptor next, EntryOperationContext opContext ) throws Exception
159        {
160            return next.hasEntry( opContext );
161        }
162    
163    
164        public EntryFilteringCursor list( NextInterceptor next, ListOperationContext opContext ) throws Exception
165        {
166            return next.list( opContext );
167        }
168    
169    
170        public Set<String> listSuffixes ( NextInterceptor next, ListSuffixOperationContext opContext ) 
171            throws Exception
172        {
173            return next.listSuffixes( opContext );
174        }
175    
176    
177        public ClonedServerEntry lookup( NextInterceptor next, LookupOperationContext opContext ) throws Exception
178        {
179            return next.lookup( opContext );
180        }
181    
182        
183        public void modify( NextInterceptor next, ModifyOperationContext opContext ) throws Exception
184        {
185            next.modify( opContext );
186        }
187    
188    
189        public void moveAndRename( NextInterceptor next, MoveAndRenameOperationContext opContext )
190            throws Exception
191        {
192            next.moveAndRename( opContext );
193        }
194    
195    
196        public void rename( NextInterceptor next, RenameOperationContext opContext )
197            throws Exception
198        {
199            next.rename( opContext );
200        }
201    
202    
203        public void move( NextInterceptor next, MoveOperationContext opContext ) throws Exception
204        {
205            next.move( opContext );
206        }
207    
208    
209        public EntryFilteringCursor search( NextInterceptor next, SearchOperationContext opContext ) throws Exception
210        {
211            return next.search( opContext );
212        }
213    
214    
215        public void addContextPartition( NextInterceptor next, AddContextPartitionOperationContext opContext ) throws Exception
216        {
217            next.addContextPartition( opContext );
218        }
219    
220    
221        public void removeContextPartition( NextInterceptor next, RemoveContextPartitionOperationContext opContext ) throws Exception
222        {
223            next.removeContextPartition( opContext );
224        }
225    
226    
227        public boolean compare( NextInterceptor next, CompareOperationContext opContext ) throws Exception
228        {
229            return next.compare( opContext );
230        }
231    
232    
233        public void bind( NextInterceptor next, BindOperationContext opContext ) throws Exception
234        {
235            next.bind( opContext );
236        }
237    
238    
239        public void unbind( NextInterceptor next, UnbindOperationContext opContext ) throws Exception
240        {
241            next.unbind( opContext );
242        }
243    }