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.interceptor.context;
21   
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  import org.apache.directory.server.core.CoreSession;
28  import org.apache.directory.shared.ldap.constants.SchemaConstants;
29  import org.apache.directory.shared.ldap.name.LdapDN;
30  import org.apache.directory.shared.ldap.util.StringTools;
31  
32  
33  /**
34   * A context for tracking lookup operations. Lookup operations will return a
35   * cloned server entry.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$, $Date$
39   */
40  public class LookupOperationContext extends AbstractOperationContext
41  {
42      private static final String[] EMPTY = new String[] {};
43      
44      /** The list of attributes id to return */
45      private List<String> attrsId = new ArrayList<String>();
46      
47      private Boolean allOperational;
48      
49      private Boolean allUser;
50      
51      
52      /**
53       * 
54       * Creates a new instance of LookupOperationContext.
55       *
56       */
57      public LookupOperationContext( CoreSession session )
58      {
59      	super( session );
60      }
61      
62  
63      /**
64       * 
65       * Creates a new instance of LookupOperationContext.
66       *
67       */
68      public LookupOperationContext( CoreSession session, LdapDN dn )
69      {
70          super( session, dn );
71      }
72      
73  
74      /**
75       * 
76       * Creates a new instance of LookupOperationContext.
77       *
78       */
79      public LookupOperationContext( CoreSession session, String attrsId[] )
80      {
81      	super( session );
82          setAttrsId( attrsId );
83      }
84  
85      
86      /**
87       * 
88       * Creates a new instance of LookupOperationContext.
89       *
90       */
91      public LookupOperationContext( CoreSession session, LdapDN dn, String attrsId[] )
92      {
93          super( session, dn );
94          setAttrsId( attrsId );
95      }
96  
97      
98      /**
99       * @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().getUpName() + "'" + ( ( attrsId != null ) ? ", attributes : <" + StringTools.listToString( attrsId ) + ">" : "" );
216     }
217 }