950988a4b179c4c1cdf01dfc36a049535507bd9f
[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 ActivitySpecNotFoundException) {
42       response = transform(ActivitySpecNotFoundException.class.cast(exception));
43     } else if (exception instanceof ConstraintViolationException) {
44       response = transform(ConstraintViolationException.class.cast(exception));
45     } else if (exception instanceof JsonMappingException) {
46       response = transform(JsonMappingException.class.cast(exception));
47     } else {
48       response = transform(exception);
49     }
50
51     return response;
52   }
53
54   private Response transform(ActivitySpecNotFoundException notFoundException) {
55     LOGGER.error("Transforming ActivitySpecNotFoundException to Error Response  :", notFoundException);
56     return generateResponse(Status.NOT_FOUND, new ActivitySpecErrorResponse(notFoundException.getMessage()));
57   }
58
59   private Response transform(CoreException coreException) {
60     LOGGER.error("Transforming CoreException to Error Response  :", coreException);
61     return generateResponse(Status.BAD_REQUEST, new ActivitySpecErrorResponse(coreException.getMessage()));
62   }
63
64   private Response transform(ConstraintViolationException validationException) {
65     LOGGER.error("Transforming ConstraintViolationException to Error Response :",
66         validationException);
67     Set<ConstraintViolation<?>> constraintViolationSet = validationException
68         .getConstraintViolations();
69     String message;
70
71     String fieldName = null;
72     if (constraintViolationSet != null) {
73       // getting the first violation message for the output response.
74       ConstraintViolation<?> constraintViolation = constraintViolationSet.iterator().next();
75       message = constraintViolation.getMessage();
76       fieldName = ((PathImpl) constraintViolation.getPropertyPath()).getLeafNode().toString();
77
78     } else {
79       message = validationException.getMessage();
80     }
81     return generateResponse(Status.BAD_REQUEST, new ActivitySpecErrorResponse(String.format(message,fieldName)));
82   }
83
84   private Response transform(Exception exception) {
85     LOGGER.error("Transforming Exception to Error Response " + exception);
86     return generateResponse(Status.INTERNAL_SERVER_ERROR, new ActivitySpecErrorResponse(exception.getMessage()));
87   }
88
89   private Response transform(JsonMappingException jsonMappingException) {
90     LOGGER.error("Transforming JsonMappingException to Error Response " + jsonMappingException);
91     return generateResponse(Status.BAD_REQUEST, new ActivitySpecErrorResponse("Invalid Json Input"));
92   }
93
94   private Response generateResponse(Response.Status status, ActivitySpecErrorResponse
95       activitySpecErrorResponse) {
96     return Response.status(status).entity(activitySpecErrorResponse).type(MediaType
97         .APPLICATION_JSON).build();
98   }
99 }