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