2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.vid.mso;
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import io.joshworks.restclient.http.HttpResponse;
35 This is a brother of MsoResponseWrapper. I (Ittay) think it's better.
36 It is generic, immutable, and has some knowledge about RestObject.
37 The serialized "entity" field may be either String or nested object.
39 public class MsoResponseWrapper2<T> implements MsoResponseWrapperInterface {
41 final static ObjectMapper objectMapper = new ObjectMapper();
43 private final int status;
44 private final T entity;
45 private final String raw;
47 public MsoResponseWrapper2(RestObject<T> msoResponse) {
48 this.status = msoResponse.getStatusCode();
49 this.entity = msoResponse.get();
50 this.raw = msoResponse.getRaw();
53 public MsoResponseWrapper2(HttpResponse<T> msoResponse) {
54 this.status = msoResponse.getStatus();
55 this.entity = msoResponse.getBody();
56 this.raw = msoResponse.getBody().toString();
59 public MsoResponseWrapper2(
60 @JsonProperty(value = "status", required = true) int status,
61 @JsonProperty(value = "entity", required = true) T entity) {
67 public int getStatus() {
72 @org.codehaus.jackson.annotate.JsonIgnore
73 @com.fasterxml.jackson.annotation.JsonIgnore
74 public String getResponse() {
76 return objectMapper.writeValueAsString(this);
77 } catch (JsonProcessingException e) {
78 return getEntity() != null ? getEntity().toString() : null;
83 public Object getEntity() {
84 return entity != null ? entity : raw;