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