Search in sources :

Example 1 with MalformedJsonException

use of org.apache.geode.rest.internal.web.exception.MalformedJsonException in project geode by apache.

the class AbstractBaseController method jsonToObjectArray.

Object[] jsonToObjectArray(final String arguments) {
    final JSONTypes jsonType = validateJsonAndFindType(arguments);
    if (JSONTypes.JSON_ARRAY.equals(jsonType)) {
        try {
            JSONArray jsonArray = new JSONArray(arguments);
            Object[] args = new Object[jsonArray.length()];
            for (int index = 0; index < jsonArray.length(); index++) {
                args[index] = jsonToObject(jsonArray.get(index).toString());
            }
            return args;
        } catch (JSONException je) {
            throw new MalformedJsonException("Json document specified in request body is not valid!", je);
        }
    } else if (JSONTypes.JSON_OBJECT.equals(jsonType)) {
        return new Object[] { jsonToObject(arguments) };
    } else {
        throw new MalformedJsonException("Json document specified in request body is not valid!");
    }
}
Also used : JSONArray(org.json.JSONArray) JSONTypes(org.apache.geode.rest.internal.web.controllers.support.JSONTypes) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) MalformedJsonException(org.apache.geode.rest.internal.web.exception.MalformedJsonException)

Example 2 with MalformedJsonException

use of org.apache.geode.rest.internal.web.exception.MalformedJsonException in project geode by apache.

the class AbstractBaseController method updateMultipleKeys.

ResponseEntity<String> updateMultipleKeys(final String region, final String[] keys, final String json) {
    JSONArray jsonArr = null;
    try {
        jsonArr = new JSONArray(json);
    } catch (JSONException e) {
        throw new MalformedJsonException("JSON document specified in the request is incorrect", e);
    }
    if (jsonArr.length() != keys.length) {
        throw new MalformedJsonException("Each key must have corresponding value (JSON document) specified in the request");
    }
    Map<Object, PdxInstance> map = new HashMap<Object, PdxInstance>();
    for (int i = 0; i < keys.length; i++) {
        if (logger.isDebugEnabled()) {
            logger.debug("Updating (put) Json document ({}) having key ({}) in Region ({})", json, keys[i], region);
        }
        try {
            PdxInstance pdxObj = convert(jsonArr.getJSONObject(i).toString());
            map.put(keys[i], pdxObj);
        } catch (JSONException e) {
            throw new MalformedJsonException(String.format("JSON document at index (%1$s) in the request body is incorrect", i), e);
        }
    }
    if (!CollectionUtils.isEmpty(map)) {
        putPdxValues(region, map);
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(toUri(region, StringUtils.arrayToCommaDelimitedString(keys)));
    return new ResponseEntity<String>(headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) PdxInstance(org.apache.geode.pdx.PdxInstance) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) MalformedJsonException(org.apache.geode.rest.internal.web.exception.MalformedJsonException)

Example 3 with MalformedJsonException

use of org.apache.geode.rest.internal.web.exception.MalformedJsonException in project geode by apache.

the class AbstractBaseController method convertJsonArrayIntoPdxCollection.

Collection<PdxInstance> convertJsonArrayIntoPdxCollection(final String jsonArray) {
    JSONArray jsonArr = null;
    try {
        jsonArr = new JSONArray(jsonArray);
        Collection<PdxInstance> pdxInstances = new ArrayList<PdxInstance>();
        for (int index = 0; index < jsonArr.length(); index++) {
            // String element = jsonArr.getJSONObject(i).toString();
            // String element = jsonArr.getString(i);
            Object object = jsonArr.get(index);
            String element = object.toString();
            PdxInstance pi = convert(element);
            pdxInstances.add(pi);
        }
        return pdxInstances;
    } catch (JSONException je) {
        throw new MalformedJsonException("Json document specified in request body is not valid!", je);
    }
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) MalformedJsonException(org.apache.geode.rest.internal.web.exception.MalformedJsonException)

Example 4 with MalformedJsonException

use of org.apache.geode.rest.internal.web.exception.MalformedJsonException in project geode by apache.

the class AbstractBaseController method casValue.

@SuppressWarnings("unchecked")
private <T> T casValue(String regionNamePath, String key, String jsonData) {
    JSONObject jsonObject;
    try {
        jsonObject = new JSONObject(jsonData);
        String oldValue = jsonObject.get("@old").toString();
        String newValue = jsonObject.get("@new").toString();
        return (T) casValue(regionNamePath, key, convert(oldValue), convert(newValue));
    } catch (JSONException je) {
        throw new MalformedJsonException("Json doc specified in request body is invalid!", je);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) MalformedJsonException(org.apache.geode.rest.internal.web.exception.MalformedJsonException)

Aggregations

MalformedJsonException (org.apache.geode.rest.internal.web.exception.MalformedJsonException)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 JSONArray (org.json.JSONArray)3 PdxInstance (org.apache.geode.pdx.PdxInstance)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONTypes (org.apache.geode.rest.internal.web.controllers.support.JSONTypes)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1