2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.mso;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import io.joshworks.restclient.http.HttpResponse;
26 import org.apache.commons.lang3.ObjectUtils;
27 import org.glassfish.jersey.client.ClientResponse;
28 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import static org.onap.vid.utils.Logging.getMethodName;
35 public class MsoUtil {
38 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoUtil.class);
43 * @param body the body
44 * @param statusCode the status code
45 * @return the mso response wrapper
47 public static MsoResponseWrapper wrapResponse ( String body, int statusCode ) {
49 MsoResponseWrapper w = new MsoResponseWrapper();
50 w.setStatus (statusCode);
59 * @param cres the cres
60 * @return the mso response wrapper
62 public static MsoResponseWrapper wrapResponse (ClientResponse cres) {
66 respStr = cres.readEntity(String.class);
67 statuscode = cres.getStatus();
69 MsoResponseWrapper w = MsoUtil.wrapResponse ( respStr, statuscode );
77 * @return the mso response wrapper
79 public static MsoResponseWrapper wrapResponse (RestObject<String> rs) {
80 String respStr = null;
83 respStr = rs.get() != null ? rs.get() : rs.getRaw();
84 status = rs.getStatusCode();
86 MsoResponseWrapper w = MsoUtil.wrapResponse ( respStr, status );
90 public static <T> MsoResponseWrapper wrapResponse (HttpResponse<T> rs) {
91 MsoResponseWrapper w = new MsoResponseWrapper();
92 w.setStatus (rs.getStatus());
93 if(rs.getRawBody() != null) {
94 w.setEntity(ObjectUtils.toString(rs.getBody()));
100 * Convert pojo to string.
102 * @param <T> the generic type
105 * @throws JsonProcessingException the json processing exception
107 public static <T> String convertPojoToString ( T t ) {
108 ObjectMapper mapper = new ObjectMapper();
109 String rJsonStr = "";
112 rJsonStr = mapper.writeValueAsString(t);
114 catch ( com.fasterxml.jackson.core.JsonProcessingException j ) {
115 logger.debug(EELFLoggerDelegate.debugLogger,getMethodName() + " Unable to parse object of type " + t.getClass().getName() + " as json", j);
124 * @param args the arguments
126 public static void main(String[] args) {
127 // TODO Auto-generated method stub