e95953bd56073175106b3c49d17588d30cc57a70
[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
17 package org.openecomp.activityspec.errors;
18
19 import java.util.Set;
20 import javax.validation.ConstraintViolation;
21 import javax.validation.ConstraintViolationException;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.Response.Status;
25 import javax.ws.rs.ext.ExceptionMapper;
26 import org.codehaus.jackson.map.JsonMappingException;
27 import org.hibernate.validator.internal.engine.path.PathImpl;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31
32 public class DefaultExceptionMapper implements ExceptionMapper<Exception> {
33
34   private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionMapper.class);
35
36   @Override
37   public Response toResponse(Exception exception) {
38     Response response;
39     if (exception instanceof CoreException) {
40       response = transform(CoreException.class.cast(exception));
41     } else if (exception instanceof ConstraintViolationException) {
42       response = transform(ConstraintViolationException.class.cast(exception));
43     } else if (exception instanceof JsonMappingException) {
44       response = transform(JsonMappingException.class.cast(exception));
45     } else {
46       response = transform(exception);
47     }
48
49     return response;
50   }
51
52   private Response transform(CoreException coreException) {
53     LOGGER.error("Transforming CoreException to Error Response  :", coreException);
54     return generateResponse(Status.EXPECTATION_FAILED, new ActivitySpecErrorResponse(Status.EXPECTATION_FAILED, coreException.code().id(),
55         coreException.getMessage()) );
56   }
57
58   private Response transform(ConstraintViolationException validationException) {
59     LOGGER.error("Transforming ConstraintViolationException to Error Response :",
60         validationException);
61     Set<ConstraintViolation<?>> constraintViolationSet = validationException
62         .getConstraintViolations();
63     String message;
64
65     String fieldName = null;
66     if (constraintViolationSet != null) {
67       // getting the first violation message for the output response.
68       ConstraintViolation<?> constraintViolation = constraintViolationSet.iterator().next();
69       message = constraintViolation.getMessage();
70       fieldName = ((PathImpl) constraintViolation.getPropertyPath()).getLeafNode().toString();
71
72     } else {
73       message = validationException.getMessage();
74     }
75     return generateResponse(Status.EXPECTATION_FAILED, new ActivitySpecErrorResponse(Status.EXPECTATION_FAILED,
76         "FIELD_VALIDATION_ERROR_ERR_ID",
77         String.format(message,fieldName)));
78     }
79
80   private Response transform(Exception exception) {
81     LOGGER.error("Transforming Exception to Error Response " + exception);
82     return generateResponse(Status.INTERNAL_SERVER_ERROR, new ActivitySpecErrorResponse(Status.INTERNAL_SERVER_ERROR,
83         "GENERAL_ERROR_REST_ID",
84         "An error has occurred: " + exception.getMessage()));
85   }
86
87   private Response transform(JsonMappingException jsonMappingException) {
88     LOGGER.error("Transforming JsonMappingException to Error Response " + jsonMappingException);
89     return generateResponse(Status.EXPECTATION_FAILED, new ActivitySpecErrorResponse(Status.EXPECTATION_FAILED,"JSON_MAPPING_ERROR_ERR_ID",
90         "Invalid Json Input"));
91   }
92
93   private Response generateResponse(Response.Status status, ActivitySpecErrorResponse
94       activitySpecErrorResponse) {
95     return Response.status(status).entity(activitySpecErrorResponse).type(MediaType
96         .APPLICATION_JSON).build();
97   }
98 }