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