001/*
002 * Copyright 2023-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2023-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) 2023-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 java.util.Collection;
041import java.util.Map;
042
043import com.unboundid.asn1.ASN1OctetString;
044import com.unboundid.ldap.sdk.Control;
045import com.unboundid.ldap.sdk.JSONControlDecodeHelper;
046import com.unboundid.ldap.sdk.LDAPException;
047import com.unboundid.ldap.sdk.ResultCode;
048import com.unboundid.util.Debug;
049import com.unboundid.util.NotMutable;
050import com.unboundid.util.NotNull;
051import com.unboundid.util.StaticUtils;
052import com.unboundid.util.ThreadSafety;
053import com.unboundid.util.ThreadSafetyLevel;
054import com.unboundid.util.json.JSONBoolean;
055import com.unboundid.util.json.JSONField;
056import com.unboundid.util.json.JSONNumber;
057import com.unboundid.util.json.JSONObject;
058import com.unboundid.util.json.JSONString;
059import com.unboundid.util.json.JSONValue;
060
061import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
062
063
064
065/**
066 * This class provides a request control that can be included in any type of
067 * request to indicate that the server should include one or more fields,
068 * specified as name-value pairs, that should appear in the Ping Identity
069 * Directory Server's access log message for the operation.
070 * <BR>
071 * <BLOCKQUOTE>
072 *   <B>NOTE:</B>  This class, and other classes within the
073 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
074 *   supported for use against Ping Identity, UnboundID, and
075 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
076 *   for proprietary functionality or for external specifications that are not
077 *   considered stable or mature enough to be guaranteed to work in an
078 *   interoperable way with other types of LDAP servers.
079 * </BLOCKQUOTE>
080 * <BR>
081 * The OID for this control is 1.3.6.1.4.1.30221.2.5.66, the criticality may be
082 * either {@code true} or {@code false}.  Its value should be the string
083 * representation of a JSON object whose fields will represent the fields to
084 * include in the access log message for the operation.  The JSON object may
085 * only include Boolean, number, and string fields; array, null, and object
086 * fields will not be permitted.
087 */
088@NotMutable()
089@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
090public final class AccessLogFieldRequestControl
091       extends Control
092{
093  /**
094   * The OID (1.3.6.1.4.1.30221.2.5.66) for the generate access token request
095   * control.
096   */
097  @NotNull public static final  String ACCESS_LOG_FIELD_REQUEST_OID =
098       "1.3.6.1.4.1.30221.2.5.66";
099
100
101
102  /**
103   * The serial version UID for this serializable class.
104   */
105  private static final long serialVersionUID = 6329096464063641398L;
106
107
108
109  // The JSON object that contains the fields to include in the access log
110  // message.
111  @NotNull private final JSONObject fieldsObject;
112
113
114
115  /**
116   * Creates a new access log field request control with the provided fields.
117   * It will not be marked critical.
118   *
119   * @param  fields  The set of fields to include in the access log message.  It
120   *                 must not be {@code null} or empty, and the values may only
121   *                 have Boolean, number, or string types.  Array, null, and
122   *                 object fields will not be allowed.
123   *
124   * @throws  LDAPException  If the provided set of fields is not acceptable.
125   */
126  public AccessLogFieldRequestControl(@NotNull final JSONField... fields)
127         throws LDAPException
128  {
129    this(false, fields);
130  }
131
132
133
134  /**
135   * Creates a new access log field request control with the provided fields.
136   * It will not be marked critical.
137   *
138   * @param  fields  The set of fields to include in the access log message.  It
139   *                 must not be {@code null} or empty, and the values may only
140   *                 have Boolean, number, or string types.  Array, null, and
141   *                 object fields will not be allowed.
142   *
143   * @throws  LDAPException  If the provided set of fields is not acceptable.
144   */
145  public AccessLogFieldRequestControl(
146              @NotNull final Collection<JSONField> fields)
147         throws LDAPException
148  {
149    this(false, fields);
150  }
151
152
153
154  /**
155   * Creates a new access log field request control with the specified
156   * criticality.
157   *
158   * @param  isCritical  Indicates whether this control should be marked
159   *                     critical.
160   * @param  fields      The set of fields to include in the access log message.
161   *                     It must not be {@code null} or empty, and the values
162   *                     may only have Boolean, number, or string types.  Array,
163   *                     null, and object fields will not be allowed.
164   *
165   * @throws  LDAPException  If the provided set of fields is not acceptable.
166   */
167  public AccessLogFieldRequestControl(final boolean isCritical,
168                                      @NotNull final JSONField... fields)
169         throws LDAPException
170  {
171    this(isCritical, new JSONObject(fields));
172  }
173
174
175
176  /**
177   * Creates a new access log field request control with the specified
178   * criticality.
179   *
180   * @param  isCritical  Indicates whether this control should be marked
181   *                     critical.
182   * @param  fields      The set of fields to include in the access log message.
183   *                     It must not be {@code null} or empty, and the values
184   *                     may only have Boolean, number, or string types.  Array,
185   *                     null, and object fields will not be allowed.
186   *
187   * @throws  LDAPException  If the provided set of fields is not acceptable.
188   */
189  public AccessLogFieldRequestControl(final boolean isCritical,
190              @NotNull final Collection<JSONField> fields)
191         throws LDAPException
192  {
193    this(isCritical,
194         new JSONObject(StaticUtils.toArray(fields, JSONField.class)));
195  }
196
197
198
199  /**
200   * Creates a new access log field request control with the specified
201   * criticality.
202   *
203   * @param  isCritical    Indicates whether this control should be marked
204   *                       critical.
205   * @param  fieldsObject  A JSON object containing the set of fields to include
206   *                       in the access log message.  It must not be
207   *                       {@code null}, it must have at least one field, and it
208   *                       may only have Boolean, number, or string fields.
209   *                       Array, null, and object fields will not be allowed.
210   *
211   * @throws  LDAPException  If the provided object has an unacceptable set of
212   *                         fields.
213   */
214  public AccessLogFieldRequestControl(final boolean isCritical,
215                                      @NotNull final JSONObject fieldsObject)
216         throws LDAPException
217  {
218    super(ACCESS_LOG_FIELD_REQUEST_OID, isCritical,
219         new ASN1OctetString(fieldsObject.toString()));
220
221    this.fieldsObject = fieldsObject;
222
223    validateFields(fieldsObject);
224  }
225
226
227
228  /**
229   * Ensures that the provided JSON object has an acceptable set of fields.
230   *
231   * @param  o  The JSON object to validate.  It must not be {@code null}.
232   *
233   * @throws  LDAPException  If the provided object has an unacceptable set of
234   *                         fields.
235   */
236  private static void validateFields(@NotNull final JSONObject o)
237          throws LDAPException
238  {
239    final Map<String,JSONValue> fields = o.getFields();
240    if (fields.isEmpty())
241    {
242      throw new LDAPException(ResultCode.PARAM_ERROR,
243           ERR_ACCESS_LOG_FIELD_REQUEST_NO_FIELDS.get());
244    }
245
246    for (final Map.Entry<String,JSONValue> e : fields.entrySet())
247    {
248      final String fieldName = e.getKey();
249      if (fieldName.isEmpty())
250      {
251        throw new LDAPException(ResultCode.PARAM_ERROR,
252             ERR_ACCESS_LOG_FIELD_REQUEST_EMPTY_FIELD_NAME.get());
253      }
254
255      for (final char c : fieldName.toCharArray())
256      {
257        if (! (((c >= 'a') && (c <= 'z')) ||
258             ((c >= 'A') && (c <= 'Z')) ||
259             ((c >= '0') && (c <= '9')) ||
260             (c == '-') ||
261             (c == '_')))
262        {
263          throw new LDAPException(ResultCode.PARAM_ERROR,
264               ERR_ACCESS_LOG_FIELD_REQUEST_INVALID_FIELD_NAME.get(fieldName));
265        }
266      }
267
268      final JSONValue fieldValue = e.getValue();
269      if (! ((fieldValue instanceof JSONBoolean) ||
270           (fieldValue instanceof JSONNumber) ||
271           (fieldValue instanceof JSONString)))
272      {
273        throw new LDAPException(ResultCode.PARAM_ERROR,
274             ERR_ACCESS_LOG_FIELD_REQUEST_INVALID_FIELD_TYPE.get(fieldName));
275      }
276    }
277  }
278
279
280
281  /**
282   * Creates a new access log field  request control which is decoded from the
283   * provided generic control.
284   *
285   * @param  control  The generic control to be decoded as an access log field
286   *                  request control.
287   *
288   * @throws  LDAPException  If the provided control cannot be decoded as an
289   *                         access log field request control.
290   */
291  public AccessLogFieldRequestControl(@NotNull final Control control)
292         throws LDAPException
293  {
294    super(control);
295
296    if (! control.hasValue())
297    {
298      throw new LDAPException(ResultCode.DECODING_ERROR,
299           ERR_ACCESS_LOG_FIELD_REQUEST_DECODE_NO_VALUE.get());
300    }
301
302    try
303    {
304      fieldsObject = new JSONObject(control.getValue().stringValue());
305    }
306    catch (final Exception e)
307    {
308      Debug.debugException(e);
309      throw new LDAPException(ResultCode.DECODING_ERROR,
310           ERR_ACCESS_LOG_FIELD_REQUEST_DECODE_VALUE_NOT_JSON.get());
311    }
312
313    try
314    {
315      validateFields(fieldsObject);
316    }
317    catch (final LDAPException e)
318    {
319      Debug.debugException(e);
320      throw new LDAPException(ResultCode.DECODING_ERROR,
321           ERR_ACCESS_LOG_FIELD_REQUEST_DECODE_VALUE_UNACCEPTABLE_FIELDS.get(
322                e.getMessage()),
323           e);
324    }
325  }
326
327
328
329  /**
330   * Retrieves a JSON object containing the set of fields to include in the
331   * access log message.
332   *
333   * @return  A JSON object containing the set of fields to include in the
334   *          access log message.
335   */
336  @NotNull()
337  public JSONObject getFieldsObject()
338  {
339    return fieldsObject;
340  }
341
342
343
344  /**
345   * {@inheritDoc}
346   */
347  @Override()
348  @NotNull()
349  public String getControlName()
350  {
351    return INFO_CONTROL_NAME_ACCESS_LOG_FIELD_REQUEST.get();
352  }
353
354
355
356  /**
357   * Retrieves a representation of this generate access token request control as
358   * a JSON object.  The JSON object uses the following fields:
359   * <UL>
360   *   <LI>
361   *     {@code oid} -- A mandatory string field whose value is the object
362   *     identifier for this control.  For the access log field request control,
363   *     the OID is "1.3.6.1.4.1.30221.2.5.66".
364   *   </LI>
365   *   <LI>
366   *     {@code control-name} -- An optional string field whose value is a
367   *     human-readable name for this control.  This field is only intended for
368   *     descriptive purposes, and when decoding a control, the {@code oid}
369   *     field should be used to identify the type of control.
370   *   </LI>
371   *   <LI>
372   *     {@code criticality} -- A mandatory Boolean field used to indicate
373   *     whether this control is considered critical.
374   *   </LI>
375   *   <LI>
376   *     {@code value-base64} -- An optional string field whose value is a
377   *     base64-encoded representation of the raw value for this access log
378   *     field request control.  Exactly one of the {@code value-base64} and
379   *     {@code value-json} fields must be present.
380   *   </LI>
381   *   <LI>
382   *     {@code value-json} -- An optional JSON object field in which the object
383   *     contains the set of fields to include in the access log message.
384   *     Exactly one of the {@code value-base64} and {@code value-json} fields
385   *     must be present, and if the {@code value-json} field is used, then the
386   *     object must not be empty and it must contain only Boolean, number, and
387   *     string fields.
388   *   </LI>
389   * </UL>
390   *
391   * @return  A JSON object that contains a representation of this control.
392   */
393  @Override()
394  @NotNull()
395  public JSONObject toJSONControl()
396  {
397    return new JSONObject(
398         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
399              ACCESS_LOG_FIELD_REQUEST_OID),
400         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
401              INFO_CONTROL_NAME_ACCESS_LOG_FIELD_REQUEST.get()),
402         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
403              isCritical()),
404         new JSONField(JSONControlDecodeHelper.JSON_FIELD_VALUE_JSON,
405              fieldsObject));
406  }
407
408
409
410  /**
411   * Attempts to decode the provided object as a JSON representation of an
412   * access log field request control.
413   *
414   * @param  controlObject  The JSON object to be decoded.  It must not be
415   *                        {@code null}.
416   * @param  strict         Indicates whether to use strict mode when decoding
417   *                        the provided JSON object.  If this is {@code true},
418   *                        then this method will throw an exception if the
419   *                        provided JSON object contains any unrecognized
420   *                        fields.  If this is {@code false}, then unrecognized
421   *                        fields will be ignored.
422   *
423   * @return  The access log field request control that was decoded from the
424   *          provided JSON object.
425   *
426   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
427   *                         valid access log field request control.
428   */
429  @NotNull()
430  public static AccessLogFieldRequestControl decodeJSONControl(
431              @NotNull final JSONObject controlObject,
432              final boolean strict)
433         throws LDAPException
434  {
435    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
436         controlObject, strict, true, true);
437
438    final ASN1OctetString rawValue = jsonControl.getRawValue();
439    if (rawValue != null)
440    {
441      return new AccessLogFieldRequestControl(new Control(jsonControl.getOID(),
442           jsonControl.getCriticality(), rawValue));
443    }
444
445
446    return new AccessLogFieldRequestControl(jsonControl.getCriticality(),
447         jsonControl.getValueObject());
448  }
449
450
451
452  /**
453   * {@inheritDoc}
454   */
455  @Override()
456  public void toString(@NotNull final StringBuilder buffer)
457  {
458    buffer.append("AccessLogFieldRequestControl(isCritical=");
459    buffer.append(isCritical());
460    buffer.append(", fields=");
461    buffer.append(fieldsObject.toSingleLineString());
462    buffer.append(')');
463  }
464}