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.ArrayList;
024    import java.util.Arrays;
025    import java.util.List;
026    
027    import org.apache.directory.server.core.CoreSession;
028    import org.apache.directory.shared.ldap.constants.SchemaConstants;
029    import org.apache.directory.shared.ldap.name.DN;
030    import org.apache.directory.shared.ldap.util.StringTools;
031    
032    
033    /**
034     * A context for tracking lookup operations. Lookup operations will return a
035     * cloned server entry.
036     *
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev$, $Date$
039     */
040    public class LookupOperationContext extends AbstractOperationContext
041    {
042        private static final String[] EMPTY = new String[] {};
043        
044        /** The list of attributes id to return */
045        private List<String> attrsId = new ArrayList<String>();
046        
047        private Boolean allOperational;
048        
049        private Boolean allUser;
050        
051        
052        /**
053         * 
054         * Creates a new instance of LookupOperationContext.
055         *
056         */
057        public LookupOperationContext( CoreSession session )
058        {
059            super( session );
060        }
061        
062    
063        /**
064         * 
065         * Creates a new instance of LookupOperationContext.
066         *
067         */
068        public LookupOperationContext( CoreSession session, DN dn )
069        {
070            super( session, dn );
071        }
072        
073    
074        /**
075         * 
076         * Creates a new instance of LookupOperationContext.
077         *
078         */
079        public LookupOperationContext( CoreSession session, String attrsId[] )
080        {
081            super( session );
082            setAttrsId( attrsId );
083        }
084    
085        
086        /**
087         * 
088         * Creates a new instance of LookupOperationContext.
089         *
090         */
091        public LookupOperationContext( CoreSession session, DN dn, String attrsId[] )
092        {
093            super( session, dn );
094            setAttrsId( attrsId );
095        }
096    
097        
098        /**
099         * @return Get the attribute ids as a String array
100         */
101        public String[] getAttrsIdArray()
102        {
103            if ( attrsId == null || attrsId.size() == 0 )
104            {
105                return EMPTY;
106            }
107            else
108            {
109                String[] attrs = new String[ attrsId.size()];
110                return attrsId.toArray( attrs );
111            }
112        }
113    
114        
115        /**
116         * Set the attribute Ids
117         *
118         * @param attrsId The String array containing all the attribute IDs
119         */
120        public void setAttrsId( String[] attrsId )
121        {
122            if ( attrsId != null && attrsId.length > 0 )
123            {
124                this.attrsId = new ArrayList<String>( Arrays.asList( attrsId ) );
125                
126                // filter out the '+' and '*' and set boolean parameters 
127                for ( String id : this.attrsId )
128                {
129                    if ( id.equals( SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES ) )
130                    {
131                        allOperational = true;
132                    }
133                    else if ( id.equals( SchemaConstants.ALL_USER_ATTRIBUTES ) )
134                    {
135                        allUser = true;
136                    }
137                }
138    
139                if ( allOperational != null && allOperational )
140                {
141                    this.attrsId.remove( SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES );
142                }
143                
144                if ( allUser != null && allUser )
145                {
146                    this.attrsId.remove( SchemaConstants.ALL_USER_ATTRIBUTES );
147                }
148            }
149        }
150    
151    
152        /**
153         * Add an attribute ID to the current list, creating the list if necessary
154         *
155         * @param attrId the Id to add
156         */
157        public void addAttrsId( String attrId )
158        {
159            if ( attrId.equals( SchemaConstants.ALL_USER_ATTRIBUTES ) )
160            {
161                allUser = true;
162                return;
163            }
164            
165            if ( attrId.equals( SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES ) )
166            {
167                allOperational = true;
168                return;
169            }
170            
171            if ( attrsId == null )
172            {
173                attrsId = new ArrayList<String>(); 
174            }
175            
176            attrsId.add( attrId );
177        }
178    
179        
180        /**
181         * @return The attribute IDs list
182         */
183        public List<String> getAttrsId()
184        {
185            return attrsId;
186        }
187    
188        
189        public Boolean getAllUser()
190        {
191            return allUser;
192        }
193        
194    
195        public Boolean getAllOperational()
196        {
197            return allOperational;
198        }
199        
200    
201        /**
202         * @return the operation name
203         */
204        public String getName()
205        {
206            return "Lookup";
207        }
208    
209        
210        /**
211         * @see Object#toString()
212         */
213        public String toString()
214        {
215            return "LookupContext for DN '" + getDn().getName() + "'" + ( ( attrsId != null ) ? ", attributes : <" + StringTools.listToString( attrsId ) + ">" : "" );
216        }
217    }