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 java.util.ArrayList; 041import java.util.Collections; 042import java.util.Iterator; 043import java.util.List; 044 045import com.unboundid.asn1.ASN1Buffer; 046import com.unboundid.asn1.ASN1BufferSequence; 047import com.unboundid.asn1.ASN1Element; 048import com.unboundid.asn1.ASN1OctetString; 049import com.unboundid.asn1.ASN1Sequence; 050import com.unboundid.asn1.ASN1StreamReader; 051import com.unboundid.asn1.ASN1StreamReaderSequence; 052import com.unboundid.ldap.sdk.Attribute; 053import com.unboundid.ldap.sdk.Control; 054import com.unboundid.ldap.sdk.Entry; 055import com.unboundid.ldap.sdk.LDAPException; 056import com.unboundid.ldap.sdk.ResultCode; 057import com.unboundid.ldap.sdk.SearchResultEntry; 058import com.unboundid.util.Debug; 059import com.unboundid.util.InternalUseOnly; 060import com.unboundid.util.NotMutable; 061import com.unboundid.util.NotNull; 062import com.unboundid.util.Nullable; 063import com.unboundid.util.StaticUtils; 064import com.unboundid.util.ThreadSafety; 065import com.unboundid.util.ThreadSafetyLevel; 066import com.unboundid.util.Validator; 067 068import static com.unboundid.ldap.protocol.ProtocolMessages.*; 069 070 071 072/** 073 * This class provides an implementation of an LDAP search result entry protocol 074 * op. 075 */ 076@InternalUseOnly() 077@NotMutable() 078@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 079public final class SearchResultEntryProtocolOp 080 implements ProtocolOp 081{ 082 /** 083 * The serial version UID for this serializable class. 084 */ 085 private static final long serialVersionUID = 6501366526364541767L; 086 087 088 089 // The list of attributes for this search result entry. 090 @NotNull private final List<Attribute> attributes; 091 092 // The entry DN for this search result entry. 093 @NotNull private final String dn; 094 095 096 097 /** 098 * Creates a new search result entry protocol op with the provided 099 * information. 100 * 101 * @param dn The entry DN for this search result entry. 102 * @param attributes The list of attributes to include in this search result 103 * entry. 104 */ 105 public SearchResultEntryProtocolOp(@NotNull final String dn, 106 @NotNull final List<Attribute> attributes) 107 { 108 this.dn = dn; 109 this.attributes = Collections.unmodifiableList(attributes); 110 } 111 112 113 114 /** 115 * Creates a new search result entry protocol op from the provided entry. 116 * 117 * @param entry The entry to use to create this protocol op. 118 */ 119 public SearchResultEntryProtocolOp(@NotNull final Entry entry) 120 { 121 dn = entry.getDN(); 122 attributes = Collections.unmodifiableList(new ArrayList<>( 123 entry.getAttributes())); 124 } 125 126 127 128 /** 129 * Creates a new search result entry protocol op read from the provided ASN.1 130 * stream reader. 131 * 132 * @param reader The ASN.1 stream reader from which to read the search 133 * result entry protocol op. 134 * 135 * @throws LDAPException If a problem occurs while reading or parsing the 136 * search result entry. 137 */ 138 SearchResultEntryProtocolOp(@NotNull final ASN1StreamReader reader) 139 throws LDAPException 140 { 141 try 142 { 143 reader.beginSequence(); 144 dn = reader.readString(); 145 Validator.ensureNotNull(dn); 146 147 final ArrayList<Attribute> attrs = new ArrayList<>(10); 148 final ASN1StreamReaderSequence attrSequence = reader.beginSequence(); 149 while (attrSequence.hasMoreElements()) 150 { 151 attrs.add(Attribute.readFrom(reader)); 152 } 153 154 attributes = Collections.unmodifiableList(attrs); 155 } 156 catch (final LDAPException le) 157 { 158 Debug.debugException(le); 159 throw le; 160 } 161 catch (final Exception e) 162 { 163 Debug.debugException(e); 164 165 throw new LDAPException(ResultCode.DECODING_ERROR, 166 ERR_SEARCH_ENTRY_CANNOT_DECODE.get( 167 StaticUtils.getExceptionMessage(e)), 168 e); 169 } 170 } 171 172 173 174 /** 175 * Retrieves the DN for this search result entry. 176 * 177 * @return The DN for this search result entry. 178 */ 179 @NotNull() 180 public String getDN() 181 { 182 return dn; 183 } 184 185 186 187 /** 188 * Retrieves the list of attributes for this search result entry. 189 * 190 * @return The list of attributes for this search result entry. 191 */ 192 @NotNull() 193 public List<Attribute> getAttributes() 194 { 195 return attributes; 196 } 197 198 199 200 /** 201 * {@inheritDoc} 202 */ 203 @Override() 204 public byte getProtocolOpType() 205 { 206 return LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_ENTRY; 207 } 208 209 210 211 /** 212 * {@inheritDoc} 213 */ 214 @Override() 215 @NotNull() 216 public ASN1Element encodeProtocolOp() 217 { 218 final ArrayList<ASN1Element> attrElements = 219 new ArrayList<>(attributes.size()); 220 for (final Attribute a : attributes) 221 { 222 attrElements.add(a.encode()); 223 } 224 225 return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_ENTRY, 226 new ASN1OctetString(dn), 227 new ASN1Sequence(attrElements)); 228 } 229 230 231 232 /** 233 * Decodes the provided ASN.1 element as a search result entry protocol op. 234 * 235 * @param element The ASN.1 element to be decoded. 236 * 237 * @return The decoded search result entry protocol op. 238 * 239 * @throws LDAPException If the provided ASN.1 element cannot be decoded as 240 * a search result entry protocol op. 241 */ 242 @NotNull() 243 public static SearchResultEntryProtocolOp decodeProtocolOp( 244 @NotNull final ASN1Element element) 245 throws LDAPException 246 { 247 try 248 { 249 final ASN1Element[] elements = 250 ASN1Sequence.decodeAsSequence(element).elements(); 251 final String dn = 252 ASN1OctetString.decodeAsOctetString(elements[0]).stringValue(); 253 254 final ASN1Element[] attrElements = 255 ASN1Sequence.decodeAsSequence(elements[1]).elements(); 256 final ArrayList<Attribute> attributes = 257 new ArrayList<>(attrElements.length); 258 for (final ASN1Element e : attrElements) 259 { 260 attributes.add(Attribute.decode(ASN1Sequence.decodeAsSequence(e))); 261 } 262 263 return new SearchResultEntryProtocolOp(dn, attributes); 264 } 265 catch (final Exception e) 266 { 267 Debug.debugException(e); 268 throw new LDAPException(ResultCode.DECODING_ERROR, 269 ERR_SEARCH_ENTRY_CANNOT_DECODE.get( 270 StaticUtils.getExceptionMessage(e)), 271 e); 272 } 273 } 274 275 276 277 /** 278 * {@inheritDoc} 279 */ 280 @Override() 281 public void writeTo(@NotNull final ASN1Buffer buffer) 282 { 283 final ASN1BufferSequence opSequence = 284 buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_ENTRY); 285 buffer.addOctetString(dn); 286 287 final ASN1BufferSequence attrSequence = buffer.beginSequence(); 288 for (final Attribute a : attributes) 289 { 290 a.writeTo(buffer); 291 } 292 attrSequence.end(); 293 opSequence.end(); 294 } 295 296 297 298 /** 299 * Creates a search result entry from this protocol op. 300 * 301 * @param controls The set of controls to include in the search result 302 * entry. It may be empty or {@code null} if no controls 303 * should be included. 304 * 305 * @return The search result entry that was created. 306 */ 307 @NotNull() 308 public SearchResultEntry toSearchResultEntry( 309 @Nullable final Control... controls) 310 { 311 return new SearchResultEntry(dn, attributes, controls); 312 } 313 314 315 316 /** 317 * Retrieves a string representation of this protocol op. 318 * 319 * @return A string representation of this protocol op. 320 */ 321 @Override() 322 @NotNull() 323 public String toString() 324 { 325 final StringBuilder buffer = new StringBuilder(); 326 toString(buffer); 327 return buffer.toString(); 328 } 329 330 331 332 /** 333 * {@inheritDoc} 334 */ 335 @Override() 336 public void toString(@NotNull final StringBuilder buffer) 337 { 338 buffer.append("SearchResultEntryProtocolOp(dn='"); 339 buffer.append(dn); 340 buffer.append("', attrs={"); 341 342 final Iterator<Attribute> iterator = attributes.iterator(); 343 while (iterator.hasNext()) 344 { 345 iterator.next().toString(buffer); 346 if (iterator.hasNext()) 347 { 348 buffer.append(','); 349 } 350 } 351 352 buffer.append("})"); 353 } 354}