Skip to content

Commit

Permalink
Unify the contentType setting method.
Browse files Browse the repository at this point in the history
  • Loading branch information
hubgeter committed Jan 2, 2025
1 parent b2550f4 commit 9131047
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,18 @@ private <T> T introspectAndConvert(final T value) {
return value;
}

public ResponseEntity<String> convertErrorAsJson(HttpStatus status, String errorMessage) {
return ResponseEntity.status(status)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(errorMessage));
}

public ResponseEntity<String> convertErrorAsJson(HttpStatus status, Throwable t) {
return ResponseEntity.status(status)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(t));
}

String convertErrorAsJson(String errorMessage) {
return ("{" + "\"cause\"" + ":" + "\"" + errorMessage + "\"" + "}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ protected String getRestApiVersion() {
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<String> 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());
}

/**
Expand All @@ -84,9 +82,7 @@ public ResponseEntity<String> handle(final RuntimeException e) {
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<String> 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());
}

/**
Expand All @@ -101,9 +97,7 @@ public ResponseEntity<String> handleException(final RuntimeException e) {
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<String> 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);
}

/**
Expand All @@ -119,9 +113,7 @@ public ResponseEntity<String> handleException(final GemfireRestException ge) {
@ResponseBody
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public ResponseEntity<String> 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());
}

/**
Expand All @@ -137,9 +129,7 @@ public ResponseEntity<String> handleException(final DataTypeNotSupportedExceptio
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ResponseEntity<String> 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());
}

/**
Expand All @@ -154,9 +144,7 @@ public ResponseEntity<String> handleException(final HttpRequestMethodNotSupporte
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseEntity<String> handleException(final AccessDeniedException cause) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage());
}

/**
Expand All @@ -170,9 +158,7 @@ public ResponseEntity<String> handleException(final AccessDeniedException cause)
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseEntity<String> handleException(final NotAuthorizedException cause) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.contentType(APPLICATION_JSON_UTF8)
.body(convertErrorAsJson(cause.getMessage()));
return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage());
}

/**
Expand All @@ -186,9 +172,7 @@ public ResponseEntity<String> handleException(final NotAuthorizedException cause
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<String> 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());
}

/**
Expand All @@ -211,9 +195,7 @@ public ResponseEntity<String> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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<Object, Object> valueObjs = getValues(region, keys);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9131047

Please sign in to comment.