Search in sources :

Example 21 with JSONValue

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

the class JSONAccessLogReaderTestCase method testReadObjectWithoutOperationType.

/**
 * Tests the behavior when trying to read a file containing a JSON object
 * for an operation message that doesn't include an operation type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadObjectWithoutOperationType() throws Exception {
    final JSONObject minimalMessageObject = createMinimalMessageObject(REQUEST, ABANDON);
    final Map<String, JSONValue> fieldsWithoutOperationType = new LinkedHashMap<>(minimalMessageObject.getFields());
    assertNotNull(fieldsWithoutOperationType.remove(OPERATION_TYPE.getFieldName()));
    final JSONObject objectWithoutMessageType = new JSONObject(fieldsWithoutOperationType);
    final File logFile = createTempFile(objectWithoutMessageType.toSingleLineString());
    try (JSONAccessLogReader reader = new JSONAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a file that contains a JSON object " + "without an operation type");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONString(com.unboundid.util.json.JSONString) File(java.io.File) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 22 with JSONValue

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

the class JSONLogMessageTestCase method testLogMessageWithoutTimestamp.

/**
 * Tests to ensure that it's not possible to create a log message without a
 * timestamp.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testLogMessageWithoutTimestamp() throws Exception {
    // Create a valid minimal message object and make sure we can create a log
    // message from it.
    final JSONObject validMessageObject = createMinimalMessageObject(CONNECT, null);
    new JSONConnectAccessLogMessage(validMessageObject);
    // Create a JSON object with all of the fields from the valid object except
    // the timestamp and verify that we can't create a log message from it.
    final Map<String, JSONValue> fieldsWithoutTimestamp = new LinkedHashMap<>(validMessageObject.getFields());
    assertNotNull(fieldsWithoutTimestamp.remove(TIMESTAMP.getFieldName()));
    final JSONObject messageObjectWithoutTimestamp = new JSONObject(fieldsWithoutTimestamp);
    try {
        new JSONConnectAccessLogMessage(messageObjectWithoutTimestamp);
        fail("Expected an exception when trying to create a log message " + "without a timestamp.");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONString(com.unboundid.util.json.JSONString) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 23 with JSONValue

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

the class RecentLoginHistoryAttempt method encodeToJSON.

/**
 * Encodes the provided information about a successful authentication attempt
 * to a JSON object.
 *
 * @param  successful              Indicates whether the attempt was
 *                                 successful.
 * @param  timestamp               The time of the authentication attempt.
 * @param  authenticationMethod    The name of the authentication method
 *                                 used for the attempt.  This must not be
 *                                 {@code null} or empty.
 * @param  clientIPAddress         The IP address of the client that made the
 *                                 authentication attempt.  This may be
 *                                 {@code null} if no client IP address is
 *                                 available.
 * @param  failureReason           A general reason that the authentication
 *                                 attempt failed.  It must be {@code null} if
 *                                 the attempt succeeded and must not be
 *                                 {@code null} if the attempt failed.  If
 *                                 provided, the value should be one of the
 *                                 {@code FAILURE_NAME_}* constants in the
 *                                 {@link AuthenticationFailureReason} class.
 * @param  additionalAttemptCount  The number of additional authentication
 *                                 attempts that occurred on the same date (in
 *                                 the UTC time zone) as the provided
 *                                 timestamp with the same values for the
 *                                 successful, authentication method, client
 *                                 IP address, and failure reason fields.  It
 *                                 may be {@code null} if this should not be
 *                                 included (e.g., if information about
 *                                 similar attempts should not be collapsed).
 *
 * @return  A JSON object containing the provided information.
 */
