Search in sources :

Example 6 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class TextFormattedAccessLogReaderTestCase method testReadUnbindResultMessage.

/**
 * Tests the behavior when trying to read a file that contains a log message
 * that attempts to create an unbind result message, which is not valid.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadUnbindResultMessage() throws Exception {
    final StringBuilder messageBuffer = createLogMessage(true, RESULT, UNBIND, false);
    final File logFile = createTempFile(messageBuffer.toString());
    try (TextFormattedAccessLogReader reader = new TextFormattedAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a file that contains an unbind result " + "message");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : File(java.io.File) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 7 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONLogMessageTestCase method testGetLong.

/**
 * Tests the methods for getting a Long value from a JSON object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetLong() throws Exception {
    // Test with a field whose value is a valid positive integer.
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, 1234L));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getLong(OPERATION_ID).longValue(), 1234);
    assertEquals(m.getLongNoThrow(OPERATION_ID).longValue(), 1234);
    // Test with a field whose value is a valid negative integer.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, -5678L));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getLong(OPERATION_ID).longValue(), -5678);
    assertEquals(m.getLongNoThrow(OPERATION_ID).longValue(), -5678);
    // Test with a field whose value is a string representation of an
    // integer.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, "4321"));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getLong(OPERATION_ID).longValue(), 4321);
    assertEquals(m.getLongNoThrow(OPERATION_ID).longValue(), 4321);
    // Test with a field that is missing.
    o = createMinimalMessageObject(CONNECT, null);
    m = new JSONConnectAccessLogMessage(o);
    assertNull(m.getLong(OPERATION_ID));
    assertNull(m.getLongNoThrow(OPERATION_ID));
    // Test with a field that has an invalid string value.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, "invalid"));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getLong(OPERATION_ID);
        fail("Expected an exception for an invalid string value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getLongNoThrow(OPERATION_ID));
    // Test with a field that has a non-numeric/non-string value.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, true));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getLong(OPERATION_ID);
        fail("Expected an exception for an invalid value type.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getLongNoThrow(OPERATION_ID));
    // Test with a field that has a numeric value that is out of the valid int
    // range but is in the valid long range.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, Long.MAX_VALUE));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getLong(OPERATION_ID).longValue(), Long.MAX_VALUE);
    assertEquals(m.getLongNoThrow(OPERATION_ID).longValue(), Long.MAX_VALUE);
    // Test with a field that has a string value that is out of the valid int
    // range but is in the valid long range.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, String.valueOf(Long.MIN_VALUE)));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getLong(OPERATION_ID).longValue(), Long.MIN_VALUE);
    assertEquals(m.getLongNoThrow(OPERATION_ID).longValue(), Long.MIN_VALUE);
    // Test with a field that has floating-point numeric value.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, 1.5d));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getLong(OPERATION_ID);
        fail("Expected an exception for a floating-point numeric value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getLongNoThrow(OPERATION_ID));
    // Test with a field that has floating-point string value.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, "1.5"));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getLong(OPERATION_ID);
        fail("Expected an exception for a floating-point numeric value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getLongNoThrow(OPERATION_ID));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 8 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONAccessLogReader method parseMessage.

/**
 * Parses the contents of the provided JSON object as a JSON-formatted access
 * log message.
 *
 * @param  messageObject  The JSON object to parse as an access log message.
 *                        It must not be {@code null}.
 *
 * @return  The parsed access log message.
 *
 * @throws  LogException  If the provided JSON object cannot be parsed as a
 *                        valid access log message.
 */
