695ae06a8d6b74e451b27c400fbcc01850e1da46
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.simulator.main.rest;
22
23 import java.util.Map;
24 import javax.servlet.RequestDispatcher;
25 import javax.servlet.http.HttpServletRequest;
26 import org.onap.policy.clamp.controlloop.models.messages.rest.SimpleResponse;
27 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.boot.web.error.ErrorAttributeOptions;
30 import org.springframework.boot.web.servlet.error.ErrorAttributes;
31 import org.springframework.boot.web.servlet.error.ErrorController;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.context.request.ServletWebRequest;
38
39 @Controller
40 public class ParticipantErrorController implements ErrorController {
41
42     private final ErrorAttributes errorAttributes;
43
44     @Value("${server.error.path}")
45     private String path;
46
47     /**
48      * Constructor.
49      *
50      * @param errorAttributes ErrorAttributes
51      */
52     public ParticipantErrorController(ErrorAttributes errorAttributes) {
53         this.errorAttributes = errorAttributes;
54     }
55
56     protected HttpStatus getStatus(HttpServletRequest request) {
57         Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
58         if (statusCode == null) {
59             return HttpStatus.INTERNAL_SERVER_ERROR;
60         }
61         try {
62             return HttpStatus.valueOf(statusCode);
63         } catch (Exception ex) {
64             return HttpStatus.INTERNAL_SERVER_ERROR;
65         }
66     }
67
68     /**
69      * Handle Errors not handled to GlobalControllerExceptionHandler.
70      *
71      * @param request HttpServletRequest
72      * @return ResponseEntity
73      */
74     @RequestMapping(value = "${server.error.path}", produces = MediaType.APPLICATION_JSON_VALUE)
75     public ResponseEntity<TypedSimpleResponse<SimpleResponse>> handleError(HttpServletRequest request) {
76         Map<String, Object> map = this.errorAttributes.getErrorAttributes(new ServletWebRequest(request),
77                 ErrorAttributeOptions.defaults());
78
79         StringBuilder sb = new StringBuilder();
80         final Object error = map.get("error");
81         if (error != null) {
82             sb.append(error.toString() + " ");
83         }
84         final Object message = map.get("message");
85         if (message != null) {
86             sb.append(message.toString());
87         }
88
89         TypedSimpleResponse<SimpleResponse> resp = new TypedSimpleResponse<>();
90         resp.setErrorDetails(sb.toString());
91
92         return ResponseEntity.status(getStatus(request)).body(resp);
93
94     }
95 }