aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/api
diff options
context:
space:
mode:
authorvasraz <vasyl.razinkov@est.tech>2022-10-14 11:44:03 +0100
committerVasyl Razinkov <vasyl.razinkov@est.tech>2022-10-14 15:25:20 +0000
commitb75fe3c7ce231c86cd4c6d052da453d02809c8f9 (patch)
tree726956d6771e12c1569979891098aab0c9ac910e /openecomp-be/api
parentfc768c6cea75cc8aba3233780b9f31c73e787c99 (diff)
Move DefaultExceptionMapper to be used widely
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech> Change-Id: I759b023cc37fa8c5eed8b01210bed4e91edb57cb Issue-ID: SDC-4189
Diffstat (limited to 'openecomp-be/api')
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml2
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java123
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json30
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java85
4 files changed, 1 insertions, 239 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml b/openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml
index 8c005f0169..9c2aa51a28 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml
@@ -90,7 +90,7 @@
<jaxrs:providers>
<ref bean="jsonProvider"/>
<bean class="org.openecomp.sdc.action.errors.ActionExceptionMapper"/>
- <bean class="org.openecomp.sdcrests.errors.DefaultExceptionMapper"/>
+ <bean class="org.openecomp.sdc.common.errors.DefaultExceptionMapper"/>
<bean class="org.openecomp.sdcrests.errors.ZusammenExceptionMapper"/>
<bean class="org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter">
<property name="requestIdHeaders" value="X-ONAP-RequestID,X-RequestID,X-TransactionId,X-ECOMP-RequestID"/>
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java
deleted file mode 100644
index 22d6d90d8a..0000000000
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Copyright © 2016-2018 European Support Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openecomp.sdcrests.errors;
-
-import com.fasterxml.jackson.databind.JsonMappingException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import javax.validation.ConstraintViolation;
-import javax.validation.ConstraintViolationException;
-import javax.validation.Path;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.Status;
-import javax.ws.rs.ext.ExceptionMapper;
-import org.apache.commons.collections4.CollectionUtils;
-import org.hibernate.validator.internal.engine.path.PathImpl;
-import org.openecomp.core.utilities.file.FileUtils;
-import org.openecomp.core.utilities.json.JsonUtil;
-import org.openecomp.sdc.common.errors.CoreException;
-import org.openecomp.sdc.common.errors.ErrorCategory;
-import org.openecomp.sdc.common.errors.ErrorCode;
-import org.openecomp.sdc.common.errors.ErrorCodeAndMessage;
-import org.openecomp.sdc.common.errors.GeneralErrorBuilder;
-import org.openecomp.sdc.common.errors.JsonMappingErrorBuilder;
-import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
-import org.openecomp.sdc.logging.api.Logger;
-import org.openecomp.sdc.logging.api.LoggerFactory;
-
-public class DefaultExceptionMapper implements ExceptionMapper<Exception> {
-
- private static final String ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE = "errorCodesToResponseStatusMapping.json";
- @SuppressWarnings("unchecked")
- private static final Map<String, String> ERROR_CODE_TO_RESPONSE_STATUS = FileUtils
- .readViaInputStream(ERROR_CODES_TO_RESPONSE_STATUS_MAPPING_FILE, stream -> JsonUtil.json2Object(stream, Map.class));
- private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionMapper.class);
-
- @Override
- public Response toResponse(Exception exception) {
- Response response;
- if (exception instanceof CoreException) {
- response = transform((CoreException) exception);
- } else if (exception instanceof ConstraintViolationException) {
- response = transform((ConstraintViolationException) exception);
- } else if (exception instanceof JsonMappingException) {
- response = transform((JsonMappingException) exception);
- } else {
- response = transform(exception);
- }
- List<Object> contentTypes = new ArrayList<>();
- contentTypes.add(MediaType.APPLICATION_JSON);
- response.getMetadata().put("Content-Type", contentTypes);
- return response;
- }
-
- private Response transform(final CoreException coreException) {
- final ErrorCode code = coreException.code();
- LOGGER.error(code.message(), coreException);
- if (coreException.code().category().equals(ErrorCategory.APPLICATION)) {
- final Status errorStatus = Status.valueOf(ERROR_CODE_TO_RESPONSE_STATUS.get(code.id()));
- if (List.of(Status.BAD_REQUEST, Status.FORBIDDEN, Status.NOT_FOUND, Status.INTERNAL_SERVER_ERROR).contains(errorStatus)) {
- return buildResponse(errorStatus, code);
- }
- return buildResponse(Status.EXPECTATION_FAILED, code);
- }
- return buildResponse(Status.INTERNAL_SERVER_ERROR, code);
- }
-
- private Response buildResponse(final Status status, final ErrorCode code) {
- return Response.status(status).entity(toEntity(status, code)).build();
- }
-
- private Response transform(ConstraintViolationException validationException) {
- Set<ConstraintViolation<?>> constraintViolationSet = validationException.getConstraintViolations();
- String message;
- String fieldName = null;
- if (CollectionUtils.isEmpty(constraintViolationSet)) {
- message = validationException.getMessage();
- } else {
- // getting the first violation message for the output response.
- ConstraintViolation<?> constraintViolation = constraintViolationSet.iterator().next();
- message = constraintViolation.getMessage();
- fieldName = getFieldName(constraintViolation.getPropertyPath());
- }
- ErrorCode validationErrorCode = new ValidationErrorBuilder(message, fieldName).build();
- LOGGER.error(validationErrorCode.message(), validationException);
- return buildResponse(Status.EXPECTATION_FAILED, validationErrorCode);
- }
-
- private Response transform(JsonMappingException jsonMappingException) {
- ErrorCode jsonMappingErrorCode = new JsonMappingErrorBuilder().build();
- LOGGER.error(jsonMappingErrorCode.message(), jsonMappingException);
- return buildResponse(Status.EXPECTATION_FAILED, jsonMappingErrorCode);
- }
-
- private Response transform(Exception exception) {
- ErrorCode errorCode = new GeneralErrorBuilder().build();
- LOGGER.error(errorCode.message(), exception);
- return buildResponse(Status.INTERNAL_SERVER_ERROR, errorCode);
- }
-
- private String getFieldName(Path propertyPath) {
- return ((PathImpl) propertyPath).getLeafNode().toString();
- }
-
- private Object toEntity(final Status status, final ErrorCode code) {
- return new ErrorCodeAndMessage(status, code);
- }
-}
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json
deleted file mode 100644
index 797609c8de..0000000000
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json
+++ /dev/null
@@ -1,30 +0,0 @@
-{
- "VSP_NOT_FOUND": "NOT_FOUND",
- "VSP_INVALID": "BAD_REQUEST",
- "VSP_PROCESSING_IN_PROGRESS": "FORBIDDEN",
- "VSP_CREATE_UPLOAD_LOCK_ERROR": "INTERNAL_SERVER_ERROR",
- "VSP_UPDATE_UPLOAD_LOCK_ERROR": "INTERNAL_SERVER_ERROR",
- "VSP_UNABLE_UPDATE_UPLOAD_STATUS_ERROR": "INTERNAL_SERVER_ERROR",
- "VSP_UPLOAD_LOCK_NOT_FOUND_ERROR": "NOT_FOUND",
- "VSP_UPLOAD_STATUS_NOT_FOUND_ERROR": "NOT_FOUND",
- "VSP_UPLOAD_ALREADY_FINISHED_ERROR": "INTERNAL_SERVER_ERROR",
- "VSP_UPLOAD_ALREADY_IN_STATUS_ERROR": "BAD_REQUEST",
- "VSP_DELETE_ALREADY_IN_USE_BY_VF": "FORBIDDEN",
- "VSP_DELETE_NOT_ARCHIVED": "FORBIDDEN",
- "VSP_DELETE_GENERIC_ERROR": "INTERNAL_SERVER_ERROR",
- "VSP_DELETE_FROM_STORAGE_ERROR": "INTERNAL_SERVER_ERROR",
- "VSP_DELETE_FROM_DATABASE_ERROR": "INTERNAL_SERVER_ERROR",
- "ORCHESTRATION_NOT_FOUND": "NOT_FOUND",
- "UPLOAD_INVALID" : "PRECONDITION_FAILED",
- "PACKAGE_NOT_FOUND": "NOT_FOUND",
- "PACKAGE_INVALID": "BAD_REQUEST",
- "VENDOR_LICENSE_MODEL_NOT_FOUND": "NOT_FOUND",
- "VLM_IS_IN_USE_DELETE_ERROR": "FORBIDDEN",
- "VLM_IS_CERTIFIED_AND_NOT_ARCHIVED_DELETE_ERROR": "FORBIDDEN",
- "VENDOR_LICENSE_ENTITY_NOT_FOUND": "NOT_FOUND",
- "VERSIONABLE_SUB_ENTITY_NOT_FOUND": "NOT_FOUND",
- "FEATURE_GROUP_NOT_EXIST_FOR_VSP": "NOT_FOUND",
- "INVALID_COMPUTE_FLAVOR_ID": "NOT_FOUND",
- "INVALID_COMPONENT_ID": "NOT_FOUND",
- "UNIQUE_VALUE_VIOLATION": "BAD_REQUEST"
-} \ No newline at end of file
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java
deleted file mode 100644
index 680c3409ad..0000000000
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * SDC
- * ================================================================================
- * Copyright (C) 2019 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-package org.openecomp.sdcrests.errors;
-
-import static org.junit.Assert.assertEquals;
-
-import java.util.HashSet;
-import java.util.Set;
-import javax.validation.ConstraintViolation;
-import javax.validation.ConstraintViolationException;
-import javax.ws.rs.core.Response;
-import com.fasterxml.jackson.databind.JsonMappingException;
-import org.hibernate.validator.internal.engine.path.PathImpl;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.junit.MockitoJUnitRunner;
-import org.openecomp.sdc.common.errors.CoreException;
-import org.openecomp.sdc.common.errors.ErrorCategory;
-import org.openecomp.sdc.common.errors.ErrorCode;
-import org.openecomp.sdc.common.errors.ErrorCode.ErrorCodeBuilder;
-
-@RunWith(MockitoJUnitRunner.class)
-public class DefaultExceptionMapperTest {
-
- private static final String TEST_MESSAGE = "Test message";
-
- @Mock
- private ConstraintViolation<String> constraintViolation;
- private PathImpl path = PathImpl.createRootPath();
-
- @Test
- public void shouldMapCoreExceptionToResponse() {
- DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
- ErrorCode errorCode = new ErrorCodeBuilder().withId("VSP_NOT_FOUND").withCategory(ErrorCategory.APPLICATION).build();
- CoreException exception = new CoreException(errorCode);
- Response response = defaultExceptionMapper.toResponse(exception);
- assertEquals(response.getStatus(), 404);
- }
-
- @Test
- public void shouldMapConstraintViolationExceptionToResponse() {
- Mockito.when(constraintViolation.getPropertyPath()).thenReturn(path);
- DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
- Set<ConstraintViolation<String>> violations = new HashSet<>();
- violations.add(constraintViolation);
- ConstraintViolationException exception = new ConstraintViolationException(TEST_MESSAGE, violations);
- Response response = defaultExceptionMapper.toResponse(exception);
- assertEquals(response.getStatus(), 417);
- }
-
- @Test
- public void shouldMapJsonMappingExceptionToResponse() {
- DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
- JsonMappingException exception = new JsonMappingException(TEST_MESSAGE);
- Response response = defaultExceptionMapper.toResponse(exception);
- assertEquals(response.getStatus(), 417);
- }
-
- @Test
- public void shouldMapOtherExceptionToResponse() {
- DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
- Exception exception = new Exception(TEST_MESSAGE);
- Response response = defaultExceptionMapper.toResponse(exception);
- assertEquals(response.getStatus(), 500);
- }
-} \ No newline at end of file