001/*
002 * Copyright 2018-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2018-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) 2018-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.unboundidds.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.unboundidds.controls.ControlMessages.*;
052
053
054
055/**
056 * This class provides an implementation of a request control that may be
057 * included in a search request to indicate that the server should reject the
058 * request if the server cannot use its defined indexes to identify matching
059 * entries efficiently.  This can prevent a requester with the unindexed search
060 * privilege from inadvertently submitting an expensive search request that
061 * could require a long time to complete and potentially consume significant
062 * server resources.  This request control has an OID of
063 * "1.3.6.1.4.1.30221.2.5.54", may have a criticality of either true or false,
064 * and does not take a value.
065 * <BR>
066 * <BLOCKQUOTE>
067 *   <B>NOTE:</B>  This class, and other classes within the
068 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
069 *   supported for use against Ping Identity, UnboundID, and
070 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
071 *   for proprietary functionality or for external specifications that are not
072 *   considered stable or mature enough to be guaranteed to work in an
073 *   interoperable way with other types of LDAP servers.
074 * </BLOCKQUOTE>
075 *
076 * @see  PermitUnindexedSearchRequestControl
077 */
078@NotMutable()
079@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
080public final class RejectUnindexedSearchRequestControl
081       extends Control
082{
083  /**
084   * The OID (1.3.6.1.4.1.30221.2.5.54) for the reject unindexed search request
085   * control.
086   */
087  @NotNull public static final String REJECT_UNINDEXED_SEARCH_REQUEST_OID =
088       "1.3.6.1.4.1.30221.2.5.54";
089
090
091
092  /**
093   * The serial version UID for this serializable class.
094   */
095  private static final long serialVersionUID = 6331056590003014623L;
096
097
098
099  /**
100   * Creates a new reject unindexed search request control with a criticality of
101   * {@code true}.
102   */
103  public RejectUnindexedSearchRequestControl()
104  {
105    this(true);
106  }
107
108
109
110  /**
111   * Creates a new reject unindexed search request control with the specified
112   * criticality.
113   *
114   * @param  isCritical  Indicates whether the control should be considered
115   *                     critical.
116   */
117  public RejectUnindexedSearchRequestControl(final boolean isCritical)
118  {
119    super(REJECT_UNINDEXED_SEARCH_REQUEST_OID, isCritical);
120  }
121
122
123
124  /**
125   * Creates a new reject unindexed search request control that is decoded from
126   * the provided generic control.
127   *
128   * @param  control  The generic control to be decoded as a reject unindexed
129   *                  search request control.
130   *
131   * @throws  LDAPException  If the provided control cannot be decoded as a
132   *                         reject unindexed search request control.
133   */
134  public RejectUnindexedSearchRequestControl(@NotNull final Control control)
135         throws LDAPException
136  {
137    super(control);
138
139    if (control.hasValue())
140    {
141      throw new LDAPException(ResultCode.DECODING_ERROR,
142           ERR_REJECT_UNINDEXED_SEARCH_REQUEST_HAS_VALUE.get());
143    }
144  }
145
146
147
148  /**
149   * {@inheritDoc}
150   */
151  @Override()
152  @NotNull()
153  public String getControlName()
154  {
155    return INFO_CONTROL_NAME_REJECT_UNINDEXED_SEARCH_REQUEST.get();
156  }
157
158
159
160  /**
161   * Retrieves a representation of this reject unindexed search control as a
162   * JSON object.  The JSON object uses the following fields (note that since
163   * this control does not have a value, neither the {@code value-base64} nor
164   * {@code value-json} fields may be present):
165   * <UL>
166   *   <LI>
167   *     {@code oid} -- A mandatory string field whose value is the object
168   *     identifier for this control.  For the reject unindexed search request
169   *     control, the OID is "1.3.6.1.4.1.30221.2.5.54".
170   *   </LI>
171   *   <LI>
172   *     {@code control-name} -- An optional string field whose value is a
173   *     human-readable name for this control.  This field is only intended for
174   *     descriptive purposes, and when decoding a control, the {@code oid}
175   *     field should be used to identify the type of control.
176   *   </LI>
177   *   <LI>
178   *     {@code criticality} -- A mandatory Boolean field used to indicate
179   *     whether this control is considered critical.
180   *   </LI>
181   * </UL>
182   *
183   * @return  A JSON object that contains a representation of this control.
184   */
185  @Override()
186  @NotNull()
187  public JSONObject toJSONControl()
188  {
189    return new JSONObject(
190         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
191              REJECT_UNINDEXED_SEARCH_REQUEST_OID),
192         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
193              INFO_CONTROL_NAME_REJECT_UNINDEXED_SEARCH_REQUEST.get()),
194         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
195              isCritical()));
196  }
197
198
199
200  /**
201   * Attempts to decode the provided object as a JSON representation of a
202   * reject unindexed search request control.
203   *
204   * @param  controlObject  The JSON object to be decoded.  It must not be
205   *                        {@code null}.
206   * @param  strict         Indicates whether to use strict mode when decoding
207   *                        the provided JSON object.  If this is {@code true},
208   *                        then this method will throw an exception if the
209   *                        provided JSON object contains any unrecognized
210   *                        fields.  If this is {@code false}, then unrecognized
211   *                        fields will be ignored.
212   *
213   * @return  The reject unindexed search request control that was decoded from
214   *          the provided JSON object.
215   *
216   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
217   *                         valid reject unindexed search request control.
218   */
219  @NotNull()
220  public static RejectUnindexedSearchRequestControl decodeJSONControl(
221              @NotNull final JSONObject controlObject,
222              final boolean strict)
223         throws LDAPException
224  {
225    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
226         controlObject, strict, false, false);
227
228    return new RejectUnindexedSearchRequestControl(
229         jsonControl.getCriticality());
230  }
231
232
233
234  /**
235   * {@inheritDoc}
236   */
237  @Override()
238  public void toString(@NotNull final StringBuilder buffer)
239  {
240    buffer.append("RejectUnindexedSearchRequestControl(isCritical=");
241    buffer.append(isCritical());
242    buffer.append(')');
243  }
244}