001/*
002 * Copyright 2008-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2008-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) 2008-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 the account usable request control.
057 * It may be included in search requests, in which case each search result entry
058 * matching that request should include the corresponding response control to
059 * obtain information about the usability of the user account associated with
060 * that entry.  In particular, it indicates whether a bind with valid
061 * credentials would likely succeed and the resulting connection would be
062 * usable, and if not the reason for the potential failure.  See the
063 * {@link AccountUsableResponseControl} for information about the information
064 * that is taken into account.
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 * <BR>
076 * This control was designed by Sun Microsystems and is not based on any RFC or
077 * Internet draft.  It does not include a value.
078 * <BR><BR>
079 * <H2>Example</H2>
080 * The following example demonstrates the use of the account usable controls to
081 * determine whether the account for user with uid "john.doe" is usable:
082 * <PRE>
083 * SearchRequest searchRequest =
084 *      new SearchRequest("dc=example,dc=com", SearchScope.SUB,
085 *           Filter.createEqualityFilter("uid", "john.doe"));
086 * searchRequest.addControl(new AccountUsableRequestControl());
087 * SearchResult searchResult = connection.search(searchRequest);
088 *
089 * boolean isUsable = false;
090 * for (SearchResultEntry entry : searchResult.getSearchEntries())
091 * {
092 *   AccountUsableResponseControl c =
093 *        AccountUsableResponseControl.get(entry);
094 *   isUsable = c.isUsable();
095 *   if (isUsable)
096 *   {
097 *     // The account is usable.
098 *   }
099 *   else
100 *   {
101 *     // The account is not usable.
102 *     List&lt;String&gt; unusableReasons = c.getUnusableReasons();
103 *   }
104 * }
105 * </PRE>
106 */
107@NotMutable()
108@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
109public final class AccountUsableRequestControl
110       extends Control
111{
112  /**
113   * The OID (1.3.6.1.4.1.42.2.27.9.5.8) for the account usable request control.
114   */
115  @NotNull public static final String ACCOUNT_USABLE_REQUEST_OID =
116       "1.3.6.1.4.1.42.2.27.9.5.8";
117
118
119
120  /**
121   * The serial version UID for this serializable class.
122   */
123  private static final long serialVersionUID = 2776055961624360982L;
124
125
126
127  /**
128   * Creates a new account usable request control.  It will not be marked
129   * critical.
130   */
131  public AccountUsableRequestControl()
132  {
133    this(false);
134  }
135
136
137
138  /**
139   * Creates a new account usable request control with the specified
140   * criticality.
141   *
142   * @param  isCritical  Indicates whether this control should be marked
143   *                     critical.
144   */
145  public AccountUsableRequestControl(final boolean isCritical)
146  {
147    super(ACCOUNT_USABLE_REQUEST_OID, isCritical,  null);
148  }
149
150
151
152  /**
153   * Creates a new account usable request control which is decoded from the
154   * provided generic control.
155   *
156   * @param  control  The generic control to be decoded as an account usable
157   *                  request control.
158   *
159   * @throws  LDAPException  If the provided control cannot be decoded as an
160   *                         account usable request control.
161   */
162  public AccountUsableRequestControl(@NotNull final Control control)
163         throws LDAPException
164  {
165    super(control);
166
167    if (control.hasValue())
168    {
169      throw new LDAPException(ResultCode.DECODING_ERROR,
170                              ERR_ACCOUNT_USABLE_REQUEST_HAS_VALUE.get());
171    }
172  }
173
174
175
176  /**
177   * {@inheritDoc}
178   */
179  @Override()
180  @NotNull()
181  public String getControlName()
182  {
183    return INFO_CONTROL_NAME_ACCOUNT_USABLE_REQUEST.get();
184  }
185
186
187
188  /**
189   * Retrieves a representation of this account usable request control as a JSON
190   * object.  The JSON object uses the following fields (note that since this
191   * control does not have a value, neither the {@code value-base64} nor
192   * {@code value-json} fields may be present):
193   * <UL>
194   *   <LI>
195   *     {@code oid} -- A mandatory string field whose value is the object
196   *     identifier for this control.  For the account usable request control,
197   *     the OID is "1.3.6.1.4.1.42.2.27.9.5.8".
198   *   </LI>
199   *   <LI>
200   *     {@code control-name} -- An optional string field whose value is a
201   *     human-readable name for this control.  This field is only intended for
202   *     descriptive purposes, and when decoding a control, the {@code oid}
203   *     field should be used to identify the type of control.
204   *   </LI>
205   *   <LI>
206   *     {@code criticality} -- A mandatory Boolean field used to indicate
207   *     whether this control is considered critical.
208   *   </LI>
209   * </UL>
210   *
211   * @return  A JSON object that contains a representation of this control.
212   */
213  @Override()
214  @NotNull()
215  public JSONObject toJSONControl()
216  {
217    return new JSONObject(
218         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
219              ACCOUNT_USABLE_REQUEST_OID),
220         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
221              INFO_CONTROL_NAME_ACCOUNT_USABLE_REQUEST.get()),
222         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
223              isCritical()));
224  }
225
226
227
228  /**
229   * Attempts to decode the provided object as a JSON representation of an
230   * account usable request control.
231   *
232   * @param  controlObject  The JSON object to be decoded.  It must not be
233   *                        {@code null}.
234   * @param  strict         Indicates whether to use strict mode when decoding
235   *                        the provided JSON object.  If this is {@code true},
236   *                        then this method will throw an exception if the
237   *                        provided JSON object contains any unrecognized
238   *                        fields.  If this is {@code false}, then unrecognized
239   *                        fields will be ignored.
240   *
241   * @return  The account usable request control that was decoded from the
242   *          provided JSON object.
243   *
244   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
245   *                         valid account usable request control.
246   */
247  @NotNull()
248  public static AccountUsableRequestControl decodeJSONControl(
249              @NotNull final JSONObject controlObject,
250              final boolean strict)
251         throws LDAPException
252  {
253    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
254         controlObject, strict, false, false);
255
256    return new AccountUsableRequestControl(jsonControl.getCriticality());
257  }
258
259
260
261  /**
262   * {@inheritDoc}
263   */
264  @Override()
265  public void toString(@NotNull final StringBuilder buffer)
266  {
267    buffer.append("AccountUsableRequestControl(isCritical=");
268    buffer.append(isCritical());
269    buffer.append(')');
270  }
271}