Refactored Serialization means

This commit is contained in:
Fabio Sinibaldi 2021-09-02 18:05:59 +02:00
parent 9533313b67
commit b5d72ddd82
6 changed files with 75 additions and 145 deletions

17
pom.xml
View File

@ -79,17 +79,15 @@
</exclusions>
</dependency>
<!-- INTERNAL LOGIC -->
<dependency>
<groupId>org.gcube.application</groupId>
<artifactId>geoportal-common</artifactId>
<version>[1.0.0,2.0.0)</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.gcube.application</groupId>-->
<!-- <artifactId>geoportal-logic</artifactId>-->
<!-- <version>[1.0.14,2.0.0)</version>-->
<!-- </dependency>-->
<!-- SDI -->
<dependency>
@ -137,18 +135,9 @@
<!-- Used to write centroids -->
<!-- <dependency> <groupId>net.postgis</groupId> <artifactId>postgis-jdbc</artifactId>
<version>2.5.0</version> </dependency> -->
<!-- jackson java time -->
<!-- Serialization from library -->
<!-- <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId>
<version>2.8.8</version> </dependency> -->
<!-- GPKG -->
<!-- Read Geopackage -->
<!-- <dependency> <groupId>mil.nga.geopackage</groupId> <artifactId>geopackage</artifactId>

View File

@ -1,10 +1,12 @@
package org.gcube.application.geoportal.service;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.rest.ConcessioniOverMongo;
import org.gcube.application.geoportal.service.rest.Profiles;
import org.gcube.application.geoportal.service.rest.Projects;
import org.gcube.application.geoportal.service.rest.Sections;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ApplicationPath;
@ -22,8 +24,15 @@ public class GeoPortalService extends ResourceConfig{
registerClasses(Projects.class);
registerClasses(Sections.class);
registerClasses(Profiles.class);
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(Serialization.mapper);
register(provider);
}

View File

