diff --git a/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/AbstractBaseController.java b/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/AbstractBaseController.java index 8a0d5b6461a..d1ab4ecc604 100644 --- a/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/AbstractBaseController.java +++ b/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/AbstractBaseController.java @@ -765,6 +765,18 @@ private T introspectAndConvert(final T value) { return value; } + public ResponseEntity convertErrorAsJson(HttpStatus status, String errorMessage) { + return ResponseEntity.status(status) + .contentType(APPLICATION_JSON_UTF8) + .body(convertErrorAsJson(errorMessage)); + } + + public ResponseEntity convertErrorAsJson(HttpStatus status, Throwable t) { + return ResponseEntity.status(status) + .contentType(APPLICATION_JSON_UTF8) + .body(convertErrorAsJson(t)); + } + String convertErrorAsJson(String errorMessage) { return ("{" + "\"cause\"" + ":" + "\"" + errorMessage + "\"" + "}"); } diff --git a/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/BaseControllerAdvice.java b/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/BaseControllerAdvice.java index fb3ba9228b2..a7dcbcf8b4c 100644 --- a/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/BaseControllerAdvice.java +++ b/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/BaseControllerAdvice.java @@ -67,9 +67,7 @@ protected String getRestApiVersion() { @ResponseBody @ResponseStatus(HttpStatus.NOT_FOUND) public ResponseEntity handle(final RuntimeException e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(e.getMessage())); + return convertErrorAsJson(HttpStatus.NOT_FOUND, e.getMessage()); } /** @@ -84,9 +82,7 @@ public ResponseEntity handle(final RuntimeException e) { @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseEntity handleException(final RuntimeException e) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(e.getMessage())); + return convertErrorAsJson(HttpStatus.BAD_REQUEST, e.getMessage()); } /** @@ -101,9 +97,7 @@ public ResponseEntity handleException(final RuntimeException e) { @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ResponseEntity handleException(final GemfireRestException ge) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(ge)); + return convertErrorAsJson(HttpStatus.INTERNAL_SERVER_ERROR, ge); } /** @@ -119,9 +113,7 @@ public ResponseEntity handleException(final GemfireRestException ge) { @ResponseBody @ResponseStatus(HttpStatus.NOT_ACCEPTABLE) public ResponseEntity handleException(final DataTypeNotSupportedException tns) { - return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(tns.getMessage())); + return convertErrorAsJson(HttpStatus.NOT_ACCEPTABLE, tns.getMessage()); } /** @@ -137,9 +129,7 @@ public ResponseEntity handleException(final DataTypeNotSupportedExceptio @ResponseBody @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) public ResponseEntity handleException(final HttpRequestMethodNotSupportedException e) { - return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(e.getMessage())); + return convertErrorAsJson(HttpStatus.METHOD_NOT_ALLOWED, e.getMessage()); } /** @@ -154,9 +144,7 @@ public ResponseEntity handleException(final HttpRequestMethodNotSupporte @ResponseBody @ResponseStatus(HttpStatus.FORBIDDEN) public ResponseEntity handleException(final AccessDeniedException cause) { - return ResponseEntity.status(HttpStatus.FORBIDDEN) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(cause.getMessage())); + return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage()); } /** @@ -170,9 +158,7 @@ public ResponseEntity handleException(final AccessDeniedException cause) @ResponseBody @ResponseStatus(HttpStatus.FORBIDDEN) public ResponseEntity handleException(final NotAuthorizedException cause) { - return ResponseEntity.status(HttpStatus.FORBIDDEN) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(cause.getMessage())); + return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage()); } /** @@ -186,9 +172,7 @@ public ResponseEntity handleException(final NotAuthorizedException cause @ResponseBody @ResponseStatus(HttpStatus.NOT_FOUND) public ResponseEntity handleException(final EntityNotFoundException cause) { - return ResponseEntity.status(HttpStatus.NOT_FOUND) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(cause.getMessage())); + return convertErrorAsJson(HttpStatus.NOT_FOUND, cause.getMessage()); } /** @@ -211,9 +195,7 @@ public ResponseEntity handleException(final Throwable cause) { logger.debug(stackTrace); } - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(cause.getMessage())); + return convertErrorAsJson(HttpStatus.INTERNAL_SERVER_ERROR, cause.getMessage()); } } diff --git a/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/PdxBasedCrudController.java b/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/PdxBasedCrudController.java index 42587d72bb4..a153494d67b 100644 --- a/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/PdxBasedCrudController.java +++ b/geode-web-api/src/main/java/org/apache/geode/rest/internal/web/controllers/PdxBasedCrudController.java @@ -195,9 +195,7 @@ private ResponseEntity getAllRegionData(String region, String limit) { if (maxLimit < 0) { String errorMessage = String.format("Negative limit param (%1$s) is not valid!", maxLimit); - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(errorMessage)); + return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage); } int mapSize = keys.size(); @@ -212,9 +210,7 @@ private ResponseEntity getAllRegionData(String region, String limit) { // limit param is not specified in proper format. set the HTTPHeader // for BAD_REQUEST String errorMessage = String.format("limit param (%1$s) is not valid!", limit); - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(errorMessage)); + return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage); } } @@ -253,6 +249,7 @@ private ResponseEntity getRegionKeys(String region, String ignoreMissingKey, logger.debug("Reading data for keys ({}) in Region ({})", ArrayUtils.toString(keys), region); securityService.authorize("READ", region, keys); final HttpHeaders headers = new HttpHeaders(); + headers.setContentType(APPLICATION_JSON_UTF8); if (keys.length == 1) { /* GET op on single key */ Object value = getValue(region, keys[0]); @@ -282,9 +279,7 @@ private ResponseEntity getRegionKeys(String region, String ignoreMissingKey, String errorMessage = String.format( "ignoreMissingKey param (%1$s) is not valid. valid usage is ignoreMissingKey=true!", ignoreMissingKey); - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(errorMessage)); + return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage); } final Map valueObjs = getValues(region, keys); @@ -372,9 +367,7 @@ public ResponseEntity update(@PathVariable("region") String region, String errorMessage = String.format( "The op parameter (%1$s) is not valid. Valid values are PUT, REPLACE, or CAS.", opValue); - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(errorMessage)); + return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage); } if (keys.length > 1) { updateMultipleKeys(region, keys, json); @@ -444,9 +437,7 @@ public ResponseEntity updateKeys(@PathVariable("region") final String encoded String errorMessage = String.format( "The op parameter (%1$s) is not valid. Valid values are PUT, CREATE, REPLACE, or CAS.", opValue); - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .contentType(APPLICATION_JSON_UTF8) - .body(convertErrorAsJson(errorMessage)); + return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage); } if (decodedKeys.length > 1) {