Sonar fixes
[clamp.git] / src / main / java / org / onap / clamp / configuration / ClampGsonDataFormat.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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============================================
20  * ===================================================================
21  */
22
23 package org.onap.clamp.configuration;
24
25 import com.google.gson.Gson;
26
27 import java.io.BufferedReader;
28 import java.io.BufferedWriter;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.OutputStream;
32 import java.io.OutputStreamWriter;
33 import java.lang.reflect.Type;
34 import java.nio.charset.StandardCharsets;
35
36 import org.apache.camel.Exchange;
37 import org.apache.camel.spi.DataFormat;
38 import org.apache.camel.spi.DataFormatName;
39 import org.apache.camel.support.ServiceSupport;
40 import org.apache.camel.util.IOHelper;
41 import org.onap.clamp.clds.util.JsonUtils;
42
43 public class ClampGsonDataFormat extends ServiceSupport implements DataFormat, DataFormatName {
44     private Gson gson;
45     private Class<?> unmarshalType;
46     private Type unmarshalGenericType;
47     private boolean contentTypeHeader = true;
48
49     public ClampGsonDataFormat() {
50         this(Object.class);
51     }
52
53     /**
54      * Use the default Gson {@link Gson} and with a custom unmarshal type.
55      *
56      * @param unmarshalType the custom unmarshal type
57      */
58     public ClampGsonDataFormat(Class<?> unmarshalType) {
59         this(null, unmarshalType);
60     }
61
62     /**
63      * Use a custom Gson mapper and and unmarshal type.
64      *
65      * @param gson          the custom mapper
66      * @param unmarshalType the custom unmarshal type
67      */
68     public ClampGsonDataFormat(Gson gson, Class<?> unmarshalType) {
69         this.gson = gson;
70         this.unmarshalType = unmarshalType;
71     }
72
73     /**
74      * Use the default Gson {@link Gson} and with a custom unmarshal generic type.
75      *
76      * @param unmarshalGenericType the custom unmarshal generic type
77      */
78     public ClampGsonDataFormat(Type unmarshalGenericType) {
79         this(null, unmarshalGenericType);
80     }
81
82     /**
83      * Use a custom Gson mapper and and unmarshal token type.
84      *
85      * @param gson                 the custom mapper
86      * @param unmarshalGenericType the custom unmarshal generic type
87      */
88     public ClampGsonDataFormat(Gson gson, Type unmarshalGenericType) {
89         this.gson = gson;
90         this.unmarshalGenericType = unmarshalGenericType;
91     }
92
93     @Override
94     public String getDataFormatName() {
95         return "clamp-gson";
96     }
97
98     @Override
99     public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
100         try (final OutputStreamWriter osw = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
101                 final BufferedWriter writer = IOHelper.buffered(osw)) {
102             gson.toJson(graph, writer);
103         }
104
105         if (contentTypeHeader) {
106             if (exchange.hasOut()) {
107                 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
108             } else {
109                 exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
110             }
111         }
112     }
113
114     @Override
115     public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
116         try (final InputStreamReader isr = new InputStreamReader(stream, StandardCharsets.UTF_8);
117                 final BufferedReader reader = IOHelper.buffered(isr)) {
118             if (unmarshalGenericType == null) {
119                 return gson.fromJson(reader, unmarshalType);
120             } else {
121                 return gson.fromJson(reader, unmarshalGenericType);
122             }
123         }
124     }
125
126     @Override
127     protected void doStart() throws Exception {
128         if (gson == null) {
129             gson = JsonUtils.GSON_JPA_MODEL;
130         }
131     }
132
133     @Override
134     protected void doStop() throws Exception {
135         // noop
136     }
137
138     // Properties
139     // -------------------------------------------------------------------------
140
141     public Class<?> getUnmarshalType() {
142         return this.unmarshalType;
143     }
144
145     public void setUnmarshalType(Class<?> unmarshalType) {
146         this.unmarshalType = unmarshalType;
147     }
148
149     public Type getUnmarshalGenericType() {
150         return this.unmarshalGenericType;
151     }
152
153     public void setUnmarshalGenericType(Type unmarshalGenericType) {
154         this.unmarshalGenericType = unmarshalGenericType;
155     }
156
157     public boolean isContentTypeHeader() {
158         return contentTypeHeader;
159     }
160
161     /**
162      * If enabled then Gson will set the Content-Type header to
163      * <tt>application/json</tt> when marshalling.
164      */
165     public void setContentTypeHeader(boolean contentTypeHeader) {
166         this.contentTypeHeader = contentTypeHeader;
167     }
168
169     public Gson getGson() {
170         return this.gson;
171     }
172 }