770f5d8bfaa163511ba1ed1280be3bf59b4b6bc0
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.acm.element.main.rest;
24
25 import static org.springframework.boot.web.error.ErrorAttributeOptions.Include;
26
27 import jakarta.servlet.RequestDispatcher;
28 import jakarta.servlet.http.HttpServletRequest;
29 import org.onap.policy.clamp.models.acm.messages.rest.SimpleResponse;
30 import org.onap.policy.clamp.models.acm.messages.rest.TypedSimpleResponse;
31 import org.springframework.boot.web.error.ErrorAttributeOptions;
32 import org.springframework.boot.web.servlet.error.ErrorAttributes;
33 import org.springframework.boot.web.servlet.error.ErrorController;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.MediaType;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.context.request.ServletWebRequest;
40
41 @Controller
42 public class AcElementErrorController implements ErrorController {
43
44     private final ErrorAttributes errorAttributes;
45
46     /**
47      * Constructor.
48      *
49      * @param errorAttributes ErrorAttributes
50      */
51     public AcElementErrorController(ErrorAttributes errorAttributes) {
52         this.errorAttributes = errorAttributes;
53     }
54
55     protected HttpStatus getStatus(HttpServletRequest request) {
56         var statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
57         if (statusCode == null) {
58             return HttpStatus.INTERNAL_SERVER_ERROR;
59         }
60         try {
61             return HttpStatus.valueOf(statusCode);
62         } catch (Exception ex) {
63             return HttpStatus.INTERNAL_SERVER_ERROR;
64         }
65     }
66
67     /**
68      * Handle Errors not handled to GlobalControllerExceptionHandler.
69      *
70      * @param request HttpServletRequest
71      * @return ResponseEntity
72      */
73     @SuppressWarnings("squid:S3752")
74     @RequestMapping(value = "${server.error.path}", produces = MediaType.APPLICATION_JSON_VALUE)
75     public ResponseEntity<TypedSimpleResponse<SimpleResponse>> handleError(HttpServletRequest request) {
76         var map = this.errorAttributes.getErrorAttributes(new ServletWebRequest(request),
77             ErrorAttributeOptions.of(Include.MESSAGE, Include.EXCEPTION, Include.BINDING_ERRORS));
78
79         var sb = new StringBuilder();
80         final var exception = map.get("exception");
81         if (exception != null) {
82             sb.append(exception).append(" ");
83         }
84         final var error = map.get("error");
85         if (error != null) {
86             sb.append(error).append(" ");
87         }
88         final var message = map.get("message");
89         if (message != null) {
90             sb.append(message);
91         }
92
93         TypedSimpleResponse<SimpleResponse> resp = new TypedSimpleResponse<>();
94         resp.setErrorDetails(sb.toString());
95
96         return ResponseEntity.status(getStatus(request)).body(resp);
97     }
98 }