001/*
002 * Copyright 2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 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) 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.logs.v2.syntax;
037
038
039
040import com.unboundid.util.ByteStringBuffer;
041import com.unboundid.util.NotNull;
042import com.unboundid.util.StaticUtils;
043import com.unboundid.util.ThreadSafety;
044import com.unboundid.util.ThreadSafetyLevel;
045import com.unboundid.util.json.JSONBuffer;
046
047import static com.unboundid.ldap.sdk.unboundidds.logs.v2.syntax.
048                   LogSyntaxMessages.*;
049
050
051
052/**
053 * This class defines a log field syntax for Boolean values.  This syntax does
054 * not support redacting or tokenizing individual components within the values.
055 * <BR>
056 * <BLOCKQUOTE>
057 *   <B>NOTE:</B>  This class, and other classes within the
058 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
059 *   supported for use against Ping Identity, UnboundID, and
060 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
061 *   for proprietary functionality or for external specifications that are not
062 *   considered stable or mature enough to be guaranteed to work in an
063 *   interoperable way with other types of LDAP servers.
064 * </BLOCKQUOTE>
065 */
066@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
067public final class BooleanLogFieldSyntax
068       extends LogFieldSyntax<Boolean>
069{
070  /**
071   * The name for this syntax.
072   */
073  @NotNull public static final String SYNTAX_NAME = "boolean";
074
075
076
077  /**
078   * A singleton instance of this log field syntax.
079   */
080  @NotNull private static final BooleanLogFieldSyntax INSTANCE =
081       new BooleanLogFieldSyntax();
082
083
084
085  /**
086   * Creates a new instance of this log field syntax implementation.
087   */
088  private BooleanLogFieldSyntax()
089  {
090    super(100);
091  }
092
093
094
095  /**
096   * Retrieves a singleton instance of this log field syntax.
097   *
098   * @return  A singleton instance of this log field syntax.
099   */
100  @NotNull()
101  public static BooleanLogFieldSyntax getInstance()
102  {
103    return INSTANCE;
104  }
105
106
107
108  /**
109   * {@inheritDoc}
110   */
111  @Override()
112  @NotNull()
113  public String getSyntaxName()
114  {
115    return SYNTAX_NAME;
116  }
117
118
119
120  /**
121   * {@inheritDoc}
122   */
123  @Override()
124  @NotNull()
125  public String valueToSanitizedString(@NotNull final Boolean value)
126  {
127    return value.toString();
128  }
129
130
131
132  /**
133   * {@inheritDoc}
134   */
135  @Override()
136  public void valueToSanitizedString(@NotNull final Boolean value,
137                                     @NotNull final ByteStringBuffer buffer)
138  {
139    buffer.append(value);
140  }
141
142
143
144  /**
145   * {@inheritDoc}
146   */
147  @Override()
148  public void logSanitizedFieldToTextFormattedLog(
149                   @NotNull final String fieldName,
150                   @NotNull final Boolean fieldValue,
151                   @NotNull final ByteStringBuffer buffer)
152  {
153    buffer.append(' ');
154    buffer.append(fieldName);
155    buffer.append('=');
156    buffer.append(fieldValue.toString());
157  }
158
159
160
161  /**
162   * {@inheritDoc}
163   */
164  @Override()
165  public void logSanitizedFieldToJSONFormattedLog(
166                   @NotNull final String fieldName,
167                   @NotNull final Boolean fieldValue,
168                   @NotNull final JSONBuffer buffer)
169  {
170    buffer.appendBoolean(fieldName, fieldValue);
171  }
172
173
174
175  /**
176   * {@inheritDoc}
177   */
178  @Override()
179  public void logSanitizedValueToJSONFormattedLog(
180              @NotNull final Boolean value,
181              @NotNull final JSONBuffer buffer)
182  {
183    buffer.appendBoolean(value);
184  }
185
186
187
188  /**
189   * {@inheritDoc}
190   */
191  @Override()
192  @NotNull()
193  public Boolean parseValue(@NotNull final String valueString)
194         throws RedactedValueException, TokenizedValueException,
195                LogSyntaxException
196  {
197    final String lowerValue = StaticUtils.toLowerCase(valueString);
198    if (lowerValue.equals("true"))
199    {
200      return Boolean.TRUE;
201    }
202    else if (lowerValue.equals("false"))
203    {
204      return Boolean.FALSE;
205    }
206    else if (valueStringIncludesRedactedComponent(valueString))
207    {
208      throw new RedactedValueException(
209           ERR_BOOLEAN_LOG_SYNTAX_CANNOT_PARSE_REDACTED.get());
210    }
211    else if (valueStringIncludesTokenizedComponent(valueString))
212    {
213      throw new TokenizedValueException(
214           ERR_BOOLEAN_LOG_SYNTAX_CANNOT_PARSE_TOKENIZED.get());
215    }
216    else
217    {
218      throw new LogSyntaxException(
219           ERR_BOOLEAN_LOG_SYNTAX_CANNOT_PARSE.get());
220    }
221  }
222
223
224
225  /**
226   * {@inheritDoc}
227   */
228  @Override()
229  public boolean completelyRedactedValueConformsToSyntax()
230  {
231    return false;
232  }
233
234
235
236  /**
237   * {@inheritDoc}
238   */
239  @Override()
240  public void logCompletelyRedactedFieldToTextFormattedLog(
241                   @NotNull final String fieldName,
242                   @NotNull final ByteStringBuffer buffer)
243  {
244    buffer.append(' ');
245    buffer.append(fieldName);
246    buffer.append("=\"{REDACTED}\"");
247  }
248
249
250
251  /**
252   * {@inheritDoc}
253   */
254  @Override()
255  public void logCompletelyRedactedFieldToJSONFormattedLog(
256                   @NotNull final String fieldName,
257                   @NotNull final JSONBuffer buffer)
258  {
259    buffer.appendString(fieldName, REDACTED_STRING);
260  }
261
262
263
264  /**
265   * {@inheritDoc}
266   */
267  @Override()
268  public void logCompletelyRedactedValueToJSONFormattedLog(
269                   @NotNull final JSONBuffer buffer)
270  {
271    buffer.appendString(REDACTED_STRING);
272  }
273
274
275
276  /**
277   * {@inheritDoc}
278   */
279  @Override()
280  public boolean supportsRedactedComponents()
281  {
282    return false;
283  }
284
285
286
287  /**
288   * {@inheritDoc}
289   */
290  @Override()
291  public boolean valueWithRedactedComponentsConformsToSyntax()
292  {
293    return false;
294  }
295
296
297
298  /**
299   * {@inheritDoc}
300   */
301  @Override()
302  public void logRedactedComponentsFieldToTextFormattedLog(
303                   @NotNull final String fieldName,
304                   @NotNull final Boolean fieldValue,
305                   @NotNull final ByteStringBuffer buffer)
306  {
307    logCompletelyRedactedFieldToTextFormattedLog(fieldName, buffer);
308  }
309
310
311
312  /**
313   * {@inheritDoc}
314   */
315  @Override()
316  public void logRedactedComponentsFieldToJSONFormattedLog(
317                   @NotNull final String fieldName,
318                   @NotNull final Boolean fieldValue,
319                   @NotNull final JSONBuffer buffer)
320  {
321    logCompletelyRedactedFieldToJSONFormattedLog(fieldName, buffer);
322  }
323
324
325
326  /**
327   * {@inheritDoc}
328   */
329  @Override()
330  public void logRedactedComponentsValueToJSONFormattedLog(
331                   @NotNull final Boolean value,
332                   @NotNull final JSONBuffer buffer)
333  {
334    logCompletelyRedactedValueToJSONFormattedLog(buffer);
335  }
336
337
338
339  /**
340   * {@inheritDoc}
341   */
342  @Override()
343  public boolean completelyTokenizedValueConformsToSyntax()
344  {
345    return false;
346  }
347
348
349
350
351  /**
352   * {@inheritDoc}
353   */
354  @Override()
355  public void tokenizeEntireValue(@NotNull final Boolean value,
356                                  @NotNull final byte[] pepper,
357                                  @NotNull final ByteStringBuffer buffer)
358  {
359    tokenize(value.toString(), pepper, buffer);
360  }
361
362
363
364  /**
365   * {@inheritDoc}
366   */
367  @Override()
368  public void logCompletelyTokenizedFieldToTextFormattedLog(
369                   @NotNull final String fieldName,
370                   @NotNull final Boolean fieldValue,
371                   @NotNull final byte[] pepper,
372                   @NotNull final ByteStringBuffer buffer)
373  {
374    buffer.append(' ');
375    buffer.append(fieldName);
376    buffer.append("=\"");
377    tokenize(fieldValue.toString(), pepper, buffer);
378    buffer.append('"');
379  }
380
381
382
383  /**
384   * {@inheritDoc}
385   */
386  @Override()
387  public void logCompletelyTokenizedFieldToJSONFormattedLog(
388                   @NotNull final String fieldName,
389                   @NotNull final Boolean fieldValue,
390                   @NotNull final byte[] pepper,
391                   @NotNull final JSONBuffer buffer)
392  {
393    buffer.appendString(fieldName, tokenize(fieldValue.toString(), pepper));
394  }
395
396
397
398  /**
399   * {@inheritDoc}
400   */
401  @Override()
402  public void logCompletelyTokenizedValueToJSONFormattedLog(
403                   @NotNull final Boolean value,
404                   @NotNull final byte[] pepper,
405                   @NotNull final JSONBuffer buffer)
406  {
407    buffer.appendString(tokenize(value.toString(), pepper));
408  }
409
410
411
412  /**
413   * {@inheritDoc}
414   */
415  @Override()
416  public boolean supportsTokenizedComponents()
417  {
418    return false;
419  }
420
421
422
423  /**
424   * {@inheritDoc}
425   */
426  @Override()
427  public boolean valueWithTokenizedComponentsConformsToSyntax()
428  {
429    return false;
430  }
431
432
433
434  /**
435   * {@inheritDoc}
436   */
437  @Override()
438  public void logTokenizedComponentsFieldToTextFormattedLog(
439                   @NotNull final String fieldName,
440                   @NotNull final Boolean fieldValue,
441                   @NotNull final byte[] pepper,
442                   @NotNull final ByteStringBuffer buffer)
443  {
444    logCompletelyTokenizedFieldToTextFormattedLog(fieldName, fieldValue, pepper,
445         buffer);
446  }
447
448
449
450  /**
451   * {@inheritDoc}
452   */
453  @Override()
454  public void logTokenizedComponentsFieldToJSONFormattedLog(
455                   @NotNull final String fieldName,
456                   @NotNull final Boolean fieldValue,
457                   @NotNull final byte[] pepper,
458                   @NotNull final JSONBuffer buffer)
459  {
460    logCompletelyTokenizedFieldToJSONFormattedLog(fieldName, fieldValue, pepper,
461         buffer);
462  }
463
464
465
466  /**
467   * {@inheritDoc}
468   */
469  @Override()
470  public void logTokenizedComponentsValueToJSONFormattedLog(
471                   @NotNull final Boolean value,
472                   @NotNull final byte[] pepper,
473                   @NotNull final JSONBuffer buffer)
474  {
475    logCompletelyTokenizedValueToJSONFormattedLog(value, pepper, buffer);
476  }
477}