001/*
002 * Copyright 2020-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2020-2022 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-2022 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.asn1.ASN1Boolean;
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.NotNull;
048import com.unboundid.util.StaticUtils;
049import com.unboundid.util.ThreadSafety;
050import com.unboundid.util.ThreadSafetyLevel;
051
052import static com.unboundid.ldap.sdk.controls.ControlMessages.*;
053
054
055
056/**
057 * This class provides an implementation of the LDAP subentries request control
058 * as defined in RFC 3672.  It may be included in a search request to indicate
059 * that entries with the {@code ldapSubEntry} object class should be included in
060 * the search results.  The value of the control indicates whether entries
061 * matching the filter but not containing that object class (but ) should also
062 * be returned.
063 * <BR><BR>
064 * Entries containing the {@code ldapSubentry} object class are special in that
065 * they are normally excluded from search results, unless the target entry is
066 * requested with a base-level search.  They are used to store operational
067 * information that controls how the server should behave rather than user data.
068 * Because they do not hold user data, it is generally desirable to have them
069 * excluded from search results, but for cases in which a client needs to
070 * retrieve such an entry, then this subentries request control may be included
071 * in the search request.  This control differs from the
072 * {@link DraftLDUPSubentriesRequestControl} in that you can optionally also
073 * return entries that do not contain the {@code ldapSubEntry} object class,
074 * whereas the {@code DraftLDUPSubentriesRequestControl} will cause only
075 * subentries to be returned.
076 * <BR><BR>
077 * There is no corresponding response control.
078 * <BR><BR>
079 * <H2>Example</H2>
080 * The following example illustrates the use of the subentries request control
081 * to retrieve subentries that may not otherwise be returned.
082 * <PRE>
083 * // First, perform a search to retrieve an entry with a cn of "test subentry"
084 * // but without including the subentries request control.  This should not
085 * // return any matching entries.
086 * SearchRequest searchRequest = new SearchRequest("dc=example,dc=com",
087 *      SearchScope.SUB, Filter.createEqualityFilter("cn", "test subentry"));
088 * SearchResult resultWithoutControl = connection.search(searchRequest);
089 * LDAPTestUtils.assertResultCodeEquals(resultWithoutControl,
090 *      ResultCode.SUCCESS);
091 * LDAPTestUtils.assertEntriesReturnedEquals(resultWithoutControl, 0);
092 *
093 * // Update the search request to add a subentries request control so that
094 * // subentries should be included in search results.  This should cause the
095 * // subentry to be returned.
096 * searchRequest.addControl(new RFC3672SubentriesRequestControl(true));
097 * SearchResult resultWithControl = connection.search(searchRequest);
098 * LDAPTestUtils.assertResultCodeEquals(resultWithControl, ResultCode.SUCCESS);
099 * LDAPTestUtils.assertEntriesReturnedEquals(resultWithControl, 1);
100 * </PRE>
101 *
102 * @see  DraftLDUPSubentriesRequestControl
103 */
104@NotMutable()
105@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
106public final class RFC3672SubentriesRequestControl
107       extends Control
108{
109  /**
110   * The OID (1.3.6.1.4.1.4203.1.10.1) for the LDAP subentries request control.
111   */
112  @NotNull public static final String SUBENTRIES_REQUEST_OID =
113       "1.3.6.1.4.1.4203.1.10.1";
114
115
116
117  /**
118   * The serial version UID for this serializable class.
119   */
120  private static final long serialVersionUID = -3780729008801136950L;
121
122
123
124  // Indicates whether to return only entries with the ldapSubEntry object
125  // class.
126  private final boolean returnOnlySubEntries;
127
128
129
130  /**
131   * Creates a new subentries request control.  it will not be marked critical.
132   *
133   * @param  returnOnlySubEntries  Indicates whether to return only matching
134   *                               entries that contain the {@code ldapSubEntry}
135   *                               object class.  If this is {@code true}, then
136   *                               only subentries will be returned.  If this is
137   *                               {@code false}, then both both regular entries
138   *                               and subentries may be returned.
139   */
140  public RFC3672SubentriesRequestControl(final boolean returnOnlySubEntries)
141  {
142    this(returnOnlySubEntries, false);
143  }
144
145
146
147  /**
148   * Creates a new subentries request control with the specified criticality.
149   *
150   * @param  returnOnlySubEntries  Indicates whether to return only matching
151   *                               entries that contain the {@code ldapSubEntry}
152   *                               object class.  If this is {@code true}, then
153   *                               only subentries will be returned.  If this is
154   *                               {@code false}, then both both regular entries
155   *                               and subentries may be returned.
156   * @param  isCritical            Indicates whether this control should be
157   *                               marked critical.
158   */
159  public RFC3672SubentriesRequestControl(final boolean returnOnlySubEntries,
160                                         final boolean isCritical)
161  {
162    super(SUBENTRIES_REQUEST_OID, isCritical,
163         new ASN1OctetString(new ASN1Boolean(returnOnlySubEntries).encode()));
164
165    this.returnOnlySubEntries = returnOnlySubEntries;
166  }
167
168
169
170  /**
171   * Creates a new subentries request control which is decoded from the provided
172   * generic control.
173   *
174   * @param  control  The generic control to be decoded as a subentries request
175   *                  control.
176   *
177   * @throws  LDAPException  If the provided control cannot be decoded as a
178   *                         subentries request control.
179   */
180  public RFC3672SubentriesRequestControl(@NotNull final Control control)
181         throws LDAPException
182  {
183    super(control);
184
185    if (! control.hasValue())
186    {
187      throw new LDAPException(ResultCode.DECODING_ERROR,
188           ERR_SUBENTRIES_MISSING_VALUE.get());
189    }
190
191    try
192    {
193      returnOnlySubEntries =  ASN1Boolean.decodeAsBoolean(
194           control.getValue().getValue()).booleanValue();
195    }
196    catch (final Exception e)
197    {
198      Debug.debugException(e);
199      throw new LDAPException(ResultCode.DECODING_ERROR,
200           ERR_SUBENTRIES_ERROR_DECODING_VALUE.get(
201                StaticUtils.getExceptionMessage(e)),
202           e);
203    }
204  }
205
206
207
208  /**
209   * Indicates whether the server should only return matching entries that have
210   * the {@code ldapSubEntry} object class.
211   *
212   * @return  {@code true} if the server should only return matching entries
213   *          that contain the {@code ldapSubEntry} object class, or
214   *          {@code false} if the server may return both regular entries and
215   *          subentries.
216   */
217  public boolean returnOnlySubEntries()
218  {
219    return returnOnlySubEntries;
220  }
221
222
223
224  /**
225   * {@inheritDoc}
226   */
227  @Override()
228  @NotNull()
229  public String getControlName()
230  {
231    return INFO_CONTROL_NAME_SUBENTRIES_REQUEST.get();
232  }
233
234
235
236  /**
237   * {@inheritDoc}
238   */
239  @Override()
240  public void toString(@NotNull final StringBuilder buffer)
241  {
242    buffer.append("RFC3672SubentriesRequestControl(returnOnlySubEntries=");
243    buffer.append(returnOnlySubEntries);
244    buffer.append(", isCritical=");
245    buffer.append(isCritical());
246    buffer.append(')');
247  }
248}