Refactored REST model to common library

This commit is contained in:
Fabio Sinibaldi 2021-09-01 18:14:18 +02:00
parent 66b0f604f1
commit bde5d820d9
14 changed files with 65 additions and 132 deletions

View File

@ -11,8 +11,8 @@ import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.model.legacy.*;
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
import org.gcube.application.geoportal.common.utils.Files;
import org.gcube.application.geoportal.service.model.internal.db.DatabaseConnection;
import org.gcube.application.geoportal.service.model.internal.db.PostgisTable;
import org.gcube.application.geoportal.service.model.internal.faults.SDIInteractionException;
import org.gcube.common.storagehub.client.dsl.FileContainer;
@ -342,6 +342,22 @@ public class SDIManager {
if(!currentGeoserver.getPublisher().publishDBLayer(workspace, storeName, fte, layerEncoder))
throw new SDIInteractionException("Unable to create layer "+name);
log.debug("layer "+name+" already exists");
String link=String.format("https://%1$s/geoserver/%2$s/wms?"
+"service=WMS&version=1.1.0&request=GetMap&layers=%2$s:%3$s&"
+ "styles=&bbox=%4$f,%5$f,%6$f,%7$f&srs=%8$s&format=application/openlayers&width=%9$d&height=%10$d",
geoserverHostName,
workspace,
name,
"-1563071.166172796",
"4789738.204048398",
"4334926.486925308",
"5828118.072551585",
EPSG_4326,
400,
400);
return name;
} catch (IllegalArgumentException | MalformedURLException e) {
throw new SDIInteractionException("Unable to create layer "+name,e);

View File

@ -9,6 +9,7 @@ import org.bson.types.ObjectId;
import org.gcube.application.geoportal.common.model.legacy.*;
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport;
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport.ValidationStatus;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.rest.TempFile;
import org.gcube.application.geoportal.common.utils.Files;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
@ -19,7 +20,6 @@ import org.gcube.application.geoportal.service.engine.WorkspaceManager.FolderOpt
import org.gcube.application.geoportal.service.engine.postgis.PostgisIndex;
import org.gcube.application.geoportal.service.engine.providers.StorageClientProvider;
import org.gcube.application.geoportal.service.model.internal.faults.*;
import org.gcube.application.geoportal.service.model.internal.rest.QueryRequest;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.gcube.common.storagehub.client.dsl.FolderContainer;
import org.gcube.data.transfer.library.faults.RemoteServiceException;

View File

@ -11,9 +11,9 @@ import com.mongodb.client.model.ReturnDocument;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.application.geoportal.service.model.internal.rest.QueryRequest;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Sorts.ascending;
@ -89,7 +89,7 @@ public abstract class MongoManager {
return coll.find(Document.parse(filterString));
}
public FindIterable<Document> query(QueryRequest request,String collectionName){
public FindIterable<Document> query(QueryRequest request, String collectionName){
MongoDatabase database=getDatabase();
MongoCollection<Document> coll=database.getCollection(collectionName);
FindIterable<Document> toReturn=iterate(request.getFilter(), collectionName);

View File

@ -3,10 +3,11 @@ package org.gcube.application.geoportal.service.engine.postgis;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.model.legacy.BBOX;
import org.gcube.application.geoportal.service.model.internal.db.DatabaseConnection;
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
import org.gcube.application.geoportal.service.model.internal.db.PostgisTable;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.application.geoportal.service.model.internal.faults.DataParsingException;
import org.gcube.application.geoportal.service.utils.ISUtils;
import java.sql.*;
import java.util.regex.Matcher;
@ -19,7 +20,11 @@ public class PostgisDBManager implements PostgisDBManagerI {
public static PostgisDBManager get() throws SQLException, ConfigurationException {
if(config==null) {
log.debug("Looking for Default Configuration.. ");
DatabaseConnection defaultConfiguration= DatabaseConnection.get(DatabaseConnection.TYPE.Concessioni);
// TODO GENERIC
DatabaseConnection defaultConfiguration=
ISUtils.queryForDB("postgis", "Concessioni");
log.debug("Found configuration : "+defaultConfiguration);
config=defaultConfiguration;
}
@ -41,7 +46,9 @@ public class PostgisDBManager implements PostgisDBManagerI {
private static Connection getConnection() throws SQLException {
Connection toReturn= DriverManager.getConnection(config.getUrl(),config.getUser(),config.getPwd());
toReturn.setAutoCommit(config.getAutocommit());
//TODO configure behaviour
toReturn.setAutoCommit(false);
return toReturn;
}

View File

@ -1,6 +1,6 @@
package org.gcube.application.geoportal.service.engine.postgis;
import org.gcube.application.geoportal.service.model.internal.db.DatabaseConnection;
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
import org.gcube.application.geoportal.service.model.internal.db.PostgisTable;
import org.gcube.application.geoportal.service.model.internal.faults.DataParsingException;

View File

@ -2,6 +2,8 @@ package org.gcube.application.geoportal.service.engine.postgis;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
import org.gcube.application.geoportal.common.model.rest.PostgisIndexDescriptor;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.engine.SDIManager;
import org.gcube.application.geoportal.service.model.internal.db.DBConstants;
@ -35,7 +37,7 @@ public class PostgisIndex {
private SDIManager sdiManager;
private String wmsLink=null;
private static PostgisDBManager getDB() throws ConfigurationException {
return ImplementationProvider.get().getDbProvider().getObject();
};
@ -43,23 +45,27 @@ public class PostgisIndex {
public PostgisIndex() throws SDIInteractionException, SQLException, ConfigurationException {
super();
this.sdiManager=new SDIManager();
init();
this.wmsLink=init();
}
public PostgisIndexDescriptor getInfo() throws ConfigurationException, SDIInteractionException, SQLException {
DatabaseConnection conn=getDB().getConnectionDescriptor();
return new PostgisIndexDescriptor(conn,wmsLink);
}
protected PostgisTable getCentroidsTable() {
return DBConstants.Concessioni.CENTROIDS;
}
public void init() throws SQLException, ConfigurationException, SDIInteractionException {
public String init() throws SQLException, ConfigurationException, SDIInteractionException {
log.debug("Contacting postgis DB .. ");
PostgisDBManagerI db=ImplementationProvider.get().getDbProvider().getObject();
log.debug("Checking if centroids table exists..");
PostgisTable table=getCentroidsTable();
db.create(table);
db.commit();
sdiManager.configureCentroidLayer("centroids_concessioni", "gna", "gna_postgis",table,db.getConnectionDescriptor());
return sdiManager.configureCentroidLayer("centroids_concessioni", "gna", "gna_postgis",table,db.getConnectionDescriptor());
}

View File

@ -1,14 +0,0 @@
package org.gcube.application.geoportal.service.model.internal;
import lombok.Data;
import org.gcube.application.geoportal.service.model.internal.db.DatabaseConnection;
import java.util.Map;
@Data
public class Configuration {
public DatabaseConnection postgisDBIndex;
public Map<String,String> details;
}

View File

@ -8,6 +8,11 @@ import java.util.ArrayList;
public class DBConstants {
public static enum TYPE{
Concessioni,Mosi,Mopr
}
public static class Defaults{
public static final String DEFAULT_GEOMETRY_COLUMN_NAME="geom";
public static final String INTERNAL_ID="internal_id";

View File

@ -1,30 +0,0 @@
package org.gcube.application.geoportal.service.model.internal.db;
import lombok.*;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.application.geoportal.service.utils.ISUtils;
@Getter
@Setter
@RequiredArgsConstructor
@ToString
public class DatabaseConnection {
public static enum TYPE{
Concessioni,Mosi,Mopr
}
public static DatabaseConnection get(TYPE type) throws ConfigurationException {
return ISUtils.queryForDB("postgis", type.toString());
}
@NonNull
private String user;
@NonNull
private String pwd;
@NonNull
private String url;
private Boolean autocommit=false;
}

View File

@ -1,34 +0,0 @@
package org.gcube.application.geoportal.service.model.internal.rest;
import lombok.Data;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@Data
public class AddSectionToConcessioneRequest {
// public static enum Section{
// RELAZIONE,UPLOADED_IMG,PIANTA,POSIZIONAMENTO,OTHER
// }
//
// @XmlRootElement
// @Data
// public static class SHUBFileDescriptor {
// private String filename;
// private String shubID;
// }
//
//
// private Section section;
// @XmlElements({
// @XmlElement(type=OtherContent.class),
// @XmlElement(type=RelazioneScavo.class),
// @XmlElement(type=SDILayerDescriptor.class),
// @XmlElement(type=LayerConcessione.class),
// @XmlElement(type=UploadedImage.class),
// })
// private AssociatedContent toRegister;
// private List<SHUBFileDescriptor> streams=new ArrayList<AddSectionToConcessioneRequest.SHUBFileDescriptor>();
}

View File

@ -1,27 +0,0 @@
package org.gcube.application.geoportal.service.model.internal.rest;
import lombok.Data;
import java.util.List;
@Data
public class QueryRequest {
@Data
public static class PagedRequest{
private int offset;
private int Limit;
}
@Data
public static class OrderedRequest {
public static enum Direction {ASCENDING,DESCENDING}
private Direction direction;
private String json;
private List<String> fields;
}
private String filter;
private String transformation;
private OrderedRequest ordering;
private PagedRequest paging;
}

View File

@ -2,13 +2,13 @@ package org.gcube.application.geoportal.service.rest;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.rest.AddSectionToConcessioneRequest;
import org.gcube.application.geoportal.common.model.rest.AddSectionToConcessioneRequest;
import org.gcube.application.geoportal.common.model.rest.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.engine.mongo.ConcessioniMongoManager;
import org.gcube.application.geoportal.service.model.internal.Configuration;
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.rest.QueryRequest;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.json.JSONArray;
import org.json.JSONObject;
@ -22,8 +22,8 @@ import javax.ws.rs.core.Response;
public class ConcessioniOverMongo {
@HEAD
@Path("some")
@GET
@Path(InterfaceConstants.Methods.CONFIGURATION_PATH)
@Produces(MediaType.APPLICATION_JSON)
public String getConfiguration(){
return new GuardedMethod<String>(){
@ -31,7 +31,8 @@ public class ConcessioniOverMongo {
@Override
protected String run() throws Exception, WebApplicationException {
Configuration toReturn = new Configuration();
toReturn.setPostgisDBIndex(ImplementationProvider.get().getDbProvider().getObject().getConnectionDescriptor());
toReturn.setIndex(new PostgisIndex().getInfo());
log.info("Returning configuration {} ",toReturn);
return Serialization.write(toReturn);
}
}.execute().getResult();
@ -201,7 +202,7 @@ public class ConcessioniOverMongo {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/"+"search")
@Path("/"+InterfaceConstants.Methods.SEARCH_PATH)
public Iterable<Concessione> search(String filter){
return new GuardedMethod<Iterable<Concessione>>() {
@Override
@ -215,7 +216,7 @@ public class ConcessioniOverMongo {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/"+"query")
@Path("/"+InterfaceConstants.Methods.QUERY_PATH)
public Iterable<Concessione> search(QueryRequest queryRequest){
return new GuardedMethod<Iterable<Concessione>>() {
@Override

View File

@ -1,7 +1,7 @@
package org.gcube.application.geoportal.service.utils;
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
import org.gcube.application.geoportal.service.ServiceConstants;
import org.gcube.application.geoportal.service.model.internal.db.DatabaseConnection;
import org.gcube.application.geoportal.service.model.internal.db.MongoConnection;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.common.encryption.encrypter.StringEncrypter;
@ -29,8 +29,11 @@ public class ISUtils {
}
AccessPoint point=found.get(0);
return new DatabaseConnection(point.username(),decryptString(point.password()),point.address());
DatabaseConnection toReturn=new DatabaseConnection();
toReturn.setPwd(decryptString(point.password()));
toReturn.setUser(point.username());
toReturn.setUrl(point.address());
return toReturn;
}

View File

@ -4,7 +4,7 @@ import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.legacy.Concessione.Paths;
import org.gcube.application.geoportal.common.model.legacy.LayerConcessione;
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport.ValidationStatus;
import org.gcube.application.geoportal.common.rest.AddSectionToConcessioneRequest;
import org.gcube.application.geoportal.common.model.rest.AddSectionToConcessioneRequest;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.rest.TempFile;
import org.gcube.application.geoportal.common.utils.Files;
@ -134,7 +134,7 @@ public class ConcessioniOverMongoTest extends BasicServiceTestUnit{
@Test
public void getConfiguration() throws Exception {
WebTarget target=target(PATH);
System.out.println(check(target.path("some").request(MediaType.APPLICATION_JSON).head(),String.class));
System.out.println(check(target.path("configuration").request(MediaType.APPLICATION_JSON).get(),String.class));
}