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