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