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 */
020package org.apache.directory.server.core.api.interceptor.context;
021
022
023import org.apache.directory.api.ldap.model.entry.Value;
024import org.apache.directory.api.ldap.model.message.CompareRequest;
025import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
026import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
027import org.apache.directory.api.ldap.model.name.Dn;
028import org.apache.directory.api.ldap.model.schema.AttributeType;
029import org.apache.directory.api.util.Strings;
030import org.apache.directory.server.core.api.CoreSession;
031import org.apache.directory.server.core.api.OperationEnum;
032
033
034/**
035 * A Compare context used for Interceptors. It contains all the informations
036 * needed for the compare operation, and used by all the interceptors
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class CompareOperationContext extends AbstractOperationContext
041{
042    /** The entry OID */
043    private String oid;
044
045    /** The associated AttributeType */
046    private AttributeType attributeType;
047
048    /** The value to be compared */
049    private Value value;
050
051
052    /**
053     * Creates a new instance of CompareOperationContext.
054     *
055     * @param session The session to use
056     */
057    public CompareOperationContext( CoreSession session )
058    {
059        super( session );
060
061        if ( session != null )
062        {
063            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
064        }
065    }
066
067
068    /**
069     * Creates a new instance of CompareOperationContext.
070     *
071     * @param session The session to use
072     * @param dn The Dn of teh entry to compare with
073     */
074    public CompareOperationContext( CoreSession session, Dn dn )
075    {
076        super( session, dn );
077
078        if ( session != null )
079        {
080            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
081        }
082    }
083
084
085    /**
086     * Creates a new instance of LookupOperationContext.
087     *
088     * @param session The session to use
089     * @param oid The entry's Oid
090     */
091    public CompareOperationContext( CoreSession session, String oid )
092    {
093        super( session );
094        this.oid = oid;
095
096        if ( session != null )
097        {
098            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.COMPARE ) );
099        }
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}