2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
22 package org.onap.clamp.configuration;
24 import com.google.gson.Gson;
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;
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;
42 public class ClampGsonDataFormat extends ServiceSupport implements DataFormat, DataFormatName {
44 private Class<?> unmarshalType;
45 private Type unmarshalGenericType;
46 private boolean contentTypeHeader = true;
48 public ClampGsonDataFormat() {
53 * Use the default Gson {@link Gson} and with a custom unmarshal type
55 * @param unmarshalType
56 * the custom unmarshal type
58 public ClampGsonDataFormat(Class<?> unmarshalType) {
59 this(null, unmarshalType);
63 * Use a custom Gson mapper and and unmarshal type
67 * @param unmarshalType
68 * the custom unmarshal type
70 public ClampGsonDataFormat(Gson gson, Class<?> unmarshalType) {
72 this.unmarshalType = unmarshalType;
76 * Use the default Gson {@link Gson} and with a custom unmarshal generic type
78 * @param unmarshalGenericType
79 * the custom unmarshal generic type
81 public ClampGsonDataFormat(Type unmarshalGenericType) {
82 this(null, unmarshalGenericType);
86 * Use a custom Gson mapper and and unmarshal token type
90 * @param unmarshalGenericType
91 * the custom unmarshal generic type
93 public ClampGsonDataFormat(Gson gson, Type unmarshalGenericType) {
95 this.unmarshalGenericType = unmarshalGenericType;
99 public String getDataFormatName() {
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);
110 if (contentTypeHeader) {
111 if (exchange.hasOut()) {
112 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
114 exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
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);
126 return gson.fromJson(reader, unmarshalGenericType);
132 protected void doStart() throws Exception {
134 gson = JsonUtils.GSON_JPA_MODEL;
139 protected void doStop() throws Exception {
144 // -------------------------------------------------------------------------
146 public Class<?> getUnmarshalType() {
147 return this.unmarshalType;
150 public void setUnmarshalType(Class<?> unmarshalType) {
151 this.unmarshalType = unmarshalType;
154 public Type getUnmarshalGenericType() {
155 return this.unmarshalType;
158 public void setUnmarshalGenericType(Type unmarshalGenericType) {
159 this.unmarshalGenericType = unmarshalGenericType;
162 public boolean isContentTypeHeader() {
163 return contentTypeHeader;
167 * If enabled then Gson will set the Content-Type header to
168 * <tt>application/json</tt> when marshalling.
170 public void setContentTypeHeader(boolean contentTypeHeader) {
171 this.contentTypeHeader = contentTypeHeader;
174 public Gson getGson() {