2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T 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=========================================================
21 package org.onap.so.apihandler.common;
23 import javax.ws.rs.core.HttpHeaders;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
27 import org.apache.commons.lang3.StringUtils;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.stereotype.Component;
33 public class ResponseBuilder {
35 @Value("${mso.infra.default.versions.apiMinorVersion}")
36 private String apiMinorVersion;
37 @Value("${mso.infra.default.versions.apiPatchVersion}")
38 private String apiPatchVersion;
40 public Response buildResponse(int status, String requestId, Object jsonResponse, String apiVersion) {
42 if (apiVersion.matches("v[1-9]")) {
43 apiVersion = apiVersion.substring(1);
46 String latestVersion = apiVersion + "." + apiMinorVersion + "." + apiPatchVersion;
48 javax.ws.rs.core.Response.ResponseBuilder builder = Response.status(status)
49 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
50 .header(CommonConstants.X_MINOR_VERSION, apiMinorVersion)
51 .header(CommonConstants.X_PATCH_VERSION, apiPatchVersion)
52 .header(CommonConstants.X_LATEST_VERSION, latestVersion);
54 if(StringUtils.isNotBlank(requestId)) {
55 builder.header(CommonConstants.X_TRANSACTION_ID, requestId);
58 return builder.entity(jsonResponse).build();