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