2022-05-20 12:31:39 +02:00
|
|
|
package org.gcube.application.geoportalcommon;
|
|
|
|
|
2022-09-26 16:16:26 +02:00
|
|
|
import java.io.ByteArrayInputStream;
|
2022-05-20 12:31:39 +02:00
|
|
|
import java.io.IOException;
|
2022-09-26 16:16:26 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
import javax.xml.bind.Unmarshaller;
|
|
|
|
|
2022-10-14 15:22:54 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GeoServerPlatformInfoDV;
|
2022-09-26 16:16:26 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2022-05-20 12:31:39 +02:00
|
|
|
|
2022-10-14 15:22:54 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonParser;
|
2022-05-20 12:31:39 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2022-10-14 15:22:54 +02:00
|
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
2022-05-20 12:31:39 +02:00
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
2022-10-14 15:22:54 +02:00
|
|
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
2022-05-20 12:31:39 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
|
|
|
2022-09-26 16:16:26 +02:00
|
|
|
/**
|
|
|
|
* The Class SerializerUtil.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
|
|
*
|
|
|
|
* Sep 26, 2022
|
|
|
|
*/
|
2022-05-20 12:31:39 +02:00
|
|
|
public class SerializerUtil {
|
2022-09-26 16:16:26 +02:00
|
|
|
|
|
|
|
private static Logger LOG = LoggerFactory.getLogger(SerializerUtil.class);
|
|
|
|
|
2022-05-20 12:31:39 +02:00
|
|
|
private static ObjectMapper mapper;
|
2022-09-27 11:15:11 +02:00
|
|
|
|
2022-09-26 16:16:26 +02:00
|
|
|
private static ObjectMapper mapperXML;
|
|
|
|
|
2022-05-20 12:31:39 +02:00
|
|
|
static {
|
|
|
|
mapper = new ObjectMapper();
|
2022-09-26 16:16:26 +02:00
|
|
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
2022-05-20 12:31:39 +02:00
|
|
|
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
|
|
|
mapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
|
|
|
mapper.registerModule(new JavaTimeModule());
|
|
|
|
|
2022-09-26 16:16:26 +02:00
|
|
|
SimpleModule s = new SimpleModule();
|
|
|
|
mapper.registerModule(s);
|
|
|
|
}
|
2022-05-20 12:31:39 +02:00
|
|
|
|
2022-09-26 16:16:26 +02:00
|
|
|
/**
|
|
|
|
* Read JSON.
|
|
|
|
*
|
|
|
|
* @param <T> the generic type
|
|
|
|
* @param string the string
|
|
|
|
* @param clazz the clazz
|
|
|
|
* @return the t
|
|
|
|
* @throws JsonProcessingException the json processing exception
|
|
|
|
* @throws IOException Signals that an I/O exception has occurred.
|
|
|
|
*/
|
|
|
|
public static <T> T readJSON(String string, Class<T> clazz) throws JsonProcessingException, IOException {
|
|
|
|
return mapper.readerFor(clazz).readValue(string);
|
|
|
|
}
|
2022-05-20 12:31:39 +02:00
|
|
|
|
2022-09-26 16:16:26 +02:00
|
|
|
/**
|
|
|
|
* Unmarshal XML.
|
|
|
|
*
|
2022-09-27 11:15:11 +02:00
|
|
|
* @param <T> the generic type
|
2022-09-26 16:16:26 +02:00
|
|
|
* @param string the string
|
|
|
|
* @param theClass the the class
|
|
|
|
* @return the class
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
public static <T> T unmarshalXML(String string, Class<T> theClass) throws Exception {
|
2022-09-27 11:15:11 +02:00
|
|
|
LOG.debug("unmarshalXML: " + string.trim() + " as class: " + theClass);
|
2022-09-26 16:16:26 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
JAXBContext jaxbContext = JAXBContext.newInstance(theClass);
|
|
|
|
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
|
|
|
|
|
|
|
if (string == null || string.length() == 0)
|
|
|
|
throw new Exception("Input string is null or empty");
|
|
|
|
|
|
|
|
InputStream stream = new ByteArrayInputStream(string.getBytes());
|
2022-09-27 11:15:11 +02:00
|
|
|
T umarshalledObject = (T) jaxbUnmarshaller.unmarshal(stream);
|
|
|
|
LOG.debug("returning: " + umarshalledObject);
|
2022-09-26 16:16:26 +02:00
|
|
|
|
2022-09-27 11:15:11 +02:00
|
|
|
return umarshalledObject;
|
2022-09-26 16:16:26 +02:00
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.error("An error occurred on unmarshalling " + string + "as " + theClass, e);
|
|
|
|
return null;
|
|
|
|
}
|
2022-05-20 12:31:39 +02:00
|
|
|
}
|
2022-10-14 15:22:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
public static class GeoserverPlaformInfoJsonDeserializer extends JsonDeserializer<GeoServerPlatformInfoDV[]> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeoServerPlatformInfoDV[] deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
|
|
|
LOG.info("Sono qui: " +jp.getValueAsString());
|
|
|
|
return jp.readValueAs(GeoServerPlatformInfoDV[].class);
|
|
|
|
//return jp.readValueAs(GeoServerPlatformInfoDV[].class);
|
|
|
|
//List<GeoServerPlatformInfoDV> listPlatform = jp.readValueAs(new TypeReference<List<GeoServerPlatformInfoDV>>() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-05-20 12:31:39 +02:00
|
|
|
|
|
|
|
}
|