2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdcrests.errors;
19 import java.util.ArrayList;
20 import java.util.List;
24 import javax.validation.ConstraintViolation;
25 import javax.validation.ConstraintViolationException;
26 import javax.validation.Path;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.ext.ExceptionMapper;
31 import org.codehaus.jackson.map.JsonMappingException;
32 import org.hibernate.validator.internal.engine.path.PathImpl;
33 import org.openecomp.core.utilities.CommonMethods;
34 import org.openecomp.core.utilities.file.FileUtils;
35 import org.openecomp.core.utilities.json.JsonUtil;
36 import org.openecomp.sdc.common.errors.*;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
40 public class DefaultExceptionMapper implements ExceptionMapper<Exception> {
42 private static final String ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE = "errorCodesToResponseStatusMapping.json";
43 @SuppressWarnings("unchecked")
44 private static final Map<String, String> ERROR_CODE_TO_RESPONSE_STATUS = FileUtils.readViaInputStream(
45 ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE, stream -> JsonUtil.json2Object(stream, Map.class));
47 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionMapper.class);
50 public Response toResponse(Exception exception) {
52 if (exception instanceof CoreException) {
53 response = transform(CoreException.class.cast(exception));
54 } else if (exception instanceof ConstraintViolationException) {
55 response = transform(ConstraintViolationException.class.cast(exception));
57 } else if (exception instanceof JsonMappingException) {
58 response = transform(JsonMappingException.class.cast(exception));
61 response = transform(exception);
64 List<Object> contentTypes = new ArrayList<>();
65 contentTypes.add(MediaType.APPLICATION_JSON);
66 response.getMetadata().put("Content-Type", contentTypes);
70 private Response transform(CoreException coreException) {
72 ErrorCode code = coreException.code();
73 LOGGER.error(code.message(), coreException);
75 if (coreException.code().category().equals(ErrorCategory.APPLICATION)) {
76 if (Response.Status.NOT_FOUND.name().equals(ERROR_CODE_TO_RESPONSE_STATUS.get(code.id()))) {
77 response = Response.status(Response.Status.NOT_FOUND).entity(toEntity(Response.Status.NOT_FOUND, code))
79 } else if (Response.Status.BAD_REQUEST.name().equals(ERROR_CODE_TO_RESPONSE_STATUS.get(code.id()))) {
81 Response.status(Response.Status.BAD_REQUEST).entity(toEntity(Response.Status.BAD_REQUEST, code))
84 response = Response.status(Response.Status.EXPECTATION_FAILED)
85 .entity(toEntity(Response.Status.EXPECTATION_FAILED, code)).build();
88 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
89 .entity(toEntity(Response.Status.INTERNAL_SERVER_ERROR, code)).build();
96 private Response transform(ConstraintViolationException validationException) {
97 Set<ConstraintViolation<?>> constraintViolationSet = validationException.getConstraintViolations();
100 String fieldName = null;
101 if (!CommonMethods.isEmpty(constraintViolationSet)) {
102 // getting the first violation message for the output response.
103 ConstraintViolation<?> constraintViolation = constraintViolationSet.iterator().next();
104 message = constraintViolation.getMessage();
105 fieldName = getFieldName(constraintViolation.getPropertyPath());
108 message = validationException.getMessage();
111 ErrorCode validationErrorCode = new ValidationErrorBuilder(message, fieldName).build();
113 LOGGER.error(validationErrorCode.message(), validationException);
114 return Response.status(Response.Status.EXPECTATION_FAILED) //error 417
115 .entity(toEntity(Response.Status.EXPECTATION_FAILED, validationErrorCode)).build();
118 private Response transform(JsonMappingException jsonMappingException) {
119 ErrorCode jsonMappingErrorCode = new JsonMappingErrorBuilder().build();
120 LOGGER.error(jsonMappingErrorCode.message(), jsonMappingException);
121 return Response.status(Response.Status.EXPECTATION_FAILED) //error 417
122 .entity(toEntity(Response.Status.EXPECTATION_FAILED, jsonMappingErrorCode)).build();
125 private Response transform(Exception exception) {
126 ErrorCode errorCode = new GeneralErrorBuilder().build();
127 LOGGER.error(errorCode.message(), exception);
128 return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
129 .entity(toEntity(Response.Status.INTERNAL_SERVER_ERROR, errorCode)).build();
132 private String getFieldName(Path propertyPath) {
133 return ((PathImpl) propertyPath).getLeafNode().toString();
136 private Object toEntity(Response.Status status, ErrorCode code) {
137 return new ErrorCodeAndMessage(status, code);