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.api.interceptor.context;
21  
22  
23  import static org.apache.directory.api.ldap.model.message.SearchScope.ONELEVEL;
24  
25  import javax.naming.directory.SearchControls;
26  
27  import org.apache.directory.api.ldap.model.filter.ExprNode;
28  import org.apache.directory.api.ldap.model.message.AliasDerefMode;
29  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
30  import org.apache.directory.api.ldap.model.message.SearchRequest;
31  import org.apache.directory.api.ldap.model.message.SearchScope;
32  import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
33  import org.apache.directory.api.ldap.model.name.Dn;
34  import org.apache.directory.api.util.StringConstants;
35  import org.apache.directory.server.core.api.CoreSession;
36  import org.apache.directory.server.core.api.OperationEnum;
37  
38  
39  /**
40   * A Search context used for Interceptors. It contains all the informations
41   * needed for the search operation, and used by all the interceptors
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class SearchOperationContext extends FilteringOperationContext
46  {
47      /** A flag describing the way alias should be handled */
48      private AliasDerefMode aliasDerefMode = AliasDerefMode.DEREF_ALWAYS;
49  
50      /** The sizeLimit for this search operation */
51      private long sizeLimit = 0;
52  
53      /** The timeLimit for this search operation */
54      private int timeLimit = 0;
55  
56      /** The scope for this search : default to One Level */
57      private SearchScope scope = ONELEVEL;
58  
59      /** A flag if the search operation is abandoned */
60      protected boolean abandoned = false;
61  
62      /** The filter */
63      private ExprNode filter;
64  
65  
66      /** flag to indicate if this search is done for replication */
67      private boolean syncreplSearch;
68      
69      /**
70       * Creates a new instance of SearchOperationContext.
71       * 
72       * @param session The session to use
73       */
74      public SearchOperationContext( CoreSession session )
75      {
76          super( session );
77  
78          if ( session != null )
79          {
80              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.SEARCH ) );
81          }
82      }
83  
84  
85      /**
86       * Creates a new instance of SearchOperationContext.
87       * 
88       * @param session The session to use
89       * @param searchRequest The SearchRequest to process
90       */
91      public SearchOperationContext( CoreSession session, SearchRequest searchRequest )
92      {
93          super( session, searchRequest.getBase(), searchRequest.getAttributes().toArray( StringConstants.EMPTY_STRINGS ) );
94  
95          if ( session != null )
96          {
97              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.SEARCH ) );
98          }
99  
100         this.filter = searchRequest.getFilter();
101         this.abandoned = searchRequest.isAbandoned();
102         this.aliasDerefMode = searchRequest.getDerefAliases();
103 
104         this.requestControls = searchRequest.getControls();
105         this.scope = searchRequest.getScope();
106         this.sizeLimit = searchRequest.getSizeLimit();
107         this.timeLimit = searchRequest.getTimeLimit();
108         this.typesOnly = searchRequest.getTypesOnly();
109 
110         throwReferral = !requestControls.containsKey( ManageDsaIT.OID );
111     }
112 
113 
114     /**
115      * Creates a new instance of SearchOperationContext.
116      * 
117      * @param session The session to use
118      * @param dn the dn of the search base
119      * @param filter the filter AST to use for the search
120      * @param searchControls the search controls
121      */
122     public SearchOperationContext( CoreSession session, Dn dn, ExprNode filter, SearchControls searchControls )
123     {
124         super( session, dn, searchControls.getReturningAttributes() );
125         this.filter = filter;
126         scope = SearchScope.getSearchScope( searchControls.getSearchScope() );
127         timeLimit = searchControls.getTimeLimit();
128         sizeLimit = searchControls.getCountLimit();
129         typesOnly = searchControls.getReturningObjFlag();
130 
131         if ( session != null )
132         {
133             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.SEARCH ) );
134         }
135     }
136 
137 
138     /**
139      * Creates a new instance of SearchOperationContext.
140      * 
141      * @param session the session this operation is associated with
142      * @param dn the search base
143      * @param scope the search scope
144      * @param filter the filter AST to use for the search
145      * @param returningAttributes the attributes to return
146      */
147     public SearchOperationContext( CoreSession session, Dn dn, SearchScope scope,
148         ExprNode filter, String... returningAttributes )
149     {
150         super( session, dn, returningAttributes );
151         this.scope = scope;
152         this.filter = filter;
153 
154         if ( session != null )
155         {
156             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.SEARCH ) );
157         }
158     }
159 
160 
161     /**
162      * Checks whether or not the ManageDsaITControl is present.  If not
163      * present then the filter is modified to force the return of all referral
164      * entries regardless of whether or not the filter matches the referral
165      * entry.
166      * 
167      * @return <tt>true</tt> if the ManageDSAIt control is present
168      */
169     public boolean hasManageDsaItControl()
170     {
171         return super.hasRequestControl( ManageDsaIT.OID );
172     }
173 
174 
175     /**
176      * @return The filter
177      */
178     public ExprNode getFilter()
179     {
180         return filter;
181     }
182 
183 
184     /**
185      * Set the filter into the context.
186      *
187      * @param filter The filter to set
188      */
189     public void setFilter( ExprNode filter )
190     {
191         this.filter = filter;
192     }
193 
194 
195     /**
196      * @return the operation name
197      */
198     public String getName()
199     {
200         return MessageTypeEnum.SEARCH_REQUEST.name();
201     }
202 
203 
204     /**
205      * @return true if this is a syncrepl specific search
206      */
207     public boolean isSyncreplSearch()
208     {
209         return syncreplSearch;
210     }
211 
212 
213     /**
214      * Sets the flag to indicate if this is a syncrepl specific search or not
215      * 
216      * @param syncreplSearch The flag indicating it's a syncrepl search
217      */
218     public void setSyncreplSearch( boolean syncreplSearch )
219     {
220         this.syncreplSearch = syncreplSearch;
221     }
222 
223 
224     /**
225      * @return The alias dereferencing mode
226      */
227     public AliasDerefMode getAliasDerefMode()
228     {
229         return aliasDerefMode;
230     }
231 
232 
233     /**
234      * Set the Alias dereferencing mode
235      * @param aliasDerefMode Th erequested mode
236      */
237     public void setAliasDerefMode( AliasDerefMode aliasDerefMode )
238     {
239         this.aliasDerefMode = aliasDerefMode;
240     }
241 
242 
243     /**
244      * @return the sizeLimit
245      */
246     public long getSizeLimit()
247     {
248         return sizeLimit;
249     }
250 
251 
252     /**
253      * @param sizeLimit the sizeLimit to set
254      */
255     public void setSizeLimit( long sizeLimit )
256     {
257         this.sizeLimit = sizeLimit;
258     }
259 
260 
261     /**
262      * @return the timeLimit
263      */
264     public int getTimeLimit()
265     {
266         return timeLimit;
267     }
268 
269 
270     /**
271      * @param timeLimit the timeLimit to set
272      */
273     public void setTimeLimit( int timeLimit )
274     {
275         this.timeLimit = timeLimit;
276     }
277 
278 
279     /**
280      * @return the scope
281      */
282     public SearchScope getScope()
283     {
284         return scope;
285     }
286 
287 
288     /**
289      * @param scope the scope to set
290      */
291     public void setScope( SearchScope scope )
292     {
293         this.scope = scope;
294     }
295 
296 
297     /**
298      * @return the abandoned
299      */
300     public boolean isAbandoned()
301     {
302         return abandoned;
303     }
304 
305 
306     /**
307      * @param abandoned the abandoned to set
308      */
309     public void setAbandoned( boolean abandoned )
310     {
311         this.abandoned = abandoned;
312     }
313 
314 
315     /**
316      * @see Object#toString()
317      */
318     @Override
319     public String toString()
320     {
321         return "SearchContext for Dn '" + getDn().getName() + "', filter :'"
322             + filter + "'";
323     }
324 }