All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.fraunhofer.iese.ind2uce.json.Ind2uceConverter Maven / Gradle / Ivy

Go to download

IND2UCE :: Webproject related contents like JSON, Autoconfiguration of Spring, etc.

There is a newer version: 3.2.69
Show newest version
/*-
 * =================================LICENSE_START=================================
 * IND2UCE
 * %%
 * Copyright (C) 2016 Fraunhofer IESE (www.iese.fraunhofer.de)
 * %%
 * The Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Hansastrasse 27c, 80686 Munich, Germany (further: Fraunhofer) is holder of all proprietary rights on this computer program.
 * You can only use this computer program if you have closed a license agreement with Fraunhofer or you get the right to use the computer program from someone who is authorized to grant you that right.
 * Any use of the computer program without a valid license is prohibited and liable to prosecution.
 *
 * Copyright (C) 2018 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V. acting on behalf of its Fraunhofer Institute for Experimental Software Engineering (IESE)
 *
 * All rights reserved.
 *
 * Contact: [email protected]
 * =================================LICENSE_END=================================
 */

package de.fraunhofer.iese.ind2uce.json;

import com.google.gson.Gson;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

// TODO: Auto-generated Javadoc
/**
 * Implementation of
 * {@link org.springframework.http.converter.HttpMessageConverter} that can read
 * and write JSON using the
 * Google Gson library's
 * {@link Gson} class.
 * 

* This converter can be used to bind to typed beans or untyped {@code HashMap} * s. By default, it supports {@code application/json} and * {@code application/*+json}. *

*

* Tested against Gson 2.3; compatible with Gson 2.0 and higher. *

* * @author Roy Clarkson * @see #setSupportedMediaTypes * @since 4.1 */ public class Ind2uceConverter extends AbstractHttpMessageConverter implements GenericHttpMessageConverter { /** The Constant DEFAULT_CHARSET. */ public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); /** The gson converter. */ private final GsonConverter gsonConverter; /** The jackson converter. */ private final JacksonConverter jacksonConverter; /** The jackson classes. */ Set> jacksonClasses = new HashSet<>(); /** * Instantiates a new ind 2 uce converter. * * @param jacksonClasses the jackson classes */ public Ind2uceConverter(Class... jacksonClasses) { super(new MediaType("application", "json", DEFAULT_CHARSET), new MediaType("application", "*+json", DEFAULT_CHARSET), new MediaType("*", "*", DEFAULT_CHARSET)); if (jacksonClasses != null && jacksonClasses.length > 0) { this.jacksonClasses.addAll(Arrays.asList(jacksonClasses)); } this.gsonConverter = new GsonConverter(); this.jacksonConverter = new JacksonConverter(); } /* * (non-Javadoc) * @see * org.springframework.http.converter.AbstractHttpMessageConverter#canRead( * java.lang.Class, org.springframework.http.MediaType) */ @Override public boolean canRead(Class clazz, MediaType mediaType) { return this.canRead(mediaType); } /* * (non-Javadoc) * @see * org.springframework.http.converter.GenericHttpMessageConverter#canRead(java * .lang.reflect.Type, java.lang.Class, org.springframework.http.MediaType) */ @Override public boolean canRead(Type type, Class contextClass, MediaType mediaType) { return this.canRead(mediaType); } /* * (non-Javadoc) * @see * org.springframework.http.converter.AbstractHttpMessageConverter#canWrite( * java.lang.Class, org.springframework.http.MediaType) */ @Override public boolean canWrite(Class clazz, MediaType mediaType) { return this.canWrite(mediaType); } /* * (non-Javadoc) * @see * org.springframework.http.converter.GenericHttpMessageConverter#canWrite( * java.lang.reflect.Type, java.lang.Class, * org.springframework.http.MediaType) */ @Override public boolean canWrite(Type type, Class clazz, MediaType mediaType) { return this.canWrite(mediaType); } /* * (non-Javadoc) * @see * org.springframework.http.converter.AbstractHttpMessageConverter#supports( * java.lang.Class) */ @Override protected boolean supports(Class clazz) { throw new UnsupportedOperationException(); } /* * (non-Javadoc) * @see * org.springframework.http.converter.GenericHttpMessageConverter#read(java. * lang.reflect.Type, java.lang.Class, * org.springframework.http.HttpInputMessage) */ @Override public Object read(Type type, Class contextClass, HttpInputMessage inputMessage) throws IOException { if (this.jacksonClasses.contains(contextClass)) { return this.jacksonConverter.read(type, contextClass, inputMessage); } return this.gsonConverter.read(type, contextClass, inputMessage); } /* * (non-Javadoc) * @see org.springframework.http.converter.AbstractHttpMessageConverter# * readInternal(java.lang.Class, org.springframework.http.HttpInputMessage) */ @Override protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { if (this.jacksonClasses.contains(clazz)) { return this.jacksonConverter.readInternal(clazz, inputMessage); } return this.gsonConverter.readInternal(clazz, inputMessage); } /* * (non-Javadoc) * @see org.springframework.http.converter.AbstractHttpMessageConverter# * writeInternal(java.lang.Object, org.springframework.http.HttpOutputMessage) */ @Override protected void writeInternal(Object t, HttpOutputMessage outputMessage) throws IOException { if (this.jacksonClasses.contains(t.getClass())) { this.jacksonConverter.writeInternal(t, outputMessage); } else { this.gsonConverter.writeInternal(t, outputMessage); } } /* * (non-Javadoc) * @see * org.springframework.http.converter.GenericHttpMessageConverter#write(java. * lang.Object, java.lang.reflect.Type, org.springframework.http.MediaType, * org.springframework.http.HttpOutputMessage) */ @Override public void write(Object arg0, Type arg1, MediaType arg2, HttpOutputMessage arg3) throws IOException, HttpMessageNotWritableException { if (this.jacksonClasses.contains(arg0.getClass())) { this.jacksonConverter.write(arg0, arg1, arg2, arg3); } else { this.gsonConverter.write(arg0, arg1, arg2, arg3); } } }