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