org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / MsoResponseWrapper2.java
1 package org.onap.vid.mso;
2
3 import com.fasterxml.jackson.annotation.JsonProperty;
4 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
5 import com.fasterxml.jackson.core.JsonProcessingException;
6 import com.fasterxml.jackson.databind.ObjectMapper;
7
8 @JsonPropertyOrder({
9             "status",
10             "entity"
11 })
12
13 /*
14 This is a brother of MsoResponseWrapper. I (Ittay) think it's better.
15 It is generic, immutable, and has some knowledge about RestObject.
16 The serialized "entity" field may be either String or nested object.
17  */
18 public class MsoResponseWrapper2<T> implements MsoResponseWrapperInterface {
19
20     final static ObjectMapper objectMapper = new ObjectMapper();
21
22         private final int status;
23         private final T entity;
24     private final String raw;
25
26     public MsoResponseWrapper2(RestObject<T> msoResponse) {
27         this.status = msoResponse.getStatusCode();
28         this.entity = msoResponse.get();
29         this.raw = msoResponse.getRaw();
30     }
31
32     public MsoResponseWrapper2(
33             @JsonProperty(value = "status", required = true) int status,
34             @JsonProperty(value = "entity", required = true) T entity) {
35         this.status = status;
36         this.entity = entity;
37         this.raw = null;
38     }
39
40     public int getStatus() {
41                 return status;
42         }
43
44     @Override
45     @org.codehaus.jackson.annotate.JsonIgnore
46     @com.fasterxml.jackson.annotation.JsonIgnore
47     public String getResponse() {
48         try {
49             return objectMapper.writeValueAsString(this);
50         } catch (JsonProcessingException e) {
51             return getEntity() != null ? getEntity().toString() : null;
52         }
53     }
54
55     @JsonProperty
56         public Object getEntity() {
57                 return entity != null ? entity : raw;
58         }
59
60 }