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);
}
}
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));
}
}
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));
}
}
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));
}
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);
}
Aggregations