Search in sources :

Example 36 with JSONBuffer

use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.

the class Debug method debugConnect.

/**
 * Writes debug information to indicate that a connection has been
 * established, if appropriate.
 *
 * @param  l  The log level that should be used for the debug information.
 * @param  h  The address of the server to which the connection was
 *            established.
 * @param  p  The port of the server to which the connection was established.
 * @param  c  The connection object for the connection that has been
 *            established.  It may be {@code null} for historic reasons, but
 *            should be non-{@code null} in new uses.
 */
public static void debugConnect(@NotNull final Level l, @NotNull final String h, final int p, @Nullable final LDAPConnection c) {
    if (debugEnabled && debugTypes.contains(DebugType.CONNECT)) {
        final JSONBuffer buffer = new JSONBuffer();
        addCommonHeader(buffer, l, DebugType.CONNECT);
        buffer.appendString("connected-to-address", h);
        buffer.appendNumber("connected-to-port", p);
        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);
            }
        }
        addCommonFooter(buffer);
        log(l, buffer);
    }
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer)

Example 37 with JSONBuffer

use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.

the class JSONLDAPResultWriter method toJSON.

/**
 * Encodes the provided LDAP result as a JSON object.
 *
 * @param  result  The LDAP result to be encoded as a JSON object.  It must
 *                 not be {@code null}.
 *
 * @return  The JSON object containing the encoded representation of the
 *          LDAP result.
 */
@NotNull()
public static JSONObject toJSON(@NotNull final LDAPResult result) {
    try {
        final JSONBuffer jsonBuffer = new JSONBuffer();
        toJSON(result, jsonBuffer);
        return jsonBuffer.toJSONObject();
    } catch (final JSONException e) {
        // This should never happen.
        Debug.debugException(e);
        throw new LDAPRuntimeException(new LDAPException(ResultCode.ENCODING_ERROR, e.getMessage(), e));
    }
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) JSONBuffer(com.unboundid.util.json.JSONBuffer) JSONException(com.unboundid.util.json.JSONException) LDAPRuntimeException(com.unboundid.ldap.sdk.LDAPRuntimeException) NotNull(com.unboundid.util.NotNull)

Example 38 with JSONBuffer

use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.

the class JSONLDAPResultWriter method toJSON.

/**
 * Encodes the provided entry as a JSON object.
 *
 * @param  entry  The entry to be encoded as a JSON object.  It must not be
 *                {@code null}.
 *
 * @return  The JSON object containing the encoded representation of the
 *          entry.
 */
@NotNull()
public static JSONObject toJSON(@NotNull final Entry entry) {
    try {
        final JSONBuffer jsonBuffer = new JSONBuffer();
        toJSON(entry, jsonBuffer);
        return jsonBuffer.toJSONObject();
    } catch (final JSONException e) {
        // This should never happen.
        Debug.debugException(e);
        throw new LDAPRuntimeException(new LDAPException(ResultCode.ENCODING_ERROR, e.getMessage(), e));
    }
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) JSONBuffer(com.unboundid.util.json.JSONBuffer) JSONException(com.unboundid.util.json.JSONException) LDAPRuntimeException(com.unboundid.ldap.sdk.LDAPRuntimeException) NotNull(com.unboundid.util.NotNull)

Example 39 with JSONBuffer

use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.

the class BooleanLogFieldSyntaxTestCase 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 BooleanLogFieldSyntax syntax = BooleanLogFieldSyntax.getInstance();
    final JSONBuffer buffer = new JSONBuffer();
    buffer.beginObject();
    syntax.logSanitizedFieldToJSONFormattedLog("abc", true, buffer);
    buffer.endObject();
    assertEquals(buffer.toString(), "{ \"abc\":true }");
    buffer.clear();
    buffer.beginObject();
    syntax.logSanitizedFieldToJSONFormattedLog("def", false, buffer);
    buffer.endObject();
    assertEquals(buffer.toString(), "{ \"def\":false }");
    buffer.clear();
    buffer.beginObject();
    syntax.logCompletelyRedactedFieldToJSONFormattedLog("ghi", buffer);
    buffer.endObject();
    assertEquals(buffer.toString(), "{ \"ghi\":\"{REDACTED}\" }");
    buffer.clear();
    buffer.beginObject();
    syntax.logRedactedComponentsFieldToJSONFormattedLog("jkl", true, buffer);
    buffer.endObject();
    assertEquals(buffer.toString(), "{ \"jkl\":\"{REDACTED}\" }");
    buffer.clear();
    buffer.beginObject();
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    syntax.logCompletelyTokenizedFieldToJSONFormattedLog("mno", false, pepper, buffer);
    buffer.endObject();
    final String completelyTokenizedString = buffer.toString();
    assertTrue(completelyTokenizedString.startsWith("{ \"mno\":\"{TOKENIZED:"));
    assertTrue(completelyTokenizedString.endsWith("}\" }"));
    buffer.clear();
    buffer.beginObject();
    syntax.logTokenizedComponentsFieldToJSONFormattedLog("pqr", false, pepper, buffer);
    buffer.endObject();
    final String tokenizedComponentsString = buffer.toString();
    assertTrue(tokenizedComponentsString.startsWith("{ \"pqr\":\"{TOKENIZED:"));
    assertEquals(tokenizedComponentsString, "{ \"pqr\":" + completelyTokenizedString.substring(8));
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer) Test(org.testng.annotations.Test)

Example 40 with JSONBuffer

use of com.unboundid.util.json.JSONBuffer in project ldapsdk by pingidentity.

the class BooleanLogFieldSyntaxTestCase 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 BooleanLogFieldSyntax syntax = BooleanLogFieldSyntax.getInstance();
    final JSONBuffer buffer = new JSONBuffer();
    syntax.logSanitizedValueToJSONFormattedLog(true, buffer);
    assertEquals(buffer.toString(), "true");
    buffer.clear();
    syntax.logSanitizedValueToJSONFormattedLog(false, buffer);
    assertEquals(buffer.toString(), "false");
    buffer.clear();
    syntax.logCompletelyRedactedValueToJSONFormattedLog(buffer);
    assertEquals(buffer.toString(), "\"{REDACTED}\"");
    buffer.clear();
    syntax.logRedactedComponentsValueToJSONFormattedLog(true, buffer);
    assertEquals(buffer.toString(), "\"{REDACTED}\"");
    buffer.clear();
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    syntax.logCompletelyTokenizedValueToJSONFormattedLog(false, pepper, buffer);
    final String completelyTokenizedString = buffer.toString();
    assertTrue(completelyTokenizedString.startsWith("\"{TOKENIZED:"));
    assertTrue(completelyTokenizedString.endsWith("}\""));
    buffer.clear();
    syntax.logTokenizedComponentsValueToJSONFormattedLog(false, pepper, buffer);
    final String tokenizedComponentsString = buffer.toString();
    assertTrue(tokenizedComponentsString.startsWith("\"{TOKENIZED:"));
    assertEquals(tokenizedComponentsString, completelyTokenizedString);
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer) Test(org.testng.annotations.Test)

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