001/*
002 * Copyright 2009-2017 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2017 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.sdk.unboundidds.controls;
022
023
024
025import com.unboundid.asn1.ASN1Element;
026import com.unboundid.asn1.ASN1OctetString;
027import com.unboundid.ldap.sdk.Control;
028import com.unboundid.ldap.sdk.LDAPException;
029import com.unboundid.ldap.sdk.ResultCode;
030import com.unboundid.util.NotMutable;
031import com.unboundid.util.ThreadSafety;
032import com.unboundid.util.ThreadSafetyLevel;
033
034import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
035import static com.unboundid.util.Debug.*;
036import static com.unboundid.util.StaticUtils.*;
037
038
039
040/**
041 * This class provides an implementation of an LDAP control which can be
042 * included in a search request to indicate that search result entries should be
043 * returned along with related entries based on a given set of criteria, much
044 * like an SQL join in a relational database.
045 * <BR>
046 * <BLOCKQUOTE>
047 *   <B>NOTE:</B>  This class, and other classes within the
048 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
049 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
050 *   server products.  These classes provide support for proprietary
051 *   functionality or for external specifications that are not considered stable
052 *   or mature enough to be guaranteed to work in an interoperable way with
053 *   other types of LDAP servers.
054 * </BLOCKQUOTE>
055 * <BR>
056 * <H2>Example</H2>
057 * Consider the case in which user entries include an account number, but
058 * additional information about those accounts are available in separate
059 * entries.    If you wish to retrieve both the user and account entries for a
060 * user given only a user ID, then you may accomplish that using the join
061 * request control as follows:
062 * <PRE>
063 * SearchRequest searchRequest = new SearchRequest(
064 *      "ou=People,dc=example,dc=com", SearchScope.SUB,
065 *      Filter.createEqualityFilter("uid", userID));
066 * searchRequest.addControl(new JoinRequestControl(new JoinRequestValue(
067 *      JoinRule.createEqualityJoin("accountNumber", "accountNumber", false),
068 *      JoinBaseDN.createUseCustomBaseDN("ou=Accounts,dc=example,dc=com"),
069 *      SearchScope.SUB, DereferencePolicy.NEVER, null,
070 *      Filter.createEqualityFilter("objectClass", "accountEntry"),
071 *      new String[0], false, null)));
072 * SearchResult searchResult = connection.search(searchRequest);
073 *
074 * for (SearchResultEntry userEntry : searchResult.getSearchEntries())
075 * {
076 *   JoinResultControl c = JoinResultControl.get(userEntry);
077 *   for (JoinedEntry accountEntry : c.getJoinResults())
078 *   {
079 *     // User userEntry was joined with account accountEntry
080 *   }
081 * }
082 * </PRE>
083 */
084@NotMutable()
085@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
086public final class JoinRequestControl
087       extends Control
088{
089  /**
090   * The OID (1.3.6.1.4.1.30221.2.5.9) for the join request control.
091   */
092  public static final String JOIN_REQUEST_OID = "1.3.6.1.4.1.30221.2.5.9";
093
094
095
096  /**
097   * The serial version UID for this serializable class.
098   */
099  private static final long serialVersionUID = -1321645105838145996L;
100
101
102
103  // The join request value for this control.
104  private final JoinRequestValue joinRequestValue;
105
106
107
108  /**
109   * Creates a new join request control with the provided join request value.
110   *
111   * @param  joinRequestValue  The join request value to use for this control.
112   */
113  public JoinRequestControl(final JoinRequestValue joinRequestValue)
114  {
115    super(JOIN_REQUEST_OID, true,
116          new ASN1OctetString(joinRequestValue.encode().encode()));
117
118    this.joinRequestValue = joinRequestValue;
119  }
120
121
122
123  /**
124   * Creates a new join request control which is decoded from the provided
125   * generic control.
126   *
127   * @param  control  The generic control to be decoded as a join request
128   *                  control.
129   *
130   * @throws  LDAPException  If the provided control cannot be decoded as a
131   *                         virtual attributes only request control.
132   */
133  public JoinRequestControl(final Control control)
134         throws LDAPException
135  {
136    super(control);
137
138    final ASN1OctetString value = control.getValue();
139    if (value == null)
140    {
141      throw new LDAPException(ResultCode.DECODING_ERROR,
142           ERR_JOIN_REQUEST_CONTROL_NO_VALUE.get());
143    }
144
145    final ASN1Element valueElement;
146    try
147    {
148      valueElement = ASN1Element.decode(value.getValue());
149    }
150    catch (final Exception e)
151    {
152      debugException(e);
153
154      throw new LDAPException(ResultCode.DECODING_ERROR,
155           ERR_JOIN_REQUEST_VALUE_CANNOT_DECODE.get(getExceptionMessage(e)), e);
156    }
157
158    joinRequestValue = JoinRequestValue.decode(valueElement);
159  }
160
161
162
163  /**
164   * Retrieves the join request value for this join request control.
165   *
166   * @return  The join request value for this join request control.
167   */
168  public JoinRequestValue getJoinRequestValue()
169  {
170    return joinRequestValue;
171  }
172
173
174
175  /**
176   * {@inheritDoc}
177   */
178  @Override()
179  public String getControlName()
180  {
181    return INFO_CONTROL_NAME_JOIN_REQUEST.get();
182  }
183
184
185
186  /**
187   * {@inheritDoc}
188   */
189  @Override()
190  public void toString(final StringBuilder buffer)
191  {
192    buffer.append("JoinRequestControl(value=");
193    joinRequestValue.toString(buffer);
194    buffer.append(')');
195  }
196}