Search in sources :

Example 6 with JSONBuffer

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

the class JSONLDAPConnectionLogger method logModifyRequest.

/**
 * {@inheritDoc}
 */
@Override()
public void logModifyRequest(@NotNull final LDAPConnectionInfo connectionInfo, final int messageID, @NotNull final ReadOnlyModifyRequest modifyRequest) {
    if (logRequests && operationTypes.contains(OperationType.MODIFY)) {
        final JSONBuffer buffer = startLogMessage("request", OperationType.MODIFY, connectionInfo, messageID);
        appendDN(buffer, "dn", modifyRequest.getDN());
        if (includeModifyAttributeNames) {
            final List<Modification> mods = modifyRequest.getModifications();
            if (includeModifyAttributeValues) {
                buffer.beginArray("modifications");
                for (final Modification m : mods) {
                    buffer.beginObject();
                    final String name = m.getAttributeName();
                    buffer.appendString("attribute-name", name);
                    buffer.appendString("modification-type", m.getModificationType().getName());
                    buffer.beginArray("attribute-values");
                    final String baseName = StaticUtils.toLowerCase(Attribute.getBaseName(name));
                    if (fullAttributesToRedact.contains(baseName)) {
                        for (final String value : m.getValues()) {
                            buffer.appendString(REDACTED_VALUE_STRING);
                        }
                    } else {
                        for (final String value : m.getValues()) {
                            buffer.appendString(value);
                        }
                    }
                    buffer.endArray();
                    buffer.endObject();
                }
                buffer.endArray();
            } else {
                final Map<String, String> modifiedAttributes = new LinkedHashMap<>(StaticUtils.computeMapCapacity(mods.size()));
                for (final Modification m : modifyRequest.getModifications()) {
                    final String name = m.getAttributeName();
                    final String lowerName = StaticUtils.toLowerCase(name);
                    if (!modifiedAttributes.containsKey(lowerName)) {
                        modifiedAttributes.put(lowerName, name);
                    }
                }
                buffer.beginArray("modified-attributes");
                for (final String attributeName : modifiedAttributes.values()) {
                    buffer.appendString(attributeName);
                }
                buffer.endArray();
            }
        }
        appendControls(buffer, "control-oids", modifyRequest.getControls());
        logMessage(buffer, flushAfterRequestMessages);
    }
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with JSONBuffer

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

the class JSONLDAPConnectionLogger method startLogMessage.

/**
 * Starts generating a log message.
 *
 * @param  messageType     The message type for the log message.  It must not
 *                         be {@code null}.
 * @param  operationType   The operation type for the log message.  It may be
 *                         {@code null} if there is no associated operation
 *                         type.
 * @param  connectionInfo  Information about the connection with which the
 *                         message is associated.  It must not be
 *                         {@code null}.
 * @param  messageID       The LDAP message ID for the associated operation.
 *                         This will be ignored if the value is less than
 *                         zero.
 *
 * @return  A JSON buffer that may be used to construct the remainder of the
 *          log message.
 */
@NotNull()
private JSONBuffer startLogMessage(@NotNull final String messageType, @Nullable final OperationType operationType, @NotNull final LDAPConnectionInfo connectionInfo, final int messageID) {
    JSONBuffer buffer = jsonBuffers.get();
    if (buffer == null) {
        buffer = new JSONBuffer();
        jsonBuffers.set(buffer);
    } else {
        buffer.clear();
    }
    buffer.beginObject();
    SimpleDateFormat timestampFormatter = timestampFormatters.get();
    if (timestampFormatter == null) {
        timestampFormatter = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'");
        timestampFormatter.setTimeZone(StaticUtils.getUTCTimeZone());
        timestampFormatters.set(timestampFormatter);
    }
    buffer.appendString("timestamp", timestampFormatter.format(new Date()));
    buffer.appendString("message-type", messageType);
    if (operationType != null) {
        switch(operationType) {
            case ABANDON:
                buffer.appendString("operation-type", "abandon");
                break;
            case ADD:
                buffer.appendString("operation-type", "add");
                break;
            case BIND:
                buffer.appendString("operation-type", "bind");
                break;
            case COMPARE:
                buffer.appendString("operation-type", "compare");
                break;
            case DELETE:
                buffer.appendString("operation-type", "delete");
                break;
            case EXTENDED:
                buffer.appendString("operation-type", "extended");
                break;
            case MODIFY:
                buffer.appendString("operation-type", "modify");
                break;
            case MODIFY_DN:
                buffer.appendString("operation-type", "modify-dn");
                break;
            case SEARCH:
                buffer.appendString("operation-type", "search");
                break;
            case UNBIND:
                buffer.appendString("operation-type", "unbind");
                break;
        }
    }
    buffer.appendNumber("connection-id", connectionInfo.getConnectionID());
    final String connectionName = connectionInfo.getConnectionName();
    if (connectionName != null) {
        buffer.appendString("connection-name", connectionName);
    }
    final String connectionPoolName = connectionInfo.getConnectionPoolName();
    if (connectionPoolName != null) {
        buffer.appendString("connection-pool-name", connectionPoolName);
    }
    if (messageID >= 0) {
        buffer.appendNumber("ldap-message-id", messageID);
    }
    return buffer;
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) NotNull(com.unboundid.util.NotNull)

Example 8 with JSONBuffer

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

the class JSONLDAPConnectionLogger method logLDAPResult.

/**
 * Logs a final result message for the provided result.  If the result is a
 * {@code BindResult}, an {@code ExtendedResult}, or a {@code SearchResult},
 * then additional information about that type of result may also be included.
 *
 * @param  connectionInfo  Information about the connection with which the
 *                         result is associated.  It must not be
 *                         {@code null}.
 * @param  operationType   The operation type for the log message.  It must
 *                         not be {@code null}.
 * @param  messageID       The LDAP message ID for the associated operation.
 * @param  result          The result to be logged.
 */
private void logLDAPResult(@NotNull final LDAPConnectionInfo connectionInfo, @NotNull final OperationType operationType, final int messageID, @NotNull final LDAPResult result) {
    if (logFinalResults && operationTypes.contains(operationType)) {
        final JSONBuffer buffer = startLogMessage("result", operationType, connectionInfo, messageID);
        buffer.appendNumber("result-code-value", result.getResultCode().intValue());
        buffer.appendString("result-code-name", result.getResultCode().getName());
        final String diagnosticMessage = result.getDiagnosticMessage();
        if (diagnosticMessage != null) {
            buffer.appendString("diagnostic-message", diagnosticMessage);
        }
        final String matchedDN = result.getMatchedDN();
        if (matchedDN != null) {
            buffer.appendString("matched-dn", matchedDN);
        }
        final String[] referralURLs = result.getReferralURLs();
        if ((referralURLs != null) && (referralURLs.length > 0)) {
            buffer.beginArray("referral-urls");
            for (final String url : referralURLs) {
                buffer.appendString(url);
            }
            buffer.endArray();
        }
        if (result instanceof BindResult) {
            final BindResult bindResult = (BindResult) result;
            if (bindResult.getServerSASLCredentials() != null) {
                buffer.appendBoolean("has-server-sasl-credentials", true);
            }
        } else if (result instanceof ExtendedResult) {
            final ExtendedResult extendedResult = (ExtendedResult) result;
            final String oid = extendedResult.getOID();
            if (oid != null) {
                buffer.appendString("oid", oid);
            }
            buffer.appendBoolean("has-value", (extendedResult.getValue() != null));
        }
        appendControls(buffer, "control-oids", result.getResponseControls());
        logMessage(buffer, flushAfterFinalResultMessages);
    }
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer)

Example 9 with JSONBuffer

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

the class JSONLDAPConnectionLogger method logCompareRequest.

/**
 * {@inheritDoc}
 */
@Override()
public void logCompareRequest(@NotNull final LDAPConnectionInfo connectionInfo, final int messageID, @NotNull final ReadOnlyCompareRequest compareRequest) {
    if (logRequests && operationTypes.contains(OperationType.COMPARE)) {
        final JSONBuffer buffer = startLogMessage("request", OperationType.COMPARE, connectionInfo, messageID);
        appendDN(buffer, "dn", compareRequest.getDN());
        appendDN(buffer, "attribute-type", compareRequest.getAttributeName());
        final String baseName = StaticUtils.toLowerCase(Attribute.getBaseName(compareRequest.getAttributeName()));
        if (fullAttributesToRedact.contains(baseName)) {
            buffer.appendString("assertion-value", REDACTED_VALUE_STRING);
        } else {
            buffer.appendString("assertion-value", compareRequest.getAssertionValue());
        }
        appendControls(buffer, "control-oids", compareRequest.getControls());
        logMessage(buffer, flushAfterRequestMessages);
    }
}
Also used : JSONBuffer(com.unboundid.util.json.JSONBuffer)

Example 10 with JSONBuffer

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

the class JSONLDAPConnectionLogger method logSearchReference.

/**
 * {@inheritDoc}
 */
@Override()
public void logSearchReference(@NotNull final LDAPConnectionInfo connectionInfo, final int requestMessageID, @NotNull final SearchResultReference searchReference) {
    if (logSearchReferences && operationTypes.contains(OperationType.SEARCH)) {
        final JSONBuffer buffer = startLogMessage("search-reference", OperationType.SEARCH, connectionInfo, requestMessageID);
        buffer.beginArray("referral-urls");
        for (final String url : searchReference.getReferralURLs()) {
            buffer.appendString(url);
        }
        buffer.endArray();
        appendControls(buffer, "control-oids", searchReference.getControls());
        logMessage(buffer, flushAfterRequestMessages);
    }
}
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