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 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;
31 import javax.ws.rs.core.Response;
32 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
33 import org.onap.dmaap.dbcapi.model.ApiError;
36 public class ResponseBuilder extends BaseLoggingClass {
38 Response success(Object d) {
39 return buildSuccessResponse(d, Response.Status.OK.getStatusCode());
42 Response success(int code, Object d) {
43 return buildSuccessResponse(d, code);
46 Response error(ApiError err) {
47 return buildErrResponse(err);
50 Response unauthorized(String msg) {
51 return buildErrResponse(new ApiError(UNAUTHORIZED.getStatusCode(), msg, "Authorization"));
54 Response unavailable() {
55 return buildErrResponse(new ApiError(SERVICE_UNAVAILABLE.getStatusCode(),
56 "Request is unavailable due to unexpected condition"));
60 return buildErrResponse(new ApiError(NOT_FOUND.getStatusCode(),"Requested object not found"));
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);
69 private Response buildErrResponse(ApiError err) {
70 MDC.put(MDC_STATUS_CODE, "ERROR");
71 MDC.put(MDC_RESPONSE_DESC, err.getMessage());
73 return buildResponse(err, err.getCode());
76 private Response buildResponse(Object obj, int code) {
77 MDC.put(MDC_RESPONSE_CODE, String.valueOf(code));
79 auditLogger.auditEvent("");
80 return Response.status(code)