Search in sources :

Example 6 with JSONBoolean

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

the class PasswordPolicyStateJSONTestCase method encodeRequirement.

/**
 * Encodes the provided password quality requirement to a JSON object suitable
 * for inclusion in the password policy state properties object.
 *
 * @param  requirement          The requirement to be encoded.
 * @param  appliesToAdd         Indicates whether the requirement applies to
 *                              add operations.
 * @param  appliesToSelfChange  Indicates whether the requirement applies to
 *                              self password changes.
 * @param  appliesToAdminReset  Indicates whether the requirement applies to
 *                              administrative password resets.
 * @param  appliesToBind        Indicates whether the requirement applies to
 *                              bind operations.
 *
 * @return  The encoded JSON object.
 */
private static JSONObject encodeRequirement(final PasswordQualityRequirement requirement, final boolean appliesToAdd, final boolean appliesToSelfChange, final boolean appliesToAdminReset, final boolean appliesToBind) {
    final Map<String, JSONValue> objectFields = new LinkedHashMap<>();
    objectFields.put("description", new JSONString(requirement.getDescription()));
    final String validationType = requirement.getClientSideValidationType();
    if (validationType != null) {
        objectFields.put("client-side-validation-type", new JSONString(validationType));
        final List<JSONValue> propertyObjects = new ArrayList<>();
        for (Map.Entry<String, String> e : requirement.getClientSideValidationProperties().entrySet()) {
            propertyObjects.add(new JSONObject(new JSONField("name", e.getKey()), new JSONField("value", e.getValue())));
        }
        objectFields.put("client-side-validation-properties", new JSONArray(propertyObjects));
    }
    objectFields.put("applies-to-add", new JSONBoolean(appliesToAdd));
    objectFields.put("applies-to-self-change", new JSONBoolean(appliesToSelfChange));
    objectFields.put("applies-to-administrative-reset", new JSONBoolean(appliesToAdminReset));
    objectFields.put("applies-to-bind", new JSONBoolean(appliesToBind));
    return new JSONObject(objectFields);
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.unboundid.util.json.JSONArray) PasswordPolicyStateJSONField(com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString)

Example 7 with JSONBoolean

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

the class ScrambleAttributeTransformation method scrambleJSONValue.

/**
 * Scrambles the provided JSON value.
 *
 * @param  v                  The JSON value to be scrambled.
 * @param  scrambleAllFields  Indicates whether all fields of any JSON object
 *                            should be scrambled.
 *
 * @return  The scrambled JSON value.
 */
@NotNull()
private JSONValue scrambleJSONValue(@NotNull final JSONValue v, final boolean scrambleAllFields) {
    if (v instanceof JSONArray) {
        final JSONArray a = (JSONArray) v;
        final List<JSONValue> originalValues = a.getValues();
        final ArrayList<JSONValue> scrambledValues = new ArrayList<>(originalValues.size());
        for (final JSONValue arrayValue : originalValues) {
            scrambledValues.add(scrambleJSONValue(arrayValue, true));
        }
        return new JSONArray(scrambledValues);
    } else if (v instanceof JSONBoolean) {
        return new JSONBoolean(ThreadLocalRandom.get().nextBoolean());
    } else if (v instanceof JSONNumber) {
        try {
            return new JSONNumber(scrambleNumericValue(v.toString()));
        } catch (final Exception e) {
            // This should never happen.
            Debug.debugException(e);
            return v;
        }
    } else if (v instanceof JSONObject) {
        final JSONObject o = (JSONObject) v;
        final Map<String, JSONValue> originalFields = o.getFields();
        final LinkedHashMap<String, JSONValue> scrambledFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
        for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
            final JSONValue scrambledValue;
            final String fieldName = e.getKey();
            final JSONValue originalValue = e.getValue();
            if (scrambleAllFields || jsonFields.contains(StaticUtils.toLowerCase(fieldName))) {
                scrambledValue = scrambleJSONValue(originalValue, scrambleAllFields);
            } else if (originalValue instanceof JSONArray) {
                scrambledValue = scrambleObjectsInArray((JSONArray) originalValue);
            } else if (originalValue instanceof JSONObject) {
                scrambledValue = scrambleJSONValue(originalValue, false);
            } else {
                scrambledValue = originalValue;
            }
            scrambledFields.put(fieldName, scrambledValue);
        }
        return new JSONObject(scrambledFields);
    } else if (v instanceof JSONString) {
        final JSONString s = (JSONString) v;
        return new JSONString(scrambleString(s.stringValue()));
    } else {
        // those.
        return v;
    }
}
Also used : JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) JSONValue(com.unboundid.util.json.JSONValue) Entry(com.unboundid.ldap.sdk.Entry) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONNumber(com.unboundid.util.json.JSONNumber) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Aggregations

JSONBoolean (com.unboundid.util.json.JSONBoolean)7 JSONObject (com.unboundid.util.json.JSONObject)7 JSONString (com.unboundid.util.json.JSONString)7 JSONValue (com.unboundid.util.json.JSONValue)6 JSONArray (com.unboundid.util.json.JSONArray)5 LinkedHashMap (java.util.LinkedHashMap)5 JSONField (com.unboundid.util.json.JSONField)4 JSONNumber (com.unboundid.util.json.JSONNumber)4 Entry (com.unboundid.ldap.sdk.Entry)3 NotNull (com.unboundid.util.NotNull)3 ArrayList (java.util.ArrayList)3 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)2 Date (java.util.Date)2 Map (java.util.Map)2 ModifiablePasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.ModifiablePasswordPolicyStateJSONField)1 PasswordPolicyStateAccountUsabilityError (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityError)1 PasswordPolicyStateAccountUsabilityNotice (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityNotice)1 PasswordPolicyStateAccountUsabilityWarning (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityWarning)1 HashMap (java.util.HashMap)1 List (java.util.List)1