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> </exclusions>
</dependency> </dependency>
<!-- INTERNAL LOGIC --> <!-- INTERNAL LOGIC -->
<dependency> <dependency>
<groupId>org.gcube.application</groupId> <groupId>org.gcube.application</groupId>
<artifactId>geoportal-common</artifactId> <artifactId>geoportal-common</artifactId>
<version>[1.0.0,2.0.0)</version> <version>[1.0.0,2.0.0)</version>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>org.gcube.application</groupId>-->
<!-- <artifactId>geoportal-logic</artifactId>-->
<!-- <version>[1.0.14,2.0.0)</version>-->
<!-- </dependency>-->
<!-- SDI --> <!-- SDI -->
<dependency> <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 --> <!-- GPKG -->
<!-- Read Geopackage --> <!-- Read Geopackage -->
<!-- <dependency> <groupId>mil.nga.geopackage</groupId> <artifactId>geopackage</artifactId> <!-- <dependency> <groupId>mil.nga.geopackage</groupId> <artifactId>geopackage</artifactId>

View File

@ -1,10 +1,12 @@
package org.gcube.application.geoportal.service; 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.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.rest.ConcessioniOverMongo; import org.gcube.application.geoportal.service.rest.ConcessioniOverMongo;
import org.gcube.application.geoportal.service.rest.Profiles; import org.gcube.application.geoportal.service.rest.Profiles;
import org.gcube.application.geoportal.service.rest.Projects; import org.gcube.application.geoportal.service.rest.Projects;
import org.gcube.application.geoportal.service.rest.Sections; import org.gcube.application.geoportal.service.rest.Sections;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ApplicationPath; import javax.ws.rs.ApplicationPath;
@ -22,8 +24,15 @@ public class GeoPortalService extends ResourceConfig{
registerClasses(Projects.class); registerClasses(Projects.class);
registerClasses(Sections.class); registerClasses(Sections.class);
registerClasses(Profiles.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(){ public Iterable<Concessione> list(){
ArrayList<Concessione> toReturn=new ArrayList<>(); LinkedBlockingQueue queue=new LinkedBlockingQueue<Concessione>();
iterate(null, collectionName).forEach( iterate(null, collectionName).forEach(
new Consumer<Document>() { new Consumer<Document>() {
@Override @Override
public void accept(Document d) { public void accept(Document d) {
try { try {
toReturn.add(asConcessione(d)); queue.put(asConcessione(d));
}catch(Throwable t) { }catch(Throwable t) {
log.error("Unable to read Document as concessione ",t); log.error("Unable to read Document as concessione ",t);
log.debug("Document was "+d.toJson()); log.debug("Document was "+d.toJson());
} }
} }
}); });
return toReturn; return queue;
} }
public Iterable<Concessione> search(String filter){ 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.mongo.ConcessioniMongoManager;
import org.gcube.application.geoportal.service.engine.postgis.PostgisIndex; 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.model.internal.faults.DeletionException;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@ -25,15 +23,15 @@ public class ConcessioniOverMongo {
@GET @GET
@Path(InterfaceConstants.Methods.CONFIGURATION_PATH) @Path(InterfaceConstants.Methods.CONFIGURATION_PATH)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String getConfiguration(){ public Configuration getConfiguration(){
return new GuardedMethod<String>(){ return new GuardedMethod<Configuration>(){
@Override @Override
protected String run() throws Exception, WebApplicationException { protected Configuration run() throws Exception, WebApplicationException {
Configuration toReturn = new Configuration(); Configuration toReturn = new Configuration();
toReturn.setIndex(new PostgisIndex().getInfo()); toReturn.setIndex(new PostgisIndex().getInfo());
log.info("Returning configuration {} ",toReturn); log.info("Returning configuration {} ",toReturn);
return Serialization.write(toReturn); return toReturn;
} }
}.execute().getResult(); }.execute().getResult();
} }
@ -41,15 +39,16 @@ public class ConcessioniOverMongo {
@PUT @PUT
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String replace(String jsonString) { public Concessione replace(Concessione c) {
return new GuardedMethod<String> () { return new GuardedMethod<Concessione> () {
@Override @Override
protected String run() throws Exception, WebApplicationException { protected Concessione run() throws Exception, WebApplicationException {
Concessione c=Serialization.read(jsonString, Concessione.class); //Concessione c=Serialization.read(jsonString, Concessione.class);
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
manager.replace(c); 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(); }.execute().getResult();
} }
@ -57,13 +56,13 @@ public class ConcessioniOverMongo {
@POST @POST
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String createNew(String jsonString) { public Concessione createNew(Concessione c) {
return new GuardedMethod<String> () { return new GuardedMethod<Concessione>() {
@Override @Override
protected String run() throws Exception, WebApplicationException { protected Concessione run() throws Exception, WebApplicationException {
Concessione c=Serialization.read(jsonString, Concessione.class);
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
return Serialization.write(manager.registerNew(c)); return manager.registerNew(c);
} }
}.execute().getResult(); }.execute().getResult();
} }
@ -72,19 +71,12 @@ public class ConcessioniOverMongo {
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String list() { public Iterable<Concessione> list() {
return new GuardedMethod<String> () { return new GuardedMethod<Iterable<Concessione>>() {
protected String run() throws Exception ,WebApplicationException { protected Iterable<Concessione> run() throws Exception ,WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
JSONArray toReturn=new JSONArray(); JSONArray toReturn=new JSONArray();
manager.list().forEach((Concessione c) -> { return manager.list();
try{
toReturn.put(new JSONObject(Serialization.write(c)));
}catch(Throwable t) {
log.error("Unable to serialize "+c);
}
});
return toReturn.toString();
}; };
}.execute().getResult(); }.execute().getResult();
@ -97,12 +89,12 @@ public class ConcessioniOverMongo {
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}") @Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String getById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) { public Concessione getById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<String> () { return new GuardedMethod<Concessione>() {
@Override @Override
protected String run() throws Exception, WebApplicationException { protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
return Serialization.write(manager.getById(id)); return manager.getById(id);
} }
}.execute().getResult(); }.execute().getResult();
} }
@ -132,18 +124,13 @@ public class ConcessioniOverMongo {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}") @Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String update(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,String jsonString) { public Concessione update(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,Concessione c) {
return new GuardedMethod<String> () { return new GuardedMethod<Concessione>() {
@Override @Override
protected String run() throws Exception, WebApplicationException { protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
Concessione c=Serialization.read(jsonString, Concessione.class);
c.setMongo_id(id); c.setMongo_id(id);
return manager.replace(c);
Concessione registered = manager.replace(c);
return Serialization.write(registered);
} }
}.execute().getResult(); }.execute().getResult();
} }
@ -152,25 +139,25 @@ public class ConcessioniOverMongo {
@PUT @PUT
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/{"+InterfaceConstants.Methods.PUBLISH_PATH+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") @Path("/{"+InterfaceConstants.Methods.PUBLISH_PATH+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String publish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) { public Concessione publish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<String> () { return new GuardedMethod<Concessione>() {
@Override @Override
protected String run() throws Exception, WebApplicationException { protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
return Serialization.write(manager.publish(id)); return manager.publish(id);
} }
}.execute().getResult(); }.execute().getResult();
} }
@DELETE @DELETE
@Path("/{"+InterfaceConstants.Methods.PUBLISH_PATH+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") @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); log.info("Unpublishing {} ",id);
return new GuardedMethod<String> () { return new GuardedMethod<Concessione>() {
@Override @Override
protected String run() throws Exception, WebApplicationException { protected Concessione run() throws Exception, WebApplicationException {
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
return Serialization.write(manager.unpublish(id)); return manager.unpublish(id);
} }
}.execute().getResult(); }.execute().getResult();
@ -182,19 +169,15 @@ public class ConcessioniOverMongo {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/"+InterfaceConstants.Methods.REGISTER_FILES_PATH+"/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") @Path("/"+InterfaceConstants.Methods.REGISTER_FILES_PATH+"/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public String registerFile(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,String jsonRequest) { public Concessione registerFile(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id, AddSectionToConcessioneRequest request) {
return new GuardedMethod<String> () { return new GuardedMethod<Concessione>() {
@Override @Override
protected String run() throws Exception, WebApplicationException { protected Concessione run() throws Exception, WebApplicationException {
AddSectionToConcessioneRequest request=Serialization.read(jsonRequest,AddSectionToConcessioneRequest.class);
log.info("Registering {} file(s) for {} Concessione ID {}", log.info("Registering {} file(s) for {} Concessione ID {}",
request.getStreams().size(), request.getStreams().size(),
request.getDestinationPath(),id); request.getDestinationPath(),id);
ConcessioniMongoManager manager=new ConcessioniMongoManager(); ConcessioniMongoManager manager=new ConcessioniMongoManager();
Concessione toReturn= manager.persistContent(id, request.getDestinationPath(), request.getStreams()); return manager.persistContent(id, request.getDestinationPath(), request.getStreams());
log.debug("Returning "+toReturn);
return Serialization.write(toReturn);
} }
}.execute().getResult(); }.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.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException; import java.io.IOException;
@ -19,7 +20,9 @@ public class Serialization {
static { static {
mapper=new ObjectMapper(); mapper=new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); 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 { 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 { public static String write(Object toWrite) throws JsonProcessingException {
// if(toWrite instanceof Concessione)
// detach((Concessione) toWrite);
String toReturn= mapper.writeValueAsString(toWrite); String toReturn= mapper.writeValueAsString(toWrite);
// if(toWrite instanceof Concessione)
// reattach((Concessione) toWrite);
return toReturn; 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.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -117,27 +118,22 @@ public class ConcessioniOverMongoTest extends BasicServiceTestUnit{
@Test @Test
public void search() throws Exception { public void search() throws Exception {
WebTarget target=target(PATH); 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); 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("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
// @Test public void query() throws Exception {
// public void query() throws Exception { WebTarget target=target(PATH);
// WebTarget target=target(PATH); try {
// try { System.out.println("Last Registered : " + queryFile("lastRegistered.json", target).next().getCreationTime());
// System.out.println("Last Registered : " + queryFile("lastRegistered.json", target).next().getCreationTime()); System.out.println("First Registered : " + queryFile("firstRegistered.json", target).next().getCreationTime());
// System.out.println("First Registered : " + queryFile("firstRegistered.json", target).next().getCreationTime()); }catch(NoSuchElementException e){
// }catch(NoSuchElementException e){ System.out.println("NO element found, probably empty DB");
// System.out.println("NO element found, probably empty DB"); }
// } }
// }
@Test @Test
public void getConfiguration() throws Exception { public void getConfiguration() throws Exception {