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