Containerization feature of SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandler / common / ResponseBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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 package org.onap.so.apihandler.common;
22
23 import javax.ws.rs.core.HttpHeaders;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.stereotype.Component;
30
31
32 @Component
33 public class ResponseBuilder {
34         
35         @Value("${mso.infra.default.versions.apiMinorVersion}")
36         private String apiMinorVersion;
37         @Value("${mso.infra.default.versions.apiPatchVersion}")
38         private String apiPatchVersion;
39         
40         public Response buildResponse(int status, String requestId, Object jsonResponse, String apiVersion) {
41                 
42                 if (apiVersion.matches("v[1-9]")) {
43                         apiVersion = apiVersion.substring(1);
44                 }
45                         
46                 String latestVersion = apiVersion + "." + apiMinorVersion + "." + apiPatchVersion;
47                 
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);
53                 
54                 if(StringUtils.isNotBlank(requestId)) {
55                         builder.header(CommonConstants.X_TRANSACTION_ID, requestId);
56                 }
57                 
58                 return builder.entity(jsonResponse).build();
59         }
60
61 }