2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.acm.element.main.rest;
25 import jakarta.servlet.RequestDispatcher;
26 import jakarta.servlet.http.HttpServletRequest;
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.beans.factory.annotation.Value;
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;
42 public class AcElementErrorController implements ErrorController {
44 private final ErrorAttributes errorAttributes;
46 @Value("${server.error.path}")
52 * @param errorAttributes ErrorAttributes
54 public AcElementErrorController(ErrorAttributes errorAttributes) {
55 this.errorAttributes = errorAttributes;
58 protected HttpStatus getStatus(HttpServletRequest request) {
59 Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
60 if (statusCode == null) {
61 return HttpStatus.INTERNAL_SERVER_ERROR;
64 return HttpStatus.valueOf(statusCode);
65 } catch (Exception ex) {
66 return HttpStatus.INTERNAL_SERVER_ERROR;
71 * Handle Errors not handled to GlobalControllerExceptionHandler.
73 * @param request HttpServletRequest
74 * @return ResponseEntity
76 @RequestMapping(value = "${server.error.path}", produces = MediaType.APPLICATION_JSON_VALUE)
77 public ResponseEntity<TypedSimpleResponse<SimpleResponse>> handleError(HttpServletRequest request) {
78 Map<String, Object> map = this.errorAttributes.getErrorAttributes(new ServletWebRequest(request),
79 ErrorAttributeOptions.defaults());
81 var sb = new StringBuilder();
82 final Object error = map.get("error");
84 sb.append(error.toString() + " ");
86 final Object message = map.get("message");
87 if (message != null) {
88 sb.append(message.toString());
91 TypedSimpleResponse<SimpleResponse> resp = new TypedSimpleResponse<>();
92 resp.setErrorDetails(sb.toString());
94 return ResponseEntity.status(getStatus(request)).body(resp);