Sonar fixes in MapperUtil.java
[aai/aai-common.git] / aai-core / 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 package org.onap.aai.util;
23
24 import org.onap.aai.exceptions.AAIException;
25 import com.fasterxml.jackson.annotation.JsonInclude;
26 import com.fasterxml.jackson.databind.*;
27 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
28
29 public class MapperUtil {
30
31     /**
32      * Instantiates MapperUtil.
33      */
34     private MapperUtil() {
35         // prevent instantiation
36     }
37     
38     /**
39      * Read as object of.
40      *
41      * @param <T> the generic type
42      * @param clazz the clazz
43      * @param value the value
44      * @return the t
45      * @throws AAIException the AAI exception
46      */
47     public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
48         ObjectMapper mapper = new ObjectMapper();
49         try {
50             return mapper.readValue(value, clazz);
51         } catch (Exception e) {
52             throw new AAIException("AAI_4007", e);
53         }
54     }
55
56     /**
57      * Read with dashes as object of.
58      *
59      * @param <T> the generic type
60      * @param clazz the clazz
61      * @param value the value
62      * @return the t
63      * @throws AAIException the AAI exception
64      */
65     public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
66         ObjectMapper mapper = new ObjectMapper();
67         try {
68             mapper.registerModule(new JaxbAnnotationModule());
69             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
70             mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
71
72             return mapper.readValue(value, clazz);
73         } catch (Exception e) {
74             throw new AAIException("AAI_4007", e);
75         }
76     }
77
78     /**
79      * Write as JSON string.
80      *
81      * @param obj the obj
82      * @return the string
83      * @throws AAIException the AAI exception
84      */
85     public static String writeAsJSONString(Object obj) throws AAIException {
86         ObjectMapper mapper = new ObjectMapper();
87         try {
88             return mapper.writeValueAsString(obj);
89         } catch (Exception e) {
90             throw new AAIException("AAI_4008", e);
91         }
92     }
93
94     /**
95      * Write as JSON string with dashes.
96      *
97      * @param obj the obj
98      * @return the string
99      * @throws AAIException the AAI exception
100      */
101     public static String writeAsJSONStringWithDashes(Object obj) throws AAIException {
102         ObjectMapper mapper = new ObjectMapper();
103         try {
104             mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
105
106             mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
107             mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
108             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
109
110             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
111             mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
112
113             mapper.registerModule(new JaxbAnnotationModule());
114             return mapper.writeValueAsString(obj);
115         } catch (Exception e) {
116             throw new AAIException("AAI_4008", e);
117         }
118     }
119 }