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.onap.sdc.activityspec.errors;
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;
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;
33 public class DefaultExceptionMapper implements ExceptionMapper<Exception> {
35 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionMapper.class);
38 public Response toResponse(Exception exception) {
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));
49 response = transform(exception);
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()));
60 private Response transform(CoreException coreException) {
61 LOGGER.error(coreException.getMessage(), coreException);
62 return generateResponse(Status.BAD_REQUEST, new ActivitySpecErrorResponse(coreException.getMessage()));
65 private Response transform(ConstraintViolationException validationException) {
66 LOGGER.error("ConstraintViolationException Occurred :", validationException);
67 Set<ConstraintViolation<?>> constraintViolationSet = validationException.getConstraintViolations();
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();
78 message = validationException.getMessage();
80 return generateResponse(Status.BAD_REQUEST, new ActivitySpecErrorResponse(String.format(message, fieldName)));
83 private Response transform(Exception exception) {
84 LOGGER.error("Error occurred :", exception);
85 return generateResponse(Status.INTERNAL_SERVER_ERROR, new ActivitySpecErrorResponse(exception.getMessage()));
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"));
93 private Response generateResponse(Response.Status status, ActivitySpecErrorResponse activitySpecErrorResponse) {
94 return Response.status(status).entity(activitySpecErrorResponse).type(MediaType.APPLICATION_JSON).build();