@NotNull()
public static JSONAccessLogMessage parseMessage(@NotNull final JSONObject messageObject) throws LogException {
    // Determine the message type for the log message.
    final String messageTypeStr = messageObject.getFieldAsString(JSONFormattedAccessLogFields.MESSAGE_TYPE.getFieldName());
    if (messageTypeStr == null) {
        final String messageStr = messageObject.toSingleLineString();
        throw new LogException(messageStr, ERR_JSON_ACCESS_LOG_READER_MISSING_MESSAGE_TYPE.get(messageStr, JSONFormattedAccessLogFields.MESSAGE_TYPE.getFieldName()));
    }
    final AccessLogMessageType messageType = AccessLogMessageType.forName(messageTypeStr);
    if (messageType == null) {
        final String messageStr = messageObject.toSingleLineString();
        throw new LogException(messageStr, ERR_JSON_ACCESS_LOG_READER_UNSUPPORTED_MESSAGE_TYPE.get(messageStr, messageTypeStr));
    }
    switch(messageType) {
        case CONNECT:
            return new JSONConnectAccessLogMessage(messageObject);
        case DISCONNECT:
            return new JSONDisconnectAccessLogMessage(messageObject);
        case SECURITY_NEGOTIATION:
            return new JSONSecurityNegotiationAccessLogMessage(messageObject);
        case CLIENT_CERTIFICATE:
            return new JSONClientCertificateAccessLogMessage(messageObject);
        case ENTRY_REBALANCING_REQUEST:
            return new JSONEntryRebalancingRequestAccessLogMessage(messageObject);
        case ENTRY_REBALANCING_RESULT:
            return new JSONEntryRebalancingResultAccessLogMessage(messageObject);
        case ENTRY:
            return new JSONSearchEntryAccessLogMessage(messageObject);
        case REFERENCE:
            return new JSONSearchReferenceAccessLogMessage(messageObject);
        case INTERMEDIATE_RESPONSE:
            return new JSONIntermediateResponseAccessLogMessage(messageObject);
        case REQUEST:
            return createRequestMessage(messageObject);
        case RESULT:
            return createResultMessage(messageObject);
        case FORWARD:
            return createForwardMessage(messageObject);
        case FORWARD_FAILED:
            return createForwardFailedMessage(messageObject);
        case ASSURANCE_COMPLETE:
            return createAssuranceCompleteMessage(messageObject);
        default:
            final String messageStr = messageObject.toSingleLineString();
            throw new LogException(messageStr, ERR_JSON_ACCESS_LOG_READER_UNSUPPORTED_MESSAGE_TYPE.get(messageStr, messageTypeStr));
    }
}
Also used : AccessLogMessageType(com.unboundid.ldap.sdk.unboundidds.logs.AccessLogMessageType) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) NotNull(com.unboundid.util.NotNull)

Example 9 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONAccessLogReader method getOperationType.

/**
 * Determines the operation type for the JSON-formatted access log message
 * encoded in the provided JSON object.
 *
 * @param  messageObject  The JSON object containing an encoded representation
 *                        of an access log message.  It must not be
 *                        {@code null}.
 *
 * @return  The operation type extracted from the provided JSON object.
 *
 * @throws  LogException  If it is not possible to extract a valid operation
 *                        type from the provided JSON object.
 */
@NotNull()
private static AccessLogOperationType getOperationType(@NotNull final JSONObject messageObject) throws LogException {
    final String opTypeStr = messageObject.getFieldAsString(JSONFormattedAccessLogFields.OPERATION_TYPE.getFieldName());
    if (opTypeStr == null) {
        final String messageStr = messageObject.toSingleLineString();
        throw new LogException(messageStr, ERR_JSON_ACCESS_LOG_READER_MISSING_OPERATION_TYPE.get(messageStr, JSONFormattedAccessLogFields.OPERATION_TYPE.getFieldName()));
    }
    final AccessLogOperationType opType = AccessLogOperationType.forName(opTypeStr);
    if (opType == null) {
        final String messageStr = messageObject.toSingleLineString();
        throw new LogException(messageStr, ERR_JSON_ACCESS_LOG_READER_UNSUPPORTED_OPERATION_TYPE.get(messageStr, opTypeStr));
    }
    return opType;
}
Also used : AccessLogOperationType(com.unboundid.ldap.sdk.unboundidds.logs.AccessLogOperationType) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) NotNull(com.unboundid.util.NotNull)

Example 10 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONAccessLogReaderTestCase method testReadUnbindResultMessage.

/**
 * Tests the behavior when trying to read a file containing a JSON object for
 * a result log message for an unbind operation (which doesn't have a result).
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadUnbindResultMessage() throws Exception {
    final JSONObject messageObject = createMinimalMessageObject(RESULT, UNBIND);
    final File logFile = createTempFile(messageObject.toSingleLineString());
    try (JSONAccessLogReader reader = new JSONAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a result message with an operation " + "type of unbind.");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) File(java.io.File) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Aggregations

LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)34 Test (org.testng.annotations.Test)30 File (java.io.File)22 JSONObject (com.unboundid.util.json.JSONObject)20 JSONString (com.unboundid.util.json.JSONString)8 JSONValue (com.unboundid.util.json.JSONValue)8 LinkedHashMap (java.util.LinkedHashMap)8 NotNull (com.unboundid.util.NotNull)4 AccessLogMessageType (com.unboundid.ldap.sdk.unboundidds.logs.AccessLogMessageType)2 AccessLogOperationType (com.unboundid.ldap.sdk.unboundidds.logs.AccessLogOperationType)2 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)2 FileInputStream (java.io.FileInputStream)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 SearchScope (com.unboundid.ldap.sdk.SearchScope)1 AbandonRequestAccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.AbandonRequestAccessLogMessage)1 AccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.AccessLogMessage)1 AccessLogReader (com.unboundid.ldap.sdk.unboundidds.logs.v2.AccessLogReader)1 AddResultAccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.AddResultAccessLogMessage)1 BindResultAccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.BindResultAccessLogMessage)1 CompareResultAccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.CompareResultAccessLogMessage)1