Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -19,6 +19,7 @@

import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand Down Expand Up @@ -65,8 +66,8 @@ protected String getRestApiVersion() {
@ExceptionHandler({RegionNotFoundException.class, ResourceNotFoundException.class})
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handle(final RuntimeException e) {
return convertErrorAsJson(e.getMessage());
public ResponseEntity<String> handle(final RuntimeException e) {
return convertErrorAsJson(HttpStatus.NOT_FOUND, e.getMessage());
}

/**
Expand All @@ -80,8 +81,8 @@ public String handle(final RuntimeException e) {
@ExceptionHandler({MalformedJsonException.class})
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleException(final RuntimeException e) {
return convertErrorAsJson(e.getMessage());
public ResponseEntity<String> handleException(final RuntimeException e) {
return convertErrorAsJson(HttpStatus.BAD_REQUEST, e.getMessage());
}

/**
Expand All @@ -95,8 +96,8 @@ public String handleException(final RuntimeException e) {
@ExceptionHandler(GemfireRestException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleException(final GemfireRestException ge) {
return convertErrorAsJson(ge);
public ResponseEntity<String> handleException(final GemfireRestException ge) {
return convertErrorAsJson(HttpStatus.INTERNAL_SERVER_ERROR, ge);
}

/**
Expand All @@ -111,8 +112,8 @@ public String handleException(final GemfireRestException ge) {
@ExceptionHandler(DataTypeNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public String handleException(final DataTypeNotSupportedException tns) {
return convertErrorAsJson(tns.getMessage());
public ResponseEntity<String> handleException(final DataTypeNotSupportedException tns) {
return convertErrorAsJson(HttpStatus.NOT_ACCEPTABLE, tns.getMessage());
}

/**
Expand All @@ -127,8 +128,8 @@ public String handleException(final DataTypeNotSupportedException tns) {
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public String handleException(final HttpRequestMethodNotSupportedException e) {
return convertErrorAsJson(e.getMessage());
public ResponseEntity<String> handleException(final HttpRequestMethodNotSupportedException e) {
return convertErrorAsJson(HttpStatus.METHOD_NOT_ALLOWED, e.getMessage());
}

/**
Expand All @@ -142,8 +143,8 @@ public String handleException(final HttpRequestMethodNotSupportedException e) {
@ExceptionHandler(AccessDeniedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public String handleException(final AccessDeniedException cause) {
return convertErrorAsJson(cause.getMessage());
public ResponseEntity<String> handleException(final AccessDeniedException cause) {
return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage());
}

/**
Expand All @@ -156,8 +157,8 @@ public String handleException(final AccessDeniedException cause) {
@ExceptionHandler(NotAuthorizedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.FORBIDDEN)
public String handleException(final NotAuthorizedException cause) {
return convertErrorAsJson(cause.getMessage());
public ResponseEntity<String> handleException(final NotAuthorizedException cause) {
return convertErrorAsJson(HttpStatus.FORBIDDEN, cause.getMessage());
}

/**
Expand All @@ -170,8 +171,8 @@ public String handleException(final NotAuthorizedException cause) {
@ExceptionHandler(EntityNotFoundException.class)
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleException(final EntityNotFoundException cause) {
return convertErrorAsJson(cause.getMessage());
public ResponseEntity<String> handleException(final EntityNotFoundException cause) {
return convertErrorAsJson(HttpStatus.NOT_FOUND, cause.getMessage());
}

/**
Expand All @@ -185,7 +186,7 @@ public String handleException(final EntityNotFoundException cause) {
@ExceptionHandler(Throwable.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleException(final Throwable cause) {
public ResponseEntity<String> handleException(final Throwable cause) {
final StringWriter stackTraceWriter = new StringWriter();
cause.printStackTrace(new PrintWriter(stackTraceWriter));
final String stackTrace = stackTraceWriter.toString();
Expand All @@ -194,7 +195,7 @@ public String handleException(final Throwable cause) {
logger.debug(stackTrace);
}

return convertErrorAsJson(cause.getMessage());
return convertErrorAsJson(HttpStatus.INTERNAL_SERVER_ERROR, cause.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}

int mapSize = keys.size();
Expand All @@ -210,11 +210,12 @@ 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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}
}

headers.set(HttpHeaders.CONTENT_LOCATION, toUri(region, keyList).toASCIIString());
headers.setContentType(APPLICATION_JSON_UTF8);
return new ResponseEntity<RegionData<?>>(data, headers, HttpStatus.OK);
}

Expand Down Expand Up @@ -248,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 @@ -277,7 +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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}

final Map<Object, Object> valueObjs = getValues(region, keys);
Expand Down Expand Up @@ -365,17 +367,19 @@ 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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}
if (keys.length > 1) {
updateMultipleKeys(region, keys, json);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON_UTF8);
headers.setLocation(toUri(region, StringUtils.arrayToCommaDelimitedString(keys)));
return new ResponseEntity<>(headers, HttpStatus.OK);
} else {
// put case
Object existingValue = updateSingleKey(region, keys[0], json, opValue);
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON_UTF8);
headers.setLocation(toUri(region, keys[0]));
return new ResponseEntity<>(existingValue, headers,
(existingValue == null ? HttpStatus.OK : HttpStatus.CONFLICT));
Expand Down Expand Up @@ -433,7 +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 new ResponseEntity<>(convertErrorAsJson(errorMessage), HttpStatus.BAD_REQUEST);
return convertErrorAsJson(HttpStatus.BAD_REQUEST, errorMessage);
}

if (decodedKeys.length > 1) {
Expand Down
Loading