JSONPathWrapper dynamically loads JSON Implementation

This commit is contained in:
Fabio Sinibaldi 2022-01-31 17:56:47 +01:00
parent 405129f37f
commit 5c1aee1c76
8 changed files with 90 additions and 32 deletions

View File

@ -0,0 +1,12 @@
package org.gcube.application.cms.plugins;
import org.gcube.application.geoportal.common.JSONSerializationProvider;
public class JacksonProvider implements JSONSerializationProvider {
@Override
public void setJSONWrapperDefaults() {
}
}

View File

@ -54,6 +54,22 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- STORAGE -->
<dependency>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-core</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-wrapper</artifactId>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>
<!-- TEST --> <!-- TEST -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@ -72,18 +88,9 @@
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId> <artifactId>jersey-media-json-jackson</artifactId>
</dependency> </dependency>
<!-- STORAGE -->
<dependency>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-core</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-wrapper</artifactId>
</dependency>
</dependencies>
</dependencies>
</project> </project>

View File

@ -0,0 +1,7 @@
package org.gcube.application.geoportal.common;
public interface JSONSerializationProvider {
public void setJSONWrapperDefaults();
}

View File

@ -7,33 +7,37 @@ import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider; import com.jayway.jsonpath.spi.mapper.MappingProvider;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.JSONSerializationProvider;
import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import java.util.*; import java.util.*;
@Slf4j
public class JSONPathWrapper { public class JSONPathWrapper {
public static Configuration JSON_PATH_ALWAYS_LIST_CONFIG=null; public static Configuration JSON_PATH_ALWAYS_LIST_CONFIG=null;
public static Configuration JSON_PATH_PATHS_CONFIGURATION=null; public static Configuration JSON_PATH_PATHS_CONFIGURATION=null;
static { static {
Configuration.setDefaults(new Configuration.Defaults() { Reflections reflections = new Reflections(
private JsonProvider jacksonProvider = new JacksonJsonProvider(); new ConfigurationBuilder()
.forPackage("org.gcube.application")
.filterInputsBy(new FilterBuilder().includePackage("org.gcube.application")));
private final MappingProvider mappingProvider = new JacksonMappingProvider(); reflections.getSubTypesOf(JSONSerializationProvider.class).iterator().forEachRemaining(providerClass->{
@Override if(!providerClass.isInterface()){
public JsonProvider jsonProvider() { try {
return jacksonProvider; log.warn("Loading JSON Provider {} ",providerClass);
JSONSerializationProvider provider = providerClass.newInstance();
provider.setJSONWrapperDefaults();
}catch (Throwable t){
log.error("Unable to instantiate provider "+providerClass,t);
}
} }
@Override
public Set<Option> options() {
return EnumSet.noneOf(Option.class);
}
@Override
public MappingProvider mappingProvider() {
return mappingProvider;
}
}); });
JSON_PATH_ALWAYS_LIST_CONFIG= Configuration.builder().options(Option.ALWAYS_RETURN_LIST,Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build(); JSON_PATH_ALWAYS_LIST_CONFIG= Configuration.builder().options(Option.ALWAYS_RETURN_LIST,Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build();

View File

@ -368,6 +368,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
private static final RegisteredFileSet prepareRegisteredFileSet(ProfiledDocument doc, Profile profile,String destination, private static final RegisteredFileSet prepareRegisteredFileSet(ProfiledDocument doc, Profile profile,String destination,
Document attributes,List<TempFile> files, StorageUtils storage,WorkspaceManager ws) throws StorageHubException, StorageException { Document attributes,List<TempFile> files, StorageUtils storage,WorkspaceManager ws) throws StorageHubException, StorageException {
log.debug("Preparing Registered FileSet.."); log.debug("Preparing Registered FileSet..");
attributes.putIfAbsent(RegisteredFileSet.CREATION_INFO,UserUtils.getCurrent().asInfo()); attributes.putIfAbsent(RegisteredFileSet.CREATION_INFO,UserUtils.getCurrent().asInfo());
attributes.putIfAbsent(RegisteredFileSet.ACCESS,doc.getInfo().getAccess()); attributes.putIfAbsent(RegisteredFileSet.ACCESS,doc.getInfo().getAccess());
FolderContainer base=ws.createFolder(new WorkspaceManager.FolderOptions( FolderContainer base=ws.createFolder(new WorkspaceManager.FolderOptions(

View File

@ -2,6 +2,9 @@ package org.gcube.application.geoportal.service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.cms.tests.TokenSetter;
import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.engine.providers.StorageClientProvider;
import org.gcube.application.geoportal.service.rest.GuardedMethod; import org.gcube.application.geoportal.service.rest.GuardedMethod;
import org.gcube.application.cms.Serialization; import org.gcube.application.cms.Serialization;
import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.JerseyTest;
@ -19,8 +22,8 @@ public class BasicServiceTestUnit extends JerseyTest {
return new GeoPortalService(); return new GeoPortalService();
} }
protected static String scope="/gcube/devsec/devVRE"; // protected static String scope="/gcube/devsec/devVRE";
// protected static String scope="/pred4s/preprod/preVRE"; protected static String scope="/pred4s/preprod/preVRE";
// protected static String scope="/d4science.research-infrastructures.eu/D4OS/GeoNA-Prototype"; // protected static String scope="/d4science.research-infrastructures.eu/D4OS/GeoNA-Prototype";
@BeforeClass @BeforeClass
public static void init() { public static void init() {
@ -32,7 +35,17 @@ public class BasicServiceTestUnit extends JerseyTest {
TokenSetter.set(scope); TokenSetter.set(scope);
} }
}); });
// ImplementationProvider.get().setStorageProvider(
// new StorageClientProvider(){
// @Override
// public StorageUtils retrieveObject(){
// new StorageUtils(){
// @Override
// getP
// }
// };
// }
// );
} }

View File

@ -3,9 +3,11 @@ package org.gcube.application.geoportal.service;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import org.bson.Document; import org.bson.Document;
import org.gcube.application.cms.Serialization; import org.gcube.application.cms.Serialization;
import org.gcube.application.cms.custom.gna.concessioni.model.ProfiledConcessione;
import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.cms.tests.TokenSetter;
import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel; import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel;
import org.gcube.application.geoportal.common.model.document.ProfiledDocument; import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
import org.gcube.application.geoportal.common.model.document.RegisteredFileSet;
import org.gcube.application.geoportal.common.model.legacy.Concessione; import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.rest.Configuration; import org.gcube.application.geoportal.common.model.rest.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest; import org.gcube.application.geoportal.common.model.rest.QueryRequest;
@ -28,6 +30,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ProfiledDocumentsTests extends BasicServiceTestUnit{ public class ProfiledDocumentsTests extends BasicServiceTestUnit{
@ -107,14 +110,24 @@ public class ProfiledDocumentsTests extends BasicServiceTestUnit{
public void addFileSet() throws Exception { public void addFileSet() throws Exception {
ProfiledDocument doc = createNew(); ProfiledDocument doc = createNew();
// Try set releazione scavo
String fileSetPath="relazioneScavo";
String fieldPath="relazioneScavo";
String filename = "relazione.pdf";
doc = upload( doc = upload(
new StorageUtils(), new StorageUtils(),
doc.get_id(), doc.get_id(),
"relazioneScavo", fileSetPath,
"relazioneScavo", fieldPath,
Document.parse("{\"titolo\" : \"mio titolo\"}"), Document.parse("{\"titolo\" : \"mio titolo\"}"),
RegisterFileSetRequest.ClashOptions.MERGE_EXISTING, RegisterFileSetRequest.ClashOptions.MERGE_EXISTING,
"relazione.pdf"); filename);
assertTrue("Relazione exists",doc.getTheDocument().containsKey(fileSetPath));
RegisteredFileSet set = Serialization.convert(doc.getTheDocument().get(fileSetPath), RegisteredFileSet.class);
System.out.println(set);
ProfiledConcessione.Relazione rel= Serialization.convert(doc.getTheDocument().get(fileSetPath), ProfiledConcessione.Relazione.class);
System.out.println(rel);
System.out.println(Serialization.write(doc)); System.out.println(Serialization.write(doc));
} }

View File

@ -159,6 +159,7 @@
<groupId>org.gcube.common</groupId> <groupId>org.gcube.common</groupId>
<artifactId>storagehub-client-library</artifactId> <artifactId>storagehub-client-library</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version> <version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency> </dependency>