Search in sources :

Example 1 with JSONException

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

the class JSONObjectExactMatchingRule method valuesMatch.

/**
 * {@inheritDoc}
 */
@Override()
public boolean valuesMatch(@NotNull final ASN1OctetString value1, @NotNull final ASN1OctetString value2) throws LDAPException {
    final JSONObject o1;
    try {
        o1 = new JSONObject(value1.stringValue());
    } catch (final JSONException e) {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX, e.getMessage(), e);
    }
    final JSONObject o2;
    try {
        o2 = new JSONObject(value2.stringValue());
    } catch (final JSONException e) {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX, e.getMessage(), e);
    }
    return o1.equals(o2, false, true, false);
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LDAPException(com.unboundid.ldap.sdk.LDAPException) JSONException(com.unboundid.util.json.JSONException)

Example 2 with JSONException

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

the class JSONObjectFilter method getFilters.

/**
 * Retrieves the value of the specified field from the provided JSON object as
 * a list of JSON object filters.  The specified field must be a top-level
 * field in the JSON object and it must have a value that is an array of
 * JSON objects that represent valid JSON object filters.
 *
 * @param  o          The JSON object to examine.  It must not be
 *                    {@code null}.
 * @param  fieldName  The name of a top-level field in the JSON object that is
 *                    expected to have a value that is an array of JSON
 *                    objects that represent valid JSON object filters.  It
 *                    must not be {@code null}.
 *
 * @return  The list of JSON object filters retrieved from the JSON object.
 *
 * @throws  JSONException  If the object doesn't have the specified field, or
 *                         if the value of that field is not an array of
 *                         JSON objects that represent valid JSON object
 *                         filters.
 */
@NotNull()
protected List<JSONObjectFilter> getFilters(@NotNull final JSONObject o, @NotNull final String fieldName) throws JSONException {
    final JSONValue value = o.getField(fieldName);
    if (value == null) {
        throw new JSONException(ERR_OBJECT_FILTER_MISSING_REQUIRED_FIELD.get(String.valueOf(o), getFilterType(), fieldName));
    }
    if (!(value instanceof JSONArray)) {
        throw new JSONException(ERR_OBJECT_FILTER_VALUE_NOT_ARRAY.get(String.valueOf(o), getFilterType(), fieldName));
    }
    final List<JSONValue> values = ((JSONArray) value).getValues();
    final ArrayList<JSONObjectFilter> filterList = new ArrayList<>(values.size());
    for (final JSONValue arrayValue : values) {
        if (!(arrayValue instanceof JSONObject)) {
            throw new JSONException(ERR_OBJECT_FILTER_ARRAY_ELEMENT_NOT_OBJECT.get(String.valueOf(o), getFilterType(), fieldName));
        }
        final JSONObject filterObject = (JSONObject) arrayValue;
        try {
            filterList.add(decode(filterObject));
        } catch (final JSONException e) {
            Debug.debugException(e);
            throw new JSONException(ERR_OBJECT_FILTER_ARRAY_ELEMENT_NOT_FILTER.get(String.valueOf(o), getFilterType(), String.valueOf(filterObject), fieldName, e.getMessage()), e);
        }
    }
    return filterList;
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(com.unboundid.util.json.JSONException) NotNull(com.unboundid.util.NotNull)

Example 3 with JSONException

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

the class ContainsFieldJSONObjectFilter method decodeFilter.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
protected ContainsFieldJSONObjectFilter decodeFilter(@NotNull final JSONObject filterObject) throws JSONException {
    final List<String> fieldPath = getStrings(filterObject, FIELD_FIELD_PATH, false, null);
    final Set<ExpectedValueType> expectedTypes;
    final List<String> valueTypeNames = getStrings(filterObject, FIELD_EXPECTED_TYPE, false, Collections.<String>emptyList());
    if (valueTypeNames.isEmpty()) {
        expectedTypes = ALL_EXPECTED_VALUE_TYPES;
    } else {
        final EnumSet<ExpectedValueType> valueTypes = EnumSet.noneOf(ExpectedValueType.class);
        for (final String s : valueTypeNames) {
            final ExpectedValueType t = ExpectedValueType.forName(s);
            if (t == null) {
                throw new JSONException(ERR_CONTAINS_FIELD_FILTER_UNRECOGNIZED_EXPECTED_TYPE.get(String.valueOf(filterObject), FILTER_TYPE, s, FIELD_EXPECTED_TYPE));
            } else {
                valueTypes.add(t);
            }
        }
        expectedTypes = valueTypes;
    }
    return new ContainsFieldJSONObjectFilter(fieldPath, expectedTypes);
}
Also used : JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Example 4 with JSONException

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

the class RegularExpressionJSONObjectFilter method decodeFilter.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
protected RegularExpressionJSONObjectFilter decodeFilter(@NotNull final JSONObject filterObject) throws JSONException {
    final List<String> fieldPath = getStrings(filterObject, FIELD_FIELD_PATH, false, null);
    final String regex = getString(filterObject, FIELD_REGULAR_EXPRESSION, null, true);
    final Pattern pattern;
    try {
        pattern = Pattern.compile(regex);
    } catch (final Exception e) {
        Debug.debugException(e);
        throw new JSONException(ERR_REGEX_FILTER_DECODE_INVALID_REGEX.get(String.valueOf(filterObject), FIELD_REGULAR_EXPRESSION, fieldPathToName(fieldPath), StaticUtils.getExceptionMessage(e)), e);
    }
    final boolean matchAll = getBoolean(filterObject, FIELD_MATCH_ALL_ELEMENTS, false);
    return new RegularExpressionJSONObjectFilter(fieldPath, pattern, matchAll);
}
Also used : Pattern(java.util.regex.Pattern) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) JSONException(com.unboundid.util.json.JSONException) NotNull(com.unboundid.util.NotNull)

Example 5 with JSONException

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

the class SubstringJSONObjectFilter method decodeFilter.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
protected SubstringJSONObjectFilter decodeFilter(@NotNull final JSONObject filterObject) throws JSONException {
    final List<String> fieldPath = getStrings(filterObject, FIELD_FIELD_PATH, false, null);
    final String subInitial = getString(filterObject, FIELD_STARTS_WITH, null, false);
    final List<String> subAny = getStrings(filterObject, FIELD_CONTAINS, true, Collections.<String>emptyList());
    final String subFinal = getString(filterObject, FIELD_ENDS_WITH, null, false);
    if ((subInitial == null) && (subFinal == null) && subAny.isEmpty()) {
        throw new JSONException(ERR_SUBSTRING_FILTER_NO_COMPONENTS.get(String.valueOf(filterObject), FILTER_TYPE, FIELD_STARTS_WITH, FIELD_CONTAINS, FIELD_ENDS_WITH));
    }
    final boolean isCaseSensitive = getBoolean(filterObject, FIELD_CASE_SENSITIVE, false);
    return new SubstringJSONObjectFilter(fieldPath, subInitial, subAny, subFinal, isCaseSensitive);
}
Also used : JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Aggregations

JSONException (com.unboundid.util.json.JSONException)17 NotNull (com.unboundid.util.NotNull)10 JSONString (com.unboundid.util.json.JSONString)10 JSONObject (com.unboundid.util.json.JSONObject)9 JSONArray (com.unboundid.util.json.JSONArray)5 JSONField (com.unboundid.util.json.JSONField)5 Test (org.testng.annotations.Test)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 JSONValue (com.unboundid.util.json.JSONValue)4 LDAPRuntimeException (com.unboundid.ldap.sdk.LDAPRuntimeException)3 JSONBuffer (com.unboundid.util.json.JSONBuffer)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1