2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.vid.mso;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import io.joshworks.restclient.http.HttpResponse;
27 import org.apache.commons.lang3.ObjectUtils;
28 import org.glassfish.jersey.client.ClientResponse;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
34 import static org.onap.vid.utils.Logging.getMethodName;
39 public class MsoUtil {
42 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoUtil.class);
44 /** The Constant dateFormat. */
45 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
50 * @param body the body
51 * @param statusCode the status code
52 * @return the mso response wrapper
54 public static MsoResponseWrapper wrapResponse ( String body, int statusCode ) {
56 MsoResponseWrapper w = new MsoResponseWrapper();
57 w.setStatus (statusCode);
66 * @param cres the cres
67 * @return the mso response wrapper
69 public static MsoResponseWrapper wrapResponse (ClientResponse cres) {
72 resp_str = cres.readEntity(String.class);
74 int statuscode = cres.getStatus();
75 MsoResponseWrapper w = MsoUtil.wrapResponse ( resp_str, statuscode );
83 * @return the mso response wrapper
85 public static MsoResponseWrapper wrapResponse (RestObject<String> rs) {
86 String resp_str = null;
89 resp_str = rs.get() != null ? rs.get() : rs.getRaw();
90 status = rs.getStatusCode();
92 MsoResponseWrapper w = MsoUtil.wrapResponse ( resp_str, status );
96 public static <T> MsoResponseWrapper wrapResponse (HttpResponse<T> rs) {
97 MsoResponseWrapper w = new MsoResponseWrapper();
98 w.setStatus (rs.getStatus());
99 if(rs.getRawBody() != null) {
100 w.setEntity(ObjectUtils.toString(rs.getBody()));
106 * Convert pojo to string.
108 * @param <T> the generic type
111 * @throws JsonProcessingException the json processing exception
113 public static <T> String convertPojoToString ( T t ) {
115 String methodName = "convertPojoToString";
116 ObjectMapper mapper = new ObjectMapper();
117 String r_json_str = "";
120 r_json_str = mapper.writeValueAsString(t);
122 catch ( com.fasterxml.jackson.core.JsonProcessingException j ) {
123 logger.debug(EELFLoggerDelegate.debugLogger,getMethodName() + " Unable to parse object of type " + t.getClass().getName() + " as json", j);
132 * @param args the arguments
134 public static void main(String[] args) {
135 // TODO Auto-generated method stub