2372e7fb7e092c3874b0b3c08a0f997b24c7e486
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / MsoResponseWrapper2.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.vid.mso;
22
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;
28
29 @JsonPropertyOrder({
30             "status",
31             "entity"
32 })
33
34 /*
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.
38  */
39 public class MsoResponseWrapper2<T> implements MsoResponseWrapperInterface {
40
41     final static ObjectMapper objectMapper = new ObjectMapper();
42
43         private final int status;
44         private final T entity;
45     private final String raw;
46
47     public MsoResponseWrapper2(RestObject<T> msoResponse) {
48         this.status = msoResponse.getStatusCode();
49         this.entity = msoResponse.get();
50         this.raw = msoResponse.getRaw();
51     }
52
53   public MsoResponseWrapper2(HttpResponse<T> msoResponse) {
54     this.status = msoResponse.getStatus();
55     this.entity = msoResponse.getBody();
56     this.raw = msoResponse.getBody().toString();
57   }
58
59     public MsoResponseWrapper2(
60             @JsonProperty(value = "status", required = true) int status,
61             @JsonProperty(value = "entity", required = true) T entity) {
62         this.status = status;
63         this.entity = entity;
64         this.raw = null;
65     }
66
67     public int getStatus() {
68                 return status;
69         }
70
71     @Override
72     @org.codehaus.jackson.annotate.JsonIgnore
73     @com.fasterxml.jackson.annotation.JsonIgnore
74     public String getResponse() {
75         try {
76             return objectMapper.writeValueAsString(this);
77         } catch (JsonProcessingException e) {
78             return getEntity() != null ? getEntity().toString() : null;
79         }
80     }
81
82     @JsonProperty
83         public Object getEntity() {
84                 return entity != null ? entity : raw;
85         }
86
87 }