@NotNull()
private static JSONObject encodeToJSON(final boolean successful, final long timestamp, @NotNull final String authenticationMethod, @Nullable final String clientIPAddress, @Nullable final String failureReason, @Nullable final Long additionalAttemptCount) {
    final Map<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(6));
    fields.put(JSON_FIELD_SUCCESSFUL, new JSONBoolean(successful));
    fields.put(JSON_FIELD_TIMESTAMP, new JSONString(StaticUtils.encodeRFC3339Time(timestamp)));
    fields.put(JSON_FIELD_AUTHENTICATION_METHOD, new JSONString(authenticationMethod));
    if (clientIPAddress != null) {
        fields.put(JSON_FIELD_CLIENT_IP_ADDRESS, new JSONString(clientIPAddress));
    }
    if (failureReason != null) {
        fields.put(JSON_FIELD_FAILURE_REASON, new JSONString(failureReason));
    }
    if (additionalAttemptCount != null) {
        fields.put(JSON_FIELD_ADDITIONAL_ATTEMPT_COUNT, new JSONNumber(additionalAttemptCount));
    }
    return new JSONObject(fields);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 24 with JSONValue

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

the class LDAPCompareJSONOutputHandler method formatResult.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
String formatResult(@NotNull final CompareRequest request, @NotNull final LDAPResult result) {
    final Map<String, JSONValue> fields = new LinkedHashMap<>();
    fields.put(FIELD_NAME_DN, new JSONString(request.getDN()));
    fields.put(FIELD_NAME_ATTRIBUTE, new JSONString(request.getAttributeName()));
    fields.put(FIELD_NAME_ASSERTION_VALUE, new JSONString(request.getAssertionValue()));
    fields.put(FIELD_NAME_RESULT_CODE_VALUE, new JSONNumber(result.getResultCode().intValue()));
    fields.put(FIELD_NAME_RESULT_CODE_NAME, new JSONString(result.getResultCode().getName()));
    if (result.getDiagnosticMessage() != null) {
        fields.put(FIELD_NAME_DIAGNOSTIC_MESSAGE, new JSONString(result.getDiagnosticMessage()));
    }
    if (result.getMatchedDN() != null) {
        fields.put(FIELD_NAME_MATCHED_DN, new JSONString(result.getMatchedDN()));
    }
    if (result.getReferralURLs().length > 0) {
        final JSONValue[] referralURLValues = new JSONValue[result.getReferralURLs().length];
        for (int i = 0; i < referralURLValues.length; i++) {
            referralURLValues[i] = new JSONString(result.getReferralURLs()[i]);
        }
        fields.put(FIELD_NAME_REFERRAL_URLS, new JSONArray(referralURLValues));
    }
    return new JSONObject(fields).toSingleLineString();
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 25 with JSONValue

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

the class JSONAccessLogReaderTestCase method testReadObjectWithInvalidMessageType.

/**
 * Tests the behavior when trying to read a file containing a JSON object
 * that includes an invalid message type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadObjectWithInvalidMessageType() throws Exception {
    final JSONObject minimalMessageObject = createMinimalMessageObject(CONNECT, null);
    final Map<String, JSONValue> fieldsWithInvalidMessageType = new LinkedHashMap<>(minimalMessageObject.getFields());
    assertNotNull(fieldsWithInvalidMessageType.put(MESSAGE_TYPE.getFieldName(), new JSONString("invalid")));
    final JSONObject objectWithoutMessageType = new JSONObject(fieldsWithInvalidMessageType);
    final File logFile = createTempFile(objectWithoutMessageType.toSingleLineString());
    try (JSONAccessLogReader reader = new JSONAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a file that contains a JSON object " + "with an invalid message type");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONString(com.unboundid.util.json.JSONString) File(java.io.File) JSONString(com.unboundid.util.json.JSONString) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Aggregations

JSONValue (com.unboundid.util.json.JSONValue)50 JSONString (com.unboundid.util.json.JSONString)45 JSONObject (com.unboundid.util.json.JSONObject)40 NotNull (com.unboundid.util.NotNull)33 LinkedHashMap (java.util.LinkedHashMap)31 JSONArray (com.unboundid.util.json.JSONArray)27 ArrayList (java.util.ArrayList)27 Test (org.testng.annotations.Test)9 LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)8 JSONBoolean (com.unboundid.util.json.JSONBoolean)6 Map (java.util.Map)6 JSONNumber (com.unboundid.util.json.JSONNumber)5 File (java.io.File)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 JSONException (com.unboundid.util.json.JSONException)4 JSONField (com.unboundid.util.json.JSONField)4 Date (java.util.Date)4 List (java.util.List)4 Entry (com.unboundid.ldap.sdk.Entry)3 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)2