2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * Copyright (C) 2021 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.oom.certservice.api.advice;
24 import org.onap.oom.certservice.api.CertificationController;
25 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
26 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
27 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
28 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
29 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
30 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
31 import org.onap.oom.certservice.cmpv2client.exceptions.CmpServerException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.ExceptionHandler;
37 import org.springframework.web.bind.annotation.RestControllerAdvice;
39 @RestControllerAdvice(assignableTypes = CertificationController.class)
40 public final class CertificationExceptionAdvice {
42 private static final Logger LOGGER = LoggerFactory.getLogger(CertificationExceptionAdvice.class);
44 @ExceptionHandler(value = CsrDecryptionException.class)
45 public ResponseEntity<ErrorResponseModel> handle(CsrDecryptionException exception) {
46 LOGGER.error("Exception occurred during decoding certificate sign request:", exception);
47 return getErrorResponseEntity(
48 "Wrong certificate signing request (CSR) format",
49 HttpStatus.BAD_REQUEST
53 @ExceptionHandler(value = KeyDecryptionException.class)
54 public ResponseEntity<ErrorResponseModel> handle(KeyDecryptionException exception) {
55 LOGGER.error("Exception occurred during decoding key:", exception);
56 return getErrorResponseEntity(
57 "Wrong key (PK) format",
58 HttpStatus.BAD_REQUEST
62 @ExceptionHandler(value = Cmpv2ServerNotFoundException.class)
63 public ResponseEntity<ErrorResponseModel> handle(Cmpv2ServerNotFoundException exception) {
64 LOGGER.error("Exception occurred selecting CMPv2 server:", exception);
65 return getErrorResponseEntity(
66 "Certification authority not found for given CAName",
71 @ExceptionHandler(value = RuntimeException.class)
72 public ResponseEntity<ErrorResponseModel> handle(RuntimeException exception) throws CmpClientException {
73 throw new CmpClientException("Runtime exception occurred calling cmp client business logic", exception);
76 @ExceptionHandler(value = CmpClientException.class)
77 public ResponseEntity<ErrorResponseModel> handle(CmpClientException exception) {
78 LOGGER.error("Exception occurred calling cmp client:", exception);
79 return getErrorResponseEntity(
80 "Exception occurred during call to cmp client: " + exception.getMessage(),
81 HttpStatus.INTERNAL_SERVER_ERROR
85 @ExceptionHandler(value = CmpServerException.class)
86 public ResponseEntity<ErrorResponseModel> handle(CmpServerException exception) {
87 LOGGER.error("CMPv2 server returned following error: {} ", exception.getMessage(), exception);
88 return getErrorResponseEntity(
89 "CMPv2 server returned following error: " + exception.getMessage(),
90 HttpStatus.INTERNAL_SERVER_ERROR
94 @ExceptionHandler(value = Cmpv2ClientAdapterException.class)
95 public ResponseEntity<ErrorResponseModel> handle(Cmpv2ClientAdapterException exception) {
96 LOGGER.error("Exception occurred parsing cmp client response:", exception);
97 return getErrorResponseEntity(
98 "Exception occurred parsing cmp client response: " + exception.getMessage(),
99 HttpStatus.INTERNAL_SERVER_ERROR
103 private ResponseEntity<ErrorResponseModel> getErrorResponseEntity(String errorMessage, HttpStatus status) {
104 ErrorResponseModel errorResponse = new ErrorResponseModel(errorMessage);
105 return new ResponseEntity<>(