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.sdk.unboundidds.controls;
037
038
039
040import java.util.ArrayList;
041import java.util.Collection;
042import java.util.Collections;
043import java.util.Iterator;
044import java.util.List;
045
046import com.unboundid.asn1.ASN1Element;
047import com.unboundid.asn1.ASN1OctetString;
048import com.unboundid.asn1.ASN1Sequence;
049import com.unboundid.ldap.sdk.Attribute;
050import com.unboundid.ldap.sdk.Entry;
051import com.unboundid.ldap.sdk.LDAPException;
052import com.unboundid.ldap.sdk.ReadOnlyEntry;
053import com.unboundid.ldap.sdk.ResultCode;
054import com.unboundid.util.Debug;
055import com.unboundid.util.NotMutable;
056import com.unboundid.util.NotNull;
057import com.unboundid.util.Nullable;
058import com.unboundid.util.StaticUtils;
059import com.unboundid.util.ThreadSafety;
060import com.unboundid.util.ThreadSafetyLevel;
061
062import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
063
064
065
066/**
067 * This class provides a joined entry, which is a read-only representation of an
068 * entry that has been joined with a search result entry using the LDAP join
069 * control.  See the class-level documentation for the
070 * {@link JoinRequestControl} class for additional information and an example
071 * demonstrating its use.
072 * <BR>
073 * <BLOCKQUOTE>
074 *   <B>NOTE:</B>  This class, and other classes within the
075 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
076 *   supported for use against Ping Identity, UnboundID, and
077 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
078 *   for proprietary functionality or for external specifications that are not
079 *   considered stable or mature enough to be guaranteed to work in an
080 *   interoperable way with other types of LDAP servers.
081 * </BLOCKQUOTE>
082 * <BR>
083 * Joined entries are encoded as follows:
084 * <PRE>
085 *   JoinedEntry ::= SEQUENCE {
086 *        objectName            LDAPDN,
087 *        attributes            PartialAttributeList,
088 *        nestedJoinResults     SEQUENCE OF JoinedEntry OPTIONAL }
089 * </PRE>
090 */
091@NotMutable()
092@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
093public final class JoinedEntry
094       extends ReadOnlyEntry
095{
096  /**
097   * The serial version UID for this serializable class.
098   */
099  private static final long serialVersionUID = -6519864521813773703L;
100
101
102
103  // The list of nested join results for this joined entry.
104  @NotNull private final List<JoinedEntry> nestedJoinResults;
105
106
107
108  /**
109   * Creates a new joined entry with the specified DN, attributes, and nested
110   * join results.
111   *
112   * @param  entry              The entry containing the DN and attributes to
113   *                            use for this joined entry.  It must not be
114   *                            {@code null}.
115   * @param  nestedJoinResults  A list of nested join results for this joined
116   *                            entry.  It may be {@code null} or empty if there
117   *                            are no nested join results.
118   */
119  public JoinedEntry(@NotNull final Entry entry,
120                     @Nullable final List<JoinedEntry> nestedJoinResults)
121  {
122    this(entry.getDN(), entry.getAttributes(), nestedJoinResults);
123  }
124
125
126
127  /**
128   * Creates a new joined entry with the specified DN, attributes, and nested
129   * join results.
130   *
131   * @param  dn                 The DN for this joined entry.  It must not be
132   *                            {@code null}.
133   * @param  attributes         The set of attributes for this joined entry.  It
134   *                            must not be {@code null}.
135   * @param  nestedJoinResults  A list of nested join results for this joined
136   *                            entry.  It may be {@code null} or empty if there
137   *                            are no nested join results.
138   */
139  public JoinedEntry(@NotNull final String dn,
140                     @NotNull final Collection<Attribute> attributes,
141                     @Nullable final List<JoinedEntry> nestedJoinResults)
142  {
143    super(dn, attributes);
144
145    if (nestedJoinResults == null)
146    {
147      this.nestedJoinResults = Collections.emptyList();
148    }
149    else
150    {
151      this.nestedJoinResults = Collections.unmodifiableList(nestedJoinResults);
152    }
153  }
154
155
156
157  /**
158   * Encodes this joined entry to an ASN.1 element.
159   *
160   * @return  An ASN.1 element containing the encoded representation of this
161   *          joined entry.
162   */
163  @NotNull()
164  ASN1Element encode()
165  {
166    final ArrayList<ASN1Element> elements = new ArrayList<>(3);
167
168    elements.add(new ASN1OctetString(getDN()));
169
170    final ArrayList<ASN1Element> attrElements = new ArrayList<>(20);
171    for (final Attribute a : getAttributes())
172    {
173      attrElements.add(a.encode());
174    }
175    elements.add(new ASN1Sequence(attrElements));
176
177    if (! nestedJoinResults.isEmpty())
178    {
179      final ArrayList<ASN1Element> nestedElements =
180           new ArrayList<>(nestedJoinResults.size());
181      for (final JoinedEntry je : nestedJoinResults)
182      {
183        nestedElements.add(je.encode());
184      }
185      elements.add(new ASN1Sequence(nestedElements));
186    }
187
188    return new ASN1Sequence(elements);
189  }
190
191
192
193  /**
194   * Decodes the provided ASN.1 element as a joined entry.
195   *
196   * @param  element  The ASN.1 element to decode as a joined entry.
197   *
198   * @return  The decoded joined entry.
199   *
200   * @throws  LDAPException  If a problem occurs while attempting to decode the
201   *                         provided ASN.1 element as a joined entry.
202   */
203  @NotNull()
204  static JoinedEntry decode(@NotNull final ASN1Element element)
205         throws LDAPException
206  {
207    try
208    {
209      final ASN1Element[] elements =
210           ASN1Sequence.decodeAsSequence(element).elements();
211      final String dn =
212           ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
213
214      final ASN1Element[] attrElements =
215           ASN1Sequence.decodeAsSequence(elements[1]).elements();
216      final ArrayList<Attribute> attrs = new ArrayList<>(attrElements.length);
217      for (final ASN1Element e : attrElements)
218      {
219        attrs.add(Attribute.decode(ASN1Sequence.decodeAsSequence(e)));
220      }
221
222      final ArrayList<JoinedEntry> nestedJoinResults;
223      if (elements.length == 3)
224      {
225        final ASN1Element[] nestedElements =
226             ASN1Sequence.decodeAsSequence(elements[2]).elements();
227        nestedJoinResults = new ArrayList<>(nestedElements.length);
228        for (final ASN1Element e : nestedElements)
229        {
230          nestedJoinResults.add(decode(e));
231        }
232      }
233      else
234      {
235        nestedJoinResults = new ArrayList<>(0);
236      }
237
238      return new JoinedEntry(dn, attrs, nestedJoinResults);
239    }
240    catch (final Exception e)
241    {
242      Debug.debugException(e);
243
244      throw new LDAPException(ResultCode.DECODING_ERROR,
245           ERR_JOINED_ENTRY_CANNOT_DECODE.get(
246                StaticUtils.getExceptionMessage(e)),
247           e);
248    }
249  }
250
251
252
253  /**
254   * Retrieves the list of nested join results for this joined entry.
255   *
256   * @return  The list of nested join results for this joined entry, or an
257   *          empty list if there are none.
258   */
259  @NotNull()
260  public List<JoinedEntry> getNestedJoinResults()
261  {
262    return nestedJoinResults;
263  }
264
265
266
267  /**
268   * Appends a string representation of this joined entry to the provided
269   * buffer.
270   *
271   * @param  buffer  The buffer to which the information should be appended.
272   */
273  @Override()
274  public void toString(@NotNull final StringBuilder buffer)
275  {
276    buffer.append("JoinedEntry(dn='");
277    buffer.append(getDN());
278    buffer.append("', attributes={");
279
280    final Iterator<Attribute> attrIterator = getAttributes().iterator();
281    while (attrIterator.hasNext())
282    {
283      attrIterator.next().toString(buffer);
284      if (attrIterator.hasNext())
285      {
286        buffer.append(", ");
287      }
288    }
289
290    buffer.append("}, nestedJoinResults={");
291
292    final Iterator<JoinedEntry> entryIterator = nestedJoinResults.iterator();
293    while (entryIterator.hasNext())
294    {
295      entryIterator.next().toString(buffer);
296      if (entryIterator.hasNext())
297      {
298        buffer.append(", ");
299      }
300    }
301
302    buffer.append("})");
303  }
304}