2022-10-03 17:52:16 +02:00
|
|
|
package org.gcube.portlets.user.geoportaldataentry;
|
|
|
|
|
2023-05-02 11:34:53 +02:00
|
|
|
import java.io.File;
|
2023-04-20 16:34:52 +02:00
|
|
|
import java.io.FileInputStream;
|
2023-05-02 11:34:53 +02:00
|
|
|
import java.io.FileOutputStream;
|
2023-04-20 16:34:52 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2023-05-02 11:34:53 +02:00
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.StandardCopyOption;
|
2022-10-06 17:31:10 +02:00
|
|
|
import java.util.ArrayList;
|
2022-10-18 15:30:12 +02:00
|
|
|
import java.util.Arrays;
|
2022-10-03 17:52:16 +02:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
2023-04-20 16:34:52 +02:00
|
|
|
import java.util.Properties;
|
2022-10-03 17:52:16 +02:00
|
|
|
|
2023-05-02 11:34:53 +02:00
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
import org.bson.Document;
|
2022-10-07 18:39:37 +02:00
|
|
|
import org.gcube.application.geoportal.client.utils.Serialization;
|
2023-05-02 11:34:53 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.JSONPathWrapper;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.document.Project;
|
2023-05-02 11:34:53 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.document.access.Access;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.access.AccessPolicy;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration;
|
|
|
|
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
|
2023-05-02 11:34:53 +02:00
|
|
|
import org.gcube.application.geoportalcommon.ConvertToDataServiceModel;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel;
|
|
|
|
import org.gcube.application.geoportalcommon.ProjectDVBuilder;
|
|
|
|
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
|
|
|
|
import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller;
|
|
|
|
import org.gcube.application.geoportalcommon.geoportal.UseCaseDescriptorCaller;
|
2022-10-07 18:39:37 +02:00
|
|
|
import org.gcube.application.geoportalcommon.geoportal.access.GeportalCheckAccessPolicy;
|
2022-10-18 15:30:12 +02:00
|
|
|
import org.gcube.application.geoportalcommon.geoportal.serdes.Payload;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.ConfigurationDV;
|
2022-10-07 18:39:37 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.config.FilePathDV;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.config.GcubeProfileDV;
|
2022-10-18 15:30:12 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.FilesetDV;
|
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.PayloadDV;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV;
|
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.ucd.GEOPORTAL_CONFIGURATION_TYPE;
|
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.ucd.GEOPORTAL_DATA_HANDLER;
|
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.ucd.HandlerDeclarationDV;
|
2022-10-06 17:31:10 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.view.ProjectView;
|
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.view.SectionView;
|
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.view.SubDocumentView;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2023-05-02 11:34:53 +02:00
|
|
|
import org.gcube.portlets.user.geoportaldataentry.server.FileSetDataObject;
|
2023-04-20 16:34:52 +02:00
|
|
|
import org.gcube.portlets.user.geoportaldataentry.server.FormDataObjectToJSON;
|
|
|
|
import org.gcube.portlets.user.geoportaldataentry.server.FormDataObjectToJSON.JSONObjectOrdered;
|
2023-05-02 11:34:53 +02:00
|
|
|
import org.gcube.portlets.user.geoportaldataentry.server.MongoServiceUtil;
|
2023-04-20 16:34:52 +02:00
|
|
|
import org.gcube.portlets.user.geoportaldataentry.server.json.JsonMerge;
|
2023-04-21 15:27:29 +02:00
|
|
|
import org.gcube.portlets.user.geoportaldataentry.server.json.JsonMerge.MERGE_OPTION;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.gcube.portlets.widgets.mpformbuilder.server.MetadataProfileFormBuilderServiceImpl;
|
|
|
|
import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetaDataProfileBean;
|
|
|
|
import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetadataFieldWrapper;
|
2023-05-02 11:34:53 +02:00
|
|
|
import org.gcube.portlets.widgets.mpformbuilder.shared.upload.FileUploadedRemote;
|
2022-10-07 18:39:37 +02:00
|
|
|
import org.json.JSONArray;
|
2023-04-20 16:34:52 +02:00
|
|
|
import org.json.JSONObject;
|
2022-10-03 17:52:16 +02:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2022-10-07 18:39:37 +02:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
2023-05-02 11:34:53 +02:00
|
|
|
import com.google.gwt.user.client.Random;
|
2023-04-20 16:34:52 +02:00
|
|
|
import com.jayway.jsonpath.Configuration;
|
|
|
|
import com.jayway.jsonpath.DocumentContext;
|
2022-10-03 17:52:16 +02:00
|
|
|
import com.jayway.jsonpath.JsonPath;
|
2023-04-20 16:34:52 +02:00
|
|
|
import com.jayway.jsonpath.spi.json.GsonJsonProvider;
|
2022-10-06 17:31:10 +02:00
|
|
|
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
|
2022-10-03 17:52:16 +02:00
|
|
|
|
|
|
|
public class Complex_Tests {
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
private static final String GCUBE_CONFIG_PROPERTIES_FILENAME = "gcube_config.properties";
|
|
|
|
// APP Working Directory + /src/test/resources must be the location of
|
|
|
|
// gcube_config.properties
|
|
|
|
private static String gcube_config_path = String.format("%s/%s",
|
|
|
|
System.getProperty("user.dir") + "/src/test/resources", GCUBE_CONFIG_PROPERTIES_FILENAME);
|
|
|
|
private static String CONTEXT;
|
|
|
|
private static String TOKEN;
|
2022-10-03 17:52:16 +02:00
|
|
|
|
|
|
|
private UseCaseDescriptorCaller clientUCD = null;
|
|
|
|
private ProjectsCaller clientPrj = null;
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2022-10-03 17:52:16 +02:00
|
|
|
private static String PROFILE_ID = "profiledConcessioni";
|
2023-05-02 11:34:53 +02:00
|
|
|
private static String PROJECT_ID = "644a66e944aad51c80409a3b";
|
2022-10-07 18:39:37 +02:00
|
|
|
|
|
|
|
private static String MY_LOGIN = "francesco.mangiacrapa";
|
|
|
|
|
2022-10-03 17:52:16 +02:00
|
|
|
public static final String JSON_$_POINTER = "$";
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Complex_Tests.class);
|
|
|
|
|
2022-10-18 15:30:12 +02:00
|
|
|
private LinkedHashMap<String, List<GcubeProfilesMetadataForUCD>> linkedMap_UCDId_gCubeProfiles;
|
|
|
|
|
|
|
|
public static enum COMMON_IMAGES_FORMAT {
|
|
|
|
gif, png, jpeg, jpg, bmp, tif, tiff, svg, avif, webp
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ImageDetector {
|
|
|
|
|
|
|
|
private static String[] getNames(Class<? extends Enum<?>> e) {
|
|
|
|
return Arrays.stream(e.getEnumConstants()).map(Enum::name).toArray(String[]::new);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> listFormatImages;
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
|
|
|
String[] arrayImgs = ImageDetector.getNames(COMMON_IMAGES_FORMAT.class);
|
|
|
|
listFormatImages = Arrays.asList(arrayImgs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isImage(String mimeType) {
|
|
|
|
if (mimeType == null || mimeType.isEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
String inputImageFormat = mimeType.replaceAll("image/", "");
|
|
|
|
|
|
|
|
return listFormatImages.contains(inputImageFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
/**
|
|
|
|
* Read context settings.
|
|
|
|
*/
|
|
|
|
public static void readContextSettings() {
|
2022-10-18 15:30:12 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
try (InputStream input = new FileInputStream(gcube_config_path)) {
|
|
|
|
|
|
|
|
Properties prop = new Properties();
|
|
|
|
|
|
|
|
// load a properties file
|
|
|
|
prop.load(input);
|
|
|
|
|
|
|
|
CONTEXT = prop.getProperty("CONTEXT");
|
|
|
|
TOKEN = prop.getProperty("TOKEN");
|
|
|
|
// get the property value and print it out
|
|
|
|
System.out.println("CONTEXT: " + CONTEXT);
|
|
|
|
System.out.println("TOKEN: " + TOKEN);
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
2022-10-03 17:52:16 +02:00
|
|
|
public void init() {
|
2023-04-20 16:34:52 +02:00
|
|
|
readContextSettings();
|
2022-10-03 17:52:16 +02:00
|
|
|
ScopeProvider.instance.set(CONTEXT);
|
|
|
|
SecurityTokenProvider.instance.set(TOKEN);
|
|
|
|
clientPrj = GeoportalClientCaller.projects();
|
|
|
|
clientUCD = GeoportalClientCaller.useCaseDescriptors();
|
|
|
|
}
|
2023-05-02 11:34:53 +02:00
|
|
|
|
|
|
|
// @Test
|
2023-04-20 16:34:52 +02:00
|
|
|
public void testSectionPathInnerParse() throws Exception {
|
|
|
|
String sectionPath = "$.abstractRelazione";
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
String profileID = "profiledConcessioni";
|
|
|
|
String projectID = "6425598a8593b215a1281e1c";
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
Configuration configurationGSON = Configuration.builder().jsonProvider(new GsonJsonProvider()).build();
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
ProjectsCaller client = GeoportalClientCaller.projects();
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
Project proejct = client.getProjectByID(profileID, projectID);
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
String jsonSourceProject = proejct.getTheDocument().toJson();
|
2023-05-02 11:34:53 +02:00
|
|
|
|
|
|
|
// If the section path is the Root document, passed as "$.", fixing as "$"
|
|
|
|
if (sectionPath.compareTo(FormDataObjectToJSON.JSON_$_POINTER + ".") == 0)
|
2023-04-20 16:34:52 +02:00
|
|
|
sectionPath = FormDataObjectToJSON.JSON_$_POINTER;
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
LOG.info("theString: {}", jsonSourceProject);
|
2023-05-02 11:34:53 +02:00
|
|
|
com.google.gson.JsonObject currentSectionJObject = JsonPath.parse(jsonSourceProject, configurationGSON)
|
|
|
|
.read(sectionPath);
|
2023-04-20 16:34:52 +02:00
|
|
|
LOG.info("currentSectionJObject: {}", currentSectionJObject.toString());
|
|
|
|
LOG.info("sourceSectionObject: {}", currentSectionJObject.toString());
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
JSONObject targetSectionJObject = new JSONObjectOrdered().instance();
|
|
|
|
targetSectionJObject.put("abstractIta", "Prova REJECT 1");
|
|
|
|
targetSectionJObject.put("titolo", "Prova REJECT abstract relazione di scavo 1");
|
|
|
|
LOG.info("targetSectionJObject: {}", targetSectionJObject.toString());
|
|
|
|
|
2023-05-02 11:34:53 +02:00
|
|
|
String mergedDoc = JsonMerge.merge(targetSectionJObject.toString(), currentSectionJObject.toString(),
|
|
|
|
MERGE_OPTION.REPLACE);
|
2023-04-20 16:34:52 +02:00
|
|
|
LOG.info("output: {}", mergedDoc);
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
String newDocJson;
|
2023-05-02 11:34:53 +02:00
|
|
|
// Updating path is first level of the root
|
|
|
|
if (sectionPath.equals(FormDataObjectToJSON.JSON_$_POINTER)) {
|
|
|
|
// The merged DOC is the root Document
|
2023-04-20 16:34:52 +02:00
|
|
|
newDocJson = mergedDoc;
|
2023-05-02 11:34:53 +02:00
|
|
|
} else {
|
|
|
|
// The merged DOC is a child of the root Document
|
2023-04-20 16:34:52 +02:00
|
|
|
Gson gson = new Gson();
|
|
|
|
JsonObject gsonOject = gson.fromJson(mergedDoc, JsonObject.class);
|
|
|
|
// Putting the merged section into Document
|
2023-05-02 11:34:53 +02:00
|
|
|
DocumentContext newContextDocJson = JsonPath.parse(jsonSourceProject, configurationGSON).set(sectionPath,
|
|
|
|
gsonOject);
|
|
|
|
newDocJson = newContextDocJson.json().toString();
|
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
}
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
Document updatedDocument = Serialization.read(newDocJson.toString(), Document.class);
|
|
|
|
LOG.info("New document is: {}", updatedDocument.toJson());
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
}
|
2023-05-02 11:34:53 +02:00
|
|
|
|
|
|
|
// @Test
|
2023-04-20 16:34:52 +02:00
|
|
|
public void testSectionPathRootParse() throws Exception {
|
|
|
|
String sectionPath = "$.";
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
String profileID = "profiledConcessioni";
|
|
|
|
String projectID = "6425598a8593b215a1281e1c";
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
Configuration configurationGSON = Configuration.builder().jsonProvider(new GsonJsonProvider()).build();
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
ProjectsCaller client = GeoportalClientCaller.projects();
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
Project proejct = client.getProjectByID(profileID, projectID);
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
String jsonSourceProject = proejct.getTheDocument().toJson();
|
2023-05-02 11:34:53 +02:00
|
|
|
|
|
|
|
// If the section path is the Root document, passed as "$.", fixing as "$"
|
|
|
|
if (sectionPath.compareTo(FormDataObjectToJSON.JSON_$_POINTER + ".") == 0)
|
2023-04-20 16:34:52 +02:00
|
|
|
sectionPath = FormDataObjectToJSON.JSON_$_POINTER;
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
LOG.info("theString: {}", jsonSourceProject);
|
2023-05-02 11:34:53 +02:00
|
|
|
com.google.gson.JsonObject currentSectionJObject = JsonPath.parse(jsonSourceProject, configurationGSON)
|
|
|
|
.read(sectionPath);
|
2023-04-20 16:34:52 +02:00
|
|
|
LOG.info("currentSectionJObject: {}", currentSectionJObject.toString());
|
|
|
|
LOG.info("sourceSectionObject: {}", currentSectionJObject.toString());
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
JSONObject targetSectionJObject = new JSONObjectOrdered().instance();
|
2023-05-02 11:34:53 +02:00
|
|
|
targetSectionJObject.put("nome", proejct.getTheDocument().get("nome") + " 1");
|
|
|
|
targetSectionJObject.put("titolo", proejct.getTheDocument().get("introduzione") + " 1");
|
2023-04-20 16:34:52 +02:00
|
|
|
LOG.info("targetSectionJObject: {}", targetSectionJObject.toString());
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
// JSONObject mergedSection = FormDataObjectToJSON.deepMerge(sourceSectionObject,
|
|
|
|
// targetSectionJObject);
|
2023-05-02 11:34:53 +02:00
|
|
|
|
|
|
|
String output = JsonMerge.merge(targetSectionJObject.toString(), currentSectionJObject.toString(),
|
|
|
|
MERGE_OPTION.REPLACE);
|
2023-04-20 16:34:52 +02:00
|
|
|
LOG.info("output: {}", output);
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
Gson gson = new Gson();
|
|
|
|
JsonObject gsonOject = gson.fromJson(output, JsonObject.class);
|
|
|
|
|
|
|
|
// Putting the merged section into Document
|
2023-05-02 11:34:53 +02:00
|
|
|
DocumentContext newContextDocJson = JsonPath.parse(jsonSourceProject, configurationGSON).set(sectionPath,
|
|
|
|
gsonOject);
|
|
|
|
// DocumentContext newDocument = JsonPath.parse(jsonSourceProject,
|
|
|
|
// configurationGSON).set(sectionPath, new JSONObject(output));
|
|
|
|
// String newDocJson = JsonPath.parse(jsonSourceProject).set(sectionPath, new
|
|
|
|
// JSONObject(output)).jsonString();
|
|
|
|
String newDocJson = newContextDocJson.json().toString();
|
2023-04-20 16:34:52 +02:00
|
|
|
LOG.info("Going to call updateProject with document: {}", newDocJson);
|
|
|
|
|
|
|
|
Document updatedDocument = Serialization.read(newDocJson.toString(), Document.class);
|
|
|
|
LOG.info("New document is: {}", updatedDocument.toJson());
|
2023-05-02 11:34:53 +02:00
|
|
|
|
2023-04-20 16:34:52 +02:00
|
|
|
}
|
2022-10-03 17:52:16 +02:00
|
|
|
|
2023-05-02 11:34:53 +02:00
|
|
|
// @Before
|
2022-10-18 15:30:12 +02:00
|
|
|
public void preloadgCubeProfilesForUCDs() {
|
|
|
|
LOG.debug("preloadgCubeProfilesForUCDs called");
|
|
|
|
try {
|
|
|
|
|
|
|
|
List<UseCaseDescriptor> listUCDs = clientUCD.getList();
|
|
|
|
|
|
|
|
for (UseCaseDescriptor ucd : listUCDs) {
|
|
|
|
|
|
|
|
LOG.debug("Loaded UCD for ID: " + ucd.getId());
|
|
|
|
GEOPORTAL_DATA_HANDLER theHandler = GEOPORTAL_DATA_HANDLER.geoportal_data_entry;
|
|
|
|
List<HandlerDeclaration> handlers = ucd.getHandlersByType(theHandler.getType());
|
|
|
|
|
|
|
|
if (handlers.size() == 0) {
|
|
|
|
LOG.warn("No handler " + theHandler + "found into UCD " + ucd.getId() + ", continue...");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loading Handler gcube_profiles
|
|
|
|
HandlerDeclaration dataEntryHandler = handlers.get(0);
|
|
|
|
HandlerDeclarationDV handlerGcubeProfiles = ConvertToDataValueObjectModel
|
|
|
|
.toHandlerDeclarationDV(dataEntryHandler, ucd, GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles);
|
|
|
|
|
|
|
|
LOG.debug("Handler " + GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles + " for PROFILE_ID: " + ucd.getId());
|
|
|
|
LOG.debug("" + handlerGcubeProfiles);
|
|
|
|
|
|
|
|
ConfigurationDV<?> config = handlerGcubeProfiles.getConfiguration();
|
|
|
|
// List of gCube Profiles defined in the UCD
|
|
|
|
List<GcubeProfileDV> listGcubeProfiles = toListGcubeProfiles(config);
|
|
|
|
LOG.debug("List of GcubeProfileDV are: " + listGcubeProfiles);
|
|
|
|
|
|
|
|
List<GcubeProfilesMetadataForUCD> listProfilesBean = new ArrayList<GcubeProfilesMetadataForUCD>();
|
|
|
|
// Loading Metadata Profile from IS
|
|
|
|
MetadataProfileFormBuilderServiceImpl metaProfileBUilder = new MetadataProfileFormBuilderServiceImpl();
|
|
|
|
linkedMap_UCDId_gCubeProfiles = new LinkedHashMap<String, List<GcubeProfilesMetadataForUCD>>();
|
|
|
|
for (GcubeProfileDV gcubeProfileDV : listGcubeProfiles) {
|
|
|
|
GcubeProfilesMetadataForUCD gCubeProfileMetadataForUCD = new GcubeProfilesMetadataForUCD();
|
|
|
|
ScopeProvider.instance.set(CONTEXT);
|
|
|
|
SecurityTokenProvider.instance.set(TOKEN);
|
|
|
|
List<MetaDataProfileBean> listProfiles = metaProfileBUilder.getProfilesInTheScopeForName(CONTEXT,
|
|
|
|
gcubeProfileDV.getGcubeSecondaryType(), gcubeProfileDV.getGcubeName());
|
|
|
|
|
|
|
|
String key = gcubeProfileDV.getGcubeSecondaryType() + gcubeProfileDV.getGcubeName();
|
|
|
|
LOG.debug("for key: " + key + " readi profiles: " + listGcubeProfiles);
|
|
|
|
gCubeProfileMetadataForUCD.setGcubeProfile(gcubeProfileDV);
|
|
|
|
gCubeProfileMetadataForUCD.setListMetadataProfileBean(listProfiles);
|
|
|
|
listProfilesBean.add(gCubeProfileMetadataForUCD);
|
|
|
|
|
|
|
|
}
|
|
|
|
linkedMap_UCDId_gCubeProfiles.put(ucd.getId(), listProfilesBean);
|
|
|
|
|
|
|
|
for (String key : linkedMap_UCDId_gCubeProfiles.keySet()) {
|
|
|
|
LOG.debug("For key '" + key + "' got profiles: " + linkedMap_UCDId_gCubeProfiles.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
// TODO: handle exception
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:38:42 +02:00
|
|
|
//@Test
|
2023-05-02 11:34:53 +02:00
|
|
|
public void deleteFileSet_ServiceTest() throws Exception {
|
|
|
|
ScopeProvider.instance.set(CONTEXT);
|
|
|
|
SecurityTokenProvider.instance.set(TOKEN);
|
|
|
|
|
|
|
|
boolean ignore_errors = false;
|
|
|
|
String path = "$.abstractRelazione.filesetIta";
|
|
|
|
|
|
|
|
Project doc = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID);
|
|
|
|
|
|
|
|
JSONPathWrapper wrapper = new JSONPathWrapper(doc.getTheDocument().toJson());
|
|
|
|
List<String> matchingPaths = wrapper.getMatchingPaths(path);
|
|
|
|
|
|
|
|
LOG.info("matchingPaths is: " + matchingPaths);
|
|
|
|
|
|
|
|
String error = null;
|
|
|
|
if (matchingPaths.isEmpty()) {
|
|
|
|
error = "No Registered FileSet found at " + path;
|
|
|
|
if (!ignore_errors) {
|
|
|
|
throw new WebApplicationException(error, Response.Status.BAD_REQUEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (matchingPaths.size() > 1 && !ignore_errors) {
|
|
|
|
error = "Multiple Fileset (" + matchingPaths.size() + ") matching " + path;
|
|
|
|
if (!ignore_errors)
|
|
|
|
throw new WebApplicationException(error, Response.Status.BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error != null && ignore_errors) {
|
|
|
|
LOG.info("Error detected {}. Ignoring it and returning input doc", error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Object> listPath = wrapper.getByPath(path);
|
|
|
|
LOG.info("List path: "+listPath);
|
|
|
|
// RegisteredFileSet fs = Serialization.convert(listPath.get(0), RegisteredFileSet.class);
|
|
|
|
// LOG.info("Going to delete {}", fs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void registerFileSet() throws Exception {
|
|
|
|
ScopeProvider.instance.set(CONTEXT);
|
|
|
|
SecurityTokenProvider.instance.set(TOKEN);
|
|
|
|
|
|
|
|
Project theProject = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID);
|
|
|
|
|
|
|
|
MongoServiceUtil mongoService = new MongoServiceUtil();
|
|
|
|
|
|
|
|
Access access = new Access();
|
|
|
|
access.setLicense("CC-BY");
|
|
|
|
access.setPolicy(AccessPolicy.OPEN);
|
|
|
|
|
|
|
|
String sectionJSONPath = "$.abstractRelazione";
|
|
|
|
String fieldName = "filesetIta";
|
|
|
|
String fieldDefinition = "$.abstractRelazione._children[?(@.filesetIta)]";
|
|
|
|
|
|
|
|
String theFileName = "Application_Profile_for_CSW_2.0-2.pdf";
|
|
|
|
String theFileURL = "https://data.dev.d4science.org/shub/E_bnN2aDJZZUMySy9peE9ScEVLNVFNWjBOZWx0cXQ2UUFkQ2E3Rjc1S29EelJIMEJGbDRoczBnbHVPWHczZTNQTw==";
|
|
|
|
|
|
|
|
FileUploadedRemote file = new FileUploadedRemote();
|
|
|
|
file.setUrl(theFileURL);
|
|
|
|
file.setFileName(theFileName);
|
|
|
|
|
|
|
|
File input = null;
|
|
|
|
File output = null;
|
|
|
|
try {
|
|
|
|
File tempDir = Files.createTempDirectory("GEOPORTAL_REPLACE_FILES_").toFile();
|
|
|
|
String tmpDirPath = tempDir.getAbsolutePath();
|
|
|
|
if (file instanceof FileUploadedRemote) {
|
|
|
|
FileUploadedRemote remote = (FileUploadedRemote) file;
|
|
|
|
LOG.info("Uploaded file is remote: " + remote.getUrl());
|
|
|
|
InputStream in = new URL(remote.getUrl()).openStream();
|
|
|
|
String fileName = (remote.getFileName() == null || remote.getFileName().isEmpty())
|
|
|
|
? "file_" + Random.nextInt()
|
|
|
|
: remote.getFileName();
|
|
|
|
LOG.info("the fileName is: " + fileName);
|
|
|
|
output = new File(tmpDirPath, fileName);
|
|
|
|
Path outputAbsolutePath = Paths.get(output.getAbsolutePath());
|
|
|
|
Files.copy(in, outputAbsolutePath, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
LOG.info("Remote file: " + remote.getUrl() + ", copied to new file: " + output.getName());
|
|
|
|
} else {
|
|
|
|
LOG.info("Uploaded file is local: " + file.getTempSystemPath());
|
|
|
|
input = new File(file.getTempSystemPath());
|
|
|
|
output = new File(tmpDirPath, file.getFileName());
|
|
|
|
copyContent(input, output);
|
|
|
|
LOG.info("Temp file: " + file.getTempSystemPath() + ", copied to new file: " + output.getName());
|
|
|
|
}
|
|
|
|
// tempDir.deleteOnExit();
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.warn("Skipping file: " + file.getFileName() + ". Error: " + e.getMessage(), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
File fileset = output;
|
|
|
|
LOG.info("the fileName is: " + fileset.getName());
|
|
|
|
|
|
|
|
mongoService.registerFileSet(theProject.getProfileID(), theProject, sectionJSONPath, fieldName, fieldDefinition,
|
|
|
|
access, fileset);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy content.
|
|
|
|
*
|
|
|
|
* @param a the a
|
|
|
|
* @param b the b
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
public static void copyContent(File a, File b) throws Exception {
|
|
|
|
FileInputStream in = new FileInputStream(a);
|
|
|
|
FileOutputStream out = new FileOutputStream(b);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
// read() function to read the
|
|
|
|
// byte of data
|
|
|
|
while ((n = in.read()) != -1) {
|
|
|
|
// write() function to write
|
|
|
|
// the byte of data
|
|
|
|
out.write(n);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (in != null) {
|
|
|
|
|
|
|
|
// close() function to close the
|
|
|
|
// stream
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
// close() function to close
|
|
|
|
// the stream
|
|
|
|
if (out != null) {
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG.debug("File Copied");
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Test
|
2022-10-03 17:52:16 +02:00
|
|
|
public void testReadProjectForUCDDataEntry() {
|
|
|
|
ScopeProvider.instance.set(CONTEXT);
|
|
|
|
SecurityTokenProvider.instance.set(TOKEN);
|
|
|
|
|
|
|
|
try {
|
2022-10-07 18:39:37 +02:00
|
|
|
|
2022-10-03 17:52:16 +02:00
|
|
|
Project theProject = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID);
|
|
|
|
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
|
|
|
|
ProjectDV theProjectDV = ConvertToDataValueObjectModel.toProjectDV(theProject, projectBuilder);
|
2022-10-06 17:31:10 +02:00
|
|
|
String theWholeProjectAsJSON = theProject.getTheDocument().toJson();
|
2022-10-07 18:39:37 +02:00
|
|
|
|
|
|
|
LOG.debug("theProjectDV as JSON: " + theWholeProjectAsJSON);
|
|
|
|
LOG.debug("theProjectDV as MAP: " + theProjectDV.getTheDocument().getDocumentAsMap());
|
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
ProjectView projectView = new ProjectView();
|
|
|
|
projectView.setTheProjectDV(theProjectDV);
|
|
|
|
|
2022-10-07 18:39:37 +02:00
|
|
|
// NO UCD defined, applyong default
|
2022-10-18 15:30:12 +02:00
|
|
|
if (linkedMap_UCDId_gCubeProfiles.size() == 0) {
|
2022-10-07 18:39:37 +02:00
|
|
|
LOG.warn("No " + GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles + " found in the UCD");
|
|
|
|
LOG.info("Applying default business logic to display the project");
|
|
|
|
SectionView sectionView = new SectionView();
|
|
|
|
sectionView.setSectionTitle("Document");
|
|
|
|
SubDocumentView subDocumentView = new SubDocumentView();
|
|
|
|
subDocumentView.setMetadataAsJSON(theProjectDV.getTheDocument().getDocumentAsJSON());
|
|
|
|
sectionView.addSubDocument(subDocumentView);
|
|
|
|
projectView.addSectionView(sectionView);
|
|
|
|
}
|
|
|
|
|
2022-10-18 15:30:12 +02:00
|
|
|
List<GcubeProfilesMetadataForUCD> listProfilesBean = linkedMap_UCDId_gCubeProfiles.get(PROFILE_ID);
|
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
|
|
|
|
.jsonProvider(new JsonOrgJsonProvider()).build();
|
2022-10-07 18:39:37 +02:00
|
|
|
|
|
|
|
// Reading the Project according to list of Profile defined in the UCD
|
2022-10-18 15:30:12 +02:00
|
|
|
for (GcubeProfilesMetadataForUCD gcubeProfileMetaForUCD : listProfilesBean) {
|
2022-10-07 18:39:37 +02:00
|
|
|
|
2022-10-18 15:30:12 +02:00
|
|
|
GcubeProfileDV gcubeProfileDV = gcubeProfileMetaForUCD.getGcubeProfile();
|
2022-10-06 17:31:10 +02:00
|
|
|
SectionView sectionView = new SectionView();
|
|
|
|
sectionView.setSectionTitle(gcubeProfileDV.getSectionTitle());
|
|
|
|
LOG.debug("\n\nThe profile is: " + gcubeProfileDV);
|
2022-10-03 17:52:16 +02:00
|
|
|
// Building JSON/section full PATH and section name
|
|
|
|
String sectionJSONPath = "";
|
2022-10-07 18:39:37 +02:00
|
|
|
String parentPathFromProfile = gcubeProfileDV.getParentName() == null ? ""
|
|
|
|
: gcubeProfileDV.getParentName();
|
2022-10-03 17:52:16 +02:00
|
|
|
String theSectionName = gcubeProfileDV.getSectionName();
|
|
|
|
|
2022-10-07 18:39:37 +02:00
|
|
|
if (theSectionName.compareTo(JSON_$_POINTER) == 0
|
|
|
|
|| theSectionName.compareTo(JSON_$_POINTER + ".") == 0) {
|
2022-10-03 17:52:16 +02:00
|
|
|
sectionJSONPath = JSON_$_POINTER;
|
|
|
|
theSectionName = "";
|
|
|
|
} else {
|
|
|
|
sectionJSONPath = String.format("%s%s",
|
2022-10-07 18:39:37 +02:00
|
|
|
parentPathFromProfile.endsWith(".") ? parentPathFromProfile : parentPathFromProfile + ".",
|
|
|
|
theSectionName);
|
2022-10-03 17:52:16 +02:00
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
|
2022-10-03 17:52:16 +02:00
|
|
|
LOG.debug("The sectionJSONPath is: " + sectionJSONPath);
|
2022-10-07 18:39:37 +02:00
|
|
|
|
|
|
|
JsonPath theSectionJsonPath = JsonPath.compile(sectionJSONPath);
|
|
|
|
Object data = theSectionJsonPath.read(theWholeProjectAsJSON, configuration);
|
|
|
|
|
|
|
|
LOG.debug("Data is instace of: " + data.getClass());
|
|
|
|
LOG.debug("data to string: " + data.toString());
|
|
|
|
|
|
|
|
// Splitting the General Document in bson.Document according to list of
|
|
|
|
// GcubeProfiles
|
2022-10-06 17:31:10 +02:00
|
|
|
List<Document> listBSONDocument = new ArrayList<Document>();
|
2022-10-07 18:39:37 +02:00
|
|
|
if (data instanceof org.json.JSONObject) {
|
2022-10-06 17:31:10 +02:00
|
|
|
String jsonString = data.toString();
|
2022-10-07 18:39:37 +02:00
|
|
|
LOG.debug("the JSON to string: " + jsonString);
|
|
|
|
Document sectionDoc = Document.parse(jsonString);
|
|
|
|
boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, sectionJSONPath);
|
|
|
|
if (isAccessibleSection) {
|
|
|
|
listBSONDocument.add(sectionDoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (data instanceof org.json.JSONArray) {
|
2022-10-06 17:31:10 +02:00
|
|
|
org.json.JSONArray dataArray = (org.json.JSONArray) data;
|
2022-10-07 18:39:37 +02:00
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
2022-10-06 17:31:10 +02:00
|
|
|
String jsonString = dataArray.get(i).toString();
|
2022-10-07 18:39:37 +02:00
|
|
|
LOG.debug("the array " + i + " JSON to string: " + jsonString);
|
|
|
|
Document sectionDoc = Document.parse(jsonString);
|
|
|
|
boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, sectionJSONPath);
|
|
|
|
if (isAccessibleSection) {
|
|
|
|
listBSONDocument.add(sectionDoc);
|
|
|
|
}
|
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
|
|
|
|
LOG.debug("Result for " + gcubeProfileDV.getSectionName() + " is: " + listBSONDocument);
|
2022-10-18 15:30:12 +02:00
|
|
|
List<MetaDataProfileBean> theProfileBeans = gcubeProfileMetaForUCD.getListMetadataProfileBean();
|
2022-10-03 17:52:16 +02:00
|
|
|
MetaDataProfileBean theProfileBean = theProfileBeans.get(0);
|
2022-10-07 18:39:37 +02:00
|
|
|
|
|
|
|
// For each bson.Document creating the SubDocumentView
|
2022-10-06 17:31:10 +02:00
|
|
|
for (int i = 0; i < listBSONDocument.size(); i++) {
|
2022-10-07 18:39:37 +02:00
|
|
|
Document fromSectionDoc = listBSONDocument.get(i);
|
2022-10-06 17:31:10 +02:00
|
|
|
SubDocumentView subDocumentView = new SubDocumentView();
|
2022-10-07 18:39:37 +02:00
|
|
|
Document toSectionDoc = new Document();
|
|
|
|
// Filling the SubDocumentView metadata with the metadataField.getFieldName()
|
|
|
|
// read from the Profile
|
2022-10-06 17:31:10 +02:00
|
|
|
for (MetadataFieldWrapper metadataField : theProfileBean.getMetadataFields()) {
|
2022-10-07 18:39:37 +02:00
|
|
|
|
|
|
|
String theFieldName = metadataField.getFieldId() != null ? metadataField.getFieldId()
|
|
|
|
: metadataField.getFieldName();
|
2022-10-06 17:31:10 +02:00
|
|
|
LOG.debug("reading theFieldName: " + theFieldName);
|
2022-10-07 18:39:37 +02:00
|
|
|
Object theOBJFieldValue = fromSectionDoc.get(theFieldName);
|
2022-10-06 17:31:10 +02:00
|
|
|
|
2022-10-07 18:39:37 +02:00
|
|
|
// NB: Using ALWAYS THE metadataField.getFieldName() as LABEL
|
|
|
|
toSectionDoc = sanitizeDocumentValue(toSectionDoc, metadataField.getFieldName(),
|
|
|
|
theOBJFieldValue);
|
2022-10-06 17:31:10 +02:00
|
|
|
|
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
String subToJSON = toSectionDoc.toJson();
|
2022-10-06 17:31:10 +02:00
|
|
|
LOG.debug("theSubSetionDoc is: " + subToJSON);
|
2022-10-07 18:39:37 +02:00
|
|
|
subDocumentView.setMetadataAsJSON(toSectionDoc.toJson());
|
|
|
|
|
|
|
|
// Reading filePaths
|
|
|
|
List<FilePathDV> filePaths = gcubeProfileDV.getFilePaths();
|
|
|
|
|
|
|
|
// READING fileset* field ACCORDING TO filePaths OF THE 'gcubeProfiles' CONFIG
|
|
|
|
if (filePaths != null) {
|
|
|
|
String fromSectionDocJSON = fromSectionDoc.toJson();
|
2022-10-18 15:30:12 +02:00
|
|
|
List<FilesetDV> listFiles = new ArrayList<FilesetDV>();
|
|
|
|
List<FilesetDV> listImages = new ArrayList<FilesetDV>();
|
2022-10-07 18:39:37 +02:00
|
|
|
for (FilePathDV filePath : filePaths) {
|
|
|
|
|
|
|
|
String filesetJSONPath = String.format("%s.%s", JSON_$_POINTER, filePath.getFieldName());
|
2022-10-18 15:30:12 +02:00
|
|
|
List<Payload> listPayloads = readPayloadsForFileset(filesetJSONPath, fromSectionDocJSON);
|
2022-10-07 18:39:37 +02:00
|
|
|
FilesetDV filesetDV = new FilesetDV();
|
2023-04-20 16:34:52 +02:00
|
|
|
filesetDV.setGcubeProfileFieldName(filePath.getGcubeProfileFieldName());
|
2022-10-18 15:30:12 +02:00
|
|
|
for (Payload payload : listPayloads) {
|
|
|
|
PayloadDV payloadDV = ConvertToDataValueObjectModel.toPayloadDV(payload);
|
|
|
|
filesetDV.addPayloadDV(payloadDV);
|
|
|
|
boolean isImage = ImageDetector.isImage(payload.getMimetype());
|
|
|
|
|
|
|
|
if (isImage) {
|
|
|
|
listImages.add(filesetDV);
|
|
|
|
} else {
|
|
|
|
listFiles.add(filesetDV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:39:37 +02:00
|
|
|
}
|
2022-10-18 15:30:12 +02:00
|
|
|
subDocumentView.setListFiles(listFiles);
|
|
|
|
subDocumentView.setListImages(listImages);
|
2022-10-07 18:39:37 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
sectionView.addSubDocument(subDocumentView);
|
2022-10-07 18:39:37 +02:00
|
|
|
|
2022-10-03 17:52:16 +02:00
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
projectView.addSectionView(sectionView);
|
2022-10-03 17:52:16 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
for (SectionView section : projectView.getListSections()) {
|
2022-10-07 18:39:37 +02:00
|
|
|
System.out.println("\n\n###### Section Title: " + section.getSectionTitle() + " ######");
|
|
|
|
int i = 1;
|
2022-10-06 17:31:10 +02:00
|
|
|
for (SubDocumentView subDocument : section.getListSubDocuments()) {
|
2022-10-18 15:30:12 +02:00
|
|
|
System.out.println("## " + SubDocumentView.class.getSimpleName() + " n." + i);
|
2022-10-07 18:39:37 +02:00
|
|
|
System.out.println("***** Metadata");
|
|
|
|
System.out.println(prettyPrintJSON(subDocument.getMetadataAsJSON()));
|
|
|
|
System.out.println("***** Files");
|
|
|
|
if (subDocument.getListFiles() != null) {
|
|
|
|
for (FilesetDV filesetDV : subDocument.getListFiles()) {
|
2023-04-20 16:34:52 +02:00
|
|
|
System.out.println("******* File Fileset name: " + filesetDV.getGcubeProfileFieldName());
|
2022-10-18 15:30:12 +02:00
|
|
|
for (PayloadDV payload : filesetDV.getListPayload()) {
|
|
|
|
System.out.println("********* Payload: " + payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("***** Images");
|
|
|
|
if (subDocument.getListImages() != null) {
|
|
|
|
for (FilesetDV filesetDV : subDocument.getListImages()) {
|
2023-04-20 16:34:52 +02:00
|
|
|
System.out.println("******* Image Fileset name: " + filesetDV.getGcubeProfileFieldName());
|
2022-10-07 18:39:37 +02:00
|
|
|
for (PayloadDV payload : filesetDV.getListPayload()) {
|
|
|
|
System.out.println("********* Payload: " + payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
2022-10-06 17:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-10-03 17:52:16 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
System.out.println("error");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
System.out.println("\n\n Procedure termimated!!!");
|
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
|
2022-10-18 15:30:12 +02:00
|
|
|
private List<Payload> readPayloadsForFileset(String filesetJSONPath, String sectionJSONDocument) {
|
2022-10-07 18:39:37 +02:00
|
|
|
LOG.debug("readPayloadsForFileset called");
|
|
|
|
|
2022-10-18 15:30:12 +02:00
|
|
|
List<Payload> listPayloads = new ArrayList<Payload>();
|
2022-10-07 18:39:37 +02:00
|
|
|
String _payloadsJSONPath = String.format("%s.%s", filesetJSONPath, "_payloads");
|
|
|
|
try {
|
|
|
|
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
|
|
|
|
.jsonProvider(new JsonOrgJsonProvider()).build();
|
|
|
|
|
|
|
|
LOG.info("Reading sectionPath at {} into section document {}", _payloadsJSONPath, sectionJSONDocument);
|
|
|
|
JsonPath theSectionPolycJsonPath = JsonPath.compile(_payloadsJSONPath);
|
|
|
|
Object _payloads = theSectionPolycJsonPath.read(sectionJSONDocument, configuration).toString();
|
|
|
|
|
|
|
|
if (_payloads instanceof String) {
|
|
|
|
String toStringPayloads = (String) _payloads;
|
|
|
|
LOG.trace("The _payloads is a String {}", toStringPayloads);
|
|
|
|
JSONArray jsonArray = new JSONArray(toStringPayloads);
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
2022-10-18 15:30:12 +02:00
|
|
|
Payload payloadDV = Serialization.read(jsonArray.getJSONObject(i).toString(), Payload.class);
|
2022-10-07 18:39:37 +02:00
|
|
|
listPayloads.add(payloadDV);
|
2022-10-06 17:31:10 +02:00
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG.info("returning list of payloads {}", listPayloads);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.error("Error on reading the JSON Path {} in the doc {} ", _payloadsJSONPath, sectionJSONDocument);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return listPayloads;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isAccessibleSectionAccordingToPolicy(Document section, String sectionJSONPath) {
|
|
|
|
LOG.debug("isAccessibleSectionAccordingToPolicy called");
|
|
|
|
boolean isAccessible = true;
|
|
|
|
|
|
|
|
// Skipping the root, going to check the access_policy of subsections
|
|
|
|
if (sectionJSONPath.compareTo(JSON_$_POINTER) != 0) {
|
|
|
|
isAccessible = checkAccessPolicy(section.toJson(), MY_LOGIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return isAccessible;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkAccessPolicy(String sectionDocumentJSON, String myLogin) {
|
|
|
|
LOG.debug("checkAccessPolicy called");
|
|
|
|
// CHECKING THE POLICY
|
|
|
|
String accessPolicyPath = JSON_$_POINTER + "._access._policy";
|
|
|
|
boolean isAccessible = true;
|
|
|
|
try {
|
|
|
|
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
|
|
|
|
.jsonProvider(new JsonOrgJsonProvider()).build();
|
|
|
|
|
|
|
|
LOG.info("Reading access policy at {} into section document {}", accessPolicyPath, sectionDocumentJSON);
|
|
|
|
JsonPath theSectionPolycJsonPath = JsonPath.compile(accessPolicyPath);
|
|
|
|
String _policy = theSectionPolycJsonPath.read(sectionDocumentJSON, configuration).toString();
|
|
|
|
LOG.info("The section {} has policy {}", accessPolicyPath, _policy);
|
|
|
|
isAccessible = GeportalCheckAccessPolicy.isAccessible(_policy, myLogin);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.error(accessPolicyPath + " not found. Check OK");
|
|
|
|
}
|
|
|
|
LOG.info("Is the section {} accessible? {}", sectionDocumentJSON, isAccessible);
|
|
|
|
return isAccessible;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String prettyPrintJSON(String jsonString) {
|
|
|
|
|
|
|
|
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
|
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
|
|
|
|
return gson.toJson(jsonObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Document sanitizeDocumentValue(Document toDoc, String fieldLabel, Object theObjectFieldValue) {
|
|
|
|
|
|
|
|
if (theObjectFieldValue != null) {
|
|
|
|
if (theObjectFieldValue instanceof String) {
|
|
|
|
String toString = (String) theObjectFieldValue;
|
|
|
|
if (toString != null && !toString.isEmpty()) {
|
|
|
|
toDoc.append(fieldLabel, theObjectFieldValue);
|
|
|
|
} else {
|
|
|
|
LOG.debug("Skipping String field " + fieldLabel + " its value is null or empty");
|
2022-10-06 17:31:10 +02:00
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
|
|
|
|
} else if (theObjectFieldValue instanceof ArrayList) {
|
|
|
|
ArrayList toArrayList = (ArrayList) theObjectFieldValue;
|
|
|
|
if (toArrayList != null && !toArrayList.isEmpty()) {
|
|
|
|
toDoc.append(fieldLabel, theObjectFieldValue);
|
|
|
|
} else {
|
|
|
|
LOG.debug("Skipping ArrayList field " + fieldLabel + " its value is null or empty");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
toDoc.append(fieldLabel, theObjectFieldValue);
|
2022-10-06 17:31:10 +02:00
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
} else {
|
|
|
|
LOG.debug("Skipping field " + fieldLabel + " its value is null or empty");
|
2022-10-06 17:31:10 +02:00
|
|
|
}
|
2022-10-07 18:39:37 +02:00
|
|
|
|
2022-10-06 17:31:10 +02:00
|
|
|
return toDoc;
|
2022-10-03 17:52:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To list gcube profiles.
|
|
|
|
*
|
|
|
|
* @param config the config
|
|
|
|
* @return the list
|
|
|
|
*/
|
|
|
|
private List<GcubeProfileDV> toListGcubeProfiles(ConfigurationDV<?> config) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
return (List<GcubeProfileDV>) config.getConfiguration();
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.err.println("Error on casting " + ConfigurationDV.class.getName() + " to List of "
|
|
|
|
+ GcubeProfileDV.class.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|