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