Create ResponseBuilder class
[dmaap/dbcapi.git] / 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 org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
25 import org.onap.dmaap.dbcapi.model.ApiError;
26 import org.slf4j.MDC;
27
28 import javax.ws.rs.core.Response;
29
30 import static com.att.eelf.configuration.Configuration.MDC_RESPONSE_CODE;
31 import static com.att.eelf.configuration.Configuration.MDC_RESPONSE_DESC;
32 import static com.att.eelf.configuration.Configuration.MDC_STATUS_CODE;
33 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
34 import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE;
35 import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
36
37 public class ResponseBuilder extends BaseLoggingClass {
38
39     Response success(Object d) {
40         return buildSuccessResponse(d, Response.Status.OK.getStatusCode());
41     }
42
43     Response success(int code, Object d) {
44         return buildSuccessResponse(d, code);
45     }
46
47     Response error(ApiError err) {
48         return buildErrResponse(err);
49     }
50
51     Response unauthorized(String msg) {
52         return buildErrResponse(new ApiError(UNAUTHORIZED.getStatusCode(), msg, "Authorization"));
53     }
54
55     Response unavailable() {
56         return buildErrResponse(new ApiError(SERVICE_UNAVAILABLE.getStatusCode(),
57                 "Request is unavailable due to unexpected condition"));
58     }
59
60     Response notFound() {
61         return buildErrResponse(new ApiError(NOT_FOUND.getStatusCode(),"Requested object not found"));
62     }
63
64     private Response buildSuccessResponse(Object d, int code) {
65         MDC.put(MDC_STATUS_CODE, "COMPLETE");
66         MDC.put(MDC_RESPONSE_DESC, "");
67         return buildResponse(d, code);
68     }
69
70     private Response buildErrResponse(ApiError err) {
71         MDC.put(MDC_STATUS_CODE, "ERROR");
72         MDC.put(MDC_RESPONSE_DESC, err.getMessage());
73
74         return buildResponse(err, err.getCode());
75     }
76
77     private Response buildResponse(Object obj, int code) {
78         MDC.put(MDC_RESPONSE_CODE, String.valueOf(code));
79
80         auditLogger.auditEvent("");
81         return Response.status(code)
82                 .entity(obj)
83                 .build();
84     }
85 }