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-2016 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  @Override
110  public byte getType()
111  {
112    return OP_TYPE_COMPARE_REQUEST;
113  }
114
115
116
117  /**
118   * Retrieves the name for this protocol op type.
119   *
120   * @return  The name for this protocol op type.
121   */
122  @Override
123  public String getProtocolOpName()
124  {
125    return "Compare Request";
126  }
127
128  /**
129   * Writes this protocol op to an ASN.1 output stream.
130   *
131   * @param stream The ASN.1 output stream to write to.
132   * @throws IOException If a problem occurs while writing to the stream.
133   */
134  @Override
135  public void write(ASN1Writer stream) throws IOException
136  {
137    stream.writeStartSequence(OP_TYPE_COMPARE_REQUEST);
138    stream.writeOctetString(dn);
139
140    stream.writeStartSequence();
141    stream.writeOctetString(attributeType);
142    stream.writeOctetString(assertionValue);
143    stream.writeEndSequence();
144
145    stream.writeEndSequence();
146  }
147
148
149
150  /**
151   * Appends a string representation of this LDAP protocol op to the provided
152   * buffer.
153   *
154   * @param  buffer  The buffer to which the string should be appended.
155   */
156  @Override
157  public void toString(StringBuilder buffer)
158  {
159    buffer.append("CompareRequest(dn=");
160    buffer.append(dn);
161    buffer.append(", attribute=");
162    buffer.append(attributeType);
163    buffer.append(", value=");
164    buffer.append(assertionValue);
165    buffer.append(")");
166  }
167
168
169
170  /**
171   * Appends a multi-line string representation of this LDAP protocol op to the
172   * provided buffer.
173   *
174   * @param  buffer  The buffer to which the information should be appended.
175   * @param  indent  The number of spaces from the margin that the lines should
176   *                 be indented.
177   */
178  @Override
179  public void toString(StringBuilder buffer, int indent)
180  {
181    StringBuilder indentBuf = new StringBuilder(indent);
182    for (int i=0 ; i < indent; i++)
183    {
184      indentBuf.append(' ');
185    }
186
187    buffer.append(indentBuf);
188    buffer.append("Compare Request");
189    buffer.append(EOL);
190
191    buffer.append(indentBuf);
192    buffer.append("  Target DN:  ");
193    buffer.append(dn);
194    buffer.append(EOL);
195
196    buffer.append(indentBuf);
197    buffer.append("  Attribute Type:  ");
198    buffer.append(attributeType);
199    buffer.append(EOL);
200
201    buffer.append(indentBuf);
202    buffer.append("  Assertion Value:");
203    buffer.append(EOL);
204    buffer.append(assertionValue.toHexPlusAsciiString(indent+4));
205  }
206}
207