8ec8323dd72f033d58a120970f82d80c81fcfa28
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / scheduler / policy / PolicyUtil.java
1 package org.openecomp.portalapp.portal.scheduler.policy;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6
7 import org.glassfish.jersey.client.ClientResponse;
8 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
9 /*import org.openecomp.vid.policy.PolicyResponseWrapper;
10 import org.openecomp.vid.policy.PolicyUtil;
11 import org.openecomp.vid.policy.RestObject;*/
12
13 import com.fasterxml.jackson.databind.ObjectMapper;
14
15 public class PolicyUtil {
16         
17         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PolicyUtil.class);
18         
19         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
20         
21         public static PolicyResponseWrapper wrapResponse ( String body, int statusCode ) {
22                 
23                 PolicyResponseWrapper w = new PolicyResponseWrapper();
24                 w.setStatus (statusCode);
25                 w.setEntity(body);
26                 
27                 return w;
28         }
29         
30         public static PolicyResponseWrapper wrapResponse (ClientResponse cres) {        
31                 String resp_str = "";
32                 if ( cres != null ) {
33                         resp_str = cres.readEntity(String.class);
34                 }
35                 int statuscode = cres.getStatus();
36                 PolicyResponseWrapper w = PolicyUtil.wrapResponse ( resp_str, statuscode );
37                 return (w);
38         }
39         
40         public static PolicyResponseWrapper wrapResponse (RestObject<String> rs) {      
41                 String resp_str = "";
42                 int status = 0;
43                 if ( rs != null ) {
44                         resp_str = rs.get();
45                         status = rs.getStatusCode();
46                 }
47                 PolicyResponseWrapper w = PolicyUtil.wrapResponse ( resp_str, status );
48                 return (w);
49         }
50         
51         public static <T> String convertPojoToString ( T t ) throws com.fasterxml.jackson.core.JsonProcessingException {
52                 
53                 String methodName = "convertPojoToString";
54                 ObjectMapper mapper = new ObjectMapper();
55                 String r_json_str = "";
56             if ( t != null ) {
57                     try {
58                         r_json_str = mapper.writeValueAsString(t);
59                     }
60                     catch ( com.fasterxml.jackson.core.JsonProcessingException j ) {
61                         logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " +  methodName + " Unable to parse object as json");
62                     }
63             }
64             return (r_json_str);
65         }
66         
67         
68         public static void main(String[] args) {
69                 // TODO Auto-generated method stub              
70         }
71 }