Search in sources :

Example 1 with JSONTypes

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!");
    }
}
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 JSONTypes

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));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) PdxInstance(org.apache.geode.pdx.PdxInstance) UpdateOp(org.apache.geode.rest.internal.web.controllers.support.UpdateOp) JSONTypes(org.apache.geode.rest.internal.web.controllers.support.JSONTypes)

Example 3 with JSONTypes

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);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) RegionEntryData(org.apache.geode.rest.internal.web.controllers.support.RegionEntryData) JSONTypes(org.apache.geode.rest.internal.web.controllers.support.JSONTypes) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

JSONTypes (org.apache.geode.rest.internal.web.controllers.support.JSONTypes)3 HttpHeaders (org.springframework.http.HttpHeaders)2 ResponseEntity (org.springframework.http.ResponseEntity)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 PdxInstance (org.apache.geode.pdx.PdxInstance)1 RegionEntryData (org.apache.geode.rest.internal.web.controllers.support.RegionEntryData)1 UpdateOp (org.apache.geode.rest.internal.web.controllers.support.UpdateOp)1 MalformedJsonException (org.apache.geode.rest.internal.web.exception.MalformedJsonException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1