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 org.apache.directory.api.ldap.model.entry.Value;
24  import org.apache.directory.api.ldap.model.message.CompareRequest;
25  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
26  import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
27  import org.apache.directory.api.ldap.model.name.Dn;
28  import org.apache.directory.api.ldap.model.schema.AttributeType;
29  import org.apache.directory.api.util.Strings;
30  import org.apache.directory.server.core.api.CoreSession;
31  import org.apache.directory.server.core.api.OperationEnum;
32  
33  
34  /**
35   * A Compare context used for Interceptors. It contains all the informations
36   * needed for the compare operation, and used by all the interceptors
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class CompareOperationContext extends AbstractOperationContext
41  {
42      /** The entry OID */
43      private String oid;
44  
45      /** The associated AttributeType */
46      private AttributeType attributeType;
47  
48      /** The value to be compared */
49      private Value value;
50  
51  
52      /**
53       * Creates a new instance of CompareOperationContext.
54       *
55       * @param session The session to use
56       */
57      public CompareOperationContext( CoreSession session )
58      {
59          super( session );
60  
61          if ( session != null )
62          {
63              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
64          }
65      }
66  
67  
68      /**
69       * Creates a new instance of CompareOperationContext.
70       *
71       * @param session The session to use
72       * @param dn The Dn of teh entry to compare with
73       */
74      public CompareOperationContext( CoreSession session, Dn dn )
75      {
76          super( session, dn );
77  
78          if ( session != null )
79          {
80              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
81          }
82      }
83  
84  
85      /**
86       * Creates a new instance of LookupOperationContext.
87       *
88       * @param session The session to use
89       * @param oid The entry's Oid
90       */
91      public CompareOperationContext( CoreSession session, String oid )
92      {
93          super( session );
94          this.oid = oid;
95  
96          if ( session != null )
97          {
98              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
99          }
100     }
101 
102 
103     /**
104      * Creates a new instance of LookupOperationContext.
105      *
106      * @param session The session to use
107      * @param dn The entry's Dn
108      * @param oid The entry's Oid
109      */
110     public CompareOperationContext( CoreSession session, Dn dn, String oid )
111     {
112         super( session, dn );
113         this.oid = oid;
114 
115         if ( session != null )
116         {
117             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
118         }
119     }
120 
121 
122     /**
123      * Creates a new instance of LookupOperationContext.
124      *
125      * @param session The session to use
126      * @param dn The entry's Dn
127      * @param oid The entry's Oid
128      * @param value The value to compare
129      */
130     public CompareOperationContext( CoreSession session, Dn dn, String oid, Value value )
131     {
132         super( session, dn );
133         this.oid = oid;
134         this.value = value;
135 
136         if ( session != null )
137         {
138             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
139         }
140     }
141 
142 
143     /**
144      * Creates a new instance of LookupOperationContext.
145      * 
146      * @param session The session to use
147      * @param compareRequest The Compare operation to process
148      */
149     public CompareOperationContext( CoreSession session, CompareRequest compareRequest )
150     {
151         super( session, compareRequest.getName() );
152         this.oid = compareRequest.getAttributeId();
153         this.value = compareRequest.getAssertionValue();
154         this.requestControls = compareRequest.getControls();
155 
156         if ( session != null )
157         {
158             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
159         }
160 
161         if ( requestControls.containsKey( ManageDsaIT.OID ) )
162         {
163             ignoreReferral();
164         }
165         else
166         {
167             throwReferral();
168         }
169     }
170 
171 
172     /**
173      * @return The compared OID
174      */
175     public String getOid()
176     {
177         return oid;
178     }
179 
180 
181     /**
182      * Set the compared OID
183      * @param oid The compared OID
184      */
185     public void setOid( String oid )
186     {
187         this.oid = oid;
188     }
189 
190 
191     /**
192      * @return The value to compare
193      */
194     public Value getValue()
195     {
196         return value;
197     }
198 
199 
200     /**
201      * Set the value to compare
202      * @param value The value to compare
203      */
204     public void setValue( Value value )
205     {
206         this.value = value;
207     }
208 
209 
210     /**
211      *  @return The AttributeType for the compared value
212      */
213     public AttributeType getAttributeType()
214     {
215         return attributeType;
216     }
217 
218 
219     /**
220      * Set the AttributeType associated with the OID
221      * 
222      * @param attributeType The AttributeType
223      */
224     public void setAttributeType( AttributeType attributeType )
225     {
226         this.attributeType = attributeType;
227     }
228 
229 
230     /**
231      * @return the operation name
232      */
233     public String getName()
234     {
235         return MessageTypeEnum.COMPARE_REQUEST.name();
236     }
237 
238 
239     /**
240      * @see Object#toString()
241      */
242     public String toString()
243     {
244         return "CompareContext for Dn '" + getDn().getName() + "'"
245             + ( ( oid != null ) ? ", oid : <" + oid + ">" : "" )
246             + ( ( value != null ) 
247                 ? ", value :'"
248                     + ( ( value.isHumanReadable() )
249                         ? value.getString()
250                         : ( ( !value.isHumanReadable() )
251                             ? Strings.dumpBytes( value.getBytes() )
252                             : "unknown value type" ) )
253                 + "'"
254                 : "" );
255     }
256 }