Search in sources :

Example 1 with LogException

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

the class TextFormattedAccessLogReader method parseMessage.

/**
 * Parses the contents of the provided string as a JSON-formatted access log
 * message.
 *
 * @param  messageString  The string 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 TextFormattedAccessLogMessage parseMessage(@NotNull final String messageString) throws LogException {
    // Parse the line as a generic log message, which will give us access to
    // all of its fields.
    final TextFormattedLogMessage m = new TextFormattedLogMessage(messageString);
    // Make sure that the message has at least one field without a name.
    final List<String> unnamedFields = m.getFields().get(TextFormattedLogMessage.NO_FIELD_NAME);
    if ((unnamedFields == null) || unnamedFields.isEmpty()) {
        throw new LogException(messageString, ERR_TEXT_ACCESS_READER_CANNOT_DETERMINE_MESSAGE_TYPE.get(messageString));
    }
    // Look at the first unnamed field.  For messages that are not associated
    // with an operation, then it will be the message type.  For messages that
    // are associated with an operation, then the operation type will come
    // first, so it's okay if we can't parse the first unnamed field value as a
    // message type.
    final String messageOrOpType = unnamedFields.get(0);
    AccessLogMessageType messageType = AccessLogMessageType.forName(messageOrOpType);
    if (messageType != null) {
        switch(messageType) {
            case CONNECT:
                return new TextFormattedConnectAccessLogMessage(m);
            case DISCONNECT:
                return new TextFormattedDisconnectAccessLogMessage(m);
            case SECURITY_NEGOTIATION:
                return new TextFormattedSecurityNegotiationAccessLogMessage(m);
            case CLIENT_CERTIFICATE:
                return new TextFormattedClientCertificateAccessLogMessage(m);
            case ENTRY_REBALANCING_REQUEST:
                return new TextFormattedEntryRebalancingRequestAccessLogMessage(m);
            case ENTRY_REBALANCING_RESULT:
                return new TextFormattedEntryRebalancingResultAccessLogMessage(m);
            default:
                throw new LogException(messageString, ERR_TEXT_ACCESS_READER_CANNOT_DETERMINE_MESSAGE_TYPE.get(messageString));
        }
    }
    // If we've gotten here, then we expect the first unnamed field to be the
    // operation type, and the second field will be the message type.
    final AccessLogOperationType opType = AccessLogOperationType.forName(messageOrOpType);
    if (opType == null) {
        throw new LogException(messageString, ERR_TEXT_ACCESS_READER_CANNOT_DETERMINE_MESSAGE_TYPE.get(messageOrOpType));
    }
    if (unnamedFields.size() < 2) {
        throw new LogException(messageString, ERR_TEXT_ACCESS_READER_CANNOT_DETERMINE_MESSAGE_TYPE.get(messageString));
    }
    messageType = AccessLogMessageType.forName(unnamedFields.get(1));
    if (messageType == null) {
        throw new LogException(messageString, ERR_TEXT_ACCESS_READER_CANNOT_DETERMINE_MESSAGE_TYPE.get(messageString));
    }
    switch(messageType) {
        case REQUEST:
            switch(opType) {
                case ABANDON:
                    return new TextFormattedAbandonRequestAccessLogMessage(m);
                case ADD:
                    return new TextFormattedAddRequestAccessLogMessage(m);
                case BIND:
                    return new TextFormattedBindRequestAccessLogMessage(m);
                case COMPARE:
                    return new TextFormattedCompareRequestAccessLogMessage(m);
                case DELETE:
                    return new TextFormattedDeleteRequestAccessLogMessage(m);
                case EXTENDED:
                    return new TextFormattedExtendedRequestAccessLogMessage(m);
                case MODIFY:
                    return new TextFormattedModifyRequestAccessLogMessage(m);
                case MODDN:
                    return new TextFormattedModifyDNRequestAccessLogMessage(m);
                case SEARCH:
                    return new TextFormattedSearchRequestAccessLogMessage(m);
                case UNBIND:
                    return new TextFormattedUnbindRequestAccessLogMessage(m);
                default:
                    throw new LogException(messageString, ERR_TEXT_ACCESS_READER_UNSUPPORTED_REQUEST_OP_TYPE.get(messageString));
            }
        case FORWARD:
            switch(opType) {
                case ABANDON:
                    return new TextFormattedAbandonForwardAccessLogMessage(m);
                case ADD:
                    return new TextFormattedAddForwardAccessLogMessage(m);
                case BIND:
                    return new TextFormattedBindForwardAccessLogMessage(m);
                case COMPARE:
                    return new TextFormattedCompareForwardAccessLogMessage(m);
                case DELETE:
                    return new TextFormattedDeleteForwardAccessLogMessage(m);
                case EXTENDED:
                    return new TextFormattedExtendedForwardAccessLogMessage(m);
                case MODIFY:
                    return new TextFormattedModifyForwardAccessLogMessage(m);
                case MODDN:
                    return new TextFormattedModifyDNForwardAccessLogMessage(m);
                case SEARCH:
                    return new TextFormattedSearchForwardAccessLogMessage(m);
                case UNBIND:
                default:
                    throw new LogException(messageString, ERR_TEXT_ACCESS_READER_UNSUPPORTED_FORWARD_OP_TYPE.get(messageString));
            }
        case FORWARD_FAILED:
            switch(opType) {
                case ABANDON:
                    return new TextFormattedAbandonForwardFailedAccessLogMessage(m);
                case ADD:
                    return new TextFormattedAddForwardFailedAccessLogMessage(m);
                case BIND:
                    return new TextFormattedBindForwardFailedAccessLogMessage(m);
                case COMPARE:
                    return new TextFormattedCompareForwardFailedAccessLogMessage(m);
                case DELETE:
                    return new TextFormattedDeleteForwardFailedAccessLogMessage(m);
                case EXTENDED:
                    return new TextFormattedExtendedForwardFailedAccessLogMessage(m);
                case MODIFY:
                    return new TextFormattedModifyForwardFailedAccessLogMessage(m);
                case MODDN:
                    return new TextFormattedModifyDNForwardFailedAccessLogMessage(m);
                case SEARCH:
                    return new TextFormattedSearchForwardFailedAccessLogMessage(m);
                case UNBIND:
                default:
                    throw new LogException(messageString, ERR_TEXT_ACCESS_READER_UNSUPPORTED_FORWARD_FAILED_OP_TYPE.get(messageString));
            }
        case RESULT:
            switch(opType) {
                case ABANDON:
                    return new TextFormattedAbandonResultAccessLogMessage(m);
                case ADD:
                    return new TextFormattedAddResultAccessLogMessage(m);
                case BIND:
                    return new TextFormattedBindResultAccessLogMessage(m);
                case COMPARE:
                    return new TextFormattedCompareResultAccessLogMessage(m);
                case DELETE:
                    return new TextFormattedDeleteResultAccessLogMessage(m);
                case EXTENDED:
                    return new TextFormattedExtendedResultAccessLogMessage(m);
                case MODIFY:
                    return new TextFormattedModifyResultAccessLogMessage(m);
                case MODDN:
                    return new TextFormattedModifyDNResultAccessLogMessage(m);
                case SEARCH:
                    return new TextFormattedSearchResultAccessLogMessage(m);
                case UNBIND:
                default:
                    throw new LogException(messageString, ERR_TEXT_ACCESS_READER_UNSUPPORTED_RESULT_OP_TYPE.get(messageString));
            }
        case ASSURANCE_COMPLETE:
            switch(opType) {
                case ADD:
                    return new TextFormattedAddAssuranceCompletedAccessLogMessage(m);
                case DELETE:
                    return new TextFormattedDeleteAssuranceCompletedAccessLogMessage(m);
                case MODIFY:
                    return new TextFormattedModifyAssuranceCompletedAccessLogMessage(m);
                case MODDN:
                    return new TextFormattedModifyDNAssuranceCompletedAccessLogMessage(m);
                case ABANDON:
                case BIND:
                case COMPARE:
                case EXTENDED:
                case SEARCH:
                case UNBIND:
                default:
                    throw new LogException(messageString, ERR_TEXT_ACCESS_READER_UNSUPPORTED_ASSURANCE_OP_TYPE.get(messageString));
            }
        case ENTRY:
            return new TextFormattedSearchEntryAccessLogMessage(m);
        case REFERENCE:
            return new TextFormattedSearchReferenceAccessLogMessage(m);
        case INTERMEDIATE_RESPONSE:
            return new TextFormattedIntermediateResponseAccessLogMessage(m, opType);
        default:
            throw new LogException(messageString, ERR_TEXT_ACCESS_READER_CANNOT_DETERMINE_MESSAGE_TYPE.get(messageString));
    }
}
Also used : AccessLogOperationType(com.unboundid.ldap.sdk.unboundidds.logs.AccessLogOperationType) AccessLogMessageType(com.unboundid.ldap.sdk.unboundidds.logs.AccessLogMessageType) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) NotNull(com.unboundid.util.NotNull)

Example 2 with LogException

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

the class TextFormattedAccessLogReaderTestCase method testReadMessageWithUnsupportedFirstUnnamedField.

/**
 * Tests the behavior when trying to read a file that contains a log message
 * that has one unnamed field in which the first is neither a valid message
 * type nor a valid operation type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadMessageWithUnsupportedFirstUnnamedField() throws Exception {
    final StringBuilder messageBuffer = createLogMessage(true, null, null, false);
    messageBuffer.append("UNSUPPORTED");
    appendField(messageBuffer, DIAGNOSTIC_MESSAGE, "value1");
    appendField(messageBuffer, ADDITIONAL_INFO, "value2");
    final File logFile = createTempFile(messageBuffer.toString());
    try (TextFormattedAccessLogReader reader = new TextFormattedAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a file that contains a message with " + "an unsupported first unnamed field");
    } 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 3 with LogException

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

the class TextFormattedAccessLogReaderTestCase method testReadOperationMessageWithoutOperationType.

/**
 * Tests the behavior when trying to read a file that contains a log message
 * that should be for an operation but doesn't have the operation type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadOperationMessageWithoutOperationType() throws Exception {
    final StringBuilder messageBuffer = createLogMessage(true, REQUEST, null, false);
    final File logFile = createTempFile(messageBuffer.toString());
    try (TextFormattedAccessLogReader reader = new TextFormattedAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a file that contains a request message " + "without an operation type");
    } 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 4 with LogException

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

the class TextFormattedAccessLogReaderTestCase method testReadUnbindForwardFailedMessage.

/**
 * Tests the behavior when trying to read a file that contains a log message
 * that attempts to create an unbind forward failed message, which is not
 * valid.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadUnbindForwardFailedMessage() throws Exception {
    final StringBuilder messageBuffer = createLogMessage(true, FORWARD_FAILED, 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 forward " + "failed 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 5 with LogException

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

the class TextFormattedAccessLogReaderTestCase method testReadUnbindAssuranceCompletedMessage.

/**
 * Tests the behavior when trying to read a file that contains a log message
 * that attempts to create an unbind assurance completed message, which is not
 * valid.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadUnbindAssuranceCompletedMessage() throws Exception {
    final StringBuilder messageBuffer = createLogMessage(true, ASSURANCE_COMPLETE, 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 " + "assurance completed 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)

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