001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.protocols.ldap;
028
029
030import java.io.IOException;
031
032import org.forgerock.opendj.io.*;
033import org.forgerock.opendj.ldap.ByteString;
034
035import org.forgerock.i18n.slf4j.LocalizedLogger;
036import static org.opends.server.protocols.ldap.LDAPConstants.*;
037import static org.opends.server.util.ServerConstants.*;
038
039
040/**
041 * This class defines the structures and methods for an LDAP compare request
042 * protocol op, which is used to determine whether a particular entry contains
043 * a specified attribute value.
044 */
045public class CompareRequestProtocolOp
046       extends ProtocolOp
047{
048  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
049
050  /** The assertion value for this compare request. */
051  private ByteString assertionValue;
052
053  /** The DN for this compare request. */
054  private ByteString dn;
055
056  /** The attribute type for this compare request. */
057  private String attributeType;
058
059
060
061  /**
062   * Creates a new compare request protocol op with the provided information.
063   *
064   * @param  dn              The DN for this compare request.
065   * @param  attributeType   The attribute type for this compare request.
066   * @param  assertionValue  The assertion value for this compare request.
067   */
068  public CompareRequestProtocolOp(ByteString dn, String attributeType,
069                                  ByteString assertionValue)
070  {
071    this.dn             = dn;
072    this.attributeType  = attributeType;
073    this.assertionValue = assertionValue;
074  }
075
076
077
078  /**
079   * Retrieves the DN for this compare request.
080   *
081   * @return  The DN for this compare request.
082   */
083  public ByteString getDN()
084  {
085    return dn;
086  }
087
088
089
090  /**
091   * Retrieves the attribute type for this compare request.
092   *
093   * @return  The attribute type for this compare request.
094   */
095  public String getAttributeType()
096  {
097    return attributeType;
098  }
099
100
101
102  /**
103   * Retrieves the assertion value for this compare request.
104   *
105   * @return  The assertion value for this compare request.
106   */
107  public ByteString getAssertionValue()
108  {
109    return assertionValue;
110  }
111
112
113
114  /**
115   * Retrieves the BER type for this protocol op.
116   *
117   * @return  The BER type for this protocol op.
118   */
119  public byte getType()
120  {
121    return OP_TYPE_COMPARE_REQUEST;
122  }
123
124
125
126  /**
127   * Retrieves the name for this protocol op type.
128   *
129   * @return  The name for this protocol op type.
130   */
131  public String getProtocolOpName()
132  {
133    return "Compare Request";
134  }
135
136  /**
137   * Writes this protocol op to an ASN.1 output stream.
138   *
139   * @param stream The ASN.1 output stream to write to.
140   * @throws IOException If a problem occurs while writing to the stream.
141   */
142  public void write(ASN1Writer stream) throws IOException
143  {
144    stream.writeStartSequence(OP_TYPE_COMPARE_REQUEST);
145    stream.writeOctetString(dn);
146
147    stream.writeStartSequence();
148    stream.writeOctetString(attributeType);
149    stream.writeOctetString(assertionValue);
150    stream.writeEndSequence();
151
152    stream.writeEndSequence();
153  }
154
155
156
157  /**
158   * Appends a string representation of this LDAP protocol op to the provided
159   * buffer.
160   *
161   * @param  buffer  The buffer to which the string should be appended.
162   */
163  public void toString(StringBuilder buffer)
164  {
165    buffer.append("CompareRequest(dn=");
166    buffer.append(dn);
167    buffer.append(", attribute=");
168    buffer.append(attributeType);
169    buffer.append(", value=");
170    buffer.append(assertionValue);
171    buffer.append(")");
172  }
173
174
175
176  /**
177   * Appends a multi-line string representation of this LDAP protocol op to the
178   * provided buffer.
179   *
180   * @param  buffer  The buffer to which the information should be appended.
181   * @param  indent  The number of spaces from the margin that the lines should
182   *                 be indented.
183   */
184  public void toString(StringBuilder buffer, int indent)
185  {
186    StringBuilder indentBuf = new StringBuilder(indent);
187    for (int i=0 ; i < indent; i++)
188    {
189      indentBuf.append(' ');
190    }
191
192    buffer.append(indentBuf);
193    buffer.append("Compare Request");
194    buffer.append(EOL);
195
196    buffer.append(indentBuf);
197    buffer.append("  Target DN:  ");
198    buffer.append(dn);
199    buffer.append(EOL);
200
201    buffer.append(indentBuf);
202    buffer.append("  Attribute Type:  ");
203    buffer.append(attributeType);
204    buffer.append(EOL);
205
206    buffer.append(indentBuf);
207    buffer.append("  Assertion Value:");
208    buffer.append(EOL);
209    buffer.append(assertionValue.toHexPlusAsciiString(indent+4));
210  }
211}
212