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