Search in sources :

Example 26 with JSONBuffer

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}\" } }"));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONBuffer(com.unboundid.util.json.JSONBuffer) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 27 with JSONBuffer

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));
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 28 with JSONBuffer

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);
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 29 with JSONBuffer

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);
    }
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer)

Example 30 with JSONBuffer

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);
    }
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer)

Aggregations

JSONBuffer (com.unboundid.util.json.JSONBuffer)73 Test (org.testng.annotations.Test)20 NotNull (com.unboundid.util.NotNull)16 LogRecord (java.util.logging.LogRecord)12 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)8 Date (java.util.Date)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)3 LDAPRuntimeException (com.unboundid.ldap.sdk.LDAPRuntimeException)3 JSONException (com.unboundid.util.json.JSONException)3 DN (com.unboundid.ldap.sdk.DN)2 RDN (com.unboundid.ldap.sdk.RDN)2 JSONField (com.unboundid.util.json.JSONField)2 JSONObject (com.unboundid.util.json.JSONObject)2 JSONString (com.unboundid.util.json.JSONString)2 AddResponseProtocolOp (com.unboundid.ldap.protocol.AddResponseProtocolOp)1 BindResponseProtocolOp (com.unboundid.ldap.protocol.BindResponseProtocolOp)1 CompareResponseProtocolOp (com.unboundid.ldap.protocol.CompareResponseProtocolOp)1 DeleteResponseProtocolOp (com.unboundid.ldap.protocol.DeleteResponseProtocolOp)1 ExtendedResponseProtocolOp (com.unboundid.ldap.protocol.ExtendedResponseProtocolOp)1 ModifyDNResponseProtocolOp (com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp)1