001/*
002 * Copyright 2020-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2020-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) 2020-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.controls;
037
038
039
040import com.unboundid.ldap.sdk.Control;
041import com.unboundid.ldap.sdk.JSONControlDecodeHelper;
042import com.unboundid.ldap.sdk.LDAPException;
043import com.unboundid.ldap.sdk.ResultCode;
044import com.unboundid.util.NotMutable;
045import com.unboundid.util.NotNull;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048import com.unboundid.util.json.JSONField;
049import com.unboundid.util.json.JSONObject;
050
051import static com.unboundid.ldap.sdk.controls.ControlMessages.*;
052
053
054
055/**
056 * This class provides an implementation of the LDAP subentries request control
057 * as defined in draft-ietf-ldup-subentry.  It may be included in a search
058 * request to indicate that the entries with the {@code ldapSubentry} object
059 * class should be included in the search results.
060 * <BR><BR>
061 * Entries containing the {@code ldapSubentry} object class are special in that
062 * they are normally excluded from search results, unless the target entry is
063 * requested with a base-level search.  They are used to store operational
064 * information that controls how the server should behave rather than user data.
065 * Because they do not hold user data, it is generally desirable to have them
066 * excluded from search results, but for cases in which a client needs to
067 * retrieve such an entry, then this subentries request control may be included
068 * in the search request.  This control differs from the
069 * {@link RFC3672SubentriesRequestControl} in that it will cause only entries
070 * with the {@code ldapSubEntry} object class to be returned, while the
071 * {@code RFC3672SubentriesRequestControl} may optionally return both regular
072 * entries and subentries.
073 * <BR><BR>
074 * There is no corresponding response control.
075 * <BR><BR>
076 * <H2>Example</H2>
077 * The following example illustrates the use of the subentries request control
078 * to retrieve subentries that may not otherwise be returned.
079 * <PRE>
080 * // First, perform a search to retrieve an entry with a cn of "test subentry"
081 * // but without including the subentries request control.  This should not
082 * // return any matching entries.
083 * SearchRequest searchRequest = new SearchRequest("dc=example,dc=com",
084 *      SearchScope.SUB, Filter.createEqualityFilter("cn", "test subentry"));
085 * SearchResult resultWithoutControl = connection.search(searchRequest);
086 * LDAPTestUtils.assertResultCodeEquals(resultWithoutControl,
087 *      ResultCode.SUCCESS);
088 * LDAPTestUtils.assertEntriesReturnedEquals(resultWithoutControl, 0);
089 *
090 * // Update the search request to add a subentries request control so that
091 * // subentries should be included in search results.  This should cause the
092 * // subentry to be returned.
093 * searchRequest.addControl(new DraftLDUPSubentriesRequestControl());
094 * SearchResult resultWithControl = connection.search(searchRequest);
095 * LDAPTestUtils.assertResultCodeEquals(resultWithControl, ResultCode.SUCCESS);
096 * LDAPTestUtils.assertEntriesReturnedEquals(resultWithControl, 1);
097 * </PRE>
098 *
099 * @see  RFC3672SubentriesRequestControl
100 */
101@NotMutable()
102@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
103public final class DraftLDUPSubentriesRequestControl
104       extends Control
105{
106  /**
107   * The OID (1.3.6.1.4.1.7628.5.101.1) for the LDAP subentries request control.
108   */
109  @NotNull public static final String SUBENTRIES_REQUEST_OID =
110       "1.3.6.1.4.1.7628.5.101.1";
111
112
113
114  /**
115   * The serial version UID for this serializable class.
116   */
117  private static final long serialVersionUID = 4772130172594841481L;
118
119
120
121  /**
122   * Creates a new subentries request control.  it will not be marked critical.
123   */
124  public DraftLDUPSubentriesRequestControl()
125  {
126    this(false);
127  }
128
129
130
131  /**
132   * Creates a new subentries request control with the specified criticality.
133   *
134   * @param  isCritical  Indicates whether this control should be marked
135   *                     critical.
136   */
137  public DraftLDUPSubentriesRequestControl(final boolean isCritical)
138  {
139    super(SUBENTRIES_REQUEST_OID, isCritical, null);
140  }
141
142
143
144  /**
145   * Creates a new subentries request control which is decoded from the provided
146   * generic control.
147   *
148   * @param  control  The generic control to be decoded as a subentries request
149   *                  control.
150   *
151   * @throws  LDAPException  If the provided control cannot be decoded as a
152   *                         subentries request control.
153   */
154  public DraftLDUPSubentriesRequestControl(@NotNull final Control control)
155         throws LDAPException
156  {
157    super(control);
158
159    if (control.hasValue())
160    {
161      throw new LDAPException(ResultCode.DECODING_ERROR,
162                              ERR_SUBENTRIES_HAS_VALUE.get());
163    }
164  }
165
166
167
168  /**
169   * {@inheritDoc}
170   */
171  @Override()
172  @NotNull()
173  public String getControlName()
174  {
175    return INFO_CONTROL_NAME_SUBENTRIES_REQUEST.get();
176  }
177
178
179
180  /**
181   * Retrieves a representation of this subentries control as a JSON object.
182   * The JSON object uses the following fields (note that since this control
183   * does not have a value, neither the {@code value-base64} nor
184   * {@code value-json} fields may be present):
185   * <UL>
186   *   <LI>
187   *     {@code oid} -- A mandatory string field whose value is the object
188   *     identifier for this control.  For the subentries control, the OID is
189   *     "1.3.6.1.4.1.7628.5.101.1".
190   *   </LI>
191   *   <LI>
192   *     {@code control-name} -- An optional string field whose value is a
193   *     human-readable name for this control.  This field is only intended for
194   *     descriptive purposes, and when decoding a control, the {@code oid}
195   *     field should be used to identify the type of control.
196   *   </LI>
197   *   <LI>
198   *     {@code criticality} -- A mandatory Boolean field used to indicate
199   *     whether this control is considered critical.
200   *   </LI>
201   * </UL>
202   *
203   * @return  A JSON object that contains a representation of this control.
204   */
205  @Override()
206  @NotNull()
207  public JSONObject toJSONControl()
208  {
209    return new JSONObject(
210         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
211              SUBENTRIES_REQUEST_OID),
212         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
213              INFO_CONTROL_NAME_SUBENTRIES_REQUEST.get()),
214         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
215              isCritical()));
216  }
217
218
219
220  /**
221   * Attempts to decode the provided object as a JSON representation of an
222   * subentries request control.
223   *
224   * @param  controlObject  The JSON object to be decoded.  It must not be
225   *                        {@code null}.
226   * @param  strict         Indicates whether to use strict mode when decoding
227   *                        the provided JSON object.  If this is {@code true},
228   *                        then this method will throw an exception if the
229   *                        provided JSON object contains any unrecognized
230   *                        fields.  If this is {@code false}, then unrecognized
231   *                        fields will be ignored.
232   *
233   * @return  The subentries request control that was decoded from the provided
234   *          JSON object.
235   *
236   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
237   *                         valid subentries request control.
238   */
239  @NotNull()
240  public static DraftLDUPSubentriesRequestControl decodeJSONControl(
241              @NotNull final JSONObject controlObject,
242              final boolean strict)
243         throws LDAPException
244  {
245    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
246         controlObject, strict, false, false);
247
248    return new DraftLDUPSubentriesRequestControl(jsonControl.getCriticality());
249  }
250
251
252
253  /**
254   * {@inheritDoc}
255   */
256  @Override()
257  public void toString(@NotNull final StringBuilder buffer)
258  {
259    buffer.append("DraftLDUPSubentriesRequestControl(isCritical=");
260    buffer.append(isCritical());
261    buffer.append(')');
262  }
263}