Set Python and Ubuntu versions in .readthedocs.yaml
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / api / advice / CertificationExceptionAdvice.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.oom.certservice.api.advice;
23
24 import org.onap.oom.certservice.api.CertificationController;
25 import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
26 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
27 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
28 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
29 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
30 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
31 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
32 import org.onap.oom.certservice.cmpv2client.exceptions.CmpServerException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.ExceptionHandler;
38 import org.springframework.web.bind.annotation.RestControllerAdvice;
39
40 @RestControllerAdvice(assignableTypes = CertificationController.class)
41 public final class CertificationExceptionAdvice {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(CertificationExceptionAdvice.class);
44
45     @ExceptionHandler(value = CsrDecryptionException.class)
46     public ResponseEntity<ErrorResponseModel> handle(CsrDecryptionException exception) {
47         LOGGER.error("Exception occurred during decoding certificate sign request:", exception);
48         return getErrorResponseEntity(
49             "Wrong certificate signing request (CSR) format",
50             HttpStatus.BAD_REQUEST
51         );
52     }
53
54     @ExceptionHandler(value = KeyDecryptionException.class)
55     public ResponseEntity<ErrorResponseModel> handle(KeyDecryptionException exception) {
56         LOGGER.error("Exception occurred during decoding key:", exception);
57         return getErrorResponseEntity(
58             "Wrong key (PK) format",
59             HttpStatus.BAD_REQUEST
60         );
61     }
62
63     @ExceptionHandler(value = CertificateDecryptionException.class)
64     public ResponseEntity<ErrorResponseModel> handle(CertificateDecryptionException exception) {
65         LOGGER.error("Exception occurred decoding certificate:", exception);
66         return getErrorResponseEntity(
67                 "Wrong certificate format",
68                 HttpStatus.BAD_REQUEST
69         );
70     }
71
72     @ExceptionHandler(value = Cmpv2ServerNotFoundException.class)
73     public ResponseEntity<ErrorResponseModel> handle(Cmpv2ServerNotFoundException exception) {
74         LOGGER.error("Exception occurred selecting CMPv2 server:", exception);
75         return getErrorResponseEntity(
76             "Certification authority not found for given CAName",
77             HttpStatus.NOT_FOUND
78         );
79     }
80
81     @ExceptionHandler(value = RuntimeException.class)
82     public ResponseEntity<ErrorResponseModel> handle(RuntimeException exception) throws CmpClientException {
83         throw new CmpClientException("Runtime exception occurred calling cmp client business logic", exception);
84     }
85
86     @ExceptionHandler(value = CmpClientException.class)
87     public ResponseEntity<ErrorResponseModel> handle(CmpClientException exception) {
88         LOGGER.error("Exception occurred calling cmp client:", exception);
89         return getErrorResponseEntity(
90             "Exception occurred during call to cmp client: " + exception.getMessage(),
91             HttpStatus.INTERNAL_SERVER_ERROR
92         );
93     }
94
95     @ExceptionHandler(value = CmpServerException.class)
96     public ResponseEntity<ErrorResponseModel> handle(CmpServerException exception) {
97         LOGGER.error("CMPv2 server returned following error: {} ", exception.getMessage(), exception);
98         return getErrorResponseEntity(
99             "CMPv2 server returned following error: " + exception.getMessage(),
100             HttpStatus.INTERNAL_SERVER_ERROR
101         );
102     }
103
104     @ExceptionHandler(value = Cmpv2ClientAdapterException.class)
105     public ResponseEntity<ErrorResponseModel> handle(Cmpv2ClientAdapterException exception) {
106         LOGGER.error("Exception occurred parsing cmp client response:", exception);
107         return getErrorResponseEntity(
108             "Exception occurred parsing cmp client response: " + exception.getMessage(),
109             HttpStatus.INTERNAL_SERVER_ERROR
110         );
111     }
112
113     private ResponseEntity<ErrorResponseModel> getErrorResponseEntity(String errorMessage, HttpStatus status) {
114         ErrorResponseModel errorResponse = new ErrorResponseModel(errorMessage);
115         return new ResponseEntity<>(
116             errorResponse,
117             status
118         );
119     }
120
121 }