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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.dmaap.dbcapi.resources;
24 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
25 import org.onap.dmaap.dbcapi.model.ApiError;
28 import javax.ws.rs.core.Response;
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;
37 public class ResponseBuilder extends BaseLoggingClass {
39 Response success(Object d) {
40 return buildSuccessResponse(d, Response.Status.OK.getStatusCode());
43 Response success(int code, Object d) {
44 return buildSuccessResponse(d, code);
47 Response error(ApiError err) {
48 return buildErrResponse(err);
51 Response unauthorized(String msg) {
52 return buildErrResponse(new ApiError(UNAUTHORIZED.getStatusCode(), msg, "Authorization"));
55 Response unavailable() {
56 return buildErrResponse(new ApiError(SERVICE_UNAVAILABLE.getStatusCode(),
57 "Request is unavailable due to unexpected condition"));
61 return buildErrResponse(new ApiError(NOT_FOUND.getStatusCode(),"Requested object not found"));
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);
70 private Response buildErrResponse(ApiError err) {
71 MDC.put(MDC_STATUS_CODE, "ERROR");
72 MDC.put(MDC_RESPONSE_DESC, err.getMessage());
74 return buildResponse(err, err.getCode());
77 private Response buildResponse(Object obj, int code) {
78 MDC.put(MDC_RESPONSE_CODE, String.valueOf(code));
80 auditLogger.auditEvent("");
81 return Response.status(code)