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.ldap.handlers;
21  
22  
23  import org.apache.directory.api.ldap.model.entry.Entry;
24  import org.apache.directory.api.ldap.model.exception.LdapException;
25  import org.apache.directory.api.ldap.model.message.AbandonListener;
26  import org.apache.directory.api.ldap.model.message.AbandonableRequest;
27  import org.apache.directory.api.ldap.model.message.SearchRequest;
28  import org.apache.directory.api.ldap.model.message.SearchResultEntry;
29  import org.apache.directory.api.ldap.model.message.SearchResultEntryImpl;
30  import org.apache.directory.api.ldap.model.message.controls.ChangeType;
31  import org.apache.directory.api.ldap.model.message.controls.EntryChange;
32  import org.apache.directory.api.ldap.model.message.controls.EntryChangeImpl;
33  import org.apache.directory.api.ldap.model.message.controls.PersistentSearch;
34  import org.apache.directory.api.ldap.model.schema.SchemaManager;
35  import org.apache.directory.api.util.Strings;
36  import org.apache.directory.server.core.api.entry.ClonedServerEntry;
37  import org.apache.directory.server.core.api.entry.ServerEntryUtils;
38  import org.apache.directory.server.core.api.event.DirectoryListener;
39  import org.apache.directory.server.core.api.interceptor.context.AddOperationContext;
40  import org.apache.directory.server.core.api.interceptor.context.ChangeOperationContext;
41  import org.apache.directory.server.core.api.interceptor.context.DeleteOperationContext;
42  import org.apache.directory.server.core.api.interceptor.context.LookupOperationContext;
43  import org.apache.directory.server.core.api.interceptor.context.ModifyOperationContext;
44  import org.apache.directory.server.core.api.interceptor.context.MoveAndRenameOperationContext;
45  import org.apache.directory.server.core.api.interceptor.context.MoveOperationContext;
46  import org.apache.directory.server.core.api.interceptor.context.RenameOperationContext;
47  import org.apache.directory.server.ldap.LdapSession;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  
52  /**
53   * A DirectoryListener implementation which sends back added, deleted, modified or 
54   * renamed entries to a client that created this listener.  This class is part of the
55   * persistent search implementation which uses the event notification scheme built into
56   * the server core.  
57   * 
58   * This listener is disabled only when a session closes or when an abandon request 
59   * cancels it.  Hence time and size limits in normal search operations do not apply
60   * here.
61   * 
62   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
63   */
64  public class PersistentSearchListener implements DirectoryListener, AbandonListener
65  {
66      private static final Logger LOG = LoggerFactory.getLogger( PersistentSearchListener.class );
67      final LdapSession session;
68      final SearchRequest req;
69      final PersistentSearch psearchControl;
70  
71      private LookupOperationContext filterCtx;
72      private SchemaManager schemaManager;
73  
74      public PersistentSearchListener( LdapSession session, SearchRequest req )
75      {
76          this.session = session;
77          this.req = req;
78          req.addAbandonListener( this );
79          this.psearchControl = ( PersistentSearch ) req.getControls().get( PersistentSearch.OID );
80          
81          filterCtx = new LookupOperationContext( session.getCoreSession(), req.getAttributes().toArray( Strings.EMPTY_STRING_ARRAY ) );
82          schemaManager = session.getCoreSession().getDirectoryService().getSchemaManager();
83      }
84  
85      
86      @Override
87      public boolean isSynchronous()
88      {
89          return false; // always asynchronous
90      }
91  
92  
93      public void abandon()
94      {
95          // must abandon the operation 
96          session.getCoreSession().getDirectoryService().getEventService().removeListener( this );
97  
98          /*
99           * From RFC 2251 Section 4.11:
100          * 
101          * In the event that a server receives an Abandon Request on a Search  
102          * operation in the midst of transmitting responses to the Search, that
103          * server MUST cease transmitting entry responses to the abandoned
104          * request immediately, and MUST NOT send the SearchResultDone. Of
105          * course, the server MUST ensure that only properly encoded LDAPMessage
106          * PDUs are transmitted. 
107          * 
108          * SO DON'T SEND BACK ANYTHING!!!!!
109          */
110     }
111 
112 
113     public void requestAbandoned( AbandonableRequest req )
114     {
115         abandon();
116     }
117 
118 
119     private void setECResponseControl( SearchResultEntry response, ChangeOperationContext opContext, ChangeType type )
120     {
121         if ( psearchControl.isReturnECs() )
122         {
123             EntryChange ecControl = new EntryChangeImpl();
124             ecControl.setChangeType( type );
125 
126             if ( opContext.getChangeLogEvent() != null )
127             {
128                 ecControl.setChangeNumber( opContext.getChangeLogEvent().getRevision() );
129             }
130 
131             if ( opContext instanceof RenameOperationContext || opContext instanceof MoveOperationContext )
132             {
133                 ecControl.setPreviousDn( opContext.getDn() );
134             }
135 
136             response.addControl( ecControl );
137         }
138     }
139 
140 
141     public void entryAdded( AddOperationContext addContext )
142     {
143         if ( !psearchControl.isNotificationEnabled( ChangeType.ADD ) )
144         {
145             return;
146         }
147 
148         SearchResultEntry respEntry = new SearchResultEntryImpl( req.getMessageId() );
149         respEntry.setObjectName( addContext.getDn() );
150         
151         // the entry needs to be cloned cause addContext.getEntry() will only contain
152         // the user provided values and all the operational attributes added during
153         // Partition.add() will be applied in the cloned entry present inside it
154         // if we don't clone then the attributes will not be filtered
155         // e.x the operational attributes will also be sent even when a user requests
156         // user attributes only
157         Entry entry = new ClonedServerEntry( addContext.getEntry() );
158         filterEntry( entry );
159         respEntry.setEntry( entry );
160         
161         setECResponseControl( respEntry, addContext, ChangeType.ADD );
162         session.getIoSession().write( respEntry );
163     }
164 
165 
166     public void entryDeleted( DeleteOperationContext deleteContext )
167     {
168         if ( !psearchControl.isNotificationEnabled( ChangeType.DELETE ) )
169         {
170             return;
171         }
172 
173         SearchResultEntry respEntry = new SearchResultEntryImpl( req.getMessageId() );
174         respEntry.setObjectName( deleteContext.getDn() );
175         filterEntry( deleteContext.getEntry() );
176         respEntry.setEntry( deleteContext.getEntry() );
177         setECResponseControl( respEntry, deleteContext, ChangeType.DELETE );
178         session.getIoSession().write( respEntry );
179     }
180 
181 
182     public void entryModified( ModifyOperationContext modifyContext )
183     {
184         if ( !psearchControl.isNotificationEnabled( ChangeType.MODIFY ) )
185         {
186             return;
187         }
188 
189         SearchResultEntry respEntry = new SearchResultEntryImpl( req.getMessageId() );
190         respEntry.setObjectName( modifyContext.getDn() );
191         
192         Entry entry = new ClonedServerEntry( modifyContext.getAlteredEntry() );
193         filterEntry( entry );
194         respEntry.setEntry( entry );
195 
196         setECResponseControl( respEntry, modifyContext, ChangeType.MODIFY );
197         session.getIoSession().write( respEntry );
198     }
199 
200 
201     public void entryMoved( MoveOperationContext moveContext )
202     {
203         if ( !psearchControl.isNotificationEnabled( ChangeType.MODDN ) )
204         {
205             return;
206         }
207 
208         SearchResultEntry respEntry = new SearchResultEntryImpl( req.getMessageId() );
209         respEntry.setObjectName( moveContext.getNewDn() );
210         
211         Entry entry = new ClonedServerEntry( moveContext.getModifiedEntry() );
212         filterEntry( entry );
213         respEntry.setEntry( entry );
214         
215         setECResponseControl( respEntry, moveContext, ChangeType.MODDN );
216         session.getIoSession().write( respEntry );
217     }
218 
219 
220     public void entryMovedAndRenamed( MoveAndRenameOperationContext moveAndRenameContext )
221     {
222         entryRenamed( moveAndRenameContext );
223     }
224 
225 
226     public void entryRenamed( RenameOperationContext renameContext )
227     {
228         if ( !psearchControl.isNotificationEnabled( ChangeType.MODDN ) )
229         {
230             return;
231         }
232 
233         SearchResultEntry respEntry = new SearchResultEntryImpl( req.getMessageId() );
234         respEntry.setObjectName( renameContext.getModifiedEntry().getDn() );
235         
236         Entry entry = new ClonedServerEntry( renameContext.getModifiedEntry() );
237         filterEntry( entry );
238         respEntry.setEntry( entry );
239         
240         setECResponseControl( respEntry, renameContext, ChangeType.MODDN );
241         session.getIoSession().write( respEntry );
242     }
243     
244     
245     /**
246      * A convenient method to filter the contents of an entry
247      * 
248      * @see ServerEntryUtils#filterContents(SchemaManager, org.apache.directory.server.core.api.interceptor.context.FilteringOperationContext, Entry)
249      * 
250      * @param entry
251      */
252     private void filterEntry( Entry entry )
253     {
254         try
255         {
256             ServerEntryUtils.filterContents( schemaManager, filterCtx, entry );
257         }
258         catch ( LdapException e )
259         {
260             // shouldn't happen, if it does then blow up
261             throw new RuntimeException( e );
262         }
263     }
264 }