722a1c4cd214c68d7540d89b6d09232d3d0fca3b
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / MsoUtil.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
22 package org.onap.vid.mso;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import io.joshworks.restclient.http.HttpResponse;
27 import org.apache.commons.lang3.ObjectUtils;
28 import org.glassfish.jersey.client.ClientResponse;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
33
34 import static org.onap.vid.utils.Logging.getMethodName;
35
36 /**
37  * The Class MsoUtil.
38  */
39 public class MsoUtil {
40         
41         /** The logger. */
42         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoUtil.class);
43         
44         /** The Constant dateFormat. */
45         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
46         
47         /**
48          * Wrap response.
49          *
50          * @param body the body
51          * @param statusCode the status code
52          * @return the mso response wrapper
53          */
54         public static MsoResponseWrapper wrapResponse ( String body, int statusCode ) {
55                 
56                 MsoResponseWrapper w = new MsoResponseWrapper();
57                 w.setStatus (statusCode);
58                 w.setEntity(body);
59                 
60                 return w;
61         }
62
63         /**
64          * Wrap response.
65          *
66          * @param cres the cres
67          * @return the mso response wrapper
68          */
69         public static MsoResponseWrapper wrapResponse (ClientResponse cres) {   
70                 String resp_str = "";
71                 if ( cres != null ) {
72                         resp_str = cres.readEntity(String.class);
73                 }
74                 int statuscode = cres.getStatus();
75                 MsoResponseWrapper w = MsoUtil.wrapResponse ( resp_str, statuscode );
76                 return (w);
77         }
78         
79         /**
80          * Wrap response.
81          *
82          * @param rs the rs
83          * @return the mso response wrapper
84          */
85         public static MsoResponseWrapper wrapResponse (RestObject<String> rs) {
86                 String resp_str = null;
87                 int status = 0;
88                 if ( rs != null ) {
89                         resp_str = rs.get() != null ? rs.get() : rs.getRaw();
90                         status = rs.getStatusCode();
91                 }
92                 MsoResponseWrapper w = MsoUtil.wrapResponse ( resp_str, status );
93                 return (w);
94         }
95
96         public static <T> MsoResponseWrapper wrapResponse (HttpResponse<T> rs) {
97                 MsoResponseWrapper w = new MsoResponseWrapper();
98                 w.setStatus (rs.getStatus());
99                 if(rs.getRawBody() != null) {
100                         w.setEntity(ObjectUtils.toString(rs.getBody()));
101                 }
102                 return w;
103         }
104
105         /**
106          * Convert pojo to string.
107          *
108          * @param <T> the generic type
109          * @param t the t
110          * @return the string
111          * @throws JsonProcessingException the json processing exception
112          */
113         public static <T> String convertPojoToString ( T t ) {
114                 
115                 String methodName = "convertPojoToString";
116                 ObjectMapper mapper = new ObjectMapper();
117                 String r_json_str = "";
118             if ( t != null ) {
119                     try {
120                         r_json_str = mapper.writeValueAsString(t);
121                     }
122                     catch ( com.fasterxml.jackson.core.JsonProcessingException j ) {
123                         logger.debug(EELFLoggerDelegate.debugLogger,getMethodName() + " Unable to parse object of type " + t.getClass().getName() + " as json", j);
124                     }
125             }
126             return (r_json_str);
127         }
128         
129         /**
130          * The main method.
131          *
132          * @param args the arguments
133          */
134         public static void main(String[] args) {
135                 // TODO Auto-generated method stub
136
137         }
138 }