This commit is contained in:
Fabio Sinibaldi 2020-11-18 18:06:38 +01:00
parent ec3414d786
commit 28499b6a2b
17 changed files with 397 additions and 170 deletions

View File

@ -3,6 +3,12 @@ package org.gcube.application.geoportal.service;
public class ServiceConstants {
public static final String SE_GNA_DB_FLAG="GNA_DB";
public static final String SE_GNA_DB_CATEGORY="Database";
public static final String MONGO_SE_PLATFORM="mongodb";
public static final String MONGO_SE_GNA_FLAG="internal-db";

View File

@ -1,51 +0,0 @@
package org.gcube.application.geoportal.service.engine;
import java.util.Collection;
import org.gcube.application.geoportal.service.model.Project;
public class ProjectsArchiveManager implements ProjectsArchiveManagerI {
@Override
public Project getByID(String ID) {
// TODO Auto-generated method stub
return null;
}
@Override
public Collection<Project> getByFilters() {
// TODO Auto-generated method stub
return null;
}
@Override
public Project addSection() {
// TODO Auto-generated method stub
return null;
}
@Override
public Project createNew(Project toCreate) {
// TODO Auto-generated method stub
return null;
}
@Override
public Project publish(String id) {
// TODO Auto-generated method stub
return null;
}
@Override
public Project validate(String id) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -1,19 +0,0 @@
package org.gcube.application.geoportal.service.engine;
import java.util.Collection;
import org.gcube.application.geoportal.service.model.Project;
public interface ProjectsArchiveManagerI {
public Project getByID(String id);
public Collection<Project> getByFilters();
public Project addSection();
public Project createNew(Project toCreate);
public Project publish(String id);
public Project validate(String id);
}

View File

@ -0,0 +1,56 @@
package org.gcube.application.geoportal.service.engine.mongo;
import java.util.Arrays;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
import org.gcube.application.geoportal.service.ServiceConstants;
import org.gcube.application.geoportal.service.model.internal.db.MongoConnection;
import org.gcube.application.geoportal.service.utils.ISUtils;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import lombok.Synchronized;
public class DefaultMongoProvider implements MongoClientProvider {
public DefaultMongoProvider() {
// TODO Auto-generated constructor stub
}
@Override
public void init() {
}
@Override
public void shustdown() {
// TODO Auto-generated method stub
}
private static MongoClient instance=null;
@Override
@Synchronized
public MongoClient getClient() throws ConfigurationException{
if(instance==null) {
MongoConnection conn= ISUtils.queryForMongoDB(ServiceConstants.MONGO_SE_PLATFORM, ServiceConstants.MONGO_SE_GNA_FLAG);
MongoCredential credential = MongoCredential.createCredential(conn.getUser(), conn.getDatabase(),
conn.getPassword().toCharArray());
MongoClientOptions options = MongoClientOptions.builder().sslEnabled(true).build();
instance = new MongoClient(new ServerAddress(conn.getHosts().get(0),conn.getPort()),
credential,
options);
}
return instance;
}
}

View File

@ -1,5 +1,6 @@
package org.gcube.application.geoportal.service.engine.mongo;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
import org.gcube.application.geoportal.service.engine.Engine;
import com.mongodb.MongoClient;
@ -7,7 +8,7 @@ import com.mongodb.MongoClient;
public interface MongoClientProvider extends Engine{
public MongoClient getClient();
public MongoClient getClient() throws ConfigurationException;
}

View File

@ -1,39 +1,68 @@
package org.gcube.application.geoportal.service.engine.mongo;
import org.bson.Document;
import org.gcube.application.geoportal.common.model.profile.Profile;
import org.gcube.application.geoportal.common.model.project.Project;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.model.Project;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class MongoManager {
private MongoClient client=null;
public MongoManager() {
public MongoManager() throws ConfigurationException {
client=ImplementationProvider.get().getMongoClientProvider().getClient();
// init profile
}
private MongoDatabase getDatabase() {
return client.getDatabase("gna-db");
}
//*********** PROJECTS
// public Project insert(Project proj) {
// // TODO check if existing DB
// // TODO check if existing collection
// client.
//
// }
public void insert(Project proj, Profile profile) {
MongoDatabase database=getDatabase();
// TODO check if existing DB
String collectionName=profile.getName();
MongoCollection<Document> collection = database.getCollection(collectionName);
// TODO check if existing collection
collection.insertOne(Document.parse(proj.toString()));
}
// public Project update(Project proj) {
//
// }
//
// public void delete(String id) {
//
// }
// public Project load(String id) {
//
// }
public void delete(String id) {
}
public Document getById(String id,Profile p) {
MongoDatabase database=getDatabase();
MongoCollection<Document> coll=database.getCollection(p.getName());
return coll.find(new Document("id",id)).first();
}
public FindIterable<Document> iterate(Document filter,Profile p) {
MongoDatabase database=getDatabase();
MongoCollection<Document> coll=database.getCollection(p.getName());
return coll.find(filter);
}
//********** PROFILES
//
}

View File

@ -1,29 +0,0 @@
package org.gcube.application.geoportal.service.model;
public class Profile {
/**
* Profile{
_id :
fields :[
field : {
label :
description :
type: String,Numeric,Date,
File,Document,Layer
cardinality:
defaultValue :
validation :{type : // e.g. regexp, list}
}
]
default_compiler:{type : //XSLT,JAVA_CLAS}
validator: {}
iso_mapper: {}
centroid_fields : {}
index_definition : }
*
*
*/
}

View File

@ -1,5 +0,0 @@
package org.gcube.application.geoportal.service.model;
public class Project {
}

View File

@ -1,5 +0,0 @@
package org.gcube.application.geoportal.service.model;
public class Section {
}

View File

@ -0,0 +1,16 @@
package org.gcube.application.geoportal.service.model.internal.db;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
@Data
public class MongoConnection {
private String user;
private String password;
private String database;
private List<String> hosts=new ArrayList<String>();
private int port;
}

View File

@ -9,8 +9,8 @@ import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import org.gcube.application.geoportal.common.model.project.Project;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.model.Project;
import lombok.extern.slf4j.Slf4j;

View File

@ -1,18 +1,9 @@
package org.gcube.application.geoportal.service.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.model.Project;
import org.gcube.application.geoportal.service.model.Section;
import lombok.extern.slf4j.Slf4j;
@ -24,38 +15,38 @@ public class Sections {
// Add Section to Project Document
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Section registerNewSection(Section toRegister) {
try {
log.info("Creating new Section [PROJECT_ID:{}]",projectID);
throw new RuntimeException("Feature not yet available");
}catch(WebApplicationException e){
log.warn("Unable to serve request",e);
throw e;
}catch(Throwable e){
log.warn("Unable to serve request",e);
throw new WebApplicationException("Unable to serve request", e);
}
}
// Remove Section to Project Document
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.SECTION_ID+"}")
public Project deleteSection(@QueryParam(InterfaceConstants.Parameters.SECTION_ID) String sectionID) {
try {
log.info("Deleting Section [ID : {}, PROJECT_ID:{}]",projectID,sectionID);
throw new RuntimeException("Feature not yet available");
}catch(WebApplicationException e){
log.warn("Unable to serve request",e);
throw e;
}catch(Throwable e){
log.warn("Unable to serve request",e);
throw new WebApplicationException("Unable to serve request", e);
}
}
// @PUT
// @Produces(MediaType.APPLICATION_JSON)
// @Consumes(MediaType.APPLICATION_JSON)
// public Section registerNewSection(Section toRegister) {
// try {
// log.info("Creating new Section [PROJECT_ID:{}]",projectID);
// throw new RuntimeException("Feature not yet available");
// }catch(WebApplicationException e){
// log.warn("Unable to serve request",e);
// throw e;
// }catch(Throwable e){
// log.warn("Unable to serve request",e);
// throw new WebApplicationException("Unable to serve request", e);
// }
// }
//
//
// // Remove Section to Project Document
// @DELETE
// @Produces(MediaType.APPLICATION_JSON)
// @Consumes(MediaType.APPLICATION_JSON)
// @Path("{"+InterfaceConstants.Parameters.SECTION_ID+"}")
// public Project deleteSection(@QueryParam(InterfaceConstants.Parameters.SECTION_ID) String sectionID) {
// try {
// log.info("Deleting Section [ID : {}, PROJECT_ID:{}]",projectID,sectionID);
// throw new RuntimeException("Feature not yet available");
// }catch(WebApplicationException e){
// log.warn("Unable to serve request",e);
// throw e;
// }catch(Throwable e){
// log.warn("Unable to serve request",e);
// throw new WebApplicationException("Unable to serve request", e);
// }
// }
}

View File

@ -0,0 +1,124 @@
package org.gcube.application.geoportal.service.utils;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.List;
import java.util.Map;
import org.gcube.application.geoportal.model.db.DatabaseConnection;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
import org.gcube.application.geoportal.service.ServiceConstants;
import org.gcube.application.geoportal.service.model.internal.db.MongoConnection;
import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
public class ISUtils {
public static DatabaseConnection queryForDB(String platform,String flag) throws ConfigurationException {
List<AccessPoint> found=getAP(platform, flag);
if(found.size()>1) {
throw new ConfigurationException("Multiple SE found ["+found.size()+"] for platform : "+platform+" flag : "+flag);
}else if (found.isEmpty()){
throw new ConfigurationException("No SE found for platform : "+platform+" flag : "+flag);
}
AccessPoint point=found.get(0);
return new DatabaseConnection(point.username(),decryptString(point.password()),point.address());
}
public static List<AccessPoint> getAP(String platform,String flag) {
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '"+ServiceConstants.SE_GNA_DB_CATEGORY+"'")
.addCondition("$resource/Profile/Platform/Name/text() eq '"+platform+"'")
.addCondition("$resource/Profile/AccessPoint//Property[Name/text() eq '"+
ServiceConstants.SE_GNA_DB_FLAG+"'][Value/text() eq '"+flag+"']")
.setResult("$resource/Profile/AccessPoint");
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
return client.submit(query);
}
public static MongoConnection queryForMongoDB(String platform,String flag) throws ConfigurationException {
List<AccessPoint> found=getAP(platform, flag);
if(found.size()>1) {
throw new ConfigurationException("Multiple SE found ["+found.size()+"] for platform : "+platform+" flag : "+flag);
}else if (found.isEmpty()){
throw new ConfigurationException("No SE found for platform : "+platform+" flag : "+flag);
}
AccessPoint point=found.get(0);
MongoConnection toReturn=new MongoConnection();
for(Property prop:point.properties()) {
switch(prop.name()) {
case "host" : {
toReturn.getHosts().add(prop.value());
break;}
}
}
toReturn.getHosts().add(point.address());
Map<String, Property> props=point.propertyMap();
toReturn.setDatabase(props.get("database").value());
toReturn.setPassword(decryptString(point.password()));
toReturn.setPort(Integer.parseInt(props.get("port").value()));
toReturn.setUser(point.username());
return toReturn;
}
public static String getToken() throws ConfigurationException {
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq 'Application'")
.addCondition("$resource/Profile/Name/text() eq 'GNA-APP'")
.setResult("$resource/Profile/AccessPoint");
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
List<AccessPoint> found= client.submit(query);
if(found.size()>1) {
throw new ConfigurationException("Multiple Token SE found ["+found.size()+"] for Category : Application name : GNA-APP");
}else if (found.isEmpty()){
throw new ConfigurationException("No Token SE found ["+found.size()+"] for Category : Application name : GNA-APP");
}
AccessPoint point=found.get(0);
return decryptString(point.password());
}
public static String decryptString(String toDecrypt){
try{
return StringEncrypter.getEncrypter().decrypt(toDecrypt);
}catch(Exception e) {
throw new RuntimeException("Unable to decrypt : "+toDecrypt,e);
}
}
public static String getgCubeBaseEndpoint(String category,String name) {
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '"+category+"'")
.addCondition("$resource/Profile/Name/text() eq '"+name+"'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
AccessPoint point=client.submit(query).get(0).profile().accessPoints().asCollection().iterator().next();
return point.address();
}
}

View File

@ -0,0 +1,24 @@
package org.gcube.application.geoportal.service.utils;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public class Serialization {
public static ObjectMapper mapper;
static {
mapper=new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
mapper.registerModule(new JavaTimeModule());
}
public static <T> T read(String jsonString,Class<T> clazz) throws JsonProcessingException, IOException {
return mapper.readerFor(clazz).readValue(jsonString);
}
}

View File

@ -0,0 +1,57 @@
package org.gcube.application.geoportal.service.legacy;
import java.io.IOException;
import org.bson.Document;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.profile.Profile;
import org.gcube.application.geoportal.common.utils.Files;
import org.gcube.application.geoportal.model.fault.ConfigurationException;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.engine.mongo.DefaultMongoProvider;
import org.gcube.application.geoportal.service.engine.mongo.MongoManager;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.junit.BeforeClass;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.mongodb.Block;
import com.mongodb.MongoClient;
public class MongoTests {
@BeforeClass
public static final void init() {
ImplementationProvider.get().setMongoClientProvider(new DefaultMongoProvider() {
@Override
public MongoClient getClient() throws ConfigurationException {
TokenSetter.set("/gcube/devNext/NextNext");
return super.getClient();
}
});
}
Block<Document> printBlock = new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
@Test
public void listProfiles() throws JsonProcessingException, IOException, ConfigurationException {
MongoManager manager=new MongoManager();
Profile f=Serialization.mapper.readerFor(Profile.class).readValue(
Files.getFileFromResources("fakeProfile.json"));
manager.iterate(new Document(),f).forEach(printBlock);
}
// @Test
// public void writeProject() {
// MongoManager manager=new MongoManager();
// Concessione f=Serialization.mapper.readerFor(Concessione.class).readValue(
// Files.getFileFromResources("fakeProfile.json"));
// }
}

View File

@ -0,0 +1,3 @@
{
}

View File

@ -0,0 +1,29 @@
{
"name" : "Concessioni",
"_id" : "",
"fields" :[
{ "name" : "introduzione", "label" : "Introduzione", "type" : "STRING"},
{ "label" : "Descrizione Contenuto", "type" : "STRING"},
{ "label" : "Autori", "type" : "STRING", "cardinality" : "MULTIPLE"},
{ "label" : "Contributore", "type" : "STRING"},
{ "label" : "Titolari", "type" : "STRING", "cardinality" : "MULTIPLE"},
{ "name":"relazioneScavo", "label" : "Relazione di Scavo", "type" : "DOCUMENT"},
{ "name":"posizionameno",
"label" : "Posizionamento Scavo",
"type" : "DOCUMENT",
"fields":[
{ "label" : "Descrizione Contenuto", "type" : "STRING"},
{ "label" : "Autori", "type" : "STRING", "cardinality" : "MULTIPLE"},
{ "label" : "Contributore", "type" : "STRING"}
]
}
],
"validators" : [
{"name":"Validator Concessioni",
"type":"JAVA",
"qName":"org.gcube.application.concessioni.Validator"}
]
}