/*- * ============LICENSE_START======================================================= * ONAP Policy API * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.policy.api.main.rest; import java.util.UUID; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.models.errors.concepts.ErrorResponseInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Super class from which REST controllers are derived. */ public class CommonRestController { private static final Logger LOGGER = LoggerFactory.getLogger(CommonRestController.class); private final Coder coder = new StandardCoder(); protected Response makeOkResponse(UUID requestId, Object respEntity) { return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId) .entity(respEntity).build(); } protected Response makeErrorResponse(UUID requestId, T pfme) { return addLoggingHeaders(addVersionControlHeaders(Response.status(pfme.getErrorResponse().getResponseCode())), requestId).entity(pfme.getErrorResponse()).build(); } protected ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) { return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0"); } protected ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) { if (requestId == null) { // Generate a random uuid if client does not embed requestId in rest request return rb.header("X-ONAP-RequestID", UUID.randomUUID()); } return rb.header("X-ONAP-RequestID", requestId); } /** * Converts an object to a JSON string. * * @param object object to convert * @return a JSON string representing the object */ protected String toJson(Object object) { if (object == null) { return null; } try { return coder.encode(object); } catch (CoderException e) { LOGGER.warn("cannot convert {} to JSON", object.getClass().getName(), e); return null; } } }