081eb035de3e618d8b33c5dbd9a291155c426755
[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 jakarta.servlet.RequestDispatcher;
26 import jakarta.servlet.http.HttpServletRequest;
27 import java.util.Map;
28 import org.onap.policy.clamp.models.acm.messages.rest.SimpleResponse;
29 import org.onap.policy.clamp.models.acm.messages.rest.TypedSimpleResponse;
30 import org.springframework.boot.web.error.ErrorAttributeOptions;
31 import org.springframework.boot.web.servlet.error.ErrorAttributes;
32 import org.springframework.boot.web.servlet.error.ErrorController;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.MediaType;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Controller;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.context.request.ServletWebRequest;
39
40 @Controller
41 public class AcElementErrorController implements ErrorController {
42
43     private final ErrorAttributes errorAttributes;
44
45     /**
46      * Constructor.
47      *
48      * @param errorAttributes ErrorAttributes
49      */
50     public AcElementErrorController(ErrorAttributes errorAttributes) {
51         this.errorAttributes = errorAttributes;
52     }
53
54     protected HttpStatus getStatus(HttpServletRequest request) {
55         var statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
56         if (statusCode == null) {
57             return HttpStatus.INTERNAL_SERVER_ERROR;
58         }
59         try {
60             return HttpStatus.valueOf(statusCode);
61         } catch (Exception ex) {
62             return HttpStatus.INTERNAL_SERVER_ERROR;
63         }
64     }
65
66     /**
67      * Handle Errors not handled to GlobalControllerExceptionHandler.
68      *
69      * @param request HttpServletRequest
70      * @return ResponseEntity
71      */
72     @SuppressWarnings("squid:S3752")
73     @RequestMapping(value = "${server.error.path}", produces = MediaType.APPLICATION_JSON_VALUE)
74     public ResponseEntity<TypedSimpleResponse<SimpleResponse>> handleError(HttpServletRequest request) {
75         Map<String, Object> map = this.errorAttributes.getErrorAttributes(new ServletWebRequest(request),
76                 ErrorAttributeOptions.defaults());
77
78         var sb = new StringBuilder();
79         final var error = map.get("error");
80         if (error != null) {
81             sb.append(error).append(" ");
82         }
83         final var message = map.get("message");
84         if (message != null) {
85             sb.append(message);
86         }
87
88         TypedSimpleResponse<SimpleResponse> resp = new TypedSimpleResponse<>();
89         resp.setErrorDetails(sb.toString());
90
91         return ResponseEntity.status(getStatus(request)).body(resp);
92     }
93 }