Various refactoring
parent
da12a4b1aa
commit
1cdf92ba1a
@ -0,0 +1,138 @@
|
||||
package org.gcube.application.cms;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.gcube.application.geoportal.common.model.document.ComparableVersion;
|
||||
import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
|
||||
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Iterator;
|
||||
|
||||
@Slf4j
|
||||
public class Serialization {
|
||||
|
||||
public static final DateTimeFormatter FULL_FORMATTER=DateTimeFormatter.ofPattern("uuuuMMdd_HH-mm-ss");
|
||||
|
||||
public static ObjectMapper mapper;
|
||||
|
||||
static {
|
||||
mapper=new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
|
||||
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
mapper.registerModule(new JavaTimeModule());
|
||||
|
||||
SimpleModule s=new SimpleModule();
|
||||
s.addDeserializer(ObjectId.class,new ObjectIdDeserializer());
|
||||
s.addSerializer(ObjectId.class,new ObjectIdSerializer());
|
||||
s.addDeserializer(ComparableVersion.class,new ComparableVersionDeserializer());
|
||||
s.addSerializer(ComparableVersion.class,new ComparableVersionSerializer());
|
||||
mapper.registerModule(s);
|
||||
}
|
||||
|
||||
public static <T> T read(String jsonString,Class<T> clazz) throws JsonProcessingException, IOException {
|
||||
return mapper.readerFor(clazz).readValue(jsonString);
|
||||
}
|
||||
|
||||
public static <T> Iterator<T> readCollection(String jsonString, Class<T> clazz) throws IOException {
|
||||
return mapper.readerFor(clazz).readValues(jsonString);
|
||||
}
|
||||
|
||||
|
||||
public static String write(Object toWrite) throws JsonProcessingException {
|
||||
String toReturn= mapper.writeValueAsString(toWrite);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
|
||||
public static QueryRequest parseQuery(String queryString) throws IOException {
|
||||
log.debug("Parsing query Request {} ",queryString);
|
||||
Document queryDocument = Document.parse(queryString);
|
||||
log.debug("Document is ",queryDocument.toJson());
|
||||
QueryRequest req = new QueryRequest();
|
||||
if (queryDocument.getOrDefault("ordering",null)!=null)
|
||||
req.setOrdering(Serialization.read(((Document) queryDocument.get("ordering")).toJson(), QueryRequest.OrderedRequest.class));
|
||||
if (queryDocument.getOrDefault("paging",null)!=null)
|
||||
req.setPaging(Serialization.read(((Document) queryDocument.get("paging")).toJson(), QueryRequest.PagedRequest.class));
|
||||
req.setProjection(queryDocument.get("projection", Document.class));
|
||||
req.setFilter(queryDocument.get("filter", Document.class));
|
||||
return req;
|
||||
}
|
||||
|
||||
|
||||
//**** PROFILED DOCUMENTS
|
||||
|
||||
public static final <T> T convert(Object d,Class<T> clazz){
|
||||
return mapper.convertValue(d,clazz);
|
||||
}
|
||||
|
||||
public static final Document asDocument(Object obj) throws JsonProcessingException {
|
||||
return Document.parse(mapper.writeValueAsString(obj));
|
||||
}
|
||||
|
||||
// ***** Serialization Exceptions
|
||||
|
||||
// OBJECT ID
|
||||
private static class ObjectIdSerializer extends JsonSerializer<ObjectId> {
|
||||
@Override
|
||||
public void serialize(ObjectId objectId, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
|
||||
if (objectId == null) jsonGenerator.writeNull();
|
||||
else jsonGenerator.writeString(objectId.toHexString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<ObjectId> handledType() {
|
||||
return ObjectId.class;
|
||||
}
|
||||
}
|
||||
private static class ObjectIdDeserializer extends JsonDeserializer<ObjectId> {
|
||||
|
||||
@Override
|
||||
public ObjectId deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
|
||||
String value=jsonParser.getValueAsString();
|
||||
if(value==null || value.isEmpty() || value.equals("null"))
|
||||
return null;
|
||||
else return new ObjectId(value);
|
||||
}
|
||||
@Override
|
||||
public Class<ObjectId> handledType() {
|
||||
return ObjectId.class;
|
||||
}
|
||||
}
|
||||
|
||||
//Comparable Version
|
||||
private static class ComparableVersionSerializer extends JsonSerializer<ComparableVersion> {
|
||||
@Override
|
||||
public void serialize(ComparableVersion comparableVersion, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
|
||||
if (comparableVersion == null) jsonGenerator.writeNull();
|
||||
else jsonGenerator.writeString(comparableVersion.getCanonical());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<ComparableVersion> handledType() {
|
||||
return ComparableVersion.class;
|
||||
}
|
||||
}
|
||||
private static class ComparableVersionDeserializer extends JsonDeserializer<ComparableVersion> {
|
||||
|
||||
@Override
|
||||
public ComparableVersion deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
|
||||
String value=jsonParser.getValueAsString();
|
||||
if(value==null || value.isEmpty() || value.equals("null"))
|
||||
return null;
|
||||
else return new ComparableVersion(value);
|
||||
}
|
||||
@Override
|
||||
public Class<ComparableVersion> handledType() {
|
||||
return ComparableVersion.class;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package org.gcube.application.geoportal.common.faults;
|
||||
|
||||
public class JsonParseException extends Exception{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public JsonParseException() {
|
||||
super();
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public JsonParseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public JsonParseException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public JsonParseException(String message) {
|
||||
super(message);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public JsonParseException(Throwable cause) {
|
||||
super(cause);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
package org.gcube.application.geoportal.service.utils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.Document;
|
||||
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Iterator;
|
||||
|
||||
@Slf4j
|
||||
public class Serialization {
|
||||
|
||||
|
||||
public static final DateTimeFormatter FULL_FORMATTER=DateTimeFormatter.ofPattern("uuuuMMdd_HH-mm-ss");
|
||||
|
||||
public static ObjectMapper mapper;
|
||||
|
||||
static {
|
||||
mapper=new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
|
||||
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
mapper.registerModule(new JavaTimeModule());
|
||||
|
||||
}
|
||||
|
||||
public static <T> T read(String jsonString,Class<T> clazz) throws JsonProcessingException, IOException {
|
||||
return mapper.readerFor(clazz).readValue(jsonString);
|
||||
}
|
||||
|
||||
public static <T> Iterator<T> readCollection(String jsonString, Class<T> clazz) throws IOException {
|
||||
return mapper.readerFor(clazz).readValues(jsonString);
|
||||
}
|
||||
|
||||
|
||||
public static String write(Object toWrite) throws JsonProcessingException {
|
||||
String toReturn= mapper.writeValueAsString(toWrite);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
|
||||
public static QueryRequest parseQuery(String queryString) throws IOException {
|
||||
log.debug("Parsing query Request {} ",queryString);
|
||||
Document queryDocument = Document.parse(queryString);
|
||||
log.debug("Document is ",queryDocument.toJson());
|
||||
QueryRequest req = new QueryRequest();
|
||||
if (queryDocument.getOrDefault("ordering",null)!=null)
|
||||
req.setOrdering(Serialization.read(((Document) queryDocument.get("ordering")).toJson(), QueryRequest.OrderedRequest.class));
|
||||
if (queryDocument.getOrDefault("paging",null)!=null)
|
||||
req.setPaging(Serialization.read(((Document) queryDocument.get("paging")).toJson(), QueryRequest.PagedRequest.class));
|
||||
req.setProjection(queryDocument.get("projection", Document.class));
|
||||
req.setFilter(queryDocument.get("filter", Document.class));
|
||||
return req;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue