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