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
57 * the custom unmarshal type
59 public ClampGsonDataFormat(Class<?> unmarshalType) {
60 this(null, unmarshalType);
64 * Use a custom Gson mapper and and unmarshal type.
68 * @param unmarshalType
69 * the custom unmarshal type
71 public ClampGsonDataFormat(Gson gson, Class<?> unmarshalType) {
73 this.unmarshalType = unmarshalType;
77 * Use the default Gson {@link Gson} and with a custom unmarshal generic type.
79 * @param unmarshalGenericType
80 * the custom unmarshal generic type
82 public ClampGsonDataFormat(Type unmarshalGenericType) {
83 this(null, unmarshalGenericType);
87 * Use a custom Gson mapper and and unmarshal token type.
91 * @param unmarshalGenericType
92 * the custom unmarshal generic type
94 public ClampGsonDataFormat(Gson gson, Type unmarshalGenericType) {
96 this.unmarshalGenericType = unmarshalGenericType;
100 public String getDataFormatName() {
105 public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
106 try (final OutputStreamWriter osw = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
107 final BufferedWriter writer = IOHelper.buffered(osw)) {
108 gson.toJson(graph, writer);
111 if (contentTypeHeader) {
112 if (exchange.hasOut()) {
113 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
115 exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
121 public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
122 try (final InputStreamReader isr = new InputStreamReader(stream, StandardCharsets.UTF_8);
123 final BufferedReader reader = IOHelper.buffered(isr)) {
124 if (unmarshalGenericType == null) {
125 return gson.fromJson(reader, unmarshalType);
127 return gson.fromJson(reader, unmarshalGenericType);
133 protected void doStart() throws Exception {
135 gson = JsonUtils.GSON_JPA_MODEL;
140 protected void doStop() throws Exception {
145 // -------------------------------------------------------------------------
147 public Class<?> getUnmarshalType() {
148 return this.unmarshalType;
151 public void setUnmarshalType(Class<?> unmarshalType) {
152 this.unmarshalType = unmarshalType;
155 public Type getUnmarshalGenericType() {
156 return this.unmarshalType;
159 public void setUnmarshalGenericType(Type unmarshalGenericType) {
160 this.unmarshalGenericType = unmarshalGenericType;
163 public boolean isContentTypeHeader() {
164 return contentTypeHeader;
168 * If enabled then Gson will set the Content-Type header to
169 * <tt>application/json</tt> when marshalling.
171 public void setContentTypeHeader(boolean contentTypeHeader) {
172 this.contentTypeHeader = contentTypeHeader;
175 public Gson getGson() {