[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / resources / ResponseBuilder.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  * ============LICENSE_END=========================================================
19  */
20
21
22 package org.onap.dmaap.dbcapi.resources;
23
24 import static com.att.eelf.configuration.Configuration.MDC_RESPONSE_CODE;
25 import static com.att.eelf.configuration.Configuration.MDC_RESPONSE_DESC;
26 import static com.att.eelf.configuration.Configuration.MDC_STATUS_CODE;
27 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
28 import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE;
29 import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
30
31 import javax.ws.rs.core.Response;
32 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
33 import org.onap.dmaap.dbcapi.model.ApiError;
34 import org.slf4j.MDC;
35
36 public class ResponseBuilder extends BaseLoggingClass {
37
38     Response success(Object d) {
39         return buildSuccessResponse(d, Response.Status.OK.getStatusCode());
40     }
41
42     Response success(int code, Object d) {
43         return buildSuccessResponse(d, code);
44     }
45
46     Response error(ApiError err) {
47         return buildErrResponse(err);
48     }
49
50     Response unauthorized(String msg) {
51         return buildErrResponse(new ApiError(UNAUTHORIZED.getStatusCode(), msg, "Authorization"));
52     }
53
54     Response unavailable() {
55         return buildErrResponse(new ApiError(SERVICE_UNAVAILABLE.getStatusCode(),
56                 "Request is unavailable due to unexpected condition"));
57     }
58
59     Response notFound() {
60         return buildErrResponse(new ApiError(NOT_FOUND.getStatusCode(),"Requested object not found"));
61     }
62
63     private Response buildSuccessResponse(Object d, int code) {
64         MDC.put(MDC_STATUS_CODE, "COMPLETE");
65         MDC.put(MDC_RESPONSE_DESC, "");
66         return buildResponse(d, code);
67     }
68
69     private Response buildErrResponse(ApiError err) {
70         MDC.put(MDC_STATUS_CODE, "ERROR");
71         MDC.put(MDC_RESPONSE_DESC, err.getMessage());
72
73         return buildResponse(err, err.getCode());
74     }
75
76     private Response buildResponse(Object obj, int code) {
77         MDC.put(MDC_RESPONSE_CODE, String.valueOf(code));
78
79         auditLogger.auditEvent("");
80         return Response.status(code)
81                 .entity(obj)
82                 .build();
83     }
84 }