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