summaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java')
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java216
1 files changed, 100 insertions, 116 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java
index 098a89d6ff..0f17bae35c 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java
@@ -16,6 +16,18 @@
package org.openecomp.sdcrests.errors;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Path;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
import org.codehaus.jackson.map.JsonMappingException;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.openecomp.core.utilities.CommonMethods;
@@ -31,131 +43,103 @@ import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
-import javax.validation.ConstraintViolation;
-import javax.validation.ConstraintViolationException;
-import javax.validation.Path;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.ExceptionMapper;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
public class DefaultExceptionMapper implements ExceptionMapper<Exception> {
- private static final String ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE =
- "errorCodesToResponseStatusMapping.json";
- @SuppressWarnings("unchecked")
- private static final Map<String, String> ERROR_CODE_TO_RESPONSE_STATUS =
- FileUtils.readViaInputStream(ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE,
- stream -> JsonUtil.json2Object(stream, Map.class));
-
- private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionMapper
- .class);
-
- @Override
- public Response toResponse(Exception exception) {
- Response response;
- if (exception instanceof CoreException) {
- response = transform(CoreException.class.cast(exception));
- } else if (exception instanceof ConstraintViolationException) {
- response = transform(ConstraintViolationException.class.cast(exception));
-
- } else if (exception instanceof JsonMappingException) {
- response = transform(JsonMappingException.class.cast(exception));
-
- } else {
- response = transform(exception);
+
+ private static final String ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE = "errorCodesToResponseStatusMapping.json";
+ @SuppressWarnings("unchecked")
+ private static final Map<String, String> ERROR_CODE_TO_RESPONSE_STATUS = FileUtils.readViaInputStream(
+ ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE, stream -> JsonUtil.json2Object(stream, Map.class));
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionMapper.class);
+
+ @Override
+ public Response toResponse(Exception exception) {
+ Response response;
+ if (exception instanceof CoreException) {
+ response = transform(CoreException.class.cast(exception));
+ } else if (exception instanceof ConstraintViolationException) {
+ response = transform(ConstraintViolationException.class.cast(exception));
+
+ } else if (exception instanceof JsonMappingException) {
+ response = transform(JsonMappingException.class.cast(exception));
+
+ } else {
+ response = transform(exception);
+ }
+
+ List<Object> contentTypes = new ArrayList<>();
+ contentTypes.add(MediaType.APPLICATION_JSON);
+ response.getMetadata().put("Content-Type", contentTypes);
+ return response;
}
- List<Object> contentTypes = new ArrayList<>();
- contentTypes.add(MediaType.APPLICATION_JSON);
- response.getMetadata().put("Content-Type", contentTypes);
- return response;
- }
-
- private Response transform(CoreException coreException) {
- Response response;
- ErrorCode code = coreException.code();
- LOGGER.error(code.message(), coreException);
-
- if (coreException.code().category().equals(ErrorCategory.APPLICATION)) {
- if (Response.Status.NOT_FOUND.name().equals(ERROR_CODE_TO_RESPONSE_STATUS.get(code.id()))) {
- response = Response
- .status(Response.Status.NOT_FOUND)
- .entity(toEntity(Response.Status.NOT_FOUND, code))
- .build();
- } else if (Response.Status.BAD_REQUEST.name()
- .equals(ERROR_CODE_TO_RESPONSE_STATUS.get(code.id()))) {
- response = Response
- .status(Response.Status.BAD_REQUEST)
- .entity(toEntity(Response.Status.BAD_REQUEST, code))
- .build();
- } else {
- response = Response
- .status(Response.Status.EXPECTATION_FAILED)
- .entity(toEntity(Response.Status.EXPECTATION_FAILED, code))
- .build();
- }
- } else {
- response = Response
- .status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity(toEntity(Response.Status.INTERNAL_SERVER_ERROR, code))
- .build();
+ private Response transform(CoreException coreException) {
+ Response response;
+ ErrorCode code = coreException.code();
+ LOGGER.error(code.message(), coreException);
+
+ if (coreException.code().category().equals(ErrorCategory.APPLICATION)) {
+ if (Response.Status.NOT_FOUND.name().equals(ERROR_CODE_TO_RESPONSE_STATUS.get(code.id()))) {
+ response = Response.status(Response.Status.NOT_FOUND).entity(toEntity(Response.Status.NOT_FOUND, code))
+ .build();
+ } else if (Response.Status.BAD_REQUEST.name().equals(ERROR_CODE_TO_RESPONSE_STATUS.get(code.id()))) {
+ response =
+ Response.status(Response.Status.BAD_REQUEST).entity(toEntity(Response.Status.BAD_REQUEST, code))
+ .build();
+ } else {
+ response = Response.status(Response.Status.EXPECTATION_FAILED)
+ .entity(toEntity(Response.Status.EXPECTATION_FAILED, code)).build();
+ }
+ } else {
+ response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .entity(toEntity(Response.Status.INTERNAL_SERVER_ERROR, code)).build();
+ }
+
+
+ return response;
}
+ private Response transform(ConstraintViolationException validationException) {
+ Set<ConstraintViolation<?>> constraintViolationSet = validationException.getConstraintViolations();
+ String message;
- return response;
- }
+ String fieldName = null;
+ if (!CommonMethods.isEmpty(constraintViolationSet)) {
+ // getting the first violation message for the output response.
+ ConstraintViolation<?> constraintViolation = constraintViolationSet.iterator().next();
+ message = constraintViolation.getMessage();
+ fieldName = getFieldName(constraintViolation.getPropertyPath());
- private Response transform(ConstraintViolationException validationException) {
- Set<ConstraintViolation<?>> constraintViolationSet =
- validationException.getConstraintViolations();
- String message;
+ } else {
+ message = validationException.getMessage();
+ }
- String fieldName = null;
- if (!CommonMethods.isEmpty(constraintViolationSet)) {
- // getting the first violation message for the output response.
- ConstraintViolation<?> constraintViolation = constraintViolationSet.iterator().next();
- message = constraintViolation.getMessage();
- fieldName = getFieldName(constraintViolation.getPropertyPath());
+ ErrorCode validationErrorCode = new ValidationErrorBuilder(message, fieldName).build();
- } else {
- message = validationException.getMessage();
+ LOGGER.error(validationErrorCode.message(), validationException);
+ return Response.status(Response.Status.EXPECTATION_FAILED) //error 417
+ .entity(toEntity(Response.Status.EXPECTATION_FAILED, validationErrorCode)).build();
}
- ErrorCode validationErrorCode = new ValidationErrorBuilder(message, fieldName).build();
-
- LOGGER.error(validationErrorCode.message(), validationException);
- return Response
- .status(Response.Status.EXPECTATION_FAILED) //error 417
- .entity(toEntity(Response.Status.EXPECTATION_FAILED, validationErrorCode))
- .build();
- }
-
- private Response transform(JsonMappingException jsonMappingException) {
- ErrorCode jsonMappingErrorCode = new JsonMappingErrorBuilder().build();
- LOGGER.error(jsonMappingErrorCode.message(), jsonMappingException);
- return Response
- .status(Response.Status.EXPECTATION_FAILED) //error 417
- .entity(toEntity(Response.Status.EXPECTATION_FAILED, jsonMappingErrorCode))
- .build();
- }
-
- private Response transform(Exception exception) {
- ErrorCode errorCode = new GeneralErrorBuilder().build();
- LOGGER.error(errorCode.message(), exception);
- return Response
- .status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity(toEntity(Response.Status.INTERNAL_SERVER_ERROR, errorCode))
- .build();
- }
-
- private String getFieldName(Path propertyPath) {
- return ((PathImpl) propertyPath).getLeafNode().toString();
- }
-
- private Object toEntity(Response.Status status, ErrorCode code) {
- return new ErrorCodeAndMessage(status, code);
- }
+ private Response transform(JsonMappingException jsonMappingException) {
+ ErrorCode jsonMappingErrorCode = new JsonMappingErrorBuilder().build();
+ LOGGER.error(jsonMappingErrorCode.message(), jsonMappingException);
+ return Response.status(Response.Status.EXPECTATION_FAILED) //error 417
+ .entity(toEntity(Response.Status.EXPECTATION_FAILED, jsonMappingErrorCode)).build();
+ }
+
+ private Response transform(Exception exception) {
+ ErrorCode errorCode = new GeneralErrorBuilder().build();
+ LOGGER.error(errorCode.message(), exception);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .entity(toEntity(Response.Status.INTERNAL_SERVER_ERROR, errorCode)).build();
+ }
+
+ private String getFieldName(Path propertyPath) {
+ return ((PathImpl) propertyPath).getLeafNode().toString();
+ }
+
+ private Object toEntity(Response.Status status, ErrorCode code) {
+ return new ErrorCodeAndMessage(status, code);
+ }
}