Upgrade sonar plugin
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / mso / MsoUtil.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * VID\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.vid.mso;\r
22 import java.text.DateFormat;\r
23 import java.text.SimpleDateFormat;\r
24 import java.util.Date;\r
25 \r
26 import org.glassfish.jersey.client.ClientResponse;\r
27 \r
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
29 import org.openecomp.vid.controller.MsoController;\r
30 import com.fasterxml.jackson.databind.ObjectMapper;\r
31 \r
32 /**\r
33  * The Class MsoUtil.\r
34  */\r
35 public class MsoUtil {\r
36         \r
37         /** The logger. */\r
38         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoController.class);\r
39         \r
40         /** The Constant dateFormat. */\r
41         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");\r
42         \r
43         /**\r
44          * Wrap response.\r
45          *\r
46          * @param body the body\r
47          * @param statusCode the status code\r
48          * @return the mso response wrapper\r
49          */\r
50         public static MsoResponseWrapper wrapResponse ( String body, int statusCode ) {\r
51                 \r
52                 MsoResponseWrapper w = new MsoResponseWrapper();\r
53                 w.setStatus (statusCode);\r
54                 w.setEntity(body);\r
55                 \r
56                 return w;\r
57         }\r
58         \r
59         /**\r
60          * Wrap response.\r
61          *\r
62          * @param cres the cres\r
63          * @return the mso response wrapper\r
64          */\r
65         public static MsoResponseWrapper wrapResponse (ClientResponse cres) {   \r
66                 String resp_str = "";\r
67                 if ( cres != null ) {\r
68                         resp_str = cres.readEntity(String.class);\r
69                 }\r
70                 int statuscode = cres.getStatus();\r
71                 MsoResponseWrapper w = MsoUtil.wrapResponse ( resp_str, statuscode );\r
72                 return (w);\r
73         }\r
74         \r
75         /**\r
76          * Wrap response.\r
77          *\r
78          * @param rs the rs\r
79          * @return the mso response wrapper\r
80          */\r
81         public static MsoResponseWrapper wrapResponse (RestObject<String> rs) { \r
82                 String resp_str = "";\r
83                 int status = 0;\r
84                 if ( rs != null ) {\r
85                         resp_str = rs.get();\r
86                         status = rs.getStatusCode();\r
87                 }\r
88                 MsoResponseWrapper w = MsoUtil.wrapResponse ( resp_str, status );\r
89                 return (w);\r
90         }       \r
91         \r
92         /**\r
93          * Convert pojo to string.\r
94          *\r
95          * @param <T> the generic type\r
96          * @param t the t\r
97          * @return the string\r
98          * @throws JsonProcessingException the json processing exception\r
99          */\r
100         public static <T> String convertPojoToString ( T t ) throws com.fasterxml.jackson.core.JsonProcessingException {\r
101                 \r
102                 String methodName = "convertPojoToString";\r
103                 ObjectMapper mapper = new ObjectMapper();\r
104                 String r_json_str = "";\r
105             if ( t != null ) {\r
106                     try {\r
107                         r_json_str = mapper.writeValueAsString(t);\r
108                     }\r
109                     catch ( com.fasterxml.jackson.core.JsonProcessingException j ) {\r
110                         logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " +  methodName + " Unable to parse object as json");\r
111                     }\r
112             }\r
113             return (r_json_str);\r
114         }\r
115         \r
116         /**\r
117          * The main method.\r
118          *\r
119          * @param args the arguments\r
120          */\r
121         public static void main(String[] args) {\r
122                 // TODO Auto-generated method stub\r
123 \r
124         }\r
125 }\r