use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.
the class JSONLogFieldSyntaxTestCase method testJSONLogMethods.
/**
* Tests the methods that may be used for logging JSON-formatted messages.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testJSONLogMethods() throws Exception {
final Set<String> includeFields = StaticUtils.setOf("a");
final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, includeFields, null);
final JSONObject o = new JSONObject(new JSONField("a", "foo"), new JSONField("b", "ThisIsALongerValue"));
final JSONBuffer buffer = new JSONBuffer();
buffer.beginObject();
syntax.logSanitizedFieldToJSONFormattedLog("abc", o, buffer);
buffer.endObject();
assertEquals(buffer.toString(), "{ \"abc\":{ \"a\":\"foo\", " + "\"b\":\"ThisIsALon{8 more characters}\" } }");
buffer.clear();
buffer.beginObject();
syntax.logCompletelyRedactedFieldToJSONFormattedLog("def", buffer);
buffer.endObject();
assertEquals(buffer.toString(), "{ \"def\":{ \"redacted\":\"{REDACTED}\" } }");
buffer.clear();
buffer.beginObject();
syntax.logRedactedComponentsFieldToJSONFormattedLog("ghi", o, buffer);
buffer.endObject();
assertEquals(buffer.toString(), "{ \"ghi\":{ \"a\":\"{REDACTED}\", " + "\"b\":\"ThisIsALon{8 more characters}\" } }");
buffer.clear();
buffer.beginObject();
final byte[] pepper = StaticUtils.randomBytes(8, false);
syntax.logCompletelyTokenizedFieldToJSONFormattedLog("jkl", o, pepper, buffer);
buffer.endObject();
final String completelyTokenizedString = buffer.toString();
assertTrue(completelyTokenizedString.startsWith("{ \"jkl\":{ \"tokenized\":\"{TOKENIZED:"));
assertTrue(completelyTokenizedString.endsWith("}\" } }"));
buffer.clear();
buffer.beginObject();
syntax.logTokenizedComponentsFieldToJSONFormattedLog("mno", o, pepper, buffer);
buffer.endObject();
final String tokenizedComponentsString = buffer.toString();
assertTrue(tokenizedComponentsString.startsWith("{ \"mno\":{ \"a\":\"{TOKENIZED:"));
assertTrue(tokenizedComponentsString.endsWith("}\", \"b\":\"ThisIsALon{8 more characters}\" } }"));
}
use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.
the class RFC3339TimestampLogFieldSyntaxTestCase method testJSONLogMethods.
/**
* Tests the methods that may be used for logging JSON-formatted messages.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testJSONLogMethods() throws Exception {
final RFC3339TimestampLogFieldSyntax syntax = RFC3339TimestampLogFieldSyntax.getInstance();
final Date now = new Date();
final JSONBuffer buffer = new JSONBuffer();
buffer.beginObject();
syntax.logSanitizedFieldToJSONFormattedLog("abc", now, buffer);
buffer.endObject();
assertEquals(buffer.toString(), "{ \"abc\":\"" + StaticUtils.encodeRFC3339Time(now) + "\" }");
buffer.clear();
buffer.beginObject();
syntax.logCompletelyRedactedFieldToJSONFormattedLog("def", buffer);
buffer.endObject();
assertEquals(buffer.toString(), "{ \"def\":\"9999-01-01T00:00:00.000Z\" }");
buffer.clear();
buffer.beginObject();
syntax.logRedactedComponentsFieldToJSONFormattedLog("ghi", now, buffer);
buffer.endObject();
assertEquals(buffer.toString(), "{ \"ghi\":\"9999-01-01T00:00:00.000Z\" }");
buffer.clear();
buffer.beginObject();
final byte[] pepper = StaticUtils.randomBytes(8, false);
syntax.logCompletelyTokenizedFieldToJSONFormattedLog("jkl", now, pepper, buffer);
buffer.endObject();
final String completelyTokenizedString = buffer.toString();
assertTrue(completelyTokenizedString.startsWith("{ \"jkl\":\"8888-"));
assertTrue(completelyTokenizedString.endsWith("Z\" }"));
buffer.clear();
buffer.beginObject();
syntax.logTokenizedComponentsFieldToJSONFormattedLog("mno", now, pepper, buffer);
buffer.endObject();
final String tokenizedComponentsString = buffer.toString();
assertTrue(tokenizedComponentsString.startsWith("{ \"mno\":\"8888-"));
assertEquals(tokenizedComponentsString, "{ \"mno\":" + completelyTokenizedString.substring(8));
}
use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.
the class RFC3339TimestampLogFieldSyntaxTestCase method testJSONValueLogMethods.
/**
* Tests the methods that may be used for logging JSON-formatted values
* (without field names).
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testJSONValueLogMethods() throws Exception {
final RFC3339TimestampLogFieldSyntax syntax = RFC3339TimestampLogFieldSyntax.getInstance();
final Date now = new Date();
final JSONBuffer buffer = new JSONBuffer();
syntax.logSanitizedValueToJSONFormattedLog(now, buffer);
assertEquals(buffer.toString(), '"' + StaticUtils.encodeRFC3339Time(now) + '"');
buffer.clear();
syntax.logCompletelyRedactedValueToJSONFormattedLog(buffer);
assertEquals(buffer.toString(), "\"9999-01-01T00:00:00.000Z\"");
buffer.clear();
syntax.logRedactedComponentsValueToJSONFormattedLog(now, buffer);
assertEquals(buffer.toString(), "\"9999-01-01T00:00:00.000Z\"");
buffer.clear();
final byte[] pepper = StaticUtils.randomBytes(8, false);
syntax.logCompletelyTokenizedValueToJSONFormattedLog(now, pepper, buffer);
final String completelyTokenizedString = buffer.toString();
assertTrue(completelyTokenizedString.startsWith("\"8888-"));
assertTrue(completelyTokenizedString.endsWith("Z\""));
buffer.clear();
syntax.logTokenizedComponentsValueToJSONFormattedLog(now, pepper, buffer);
final String tokenizedComponentsString = buffer.toString();
assertTrue(tokenizedComponentsString.startsWith("\"8888-"));
assertEquals(tokenizedComponentsString, completelyTokenizedString);
}
use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.
the class Debug method debugLDAPRequest.
/**
* Writes debug information about the provided request, if appropriate.
*
* @param l The log level that should be used for the debug information.
* @param s A string representation of the LDAP request for which debug
* information should be written.
* @param i The message ID for the request that will be sent. It may be
* negative if no message ID is available.
* @param c The connection on which the request will be sent. It may be
* {@code null} for historic reasons, but should be
* non-{@code null} in new uses.
*/
public static void debugLDAPRequest(@NotNull final Level l, @NotNull final String s, final int i, @Nullable final LDAPConnection c) {
if (debugEnabled && debugTypes.contains(DebugType.LDAP)) {
final JSONBuffer buffer = new JSONBuffer();
addCommonHeader(buffer, l, DebugType.LDAP);
if (c != null) {
buffer.appendNumber("connection-id", c.getConnectionID());
final String connectionName = c.getConnectionName();
if (connectionName != null) {
buffer.appendString("connection-name", connectionName);
}
final String connectionPoolName = c.getConnectionPoolName();
if (connectionPoolName != null) {
buffer.appendString("connection-pool-name", connectionPoolName);
}
final String connectedAddress = c.getConnectedAddress();
if (connectedAddress != null) {
buffer.appendString("connected-to-address", connectedAddress);
buffer.appendNumber("connected-to-port", c.getConnectedPort());
}
try {
final int soTimeout = InternalSDKHelper.getSoTimeout(c);
buffer.appendNumber("socket-timeout-millis", soTimeout);
} catch (final Exception e) {
}
}
if (i >= 0) {
buffer.appendNumber("message-id", i);
}
buffer.appendString("sending-ldap-request", s);
addCommonFooter(buffer);
log(l, buffer);
}
}
use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.
the class Debug method debug.
/**
* Writes a generic debug message, if appropriate.
*
* @param l The log level that should be used for the debug information.
* @param t The debug type to use to determine whether to write the message.
* @param m The message to be written.
* @param e An exception to include with the log message.
*/
public static void debug(@NotNull final Level l, @NotNull final DebugType t, @Nullable final String m, @Nullable final Throwable e) {
if (debugEnabled && debugTypes.contains(t)) {
final JSONBuffer buffer = new JSONBuffer();
addCommonHeader(buffer, l, t);
if (m != null) {
buffer.appendString("message", m);
}
if (e != null) {
addCaughtException(buffer, "caught-exception", e);
}
addCommonFooter(buffer);
log(l, buffer, e);
}
}
Aggregations