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