Java 17 Upgrade
[policy/models.git] / models-sim / models-sim-dmaap / src / main / java / org / onap / policy / models / sim / dmaap / rest / BaseRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 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.models.sim.dmaap.rest;
22
23 import jakarta.ws.rs.Consumes;
24 import jakarta.ws.rs.Produces;
25 import jakarta.ws.rs.core.Response.ResponseBuilder;
26 import java.net.HttpURLConnection;
27 import java.util.UUID;
28
29 /**
30  * Version v1 common superclass to provide DMaaP endpoints for the DMaaP simulator component.
31  */
32 @Produces("application/json")
33 @Consumes({"application/cambria", "application/json"})
34 public class BaseRestControllerV1 {
35     public static final String EXTENSION_NAME = "interface info";
36
37     public static final String API_VERSION_NAME = "api-version";
38     public static final String API_VERSION = "1.0.0";
39
40     public static final String LAST_MOD_NAME = "last-mod-release";
41     public static final String LAST_MOD_RELEASE = "Dublin";
42
43     public static final String VERSION_MINOR_NAME = "X-MinorVersion";
44     public static final String VERSION_MINOR_DESCRIPTION =
45             "Used to request or communicate a MINOR version back from the client"
46                     + " to the server, and from the server back to the client";
47
48     public static final String VERSION_PATCH_NAME = "X-PatchVersion";
49     public static final String VERSION_PATCH_DESCRIPTION = "Used only to communicate a PATCH version in a response for"
50             + " troubleshooting purposes only, and will not be provided by" + " the client on request";
51
52     public static final String VERSION_LATEST_NAME = "X-LatestVersion";
53     public static final String VERSION_LATEST_DESCRIPTION = "Used only to communicate an API's latest version";
54
55     public static final String REQUEST_ID_NAME = "X-ONAP-RequestID";
56     public static final String REQUEST_ID_HDR_DESCRIPTION = "Used to track REST transactions for logging purpose";
57     public static final String REQUEST_ID_PARAM_DESCRIPTION = "RequestID for http transaction";
58
59     public static final String AUTHORIZATION_TYPE = "basicAuth";
60
61     public static final int AUTHENTICATION_ERROR_CODE = HttpURLConnection.HTTP_UNAUTHORIZED;
62     public static final int AUTHORIZATION_ERROR_CODE = HttpURLConnection.HTTP_FORBIDDEN;
63     public static final int SERVER_ERROR_CODE = HttpURLConnection.HTTP_INTERNAL_ERROR;
64
65     public static final String AUTHENTICATION_ERROR_MESSAGE = "Authentication Error";
66     public static final String AUTHORIZATION_ERROR_MESSAGE = "Authorization Error";
67     public static final String SERVER_ERROR_MESSAGE = "Internal Server Error";
68
69     /**
70      * Adds version headers to the response.
71      *
72      * @param respBuilder response builder
73      * @return the response builder, with version headers
74      */
75     public ResponseBuilder addVersionControlHeaders(ResponseBuilder respBuilder) {
76         return respBuilder.header(VERSION_MINOR_NAME, "0").header(VERSION_PATCH_NAME, "0").header(VERSION_LATEST_NAME,
77                 API_VERSION);
78     }
79
80     /**
81      * Adds logging headers to the response.
82      *
83      * @param respBuilder response builder
84      * @return the response builder, with version logging
85      */
86     public ResponseBuilder addLoggingHeaders(ResponseBuilder respBuilder, UUID requestId) {
87         if (requestId == null) {
88             // Generate a random uuid if client does not embed requestId in rest request
89             return respBuilder.header(REQUEST_ID_NAME, UUID.randomUUID());
90         }
91
92         return respBuilder.header(REQUEST_ID_NAME, requestId);
93     }
94 }