001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2009-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.protocol;
037
038
039
040import com.unboundid.asn1.ASN1Buffer;
041import com.unboundid.asn1.ASN1BufferSequence;
042import com.unboundid.asn1.ASN1Element;
043import com.unboundid.asn1.ASN1OctetString;
044import com.unboundid.asn1.ASN1Sequence;
045import com.unboundid.asn1.ASN1StreamReader;
046import com.unboundid.ldap.sdk.CompareRequest;
047import com.unboundid.ldap.sdk.Control;
048import com.unboundid.ldap.sdk.LDAPException;
049import com.unboundid.ldap.sdk.ResultCode;
050import com.unboundid.util.Debug;
051import com.unboundid.util.InternalUseOnly;
052import com.unboundid.util.NotMutable;
053import com.unboundid.util.NotNull;
054import com.unboundid.util.Nullable;
055import com.unboundid.util.StaticUtils;
056import com.unboundid.util.ThreadSafety;
057import com.unboundid.util.ThreadSafetyLevel;
058import com.unboundid.util.Validator;
059
060import static com.unboundid.ldap.protocol.ProtocolMessages.*;
061
062
063
064/**
065 * This class provides an implementation of an LDAP compare request protocol op.
066 */
067@InternalUseOnly()
068@NotMutable()
069@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
070public final class CompareRequestProtocolOp
071       implements ProtocolOp
072{
073  /**
074   * The serial version UID for this serializable class.
075   */
076  private static final long serialVersionUID = -562642367801440060L;
077
078
079
080  // The assertion value for this compare request.
081  @NotNull private final ASN1OctetString assertionValue;
082
083  // The attribute name for this compare request.
084  @NotNull private final String attributeName;
085
086  // The entry DN for this compare request.
087  @NotNull private final String dn;
088
089
090
091  /**
092   * Creates a new compare request protocol op with the provided information.
093   *
094   * @param  dn              The DN for this compare request.
095   * @param  attributeName   The attribute name for this compare request.
096   * @param  assertionValue  The assertion value for this compare request.
097   */
098  public CompareRequestProtocolOp(@NotNull final String dn,
099                                  @NotNull final String attributeName,
100                                  @NotNull final ASN1OctetString assertionValue)
101  {
102    this.dn             = dn;
103    this.attributeName  = attributeName;
104    this.assertionValue = assertionValue;
105  }
106
107
108
109  /**
110   * Creates a new compare request protocol op from the provided compare request
111   * object.
112   *
113   * @param  request  The compare request object to use to create this protocol
114   *                  op.
115   */
116  public CompareRequestProtocolOp(@NotNull final CompareRequest request)
117  {
118    dn             = request.getDN();
119    attributeName  = request.getAttributeName();
120    assertionValue = request.getRawAssertionValue();
121  }
122
123
124
125  /**
126   * Creates a new compare request protocol op read from the provided ASN.1
127   * stream reader.
128   *
129   * @param  reader  The ASN.1 stream reader from which to read the compare
130   *                 request protocol op.
131   *
132   * @throws  LDAPException  If a problem occurs while reading or parsing the
133   *                         compare request.
134   */
135  CompareRequestProtocolOp(@NotNull final ASN1StreamReader reader)
136       throws LDAPException
137  {
138    try
139    {
140      reader.beginSequence();
141      dn = reader.readString();
142
143      reader.beginSequence();
144      attributeName = reader.readString();
145      assertionValue = new ASN1OctetString(reader.readBytes());
146      Validator.ensureNotNull(dn, attributeName, assertionValue);
147    }
148    catch (final Exception e)
149    {
150      Debug.debugException(e);
151
152      throw new LDAPException(ResultCode.DECODING_ERROR,
153           ERR_COMPARE_REQUEST_CANNOT_DECODE.get(
154                StaticUtils.getExceptionMessage(e)),
155           e);
156    }
157  }
158
159
160
161  /**
162   * Retrieves the DN for this compare request.
163   *
164   * @return  The DN for this compare request.
165   */
166  @NotNull()
167  public String getDN()
168  {
169    return dn;
170  }
171
172
173
174  /**
175   * Retrieves the attribute name for this compare request.
176   *
177   * @return  The attribute name for this compare request.
178   */
179  @NotNull()
180  public String getAttributeName()
181  {
182    return attributeName;
183  }
184
185
186
187  /**
188   * Retrieves the assertion value for this compare request.
189   *
190   * @return  The assertion value for this compare request.
191   */
192  @NotNull()
193  public ASN1OctetString getAssertionValue()
194  {
195    return assertionValue;
196  }
197
198
199
200  /**
201   * {@inheritDoc}
202   */
203  @Override()
204  public byte getProtocolOpType()
205  {
206    return LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST;
207  }
208
209
210
211  /**
212   * {@inheritDoc}
213   */
214  @Override()
215  @NotNull()
216  public ASN1Element encodeProtocolOp()
217  {
218    return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST,
219         new ASN1OctetString(dn),
220         new ASN1Sequence(
221              new ASN1OctetString(attributeName),
222              assertionValue));
223  }
224
225
226
227  /**
228   * Decodes the provided ASN.1 element as a compare request protocol op.
229   *
230   * @param  element  The ASN.1 element to be decoded.
231   *
232   * @return  The decoded compare request protocol op.
233   *
234   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
235   *                         a compare request protocol op.
236   */
237  @NotNull()
238  public static CompareRequestProtocolOp decodeProtocolOp(
239                     @NotNull final ASN1Element element)
240         throws LDAPException
241  {
242    try
243    {
244      final ASN1Element[] elements =
245           ASN1Sequence.decodeAsSequence(element).elements();
246      final String dn =
247           ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
248
249      final ASN1Element[] avaElements =
250           ASN1Sequence.decodeAsSequence(elements[1]).elements();
251      final String attributeName =
252           ASN1OctetString.decodeAsOctetString(avaElements[0]).stringValue();
253      final ASN1OctetString assertionValue =
254           ASN1OctetString.decodeAsOctetString(avaElements[1]);
255
256      return new CompareRequestProtocolOp(dn, attributeName, assertionValue);
257    }
258    catch (final Exception e)
259    {
260      Debug.debugException(e);
261      throw new LDAPException(ResultCode.DECODING_ERROR,
262           ERR_COMPARE_REQUEST_CANNOT_DECODE.get(
263                StaticUtils.getExceptionMessage(e)),
264           e);
265    }
266  }
267
268
269
270  /**
271   * {@inheritDoc}
272   */
273  @Override()
274  public void writeTo(@NotNull final ASN1Buffer buffer)
275  {
276    final ASN1BufferSequence opSequence =
277         buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST);
278    buffer.addOctetString(dn);
279
280    final ASN1BufferSequence avaSequence = buffer.beginSequence();
281    buffer.addOctetString(attributeName);
282    buffer.addElement(assertionValue);
283    avaSequence.end();
284    opSequence.end();
285  }
286
287
288
289  /**
290   * Creates a compare request from this protocol op.
291   *
292   * @param  controls  The set of controls to include in the compare request.
293   *                   It may be empty or {@code null} if no controls should be
294   *                   included.
295   *
296   * @return  The compare request that was created.
297   */
298  @NotNull()
299  public CompareRequest toCompareRequest(@Nullable final Control... controls)
300  {
301    return new CompareRequest(dn, attributeName, assertionValue.getValue(),
302         controls);
303  }
304
305
306
307  /**
308   * Retrieves a string representation of this protocol op.
309   *
310   * @return  A string representation of this protocol op.
311   */
312  @Override()
313  @NotNull()
314  public String toString()
315  {
316    final StringBuilder buffer = new StringBuilder();
317    toString(buffer);
318    return buffer.toString();
319  }
320
321
322
323  /**
324   * {@inheritDoc}
325   */
326  @Override()
327  public void toString(@NotNull final StringBuilder buffer)
328  {
329    buffer.append("CompareRequestProtocolOp(dn='");
330    buffer.append(dn);
331    buffer.append("', attributeName='");
332    buffer.append(attributeName);
333    buffer.append("', assertionValue='");
334    buffer.append(assertionValue.stringValue());
335    buffer.append("')");
336  }
337}