@ -98,21 +98,21 @@ public class ConcessioniMongoManager extends MongoManager{
public List<Concessione> list(){
ArrayList<Concessione> toReturn=new ArrayList<>();
public Iterable<Concessione> list(){
LinkedBlockingQueue queue=new LinkedBlockingQueue<Concessione>();
iterate(null, collectionName).forEach(
new Consumer<Document>() {
@Override
public void accept(Document d) {
try {
toReturn.add(asConcessione(d));
queue.put(asConcessione(d));
}catch(Throwable t) {
log.error("Unable to read Document as concessione ",t);
log.debug("Document was "+d.toJson());
}
}
});
return toReturn;
return queue;
}
public Iterable<Concessione> search(String filter){

View File

@ -9,9 +9,7 @@ import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.engine.mongo.ConcessioniMongoManager;
import org.gcube.application.geoportal.service.engine.postgis.PostgisIndex;
import org.gcube.application.geoportal.service.model.internal.faults.DeletionException;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@ -25,15 +23,15 @@ public class ConcessioniOverMongo {
@GET
@Path(InterfaceConstants.Methods.CONFIGURATION_PATH)
@Produces(MediaType.APPLICATION_JSON)
public String getConfiguration(){
return new GuardedMethod<String>(){
public Configuration getConfiguration(){
return new GuardedMethod<Configuration>(){
@Override
protected String run() throws Exception, WebApplicationException {
protected Configuration run() throws Exception, WebApplicationException {
Configuration toReturn = new Configuration();
toReturn.setIndex(new PostgisIndex().getInfo());
log.info("Returning configuration {} ",toReturn);
return Serialization.write(toReturn);
return toReturn;
}
}.execute().getResult();
}
@ -41,15 +39,16 @@ public class ConcessioniOverMongo {
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String replace(String jsonString) {
return new GuardedMethod<String> () {
public Concessione replace(Concessione c) {
return new GuardedMethod<Concessione> () {
@Override
protected String run() throws Exception, WebApplicationException {
Concessione c=Serialization.read(jsonString, Concessione.class);
protected Concessione run() throws Exception, WebApplicationException {
//Concessione c=Serialization.read(jsonString, Concessione.class);
ConcessioniMongoManager manager=new ConcessioniMongoManager();
manager.replace(c);
return Serialization.write(manager.getById(c.getMongo_id()));
// return Serialization.write(manager.getById(c.getMongo_id()));
return manager.getById(c.getMongo_id());
}
}.execute().getResult();
}
@ -57,13 +56,13 @@ public class ConcessioniOverMongo {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String createNew(String jsonString) {
return new GuardedMethod<String> () {
public Concessione createNew(Concessione c) {
return new GuardedMethod<Concessione>() {
@Override
protected String run() throws Exception, WebApplicationException {
Concessione c=Serialization.read(jsonString, Concessione.class);
protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager();
return Serialization.write(manager.registerNew(c));
return manager.registerNew(c);
}
}.execute().getResult();
}
@ -72,19 +71,12 @@ public class ConcessioniOverMongo {
@GET
@Produces(MediaType.APPLICATION_JSON)
public String list() {
return new GuardedMethod<String> () {
protected String run() throws Exception ,WebApplicationException {
public Iterable<Concessione> list() {
return new GuardedMethod<Iterable<Concessione>>() {
protected Iterable<Concessione> run() throws Exception ,WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager();
JSONArray toReturn=new JSONArray();
manager.list().forEach((Concessione c) -> {
try{
toReturn.put(new JSONObject(Serialization.write(c)));
}catch(Throwable t) {
log.error("Unable to serialize "+c);
}
});
return toReturn.toString();
return manager.list();
};
}.execute().getResult();
@ -97,12 +89,12 @@ public class ConcessioniOverMongo {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String getById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<String> () {
public Concessione getById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<Concessione>() {
@Override
protected String run() throws Exception, WebApplicationException {
protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager();
return Serialization.write(manager.getById(id));
return manager.getById(id);
}
}.execute().getResult();
}
@ -132,18 +124,13 @@ public class ConcessioniOverMongo {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String update(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,String jsonString) {
return new GuardedMethod<String> () {
public Concessione update(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,Concessione c) {
return new GuardedMethod<Concessione>() {
@Override
protected String run() throws Exception, WebApplicationException {
protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager();
Concessione c=Serialization.read(jsonString, Concessione.class);
c.setMongo_id(id);
Concessione registered = manager.replace(c);
return Serialization.write(registered);
return manager.replace(c);
}
}.execute().getResult();
}
@ -152,25 +139,25 @@ public class ConcessioniOverMongo {
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("/{"+InterfaceConstants.Methods.PUBLISH_PATH+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String publish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<String> () {
public Concessione publish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<Concessione>() {
@Override
protected String run() throws Exception, WebApplicationException {
protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager();
return Serialization.write(manager.publish(id));
return manager.publish(id);
}
}.execute().getResult();
}
@DELETE
@Path("/{"+InterfaceConstants.Methods.PUBLISH_PATH+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String unpublish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
public Concessione unpublish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
log.info("Unpublishing {} ",id);
return new GuardedMethod<String> () {
return new GuardedMethod<Concessione>() {
@Override
protected String run() throws Exception, WebApplicationException {
protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager();
return Serialization.write(manager.unpublish(id));
return manager.unpublish(id);
}
}.execute().getResult();
@ -182,19 +169,15 @@ public class ConcessioniOverMongo {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/"+InterfaceConstants.Methods.REGISTER_FILES_PATH+"/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String registerFile(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,String jsonRequest) {
return new GuardedMethod<String> () {
public Concessione registerFile(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id, AddSectionToConcessioneRequest request) {
return new GuardedMethod<Concessione>() {
@Override
protected String run() throws Exception, WebApplicationException {
AddSectionToConcessioneRequest request=Serialization.read(jsonRequest,AddSectionToConcessioneRequest.class);
protected Concessione run() throws Exception, WebApplicationException {
log.info("Registering {} file(s) for {} Concessione ID {}",
request.getStreams().size(),
request.getDestinationPath(),id);
ConcessioniMongoManager manager=new ConcessioniMongoManager();
Concessione toReturn= manager.persistContent(id, request.getDestinationPath(), request.getStreams());
log.debug("Returning "+toReturn);
return Serialization.write(toReturn);
return manager.persistContent(id, request.getDestinationPath(), request.getStreams());
}
}.execute().getResult();
}

View File

@ -3,6 +3,7 @@ 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 java.io.IOException;
@ -19,7 +20,9 @@ public class Serialization {
static {
mapper=new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
mapper.registerModule(new JavaTimeModule());
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 {
@ -32,61 +35,11 @@ public class Serialization {
public static String write(Object toWrite) throws JsonProcessingException {
// if(toWrite instanceof Concessione)
// detach((Concessione) toWrite);
String toReturn= mapper.writeValueAsString(toWrite);
// if(toWrite instanceof Concessione)
// reattach((Concessione) toWrite);
return toReturn;
}
// Avoid infiniteLoop in JPA
// private static void detach(Concessione c) {
// if (c!=null) {
// detach(c.getRelazioneScavo());
// detach(c.getPosizionamentoScavo());
// if(c.getPianteFineScavo()!=null)
// c.getPianteFineScavo().forEach((LayerConcessione l)->{detach(l);});
// if(c.getImmaginiRappresentative()!=null)
// c.getImmaginiRappresentative().forEach(((UploadedImage u)->{detach(u);}));
// if(c.getGenericContent()!=null)
// c.getGenericContent().forEach(((OtherContent u)->{detach(u);}));
// }
// }
//
// private static void detach(AssociatedContent a) {
// if(a!=null) {
// a.setRecord(null);
// if(a.getActualContent()!=null)
// a.getActualContent().forEach((PersistedContent p)->{p.setAssociated(null);});
// }
// }
//
//
// private static void reattach(Concessione c) {
// if(c!=null) {
// reattach(c.getRelazioneScavo(),c);
// reattach(c.getPosizionamentoScavo(),c);
// if(c.getPianteFineScavo()!=null)
// c.getPianteFineScavo().forEach((LayerConcessione l)->{reattach(l,c);});
// if(c.getImmaginiRappresentative()!=null)
// c.getImmaginiRappresentative().forEach(((UploadedImage u)->{reattach(u,c);}));
// if(c.getGenericContent()!=null)
// c.getGenericContent().forEach(((OtherContent u)->{reattach(u,c);}));
// }
// }
//
// private static void reattach(AssociatedContent a,Record r) {
// if(a!=null) {
// a.setRecord(r);
// if(a.getActualContent()!=null)
// a.getActualContent().forEach((PersistedContent p)->{p.setAssociated(a);});
// }
// }
}

View File

@ -23,6 +23,7 @@ import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.*;
@ -117,27 +118,22 @@ public class ConcessioniOverMongoTest extends BasicServiceTestUnit{
@Test
public void search() throws Exception {
WebTarget target=target(PATH);
// System.out.println("All :"+query(null,target).size());
// System.out.println("All (filter):"+queryFile("all.json",target).size());
AtomicLong validatedCount= new AtomicLong(0);
searchFile("validated.json",target).forEachRemaining(concessione -> {validatedCount.incrementAndGet();});
Iterator<Concessione> it=searchFile("validated.json",target);
it.forEachRemaining(concessione -> {validatedCount.incrementAndGet();});
System.out.println("Validated : "+ validatedCount.get());
// System.out.println("Legacy ID : "+ queryFile("legacyid.json",target).size());
// System.out.println("Non fabio : "+ queryFile("nonFabio.json",target).size());
// System.out.println("Missing Centroid : "+ queryFile("missingCentroid.json",target).size());
}
// TODO check
// @Test
// public void query() throws Exception {
// WebTarget target=target(PATH);
// try {
// System.out.println("Last Registered : " + queryFile("lastRegistered.json", target).next().getCreationTime());
// System.out.println("First Registered : " + queryFile("firstRegistered.json", target).next().getCreationTime());
// }catch(NoSuchElementException e){
// System.out.println("NO element found, probably empty DB");
// }
// }
@Test
public void query() throws Exception {
WebTarget target=target(PATH);
try {
System.out.println("Last Registered : " + queryFile("lastRegistered.json", target).next().getCreationTime());
System.out.println("First Registered : " + queryFile("firstRegistered.json", target).next().getCreationTime());
}catch(NoSuchElementException e){
System.out.println("NO element found, probably empty DB");
}
}
@Test
public void getConfiguration() throws Exception {