Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / RestObject.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 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.vid.mso;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.common.base.MoreObjects;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26
27 import javax.ws.rs.core.Response;
28
29 import static org.onap.vid.utils.Logging.getMethodCallerName;
30
31 /**
32  * The Class RestObject.
33  *
34  * @param <T> the generic type
35  */
36 public class RestObject<T> {
37
38     static final ObjectMapper objectMapper = new ObjectMapper();
39
40         /**
41          * Generic version of the RestObject class.
42          *
43          */
44     // T stands for "Type"
45     private T t;
46     
47     // The string source of t, if available
48     private String rawT;
49
50     /** The status code. */
51     private int statusCode= 0;
52
53     public RestObject() {
54     }
55
56     public void copyFrom(RestObject<T> src) {
57         set(src.get());
58         setRaw(src.getRaw());
59         setStatusCode(src.getStatusCode());
60     }
61
62     public RestObject(Response cres, Class<?> tClass, EELFLoggerDelegate logger) {
63
64         String rawEntity = null;
65         try {
66             cres.bufferEntity();
67             rawEntity = cres.readEntity(String.class);
68             T t = (T) objectMapper.readValue(rawEntity, tClass);
69             this.set(t);
70         }
71         catch ( Exception e ) {
72             try {
73                 this.setRaw(rawEntity);
74                 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + getMethodCallerName() + " Error reading response entity as " + tClass + ": , e="
75                         + e.getMessage() + ", Entity=" + rawEntity);
76             } catch (Exception e2) {
77                 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + getMethodCallerName() + " No response entity, this is probably ok, e="
78                         + e.getMessage());
79             }
80         }
81
82         int status = cres.getStatus();
83         this.setStatusCode (status);
84     }
85
86
87     /**
88      * Sets the.
89      *
90      * @param t the t
91      */
92     public void set(T t) { this.t = t; }
93     
94     /**
95      * Gets the.
96      *
97      * @return the t
98      */
99     public T get() { return t; }
100         
101     /**
102      * Sets the status code.
103      *
104      * @param v the new status code
105      */
106     public void setStatusCode(int v) { this.statusCode = v; }
107     
108     /**
109      * Gets the status code.
110      *
111      * @return the status code
112      */
113     public int getStatusCode() { return this.statusCode; }
114
115     public String getRaw() {
116         return rawT;
117     }
118
119     public void setRaw(String rawT) {
120         this.rawT = rawT;
121     }
122
123     @Override
124     public String toString() {
125         return MoreObjects.toStringHelper(this)
126                 .add("t", t)
127                 .add("rawT", rawT)
128                 .add("statusCode", statusCode)
129                 .toString();
130     }
131 }
132