Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame^] | 1 | /*-
|
| 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 |
|
| 23 | package org.onap.osam.policy;
|
| 24 |
|
| 25 | import java.text.DateFormat;
|
| 26 | import java.text.SimpleDateFormat;
|
| 27 | import java.util.Date;
|
| 28 |
|
| 29 | import org.glassfish.jersey.client.ClientResponse;
|
| 30 | import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
|
| 31 |
|
| 32 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 33 |
|
| 34 | public class PolicyUtil {
|
| 35 |
|
| 36 | private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PolicyUtil.class);
|
| 37 |
|
| 38 | final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
|
| 39 |
|
| 40 | public static PolicyResponseWrapper wrapResponse ( String body, int statusCode ) {
|
| 41 |
|
| 42 | PolicyResponseWrapper w = new PolicyResponseWrapper();
|
| 43 | w.setStatus (statusCode);
|
| 44 | w.setEntity(body);
|
| 45 |
|
| 46 | return w;
|
| 47 | }
|
| 48 |
|
| 49 | public static PolicyResponseWrapper wrapResponse (ClientResponse cres) {
|
| 50 | String resp_str = "";
|
| 51 | if ( cres != null ) {
|
| 52 | resp_str = cres.readEntity(String.class);
|
| 53 | }
|
| 54 | int statuscode = cres.getStatus();
|
| 55 | PolicyResponseWrapper w = PolicyUtil.wrapResponse ( resp_str, statuscode );
|
| 56 | return (w);
|
| 57 | }
|
| 58 |
|
| 59 | public static PolicyResponseWrapper wrapResponse (RestObject<String> rs) {
|
| 60 | String resp_str = "";
|
| 61 | int status = 0;
|
| 62 | if ( rs != null ) {
|
| 63 | resp_str = rs.get();
|
| 64 | status = rs.getStatusCode();
|
| 65 | }
|
| 66 | PolicyResponseWrapper w = PolicyUtil.wrapResponse ( resp_str, status );
|
| 67 | return (w);
|
| 68 | }
|
| 69 |
|
| 70 | public static <T> String convertPojoToString ( T t ) throws com.fasterxml.jackson.core.JsonProcessingException {
|
| 71 |
|
| 72 | String methodName = "convertPojoToString";
|
| 73 | ObjectMapper mapper = new ObjectMapper();
|
| 74 | String r_json_str = "";
|
| 75 | if ( t != null ) {
|
| 76 | try {
|
| 77 | r_json_str = mapper.writeValueAsString(t);
|
| 78 | }
|
| 79 | catch ( com.fasterxml.jackson.core.JsonProcessingException j ) {
|
| 80 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " Unable to parse object as json");
|
| 81 | }
|
| 82 | }
|
| 83 | return (r_json_str);
|
| 84 | }
|
| 85 |
|
| 86 |
|
| 87 | public static void main(String[] args) {
|
| 88 | // TODO Auto-generated method stub
|
| 89 | }
|
| 90 | }
|