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.activityspec.errors;
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;
32 public class DefaultExceptionMapper implements ExceptionMapper<Exception> {
34 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionMapper.class);
37 public Response toResponse(Exception exception) {
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));
48 response = transform(exception);
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()));
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()));
64 private Response transform(ConstraintViolationException validationException) {
65 LOGGER.error("Transforming ConstraintViolationException to Error Response :",
67 Set<ConstraintViolation<?>> constraintViolationSet = validationException
68 .getConstraintViolations();
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();
79 message = validationException.getMessage();
81 return generateResponse(Status.BAD_REQUEST, new ActivitySpecErrorResponse(String.format(message,fieldName)));
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()));
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"));
94 private Response generateResponse(Response.Status status, ActivitySpecErrorResponse
95 activitySpecErrorResponse) {
96 return Response.status(status).entity(activitySpecErrorResponse).type(MediaType
97 .APPLICATION_JSON).build();