use of org.apache.geode.rest.internal.web.controllers.support.JSONTypes 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!");
}
}
use of org.apache.geode.rest.internal.web.controllers.support.JSONTypes in project geode by apache.
the class AbstractBaseController method updateSingleKey.
ResponseEntity<String> updateSingleKey(final String region, final String key, final String json, final String opValue) {
final JSONTypes jsonType = validateJsonAndFindType(json);
final UpdateOp op = UpdateOp.valueOf(opValue.toUpperCase());
String existingValue = null;
switch(op) {
case CAS:
PdxInstance existingPdxObj = casValue(region, key, json);
existingValue = convert(existingPdxObj);
break;
case REPLACE:
replaceValue(region, key, convert(json));
break;
case PUT:
default:
if (JSONTypes.JSON_ARRAY.equals(jsonType)) {
putValue(region, key, convertJsonArrayIntoPdxCollection(json));
// putValue(region, key, convertJsonIntoPdxCollection(json));
} else {
putValue(region, key, convert(json));
}
}
final HttpHeaders headers = new HttpHeaders();
headers.setLocation(toUri(region, key));
return new ResponseEntity<String>(existingValue, headers, (existingValue == null ? HttpStatus.OK : HttpStatus.CONFLICT));
}
use of org.apache.geode.rest.internal.web.controllers.support.JSONTypes in project geode by apache.
the class PdxBasedCrudController method create.
/**
* Creating entry into the region
*
* @param region region name where data will be created
* @param key gemfire region key
* @param json JSON document that is stored against the key
* @return JSON document
*/
@RequestMapping(method = RequestMethod.POST, value = "/{region}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE })
@ApiOperation(value = "create entry", notes = "Create (put-if-absent) data in region", response = void.class)
@ApiResponses({ @ApiResponse(code = 201, message = "Created."), @ApiResponse(code = 400, message = "Data specified (JSON doc) in the request body is invalid."), @ApiResponse(code = 401, message = "Invalid Username or Password."), @ApiResponse(code = 403, message = "Insufficient privileges for operation."), @ApiResponse(code = 404, message = "Region does not exist."), @ApiResponse(code = 409, message = "Key already exist in region."), @ApiResponse(code = 500, message = "GemFire throws an error or exception.") })
@PreAuthorize("@securityService.authorize('DATA', 'WRITE', #region)")
public ResponseEntity<?> create(@PathVariable("region") String region, @RequestParam(value = "key", required = false) String key, @RequestBody final String json) {
key = generateKey(key);
logger.debug("Posting (creating/putIfAbsent) JSON document ({}) to Region ({}) with Key ({})...", json, region, key);
region = decode(region);
Object existingPdxObj = null;
// Check whether the user has supplied single JSON doc or Array of JSON docs
final JSONTypes jsonType = validateJsonAndFindType(json);
if (JSONTypes.JSON_ARRAY.equals(jsonType)) {
existingPdxObj = postValue(region, key, convertJsonArrayIntoPdxCollection(json));
} else {
existingPdxObj = postValue(region, key, convert(json));
}
final HttpHeaders headers = new HttpHeaders();
headers.setLocation(toUri(region, key));
if (existingPdxObj != null) {
final RegionEntryData<Object> data = new RegionEntryData<>(region);
data.add(existingPdxObj);
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<RegionEntryData<?>>(data, headers, HttpStatus.CONFLICT);
} else {
return new ResponseEntity<String>(headers, HttpStatus.CREATED);
}
}
Aggregations