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 * ===================================================================
23 package org.onap.clamp.configuration;
25 import com.google.gson.Gson;
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;
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;
43 public class ClampGsonDataFormat extends ServiceSupport implements DataFormat, DataFormatName {
45 private Class<?> unmarshalType;
46 private Type unmarshalGenericType;
47 private boolean contentTypeHeader = true;
49 public ClampGsonDataFormat() {
54 * Use the default Gson {@link Gson} and with a custom unmarshal type.
56 * @param unmarshalType the custom unmarshal type
58 public ClampGsonDataFormat(Class<?> unmarshalType) {
59 this(null, unmarshalType);
63 * Use a custom Gson mapper and and unmarshal type.
65 * @param gson the custom mapper
66 * @param unmarshalType the custom unmarshal type
68 public ClampGsonDataFormat(Gson gson, Class<?> unmarshalType) {
70 this.unmarshalType = unmarshalType;
74 * Use the default Gson {@link Gson} and with a custom unmarshal generic type.
76 * @param unmarshalGenericType the custom unmarshal generic type
78 public ClampGsonDataFormat(Type unmarshalGenericType) {
79 this(null, unmarshalGenericType);
83 * Use a custom Gson mapper and and unmarshal token type.
85 * @param gson the custom mapper
86 * @param unmarshalGenericType the custom unmarshal generic type
88 public ClampGsonDataFormat(Gson gson, Type unmarshalGenericType) {
90 this.unmarshalGenericType = unmarshalGenericType;
94 public String getDataFormatName() {
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);
105 if (contentTypeHeader) {
106 if (exchange.hasOut()) {
107 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
109 exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
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);
121 return gson.fromJson(reader, unmarshalGenericType);
127 protected void doStart() throws Exception {
129 gson = JsonUtils.GSON_JPA_MODEL;
134 protected void doStop() throws Exception {
139 // -------------------------------------------------------------------------
141 public Class<?> getUnmarshalType() {
142 return this.unmarshalType;
145 public void setUnmarshalType(Class<?> unmarshalType) {
146 this.unmarshalType = unmarshalType;
149 public Type getUnmarshalGenericType() {
150 return this.unmarshalGenericType;
153 public void setUnmarshalGenericType(Type unmarshalGenericType) {
154 this.unmarshalGenericType = unmarshalGenericType;
157 public boolean isContentTypeHeader() {
158 return contentTypeHeader;
162 * If enabled then Gson will set the Content-Type header to
163 * <tt>application/json</tt> when marshalling.
165 public void setContentTypeHeader(boolean contentTypeHeader) {
166 this.contentTypeHeader = contentTypeHeader;
169 public Gson getGson() {