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.context;
021    
022    
023    import java.util.Set;
024    
025    import javax.naming.directory.SearchControls;
026    
027    import org.apache.directory.server.core.CoreSession;
028    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
029    import org.apache.directory.shared.ldap.codec.controls.ManageDsaITControl;
030    import org.apache.directory.shared.ldap.constants.SchemaConstants;
031    import org.apache.directory.shared.ldap.filter.ExprNode;
032    import org.apache.directory.shared.ldap.filter.SearchScope;
033    import org.apache.directory.shared.ldap.message.internal.InternalSearchRequest;
034    import org.apache.directory.shared.ldap.name.DN;
035    import org.apache.directory.shared.ldap.schema.AttributeTypeOptions;
036    
037    
038    /**
039     * A Search context used for Interceptors. It contains all the informations
040     * needed for the search operation, and used by all the interceptors
041     *
042     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043     * @version $Rev$, $Date$
044     */
045    public class SearchOperationContext extends SearchingOperationContext
046    {
047        /** The filter */
048        private ExprNode filter;
049    
050    
051        /**
052         * Creates a new instance of SearchOperationContext.
053         */
054        public SearchOperationContext( CoreSession session )
055        {
056            super( session );
057        }
058    
059    
060        /**
061         * Creates a new instance of SearchOperationContext.
062         * @throws Exception 
063         */
064        public SearchOperationContext( CoreSession session, InternalSearchRequest searchRequest ) throws Exception
065        {
066            super( session );
067            
068            this.dn = searchRequest.getBase();
069            this.filter = searchRequest.getFilter();
070            this.abandoned = searchRequest.isAbandoned();
071            this.aliasDerefMode = searchRequest.getDerefAliases();
072            
073            this.requestControls = searchRequest.getControls();
074            this.scope = searchRequest.getScope();
075            this.sizeLimit = searchRequest.getSizeLimit();
076            this.timeLimit = searchRequest.getTimeLimit();
077            this.typesOnly = searchRequest.getTypesOnly();
078            setReturningAttributes( searchRequest.getAttributes() );
079            
080            if ( requestControls.containsKey( ManageDsaITControl.CONTROL_OID ) )
081            {
082                throwReferral = false;
083            }
084            else
085            {
086                throwReferral = true;
087            }
088        }
089    
090    
091        /**
092         * Creates a new instance of SearchOperationContext.
093         * 
094         * @param dn the dn of the search base
095         * @param filter the filter AST to use for the search
096         * @param searchControls the search controls
097         */
098        public SearchOperationContext( CoreSession session, DN dn, ExprNode filter, SearchControls searchControls ) throws Exception
099        {
100            super( session, dn );
101            this.filter = filter;
102            scope = SearchScope.getSearchScope( searchControls.getSearchScope() );
103            timeLimit = searchControls.getTimeLimit();
104            sizeLimit = searchControls.getCountLimit();
105            typesOnly = searchControls.getReturningObjFlag();
106    
107            if ( searchControls.getReturningAttributes() != null )
108            {
109                setReturningAttributes( searchControls.getReturningAttributes() );
110            }
111            else
112            {
113                setReturningAttributes( SchemaConstants.ALL_USER_ATTRIBUTES_ARRAY );
114            }
115        }
116    
117    
118        /**
119         * Creates a new instance of SearchOperationContext.
120         * 
121         * @param session the session this operation is associated with
122         * @param dn the search base
123         * @param scope the search scope
124         * @param filter the filter AST to use for the search
125         * @param aliasDerefMode the alias dereferencing mode
126         * @param returningAttributes the attributes to return
127         */
128        public SearchOperationContext( CoreSession session, DN dn, SearchScope scope,
129            ExprNode filter, Set<AttributeTypeOptions> returningAttributes )
130        {
131            super( session, dn, returningAttributes );
132            super.setScope( scope );
133            this.filter = filter;
134        }
135    
136    
137        /**
138         * Checks whether or not the ManageDsaITControl is present.  If not 
139         * present then the filter is modified to force the return of all referral
140         * entries regardless of whether or not the filter matches the referral
141         * entry.
142         */
143        public boolean hasManageDsaItControl()
144        {
145            return super.hasRequestControl( ManageDsaITControl.CONTROL_OID );
146        }
147        
148        
149        /**
150         * @return The filter
151         */
152        public ExprNode getFilter()
153        {
154            return filter;
155        }
156    
157    
158        /**
159         * Set the filter into the context.
160         *
161         * @param filter The filter to set
162         */
163        public void setFilter( ExprNode filter )
164        {
165            this.filter = filter;
166        }
167    
168    
169        /**
170         * @see Object#toString()
171         */
172        public String toString()
173        {
174            return "SearchContext for DN '" + getDn().getName() + "', filter :'"
175            + filter + "'"; 
176        }
177    
178    
179        /**
180         * @return the operation name
181         */
182        public String getName()
183        {
184            return MessageTypeEnum.SEARCH_REQUEST.name();
185        }
186    }