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