Fix sonar issues in policy-api
[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  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  * SPDX-License-Identifier: Apache-2.0\r
20  * ============LICENSE_END=========================================================\r
21  */\r
22 \r
23 package org.onap.policy.api.main.rest;\r
24 \r
25 import java.util.UUID;\r
26 import javax.ws.rs.core.Response;\r
27 import javax.ws.rs.core.Response.ResponseBuilder;\r
28 import org.onap.policy.common.utils.coder.Coder;\r
29 import org.onap.policy.common.utils.coder.CoderException;\r
30 import org.onap.policy.common.utils.coder.StandardCoder;\r
31 import org.onap.policy.models.errors.concepts.ErrorResponseInfo;\r
32 import org.slf4j.Logger;\r
33 import org.slf4j.LoggerFactory;\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 \r
45     protected Response makeOkResponse(UUID requestId, Object respEntity) {\r
46         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
47                         .entity(respEntity).build();\r
48     }\r
49 \r
50     protected <T extends ErrorResponseInfo> Response makeErrorResponse(UUID requestId, T pfme) {\r
51         return addLoggingHeaders(addVersionControlHeaders(Response.status(pfme.getErrorResponse().getResponseCode())),\r
52                         requestId).entity(pfme.getErrorResponse()).build();\r
53     }\r
54 \r
55     protected ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {\r
56         return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");\r
57     }\r
58 \r
59     protected ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) {\r
60         if (requestId == null) {\r
61             // Generate a random uuid if client does not embed requestId in rest request\r
62             return rb.header("X-ONAP-RequestID", UUID.randomUUID());\r
63         }\r
64         return rb.header("X-ONAP-RequestID", requestId);\r
65     }\r
66 \r
67     /**\r
68      * Converts an object to a JSON string.\r
69      *\r
70      * @param object object to convert\r
71      * @return a JSON string representing the object\r
72      */\r
73     protected String toJson(Object object) {\r
74         if (object == null) {\r
75             return null;\r
76         }\r
77 \r
78         try {\r
79             return coder.encode(object);\r
80 \r
81         } catch (CoderException e) {\r
82             LOGGER.warn("cannot convert {} to JSON", object.getClass().getName(), e);\r
83             return null;\r
84         }\r
85     }\r
86 }\r