a059434709c73d0d96f26d6a076885afb214c35f
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openecomp.sdc.common.errors;
17
18 import com.fasterxml.jackson.databind.JsonMappingException;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import javax.validation.ConstraintViolation;
24 import javax.validation.ConstraintViolationException;
25 import javax.validation.Path;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.Status;
29 import javax.ws.rs.ext.ExceptionMapper;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.hibernate.validator.internal.engine.path.PathImpl;
32 import org.openecomp.core.utilities.file.FileUtils;
33 import org.openecomp.core.utilities.json.JsonUtil;
34 import org.openecomp.sdc.logging.api.Logger;
35 import org.openecomp.sdc.logging.api.LoggerFactory;
36
37 public class DefaultExceptionMapper implements ExceptionMapper<Exception> {
38
39     private static final String ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE = "errorCodesToResponseStatusMapping.json";
40     @SuppressWarnings("unchecked")
41     private static final Map<String, String> ERROR_CODE_TO_RESPONSE_STATUS = FileUtils
42         .readViaInputStream(ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE, stream -> JsonUtil.json2Object(stream, Map.class));
43     private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionMapper.class);
44
45     @Override
46     public Response toResponse(Exception exception) {
47         Response response;
48         if (exception instanceof CoreException) {
49             response = transform((CoreException) exception);
50         } else if (exception instanceof ConstraintViolationException) {
51             response = transform((ConstraintViolationException) exception);
52         } else if (exception instanceof JsonMappingException) {
53             response = transform((JsonMappingException) exception);
54         } else {
55             response = transform(exception);
56         }
57         List<Object> contentTypes = new ArrayList<>();
58         contentTypes.add(MediaType.APPLICATION_JSON);
59         response.getMetadata().put("Content-Type", contentTypes);
60         return response;
61     }
62
63     private Response transform(final CoreException coreException) {
64         final ErrorCode code = coreException.code();
65         LOGGER.error(code.message(), coreException);
66         if (coreException.code().category().equals(ErrorCategory.APPLICATION)) {
67             final Status errorStatus = Status.valueOf(ERROR_CODE_TO_RESPONSE_STATUS.get(code.id()));
68             if (List.of(Status.BAD_REQUEST, Status.FORBIDDEN, Status.NOT_FOUND, Status.INTERNAL_SERVER_ERROR).contains(errorStatus)) {
69                 return buildResponse(errorStatus, code);
70             }
71             return buildResponse(Status.EXPECTATION_FAILED, code);
72         }
73         return buildResponse(Status.INTERNAL_SERVER_ERROR, code);
74     }
75
76     private Response buildResponse(final Status status, final ErrorCode code) {
77         return Response.status(status).entity(toEntity(status, code)).build();
78     }
79
80     private Response transform(ConstraintViolationException validationException) {
81         Set<ConstraintViolation<?>> constraintViolationSet = validationException.getConstraintViolations();
82         String message;
83         String fieldName = null;
84         if (CollectionUtils.isEmpty(constraintViolationSet)) {
85             message = validationException.getMessage();
86         } else {
87             // getting the first violation message for the output response.
88             ConstraintViolation<?> constraintViolation = constraintViolationSet.iterator().next();
89             message = constraintViolation.getMessage();
90             fieldName = getFieldName(constraintViolation.getPropertyPath());
91         }
92         ErrorCode validationErrorCode = new ValidationErrorBuilder(message, fieldName).build();
93         LOGGER.error(validationErrorCode.message(), validationException);
94         return buildResponse(Status.EXPECTATION_FAILED, validationErrorCode);
95     }
96
97     private Response transform(JsonMappingException jsonMappingException) {
98         ErrorCode jsonMappingErrorCode = new JsonMappingErrorBuilder().build();
99         LOGGER.error(jsonMappingErrorCode.message(), jsonMappingException);
100         return buildResponse(Status.EXPECTATION_FAILED, jsonMappingErrorCode);
101     }
102
103     private Response transform(Exception exception) {
104         ErrorCode errorCode = new GeneralErrorBuilder().build();
105         LOGGER.error(errorCode.message(), exception);
106         return buildResponse(Status.INTERNAL_SERVER_ERROR, errorCode);
107     }
108
109     private String getFieldName(Path propertyPath) {
110         return ((PathImpl) propertyPath).getLeafNode().toString();
111     }
112
113     private Object toEntity(final Status status, final ErrorCode code) {
114         return new ErrorCodeAndMessage(status, code);
115     }
116 }