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