Migrate policy api startup & config, controller to springboot
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / CommonRestController.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy API\r
4  * ================================================================================\r
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.\r
6  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  *\r
20  * SPDX-License-Identifier: Apache-2.0\r
21  * ============LICENSE_END=========================================================\r
22  */\r
23 \r
24 package org.onap.policy.api.main.rest;\r
25 \r
26 import java.util.UUID;\r
27 import org.onap.policy.common.utils.coder.Coder;\r
28 import org.onap.policy.common.utils.coder.CoderException;\r
29 import org.onap.policy.common.utils.coder.StandardCoder;\r
30 import org.slf4j.Logger;\r
31 import org.slf4j.LoggerFactory;\r
32 import org.springframework.http.HttpHeaders;\r
33 import org.springframework.http.ResponseEntity;\r
34 \r
35 /**\r
36  * Super class from which REST controllers are derived.\r
37  */\r
38 public class CommonRestController {\r
39 \r
40     private static final Logger LOGGER = LoggerFactory.getLogger(CommonRestController.class);\r
41 \r
42     private final Coder coder = new StandardCoder();\r
43 \r
44     protected <T> ResponseEntity<T> makeOkResponse(UUID requestId, T respEntity) {\r
45         HttpHeaders headers = new HttpHeaders();\r
46         addVersionControlHeaders(headers);\r
47         addLoggingHeaders(headers, requestId);\r
48         return ResponseEntity.ok().headers(headers).body(respEntity);\r
49     }\r
50 \r
51     protected <T> ResponseEntity<T> makeErrorResponse(UUID requestId, T respEntity, int status) {\r
52         HttpHeaders headers = new HttpHeaders();\r
53         addVersionControlHeaders(headers);\r
54         addLoggingHeaders(headers, requestId);\r
55         return ResponseEntity.status(status).headers(headers).body(respEntity);\r
56     }\r
57 \r
58     private void addVersionControlHeaders(HttpHeaders headers) {\r
59         headers.add("X-MinorVersion", "0");\r
60         headers.add("X-PatchVersion", "0");\r
61         headers.add("X-LatestVersion", "1.0.0");\r
62     }\r
63 \r
64     private void addLoggingHeaders(HttpHeaders headers, UUID requestId) {\r
65         if (requestId == null) {\r
66             // Generate a random uuid if client does not embed requestId in rest request\r
67             headers.add("X-ONAP-RequestID", UUID.randomUUID().toString());\r
68         } else {\r
69             headers.add("X-ONAP-RequestID", requestId.toString());\r
70         }\r
71     }\r
72 \r
73     /**\r
74      * Converts an object to a JSON string.\r
75      *\r
76      * @param object object to convert\r
77      * @return a JSON string representing the object\r
78      */\r
79     protected String toJson(Object object) {\r
80         if (object == null) {\r
81             return null;\r
82         }\r
83 \r
84         try {\r
85             return coder.encode(object);\r
86 \r
87         } catch (CoderException e) {\r
88             LOGGER.warn("cannot convert {} to JSON", object.getClass().getName(), e);\r
89             return null;\r
90         }\r
91     }\r
92 }