001/*
002 * Copyright 2009-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2020 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-2020 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.StaticUtils;
054import com.unboundid.util.ThreadSafety;
055import com.unboundid.util.ThreadSafetyLevel;
056import com.unboundid.util.Validator;
057
058import static com.unboundid.ldap.protocol.ProtocolMessages.*;
059
060
061
062/**
063 * This class provides an implementation of an LDAP compare request protocol op.
064 */
065@InternalUseOnly()
066@NotMutable()
067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
068public final class CompareRequestProtocolOp
069       implements ProtocolOp
070{
071  /**
072   * The serial version UID for this serializable class.
073   */
074  private static final long serialVersionUID = -562642367801440060L;
075
076
077
078  // The assertion value for this compare request.
079  private final ASN1OctetString assertionValue;
080
081  // The attribute name for this compare request.
082  private final String attributeName;
083
084  // The entry DN for this compare request.
085  private final String dn;
086
087
088
089  /**
090   * Creates a new compare request protocol op with the provided information.
091   *
092   * @param  dn              The DN for this compare request.
093   * @param  attributeName   The attribute name for this compare request.
094   * @param  assertionValue  The assertion value for this compare request.
095   */
096  public CompareRequestProtocolOp(final String dn, final String attributeName,
097                                  final ASN1OctetString assertionValue)
098  {
099    this.dn             = dn;
100    this.attributeName  = attributeName;
101    this.assertionValue = assertionValue;
102  }
103
104
105
106  /**
107   * Creates a new compare request protocol op from the provided compare request
108   * object.
109   *
110   * @param  request  The compare request object to use to create this protocol
111   *                  op.
112   */
113  public CompareRequestProtocolOp(final CompareRequest request)
114  {
115    dn             = request.getDN();
116    attributeName  = request.getAttributeName();
117    assertionValue = request.getRawAssertionValue();
118  }
119
120
121
122  /**
123   * Creates a new compare request protocol op read from the provided ASN.1
124   * stream reader.
125   *
126   * @param  reader  The ASN.1 stream reader from which to read the compare
127   *                 request protocol op.
128   *
129   * @throws  LDAPException  If a problem occurs while reading or parsing the
130   *                         compare request.
131   */
132  CompareRequestProtocolOp(final ASN1StreamReader reader)
133       throws LDAPException
134  {
135    try
136    {
137      reader.beginSequence();
138      dn = reader.readString();
139
140      reader.beginSequence();
141      attributeName = reader.readString();
142      assertionValue = new ASN1OctetString(reader.readBytes());
143      Validator.ensureNotNull(dn, attributeName, assertionValue);
144    }
145    catch (final Exception e)
146    {
147      Debug.debugException(e);
148
149      throw new LDAPException(ResultCode.DECODING_ERROR,
150           ERR_COMPARE_REQUEST_CANNOT_DECODE.get(
151                StaticUtils.getExceptionMessage(e)),
152           e);
153    }
154  }
155
156
157
158  /**
159   * Retrieves the DN for this compare request.
160   *
161   * @return  The DN for this compare request.
162   */
163  public String getDN()
164  {
165    return dn;
166  }
167
168
169
170  /**
171   * Retrieves the attribute name for this compare request.
172   *
173   * @return  The attribute name for this compare request.
174   */
175  public String getAttributeName()
176  {
177    return attributeName;
178  }
179
180
181
182  /**
183   * Retrieves the assertion value for this compare request.
184   *
185   * @return  The assertion value for this compare request.
186   */
187  public ASN1OctetString getAssertionValue()
188  {
189    return assertionValue;
190  }
191
192
193
194  /**
195   * {@inheritDoc}
196   */
197  @Override()
198  public byte getProtocolOpType()
199  {
200    return LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST;
201  }
202
203
204
205  /**
206   * {@inheritDoc}
207   */
208  @Override()
209  public ASN1Element encodeProtocolOp()
210  {
211    return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST,
212         new ASN1OctetString(dn),
213         new ASN1Sequence(
214              new ASN1OctetString(attributeName),
215              assertionValue));
216  }
217
218
219
220  /**
221   * Decodes the provided ASN.1 element as a compare request protocol op.
222   *
223   * @param  element  The ASN.1 element to be decoded.
224   *
225   * @return  The decoded compare request protocol op.
226   *
227   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
228   *                         a compare request protocol op.
229   */
230  public static CompareRequestProtocolOp decodeProtocolOp(
231                                              final ASN1Element element)
232         throws LDAPException
233  {
234    try
235    {
236      final ASN1Element[] elements =
237           ASN1Sequence.decodeAsSequence(element).elements();
238      final String dn =
239           ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
240
241      final ASN1Element[] avaElements =
242           ASN1Sequence.decodeAsSequence(elements[1]).elements();
243      final String attributeName =
244           ASN1OctetString.decodeAsOctetString(avaElements[0]).stringValue();
245      final ASN1OctetString assertionValue =
246           ASN1OctetString.decodeAsOctetString(avaElements[1]);
247
248      return new CompareRequestProtocolOp(dn, attributeName, assertionValue);
249    }
250    catch (final Exception e)
251    {
252      Debug.debugException(e);
253      throw new LDAPException(ResultCode.DECODING_ERROR,
254           ERR_COMPARE_REQUEST_CANNOT_DECODE.get(
255                StaticUtils.getExceptionMessage(e)),
256           e);
257    }
258  }
259
260
261
262  /**
263   * {@inheritDoc}
264   */
265  @Override()
266  public void writeTo(final ASN1Buffer buffer)
267  {
268    final ASN1BufferSequence opSequence =
269         buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST);
270    buffer.addOctetString(dn);
271
272    final ASN1BufferSequence avaSequence = buffer.beginSequence();
273    buffer.addOctetString(attributeName);
274    buffer.addElement(assertionValue);
275    avaSequence.end();
276    opSequence.end();
277  }
278
279
280
281  /**
282   * Creates a compare request from this protocol op.
283   *
284   * @param  controls  The set of controls to include in the compare request.
285   *                   It may be empty or {@code null} if no controls should be
286   *                   included.
287   *
288   * @return  The compare request that was created.
289   */
290  public CompareRequest toCompareRequest(final Control... controls)
291  {
292    return new CompareRequest(dn, attributeName, assertionValue.getValue(),
293         controls);
294  }
295
296
297
298  /**
299   * Retrieves a string representation of this protocol op.
300   *
301   * @return  A string representation of this protocol op.
302   */
303  @Override()
304  public String toString()
305  {
306    final StringBuilder buffer = new StringBuilder();
307    toString(buffer);
308    return buffer.toString();
309  }
310
311
312
313  /**
314   * {@inheritDoc}
315   */
316  @Override()
317  public void toString(final StringBuilder buffer)
318  {
319    buffer.append("CompareRequestProtocolOp(dn='");
320    buffer.append(dn);
321    buffer.append("', attributeName='");
322    buffer.append(attributeName);
323    buffer.append("', assertionValue='");
324    buffer.append(assertionValue.stringValue());
325    buffer.append("')");
326  }
327}