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