2019-03-22 15:18:23 +01:00
|
|
|
package org.gcube.informationsystem.utils;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.Writer;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.ServiceLoader;
|
|
|
|
|
2019-10-23 16:55:48 +02:00
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
2020-01-30 10:26:43 +01:00
|
|
|
import org.gcube.informationsystem.base.reference.Element;
|
2020-02-03 10:51:29 +01:00
|
|
|
import org.gcube.informationsystem.types.reference.Type;
|
|
|
|
import org.gcube.informationsystem.utils.discovery.ElementSpecilizationDiscovery;
|
2019-03-22 15:18:23 +01:00
|
|
|
import org.gcube.informationsystem.utils.discovery.RegistrationProvider;
|
|
|
|
import org.gcube.informationsystem.utils.discovery.SchemaAction;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonGenerationException;
|
|
|
|
import com.fasterxml.jackson.core.JsonParseException;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.JavaType;
|
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
2020-02-03 10:51:29 +01:00
|
|
|
public abstract class ElementMapper {
|
2019-03-22 15:18:23 +01:00
|
|
|
|
2020-02-03 10:51:29 +01:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(ElementMapper.class);
|
2019-03-22 15:18:23 +01:00
|
|
|
|
|
|
|
protected static final ObjectMapper mapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the ObjectMapper
|
|
|
|
*/
|
|
|
|
public static ObjectMapper getObjectMapper() {
|
|
|
|
return mapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
static {
|
|
|
|
mapper = new ObjectMapper();
|
|
|
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
2019-10-30 09:57:14 +01:00
|
|
|
List<Package> packages = new ArrayList<Package>();
|
|
|
|
|
2020-02-03 10:51:29 +01:00
|
|
|
/*
|
|
|
|
Class<Type> tdClz = Type.class;
|
|
|
|
ElementMapper.registerSubtypes(tdClz);
|
2019-10-30 09:57:14 +01:00
|
|
|
packages.add(tdClz.getPackage());
|
2020-02-03 10:51:29 +01:00
|
|
|
*/
|
2019-10-30 09:57:14 +01:00
|
|
|
|
2019-03-22 15:18:23 +01:00
|
|
|
AccessType[] accessTypes = AccessType.values();
|
|
|
|
for(AccessType accessType : accessTypes) {
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
Class clz = accessType.getTypeClass();
|
2020-02-03 10:51:29 +01:00
|
|
|
if(!Type.class.isAssignableFrom(clz)) {
|
|
|
|
Class<Element> dummyClz = accessType.getDummyImplementationClass();
|
|
|
|
if(dummyClz != null) {
|
|
|
|
SimpleModule isModule = new SimpleModule(accessType.getName());
|
|
|
|
isModule.addDeserializer(clz, new ElementDeserializer<>(clz, mapper));
|
|
|
|
mapper.registerModule(isModule);
|
|
|
|
ElementMapper.registerSubtypes(dummyClz);
|
|
|
|
}
|
|
|
|
packages.add(clz.getPackage());
|
2019-10-28 17:36:09 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 15:18:23 +01:00
|
|
|
}
|
2019-10-30 09:57:14 +01:00
|
|
|
registerPackages(packages);
|
2019-03-22 15:18:23 +01:00
|
|
|
|
|
|
|
ServiceLoader<? extends RegistrationProvider> regsitrationProviders = ServiceLoader.load(RegistrationProvider.class);
|
|
|
|
for (RegistrationProvider registrationProvider : regsitrationProviders) {
|
|
|
|
registerPackages(registrationProvider.getPackagesToRegister());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerPackages(List<Package> packages) {
|
2019-10-30 09:57:14 +01:00
|
|
|
SchemaAction schemaAction = new ISMappingAction();
|
2019-03-22 15:18:23 +01:00
|
|
|
try {
|
2020-02-03 10:51:29 +01:00
|
|
|
ElementSpecilizationDiscovery.manageISM(schemaAction, packages);
|
2019-03-22 15:18:23 +01:00
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Error registering types", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void registerPackages(Package... packages) {
|
2019-10-30 09:57:14 +01:00
|
|
|
SchemaAction schemaAction = new ISMappingAction();
|
2019-03-22 15:18:23 +01:00
|
|
|
try {
|
2020-02-03 10:51:29 +01:00
|
|
|
ElementSpecilizationDiscovery.manageISM(schemaAction, packages);
|
2019-03-22 15:18:23 +01:00
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Error registering types", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-03 10:51:29 +01:00
|
|
|
public static <El extends Element> void registerSubtypes(Class<El>... classes) {
|
2019-03-22 15:18:23 +01:00
|
|
|
mapper.registerSubtypes(classes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the serialization of a given resource to a given
|
|
|
|
* {@link OutputStream} .
|
|
|
|
*
|
|
|
|
* @param object the resource
|
|
|
|
* @param stream the stream in input
|
|
|
|
* @throws IOException
|
|
|
|
* @throws JsonMappingException
|
|
|
|
* @throws JsonGenerationException
|
|
|
|
*/
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <T extends OutputStream, ISM extends Element> T marshal(ISM object, T stream)
|
2019-03-22 15:18:23 +01:00
|
|
|
throws JsonGenerationException, JsonMappingException, IOException {
|
|
|
|
mapper.writeValue(stream, object);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the serialization of a given resource to a given {@link Writer} .
|
|
|
|
* @param object the resource
|
|
|
|
* @param writer the writer in input
|
|
|
|
* @throws IOException
|
|
|
|
* @throws JsonMappingException
|
|
|
|
* @throws JsonGenerationException
|
|
|
|
*/
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <T extends Writer, ISM extends Element> T marshal(ISM object, T writer)
|
2019-03-22 15:18:23 +01:00
|
|
|
throws JsonGenerationException, JsonMappingException, IOException {
|
|
|
|
mapper.writeValue(writer, object);
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the String serialization of a given object
|
|
|
|
* @param object the object to marshal
|
|
|
|
* @return the String serialization of a given resource
|
|
|
|
* @throws JsonProcessingException
|
|
|
|
*/
|
2020-02-03 10:51:29 +01:00
|
|
|
public static <El extends Element> String marshal(El object) throws JsonProcessingException {
|
2019-03-22 15:18:23 +01:00
|
|
|
return mapper.writeValueAsString(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the String serialization of a given list
|
|
|
|
* @param list the list to marshal
|
|
|
|
* @return the String serialization of a given list
|
|
|
|
* @throws JsonProcessingException
|
|
|
|
*/
|
2020-02-03 10:51:29 +01:00
|
|
|
public static <El extends Element> String marshal(List<El> list) throws JsonProcessingException {
|
2020-01-30 10:26:43 +01:00
|
|
|
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, Element.class);
|
2019-03-22 15:18:23 +01:00
|
|
|
return mapper.writerFor(type).writeValueAsString(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the String serialization of a given array
|
|
|
|
* @param array the array to marshal
|
|
|
|
* @return the String serialization of a given array
|
|
|
|
* @throws JsonProcessingException
|
|
|
|
*/
|
2020-02-03 10:51:29 +01:00
|
|
|
public static <El extends Element> String marshal(El[] array) throws JsonProcessingException {
|
2019-03-22 15:18:23 +01:00
|
|
|
return mapper.writeValueAsString(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a resource of given class from its serialization in a given
|
|
|
|
* {@link Reader}.
|
|
|
|
* @param clz the class of the resource
|
|
|
|
* @param reader the reader
|
|
|
|
* @return the resource
|
|
|
|
* @throws JsonParseException
|
|
|
|
* @throws JsonMappingException
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2020-02-03 10:51:29 +01:00
|
|
|
public static <El extends Element> El unmarshal(Class<El> clz, Reader reader)
|
2019-03-22 15:18:23 +01:00
|
|
|
throws JsonParseException, JsonMappingException, IOException {
|
|
|
|
return mapper.readValue(reader, clz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a resource of given class from its serialization in a given
|
|
|
|
* {@link InputStream}.
|
|
|
|
* @param clz the class of the resource
|
|
|
|
* @param stream the stream
|
|
|
|
* @return the resource
|
|
|
|
* @throws IOException
|
|
|
|
* @throws JsonMappingException
|
|
|
|
* @throws JsonParseException
|
|
|
|
*/
|
2020-02-03 10:51:29 +01:00
|
|
|
public static <El extends Element> El unmarshal(Class<El> clz, InputStream stream)
|
2019-03-22 15:18:23 +01:00
|
|
|
throws JsonParseException, JsonMappingException, IOException {
|
|
|
|
return mapper.readValue(stream, clz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a resource of given class from its serialization in a given String
|
|
|
|
* @param clz the class of the resource
|
|
|
|
* @param string
|
|
|
|
* @return the resource
|
|
|
|
* @throws JsonParseException
|
|
|
|
* @throws JsonMappingException
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <ISM extends Element> ISM unmarshal(Class<ISM> clz, String string)
|
2019-03-22 15:18:23 +01:00
|
|
|
throws JsonParseException, JsonMappingException, IOException {
|
|
|
|
return mapper.readValue(string, clz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a resource of given class from its serialization in a given String
|
|
|
|
* @param clz the class of the resource
|
|
|
|
* @param string
|
|
|
|
* @return the resource
|
|
|
|
* @throws JsonParseException
|
|
|
|
* @throws JsonMappingException
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <ISM extends Element> ISM unmarshalWithReader(Class<ISM> clz, String string)
|
2019-03-22 15:18:23 +01:00
|
|
|
throws JsonParseException, JsonMappingException, IOException {
|
2019-12-05 17:16:56 +01:00
|
|
|
return mapper.readerFor(clz).readValue(string);
|
2019-03-22 15:18:23 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <ISM extends Element> List<ISM> unmarshalList(Class<ISM> clz, String string)
|
2019-03-22 15:18:23 +01:00
|
|
|
throws JsonParseException, JsonMappingException, IOException {
|
|
|
|
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clz);
|
|
|
|
return mapper.readValue(string, type);
|
|
|
|
}
|
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <ISM extends Element> List<ISM> unmarshalList(String string)
|
2019-03-22 15:18:23 +01:00
|
|
|
throws JsonParseException, JsonMappingException, IOException {
|
2020-01-30 10:26:43 +01:00
|
|
|
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Element.class);
|
2019-03-22 15:18:23 +01:00
|
|
|
return mapper.readValue(string, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|