View Javadoc

1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.server.core;
21  
22  
23  import java.net.SocketAddress;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Set;
27  
28  import javax.naming.ldap.Control;
29  
30  import org.apache.directory.server.constants.ServerDNConstants;
31  import org.apache.directory.server.core.authn.LdapPrincipal;
32  import org.apache.directory.server.core.entry.ClonedServerEntry;
33  import org.apache.directory.server.core.entry.ServerEntry;
34  import org.apache.directory.server.core.entry.ServerModification;
35  import org.apache.directory.server.core.filtering.EntryFilteringCursor;
36  import org.apache.directory.server.core.interceptor.context.AddOperationContext;
37  import org.apache.directory.server.core.interceptor.context.CompareOperationContext;
38  import org.apache.directory.server.core.interceptor.context.DeleteOperationContext;
39  import org.apache.directory.server.core.interceptor.context.EntryOperationContext;
40  import org.apache.directory.server.core.interceptor.context.ListOperationContext;
41  import org.apache.directory.server.core.interceptor.context.LookupOperationContext;
42  import org.apache.directory.server.core.interceptor.context.ModifyOperationContext;
43  import org.apache.directory.server.core.interceptor.context.MoveAndRenameOperationContext;
44  import org.apache.directory.server.core.interceptor.context.MoveOperationContext;
45  import org.apache.directory.server.core.interceptor.context.OperationContext;
46  import org.apache.directory.server.core.interceptor.context.RenameOperationContext;
47  import org.apache.directory.server.core.interceptor.context.SearchOperationContext;
48  import org.apache.directory.server.core.interceptor.context.UnbindOperationContext;
49  import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
50  import org.apache.directory.shared.ldap.entry.Modification;
51  import org.apache.directory.shared.ldap.filter.ExprNode;
52  import org.apache.directory.shared.ldap.filter.SearchScope;
53  import org.apache.directory.shared.ldap.message.AddRequest;
54  import org.apache.directory.shared.ldap.message.AliasDerefMode;
55  import org.apache.directory.shared.ldap.message.CompareRequest;
56  import org.apache.directory.shared.ldap.message.DeleteRequest;
57  import org.apache.directory.shared.ldap.message.ModifyDnRequest;
58  import org.apache.directory.shared.ldap.message.ModifyRequest;
59  import org.apache.directory.shared.ldap.message.SearchRequest;
60  import org.apache.directory.shared.ldap.message.UnbindRequest;
61  import org.apache.directory.shared.ldap.name.LdapDN;
62  import org.apache.directory.shared.ldap.name.Rdn;
63  import org.apache.directory.shared.ldap.schema.AttributeTypeOptions;
64  
65  
66  /**
67   * The default CoreSession implementation.
68   * 
69   * TODO - has not been completed yet
70   * TODO - need to supply controls and other parameters to setup opContexts
71   *
72   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
73   * @version $Rev$, $Date$
74   */
75  public class DefaultCoreSession implements CoreSession
76  {
77      private final DirectoryService directoryService;
78      private final LdapPrincipal authenticatedPrincipal;
79      private LdapPrincipal authorizedPrincipal;
80      
81      
82      public DefaultCoreSession( LdapPrincipal principal, DirectoryService directoryService )
83      {
84          this.directoryService = directoryService;
85          this.authenticatedPrincipal = principal;
86      }
87  
88      
89      /* (non-Javadoc)
90       * @see org.apache.directory.server.core.CoreSession#add(org.apache.directory.server.core.entry.ServerEntry)
91       */
92      public void add( ServerEntry entry ) throws Exception
93      {
94          directoryService.getOperationManager().add( new AddOperationContext( this, entry ) );
95      }
96  
97  
98      /* (non-Javadoc)
99       * @see org.apache.directory.server.core.CoreSession#compare(org.apache.directory.shared.ldap.name.LdapDN, java.lang.String, java.lang.Object)
100      */
101     public void compare( LdapDN dn, String oid, Object value ) throws Exception
102     {
103         directoryService.getOperationManager().compare( new CompareOperationContext( this, dn, oid, value ) );
104     }
105 
106 
107     /* (non-Javadoc)
108      * @see org.apache.directory.server.core.CoreSession#delete(org.apache.directory.shared.ldap.name.LdapDN)
109      */
110     public void delete( LdapDN dn ) throws Exception
111     {
112         directoryService.getOperationManager().delete( new DeleteOperationContext( this, dn ) );
113     }
114 
115 
116     /* (non-Javadoc)
117      * @see org.apache.directory.server.core.CoreSession#getAuthenticatedPrincipal()
118      */
119     public LdapPrincipal getAuthenticatedPrincipal()
120     {
121         return authenticatedPrincipal;
122     }
123 
124 
125     /* (non-Javadoc)
126      * @see org.apache.directory.server.core.CoreSession#getAuthenticationLevel()
127      */
128     public AuthenticationLevel getAuthenticationLevel()
129     {
130         return getEffectivePrincipal().getAuthenticationLevel();
131     }
132 
133 
134     /* (non-Javadoc)
135      * @see org.apache.directory.server.core.CoreSession#getClientAddress()
136      */
137     public SocketAddress getClientAddress()
138     {
139         // TODO Auto-generated method stub
140         return null;
141     }
142 
143 
144     /* (non-Javadoc)
145      * @see org.apache.directory.server.core.CoreSession#getControls()
146      */
147     public Set<Control> getControls()
148     {
149         // TODO Auto-generated method stub
150         return null;
151     }
152 
153 
154     /* (non-Javadoc)
155      * @see org.apache.directory.server.core.CoreSession#getDirectoryService()
156      */
157     public DirectoryService getDirectoryService()
158     {
159         return directoryService;
160     }
161 
162 
163     /* (non-Javadoc)
164      * @see org.apache.directory.server.core.CoreSession#getEffectivePrincipal()
165      */
166     public LdapPrincipal getEffectivePrincipal()
167     {
168         if ( authorizedPrincipal == null )
169         {
170             return authenticatedPrincipal;
171         }
172         
173         return authorizedPrincipal;
174     }
175 
176 
177     /* (non-Javadoc)
178      * @see org.apache.directory.server.core.CoreSession#getOutstandingOperations()
179      */
180     public Set<OperationContext> getOutstandingOperations()
181     {
182         // TODO Auto-generated method stub
183         return null;
184     }
185 
186 
187     /* (non-Javadoc)
188      * @see org.apache.directory.server.core.CoreSession#getServiceAddress()
189      */
190     public SocketAddress getServiceAddress()
191     {
192         // TODO Auto-generated method stub
193         return null;
194     }
195 
196 
197     /* (non-Javadoc)
198      * @see org.apache.directory.server.core.CoreSession#isConfidential()
199      */
200     public boolean isConfidential()
201     {
202         // TODO Auto-generated method stub
203         return false;
204     }
205 
206 
207     /* (non-Javadoc)
208      * @see org.apache.directory.server.core.CoreSession#isVirtual()
209      */
210     public boolean isVirtual()
211     {
212         // TODO Auto-generated method stub
213         return true;
214     }
215     
216     
217     /**
218      * TODO - perhaps we should just use a flag that is calculated on creation
219      * of this session
220      *  
221      * @see org.apache.directory.server.core.CoreSession#isAdministrator()
222      */
223     public boolean isAdministrator()
224     {
225         String normName = getEffectivePrincipal().getJndiName().toNormName(); 
226         return normName.equals( ServerDNConstants.ADMIN_SYSTEM_DN_NORMALIZED );
227     }
228 
229 
230     /**
231      * TODO - this method impl does not check to see if the principal is in 
232      * the administrators group - it only returns true of the principal is
233      * the actual admin user.  need to make it check groups.
234      * 
235      * TODO - perhaps we should just use a flag that is calculated on creation
236      * of this session
237      *  
238      * @see org.apache.directory.server.core.CoreSession#isAnAdministrator()
239      */
240     public boolean isAnAdministrator()
241     {
242         if ( isAdministrator() )
243         {
244             return true;
245         }
246         
247         // TODO fix this so it checks groups
248         return false;
249     }
250 
251 
252     /* (non-Javadoc)
253      * @see org.apache.directory.server.core.CoreSession#list(org.apache.directory.shared.ldap.name.LdapDN, org.apache.directory.shared.ldap.message.AliasDerefMode, java.util.Set)
254      */
255     public EntryFilteringCursor list( LdapDN dn, AliasDerefMode aliasDerefMode,
256         Set<AttributeTypeOptions> returningAttributes ) throws Exception
257     {
258         return directoryService.getOperationManager().list( 
259             new ListOperationContext( this, dn, aliasDerefMode, returningAttributes ) );
260     }
261 
262 
263     /* (non-Javadoc)
264      * @see org.apache.directory.server.core.CoreSession#list(org.apache.directory.shared.ldap.name.LdapDN, org.apache.directory.shared.ldap.message.AliasDerefMode, java.util.Set, int, int)
265      */
266     public EntryFilteringCursor list( LdapDN dn, AliasDerefMode aliasDerefMode,
267         Set<AttributeTypeOptions> returningAttributes, int sizeLimit, int timeLimit ) throws Exception
268     {
269         ListOperationContext opContext = new ListOperationContext( this, dn, aliasDerefMode, returningAttributes );
270         opContext.setSizeLimit( sizeLimit );
271         opContext.setTimeLimit( timeLimit );
272         return directoryService.getOperationManager().list( opContext );
273     }
274 
275 
276     /* (non-Javadoc)
277      * @see org.apache.directory.server.core.CoreSession#lookup(org.apache.directory.shared.ldap.name.LdapDN)
278      */
279     public ClonedServerEntry lookup( LdapDN dn ) throws Exception
280     {
281         return directoryService.getOperationManager().lookup( new LookupOperationContext( this, dn ) );
282     }
283 
284 
285     /* (non-Javadoc)
286      * @see org.apache.directory.server.core.CoreSession#lookup(org.apache.directory.shared.ldap.name.LdapDN)
287      */
288     public ClonedServerEntry lookup( LdapDN dn, String[] attrId ) throws Exception
289     {
290         return directoryService.getOperationManager().lookup( 
291             new LookupOperationContext( this, dn, attrId ) );
292     }
293 
294 
295     /* (non-Javadoc)
296      * @see org.apache.directory.server.core.CoreSession#modify(org.apache.directory.shared.ldap.name.LdapDN, java.util.List)
297      */
298     public void modify( LdapDN dn, List<Modification> mods ) throws Exception
299     {
300         if ( mods == null )
301         {
302             return;
303         }
304         
305         List<Modification> serverModifications = new ArrayList<Modification>( mods.size() );
306         
307         for ( Modification mod:mods )
308         {
309             serverModifications.add( new ServerModification( directoryService.getRegistries(), mod ) );
310         }
311         
312         directoryService.getOperationManager().modify( new ModifyOperationContext( this, dn, serverModifications ) );
313     }
314 
315 
316     /* (non-Javadoc)
317      * @see org.apache.directory.server.core.CoreSession#move(org.apache.directory.shared.ldap.name.LdapDN, org.apache.directory.shared.ldap.name.LdapDN)
318      */
319     public void move( LdapDN dn, LdapDN newParent ) throws Exception
320     {
321         directoryService.getOperationManager().move( new MoveOperationContext( this, dn, newParent ) );
322     }
323 
324 
325     /* (non-Javadoc)
326      * @see org.apache.directory.server.core.CoreSession#moveAndRename(org.apache.directory.shared.ldap.name.LdapDN, org.apache.directory.shared.ldap.name.LdapDN, org.apache.directory.shared.ldap.name.Rdn, boolean)
327      */
328     public void moveAndRename( LdapDN dn, LdapDN newParent, Rdn newRdn, boolean deleteOldRdn ) throws Exception
329     {
330         directoryService.getOperationManager().moveAndRename( 
331             new MoveAndRenameOperationContext( this, dn, newParent, newRdn, deleteOldRdn ) );
332     }
333 
334 
335     /* (non-Javadoc)
336      * @see org.apache.directory.server.core.CoreSession#rename(org.apache.directory.shared.ldap.name.LdapDN, org.apache.directory.shared.ldap.name.Rdn, boolean)
337      */
338     public void rename( LdapDN dn, Rdn newRdn, boolean deleteOldRdn ) throws Exception
339     {
340         directoryService.getOperationManager().rename( new RenameOperationContext( this, dn, newRdn, deleteOldRdn ) );
341     }
342 
343 
344     /* (non-Javadoc)
345      * @see org.apache.directory.server.core.CoreSession#search(org.apache.directory.shared.ldap.name.LdapDN, org.apache.directory.shared.ldap.filter.SearchScope, org.apache.directory.shared.ldap.filter.ExprNode, org.apache.directory.shared.ldap.message.AliasDerefMode, java.util.Set)
346      */
347     public EntryFilteringCursor search( LdapDN dn, SearchScope scope, ExprNode filter, AliasDerefMode aliasDerefMode,
348         Set<AttributeTypeOptions> returningAttributes ) throws Exception
349     {
350         return directoryService.getOperationManager().search( new SearchOperationContext( this, dn, scope, filter, 
351             aliasDerefMode, returningAttributes ) );
352     }
353 
354 
355     /* (non-Javadoc)
356      * @see org.apache.directory.server.core.CoreSession#search(org.apache.directory.shared.ldap.name.LdapDN, org.apache.directory.shared.ldap.filter.SearchScope, org.apache.directory.shared.ldap.filter.ExprNode, org.apache.directory.shared.ldap.message.AliasDerefMode, java.util.Set, int, int)
357      */
358     public EntryFilteringCursor search( LdapDN dn, SearchScope scope, ExprNode filter, AliasDerefMode aliasDerefMode,
359         Set<AttributeTypeOptions> returningAttributes, int sizeLimit, int timeLimit ) throws Exception
360     {
361         SearchOperationContext opContext = new SearchOperationContext( this, dn, scope, filter, 
362             aliasDerefMode, returningAttributes );
363         opContext.setSizeLimit( sizeLimit );
364         opContext.setTimeLimit( timeLimit );
365         return directoryService.getOperationManager().search( opContext );
366     }
367 
368 
369     public boolean isAnonymous()
370     {
371         return getEffectivePrincipal().getJndiName().isEmpty();
372     }
373 
374 
375     public void add( AddRequest addRequest ) throws Exception
376     {
377         AddOperationContext opContext = new AddOperationContext( this, addRequest );
378         directoryService.getOperationManager().add( opContext );
379         addRequest.getResultResponse().addAll( opContext.getResponseControls() );
380     }
381 
382 
383     public boolean compare( CompareRequest compareRequest ) throws Exception
384     {
385         CompareOperationContext opContext = new CompareOperationContext( this, compareRequest );
386         boolean result = directoryService.getOperationManager().compare( opContext );
387         compareRequest.getResultResponse().addAll( opContext.getResponseControls() );
388         return result;
389     }
390 
391 
392     public void delete( DeleteRequest deleteRequest ) throws Exception
393     {
394         DeleteOperationContext opContext = new DeleteOperationContext( this, deleteRequest );
395         directoryService.getOperationManager().delete( opContext );
396         deleteRequest.getResultResponse().addAll( opContext.getResponseControls() );
397     }
398 
399 
400     public ClonedServerEntry lookup( LdapDN dn, Control[] requestControls, ReferralHandlingMode refMode,
401         LdapDN authorized ) throws Exception
402     {
403         LookupOperationContext opContext = new LookupOperationContext( this, dn );
404         opContext.addRequestControls( requestControls );
405         return directoryService.getOperationManager().lookup( opContext );
406     }
407 
408 
409     public boolean exists( LdapDN dn ) throws Exception
410     {
411         EntryOperationContext opContext = new EntryOperationContext( this, dn );
412         return directoryService.getOperationManager().hasEntry( opContext );
413     }
414 
415 
416     public void modify( ModifyRequest modifyRequest ) throws Exception
417     {
418         ModifyOperationContext opContext = new ModifyOperationContext( this, modifyRequest );
419         directoryService.getOperationManager().modify( opContext );
420         modifyRequest.getResultResponse().addAll( opContext.getResponseControls() );
421     }
422 
423 
424     public void move( ModifyDnRequest modifyDnRequest ) throws Exception
425     {
426         MoveOperationContext opContext = new MoveOperationContext( this, modifyDnRequest );
427         directoryService.getOperationManager().move( opContext );
428         modifyDnRequest.getResultResponse().addAll( opContext.getResponseControls() );
429     }
430 
431 
432     public void moveAndRename( ModifyDnRequest modifyDnRequest ) throws Exception
433     {
434         MoveAndRenameOperationContext opContext = new MoveAndRenameOperationContext( this, modifyDnRequest );
435         directoryService.getOperationManager().moveAndRename( opContext );
436         modifyDnRequest.getResultResponse().addAll( opContext.getResponseControls() );
437     }
438 
439 
440     public void rename( ModifyDnRequest modifyDnRequest ) throws Exception
441     {
442         RenameOperationContext opContext = new RenameOperationContext( this, modifyDnRequest );
443         directoryService.getOperationManager().rename( opContext );
444         modifyDnRequest.getResultResponse().addAll( opContext.getResponseControls() );
445     }
446 
447 
448     public EntryFilteringCursor search( SearchRequest searchRequest ) throws Exception
449     {
450         SearchOperationContext opContext = new SearchOperationContext( this, searchRequest );
451         EntryFilteringCursor cursor = directoryService.getOperationManager().search( opContext );
452         searchRequest.getResultResponse().addAll( opContext.getResponseControls() );
453         return cursor;
454     }
455 
456 
457     public void unbind() throws Exception
458     {
459         directoryService.getOperationManager().unbind( new UnbindOperationContext( this ) );
460     }
461 
462 
463     public void unbind( UnbindRequest unbindRequest )
464     {
465         // TODO Auto-generated method stub
466         
467     }
468 }