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