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 ConstraintViolationException) {
42 response = transform(ConstraintViolationException.class.cast(exception));
43 } else if (exception instanceof JsonMappingException) {
44 response = transform(JsonMappingException.class.cast(exception));
46 response = transform(exception);
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()) );
58 private Response transform(ConstraintViolationException validationException) {
59 LOGGER.error("Transforming ConstraintViolationException to Error Response :",
61 Set<ConstraintViolation<?>> constraintViolationSet = validationException
62 .getConstraintViolations();
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();
73 message = validationException.getMessage();
75 return generateResponse(Status.EXPECTATION_FAILED, new ActivitySpecErrorResponse(Status.EXPECTATION_FAILED,
76 "FIELD_VALIDATION_ERROR_ERR_ID",
77 String.format(message,fieldName)));
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()));
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"));
93 private Response generateResponse(Response.Status status, ActivitySpecErrorResponse
94 activitySpecErrorResponse) {
95 return Response.status(status).entity(activitySpecErrorResponse).type(MediaType
96 .APPLICATION_JSON).build();