AAI-common sonar fixes
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / util / MapperUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.util;
24
25 import com.fasterxml.jackson.annotation.JsonInclude;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.SerializationFeature;
29 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
30 import org.onap.aai.exceptions.AAIException;
31
32 public class MapperUtil {
33
34     /**
35      * Instantiates MapperUtil.
36      */
37     private MapperUtil() {
38         // prevent instantiation
39     }
40
41     /**
42      * Read as object of.
43      *
44      * @param <T> the generic type
45      * @param clazz the clazz
46      * @param value the value
47      * @return the t
48      * @throws AAIException the AAI exception
49      */
50     public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
51         ObjectMapper mapper = new ObjectMapper();
52         try {
53             return mapper.readValue(value, clazz);
54         } catch (Exception e) {
55             throw new AAIException("AAI_4007", e);
56         }
57     }
58
59     /**
60      * Read with dashes as object of.
61      *
62      * @param <T> the generic type
63      * @param clazz the clazz
64      * @param value the value
65      * @return the t
66      * @throws AAIException the AAI exception
67      */
68     public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
69         ObjectMapper mapper = new ObjectMapper();
70         try {
71             mapper.registerModule(new JaxbAnnotationModule());
72             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
73             mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
74
75             return mapper.readValue(value, clazz);
76         } catch (Exception e) {
77             throw new AAIException("AAI_4007", e);
78         }
79     }
80
81     /**
82      * Write as JSON string.
83      *
84      * @param obj the obj
85      * @return the string
86      * @throws AAIException the AAI exception
87      */
88     public static String writeAsJSONString(Object obj) throws AAIException {
89         ObjectMapper mapper = new ObjectMapper();
90         try {
91             return mapper.writeValueAsString(obj);
92         } catch (Exception e) {
93             throw new AAIException("AAI_4008", e);
94         }
95     }
96
97     /**
98      * Write as JSON string with dashes.
99      *
100      * @param obj the obj
101      * @return the string
102      * @throws AAIException the AAI exception
103      */
104     public static String writeAsJSONStringWithDashes(Object obj) throws AAIException {
105         ObjectMapper mapper = new ObjectMapper();
106         try {
107             mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
108
109             mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
110             mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
111             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
112
113             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
114             mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
115
116             mapper.registerModule(new JaxbAnnotationModule());
117             return mapper.writeValueAsString(obj);
118         } catch (Exception e) {
119             throw new AAIException("AAI_4008", e);
120         }
121     }
122 }