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