ac2dba355f93bcdd697a14412aa75d3240b6a51e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.runtime.main.web;
24
25 import io.swagger.v3.oas.annotations.Hidden;
26 import java.util.Map;
27 import javax.servlet.RequestDispatcher;
28 import javax.servlet.http.HttpServletRequest;
29 import org.onap.policy.clamp.controlloop.models.messages.rest.SimpleResponse;
30 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.boot.web.error.ErrorAttributeOptions;
35 import org.springframework.boot.web.servlet.error.ErrorAttributes;
36 import org.springframework.boot.web.servlet.error.ErrorController;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.MediaType;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.context.request.ServletWebRequest;
43
44 @Controller
45 @Hidden
46 public class RuntimeErrorController implements ErrorController {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeErrorController.class);
49
50     private final ErrorAttributes errorAttributes;
51
52     @Value("${server.error.path}")
53     private String path;
54
55     /**
56      * Constructor.
57      *
58      * @param errorAttributes ErrorAttributes
59      */
60     public RuntimeErrorController(ErrorAttributes errorAttributes) {
61         this.errorAttributes = errorAttributes;
62     }
63
64     protected HttpStatus getStatus(HttpServletRequest request) {
65         Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
66         if (statusCode == null) {
67             return HttpStatus.INTERNAL_SERVER_ERROR;
68         }
69         try {
70             return HttpStatus.valueOf(statusCode);
71         } catch (Exception ex) {
72             LOGGER.error("statusCode {} Not Valid", statusCode, ex);
73             return HttpStatus.INTERNAL_SERVER_ERROR;
74         }
75     }
76
77     /**
78      * Handle Errors not handled to GlobalControllerExceptionHandler.
79      *
80      * @param request HttpServletRequest
81      * @return ResponseEntity
82      */
83     @RequestMapping(value = "${server.error.path}", produces = MediaType.APPLICATION_JSON_VALUE)
84     public ResponseEntity<TypedSimpleResponse<SimpleResponse>> handleError(HttpServletRequest request) {
85         Map<String, Object> map = this.errorAttributes.getErrorAttributes(new ServletWebRequest(request),
86                 ErrorAttributeOptions.defaults());
87
88         var sb = new StringBuilder();
89         final Object error = map.get("error");
90         if (error != null) {
91             sb.append(error.toString()).append(" ");
92         }
93         final Object message = map.get("message");
94         if (message != null) {
95             sb.append(message.toString());
96         }
97
98         TypedSimpleResponse<SimpleResponse> resp = new TypedSimpleResponse<>();
99         resp.setErrorDetails(sb.toString());
100
101         return ResponseEntity.status(getStatus(request)).body(resp);
102
103     }
104 }