fixed error message

This commit is contained in:
Francesco Mangiacrapa 2024-05-10 15:58:01 +02:00
parent e11a9a49dc
commit d57b1530d0
1 changed files with 237 additions and 229 deletions

View File

@ -37,251 +37,259 @@ import lombok.Data;
import lombok.Synchronized; import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
* The Class SDIMaterializerPlugin. * The Class SDIMaterializerPlugin.
* *
* @author created by Fabio Sinibaldi * @author created by Fabio Sinibaldi
* @author new architect and maintainer - Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * @author new architect and maintainer - Francesco Mangiacrapa at ISTI-CNR
* francesco.mangiacrapa@isti.cnr.it
* *
* Apr 28, 2023 * Apr 28, 2023
*/ */
@Slf4j @Slf4j
public class SDIMaterializerPlugin extends AbstractPlugin implements MaterializationPlugin { public class SDIMaterializerPlugin extends AbstractPlugin implements MaterializationPlugin {
/** /**
* Instantiates a new materialization config. * Instantiates a new materialization config.
*/ */
@Data @Data
public static class MaterializationConfig{ public static class MaterializationConfig {
private String schemaField; private String schemaField;
private String documentPath; private String documentPath;
} }
Map<String, SDIManagerWrapper> sdiManagerMap=null; Map<String, SDIManagerWrapper> sdiManagerMap = null;
/**
* Gets the SDI manager.
*
* @return the SDI manager
*/
private SDIManagerWrapper getSDIManager() {
String context = UserUtils.getCurrent().getContext();
log.debug("Getting SDIManagerWrapper from cache map for context {}");
return sdiManagerMap.get(context);
}
/** /**
* Gets the SDI manager. * Inits the in context.
* *
* @return the SDI manager * @return the initialization report
*/ * @throws InitializationException the initialization exception
private SDIManagerWrapper getSDIManager(){ */
String context = UserUtils.getCurrent().getContext(); @Override
log.debug("Getting SDIManagerWrapper from cache map for context {}"); @Synchronized
return sdiManagerMap.get(context); public InitializationReport initInContext() throws InitializationException {
} InitializationReport report = new InitializationReport();
try {
/** String context = UserUtils.getCurrent().getContext();
* Inits the in context. if (getSDIManager() == null) {
* log.info("Initializing in " + context);
* @return the initialization report sdiManagerMap.put(context, new SDIManagerWrapper());
* @throws InitializationException the initialization exception }
*/ report.setStatus(Report.Status.OK);
@Override report.putMessage(
@Synchronized "Initialized SDI Manager in " + context + " pointing to " + getSDIManager().getGeoserverHostName());
public InitializationReport initInContext() throws InitializationException { } catch (SDIInteractionException e) {
InitializationReport report = new InitializationReport(); throw new InitializationException("Unable to initialize SDI Manager ", e);
try{
String context = UserUtils.getCurrent().getContext();
if(getSDIManager()==null) {
log.info("Initializing in " + context);
sdiManagerMap.put(context,new SDIManagerWrapper());
}
report.setStatus(Report.Status.OK);
report.putMessage("Initialized SDI Manager in "+context+" pointing to "+getSDIManager().getGeoserverHostName());
} catch (SDIInteractionException e) {
throw new InitializationException("Unable to initialize SDI Manager ",e);
}
return report;
}
/**
* Inits the.
*
* @return the initialization report
* @throws InitializationException the initialization exception
*/
@Override
@Synchronized
public InitializationReport init() throws InitializationException {
InitializationReport report = new InitializationReport();
sdiManagerMap=new HashMap<>();
report.setStatus(Report.Status.OK);
return report;
}
/**
* Shutdown.
*
* @throws ShutDownException the shut down exception
*/
@Override
public void shutdown() throws ShutDownException {
}
/**
* Expected params :
* -workspace MANDATORY
* -titleField.
*
* @param request the request
* @return the materialization report
* @throws MaterializationException the materialization exception
* @throws InvalidPluginRequestException the invalid plugin request exception
*/
@Override
public MaterializationReport materialize(MaterializationRequest request) throws MaterializationException, InvalidPluginRequestException {
log.info("Serving Materialize {} : request CallParameters {}, request Context {}",this.getDescriptor().getId(), request.getCallParameters(), request.getContext());
log.debug("Materialize request {}",request);
Project project =request.getDocument();
UseCaseDescriptor useCaseDescriptor = request.getUseCaseDescriptor();
Document requestArguments=Serialization.convert(request.getCallParameters(),Document.class);
MaterializationReport report= new MaterializationReport(request);
try{
Document profileConfiguration =getConfigurationFromProfile(useCaseDescriptor).getConfiguration();
log.debug("UseCaseDescriptor Configuration is {} ",profileConfiguration);
JSONPathWrapper documentNavigator=new JSONPathWrapper(project.getTheDocument().toJson());
JSONPathWrapper schemaNavigator=new JSONPathWrapper(useCaseDescriptor.getSchema().toJson());
for(Object fsConfigObj : profileConfiguration.get("registeredFileSetPaths", List.class)){
log.debug("Managing {} ",fsConfigObj);
MaterializationConfig fsConfig=Serialization.convert(fsConfigObj,MaterializationConfig.class);
List matchingFieldDefinitions =schemaNavigator.getByPath(fsConfig.getSchemaField());
if(matchingFieldDefinitions==null || matchingFieldDefinitions.isEmpty())
throw new MaterializationException("Invalid Field Definition path in configuration [NO MATCH] : "+
fsConfig.getSchemaField());
if(matchingFieldDefinitions.size()>1)
throw new MaterializationException("Invalid Field Definition path in configuration [MATCHES "+matchingFieldDefinitions.size()+"] : "+
fsConfig.getSchemaField());
Field f = Serialization.convert(matchingFieldDefinitions.get(0),Field.class);
// TODO Check if schema points to RegisteredFileSet
for(Object fsObject : documentNavigator.getByPath(fsConfig.getDocumentPath())){
RegisteredFileSet fs = Serialization.convert(fsObject,RegisteredFileSet.class);
log.debug("Found {} ",fs);
requestArguments.putIfAbsent("basePersistencePath", useCaseDescriptor.getId());
requestArguments.putIfAbsent("documentID", project.getId());
if(requestArguments.containsKey("titleField"))
requestArguments.putIfAbsent("layerTitle",fs.getString(requestArguments.getString("titleField")));
else requestArguments.putIfAbsent("layerTitle",fs.getUUID());
//Add FS uuid at ws_baseName
String ws = request.getMandatory("workspace")+"_"+ project.getId();
ws = ws.replaceAll("\\W","_");
log.trace("Escaped WS will be {} ",ws);
requestArguments.put("workspace",ws);
// Actually materializing
log.info("Calling materializeLayer: fs {} - requestArguments {}", fs, requestArguments);
RegisteredFileSet obtained = getSDIManager().materializeLayer(fs,requestArguments);
log.debug("Obtained {} ",obtained);
documentNavigator.setElement("$..[?(@."+ RegisteredFileSet.UUID+" == '"+fs.getUUID()+"')]",obtained);
}
}
String finalDocument = documentNavigator.getValueCTX().jsonString();
log.debug("Final document after materialization is {} ",finalDocument);
report.setResultingDocument(Document.parse(finalDocument));
report.setStatus(Report.Status.OK);
}catch (SDIInteractionException e){
log.error("Unable to materialize "+request,e);
report.setStatus(Report.Status.ERROR);
report.putMessage(e.getMessage());
}catch (Throwable t){
log.error("Exception, Unable to materialize "+request,t);
report.setStatus(Report.Status.ERROR);
report.putMessage(t.getMessage());
}finally{
return report;
}
}
/**
* Expected parameters
* - fileSetPath.
*
* @param request the request
* @return the materialization report
* @throws MaterializationException the materialization exception
* @throws InvalidPluginRequestException the invalid plugin request exception
*
* Updated by Francesco
*/
@Override
public MaterializationReport dematerialize(MaterializationRequest request) throws MaterializationException, InvalidPluginRequestException {
log.info("Serving DeMaterialize {} : request CallParameters {}, request Context {}: ",this.getDescriptor().getId(), request.getCallParameters(), request.getContext());
log.debug("DeMaterialize request {}",request);
MaterializationReport report= new MaterializationReport(request);
Boolean ignoreErrors;
try {
ignoreErrors = (Boolean) report.getTheRequest().getCallParameters().getOrDefault(InterfaceConstants.Parameters.IGNORE_ERRORS, false);
}catch (Exception e) {
ignoreErrors = false;
} }
try{ return report;
SDIManagerWrapper sdi=getSDIManager(); }
JSONPathWrapper wrapper = new JSONPathWrapper(request.getDocument().getTheDocument().toJson());
for(String s : wrapper.getMatchingPaths(request.getMandatory("fileSetPath"))){ /**
log.debug("Found matching {} ",s); * Inits the.
RegisteredFileSet registeredFileSet=Serialization.convert(wrapper.getByPath(s).get(0),RegisteredFileSet.class); *
List<Object> toKeep = new ArrayList<>(); * @return the initialization report
if(registeredFileSet.getMaterializations()!=null) * @throws InitializationException the initialization exception
for(Object matObj : registeredFileSet.getMaterializations()){ */
Materialization mat = Serialization.convert(matObj,Materialization.class); @Override
if(mat.getType().equals(GCubeSDILayer.GCUBE_SDY_LAYER_TYPE)) { @Synchronized
log.debug("Deleting Layer {} ",mat); public InitializationReport init() throws InitializationException {
try { InitializationReport report = new InitializationReport();
sdi.deleteLayer(Serialization.convert(matObj, GCubeSDILayer.class)); sdiManagerMap = new HashMap<>();
}catch (Exception e) { report.setStatus(Report.Status.OK);
if(ignoreErrors) { return report;
log.warn("Skipping error: ", e.getMessage()); }
}else
/**
* Shutdown.
*
* @throws ShutDownException the shut down exception
*/
@Override
public void shutdown() throws ShutDownException {
}
/**
* Expected params : -workspace MANDATORY -titleField.
*
* @param request the request
* @return the materialization report
* @throws MaterializationException the materialization exception
* @throws InvalidPluginRequestException the invalid plugin request exception
*/
@Override
public MaterializationReport materialize(MaterializationRequest request)
throws MaterializationException, InvalidPluginRequestException {
log.info("Serving Materialize {} : request CallParameters {}, request Context {}", this.getDescriptor().getId(),
request.getCallParameters(), request.getContext());
log.debug("Materialize request {}", request);
Project project = request.getDocument();
UseCaseDescriptor useCaseDescriptor = request.getUseCaseDescriptor();
Document requestArguments = Serialization.convert(request.getCallParameters(), Document.class);
MaterializationReport report = new MaterializationReport(request);
try {
Document profileConfiguration = getConfigurationFromProfile(useCaseDescriptor).getConfiguration();
log.debug("UseCaseDescriptor Configuration is {} ", profileConfiguration);
JSONPathWrapper documentNavigator = new JSONPathWrapper(project.getTheDocument().toJson());
JSONPathWrapper schemaNavigator = new JSONPathWrapper(useCaseDescriptor.getSchema().toJson());
for (Object fsConfigObj : profileConfiguration.get("registeredFileSetPaths", List.class)) {
log.debug("Managing {} ", fsConfigObj);
MaterializationConfig fsConfig = Serialization.convert(fsConfigObj, MaterializationConfig.class);
List matchingFieldDefinitions = schemaNavigator.getByPath(fsConfig.getSchemaField());
if (matchingFieldDefinitions == null || matchingFieldDefinitions.isEmpty())
throw new MaterializationException(
"Invalid Field Definition path in configuration [NO MATCH] : " + fsConfig.getSchemaField());
if (matchingFieldDefinitions.size() > 1)
throw new MaterializationException("Invalid Field Definition path in configuration [MATCHES "
+ matchingFieldDefinitions.size() + "] : " + fsConfig.getSchemaField());
Field f = Serialization.convert(matchingFieldDefinitions.get(0), Field.class);
// TODO Check if schema points to RegisteredFileSet
for (Object fsObject : documentNavigator.getByPath(fsConfig.getDocumentPath())) {
RegisteredFileSet fs = Serialization.convert(fsObject, RegisteredFileSet.class);
log.debug("Found {} ", fs);
requestArguments.putIfAbsent("basePersistencePath", useCaseDescriptor.getId());
requestArguments.putIfAbsent("documentID", project.getId());
if (requestArguments.containsKey("titleField"))
requestArguments.putIfAbsent("layerTitle",
fs.getString(requestArguments.getString("titleField")));
else
requestArguments.putIfAbsent("layerTitle", fs.getUUID());
// Add FS uuid at ws_baseName
String ws = request.getMandatory("workspace") + "_" + project.getId();
ws = ws.replaceAll("\\W", "_");
log.trace("Escaped WS will be {} ", ws);
requestArguments.put("workspace", ws);
// Actually materializing
log.info("Calling materializeLayer: fs {} - requestArguments {}", fs, requestArguments);
RegisteredFileSet obtained = getSDIManager().materializeLayer(fs, requestArguments);
log.debug("Obtained {} ", obtained);
documentNavigator.setElement("$..[?(@." + RegisteredFileSet.UUID + " == '" + fs.getUUID() + "')]",
obtained);
}
}
String finalDocument = documentNavigator.getValueCTX().jsonString();
log.debug("Final document after materialization is {} ", finalDocument);
report.setResultingDocument(Document.parse(finalDocument));
report.setStatus(Report.Status.OK);
} catch (SDIInteractionException e) {
log.error("Unable to materialize " + request, e);
report.setStatus(Report.Status.ERROR);
report.putMessage(e.getMessage());
} catch (Throwable t) {
log.error("Exception, Unable to materialize " + request, t);
report.setStatus(Report.Status.ERROR);
report.putMessage(t.getMessage());
} finally {
return report;
}
}
/**
* Expected parameters - fileSetPath.
*
* @param request the request
* @return the materialization report
* @throws MaterializationException the materialization exception
* @throws InvalidPluginRequestException the invalid plugin request exception
*
* Updated by Francesco
*/
@Override
public MaterializationReport dematerialize(MaterializationRequest request)
throws MaterializationException, InvalidPluginRequestException {
log.info("Serving DeMaterialize {} : request CallParameters {}, request Context {}: ",
this.getDescriptor().getId(), request.getCallParameters(), request.getContext());
log.debug("DeMaterialize request {}", request);
MaterializationReport report = new MaterializationReport(request);
Boolean ignoreErrors;
try {
ignoreErrors = (Boolean) report.getTheRequest().getCallParameters()
.getOrDefault(InterfaceConstants.Parameters.IGNORE_ERRORS, false);
} catch (Exception e) {
ignoreErrors = false;
}
try {
SDIManagerWrapper sdi = getSDIManager();
JSONPathWrapper wrapper = new JSONPathWrapper(request.getDocument().getTheDocument().toJson());
for (String s : wrapper.getMatchingPaths(request.getMandatory("fileSetPath"))) {
log.debug("Found matching {} ", s);
RegisteredFileSet registeredFileSet = Serialization.convert(wrapper.getByPath(s).get(0),
RegisteredFileSet.class);
List<Object> toKeep = new ArrayList<>();
if (registeredFileSet.getMaterializations() != null)
for (Object matObj : registeredFileSet.getMaterializations()) {
Materialization mat = Serialization.convert(matObj, Materialization.class);
if (mat.getType().equals(GCubeSDILayer.GCUBE_SDY_LAYER_TYPE)) {
log.debug("Deleting Layer {} ", mat);
try {
sdi.deleteLayer(Serialization.convert(matObj, GCubeSDILayer.class));
} catch (Exception e) {
log.error("Error on deleting layer: ", e);
if (ignoreErrors) {
log.warn("\nSkipping error: {}\n", e.getMessage());
} else
throw e; throw e;
} }
}else toKeep.add(matObj); } else
} toKeep.add(matObj);
// Resetting remaining materializations }
registeredFileSet.put(RegisteredFileSet.MATERIALIZATIONS,toKeep); // Resetting remaining materializations
// Update FS in doc registeredFileSet.put(RegisteredFileSet.MATERIALIZATIONS, toKeep);
wrapper.setElement(s,registeredFileSet); // Update FS in doc
wrapper.setElement(s, registeredFileSet);
} }
// Resetting Document // Resetting Document
report.setResultingDocument(Document.parse(wrapper.getValueCTX().jsonString())); report.setResultingDocument(Document.parse(wrapper.getValueCTX().jsonString()));
}catch (SDIInteractionException e){ } catch (SDIInteractionException e) {
log.error("Unable to materialize "+request,e); log.error("Unable to materialize " + request, e);
report.setStatus(Report.Status.ERROR); report.setStatus(Report.Status.ERROR);
report.putMessage(e.getMessage()); report.putMessage(e.getMessage());
}catch (Throwable t){ } catch (Throwable t) {
log.error("Unable to materialize "+request,t); log.error("Unable to materialize " + request, t);
report.setStatus(Report.Status.ERROR); report.setStatus(Report.Status.ERROR);
report.putMessage(t.getMessage()); report.putMessage(t.getMessage());
}finally{ } finally {
return report; return report;
} }
} }
static final PluginDescriptor DESCRIPTOR=new PluginDescriptor("SDI-Default-Materializer", MaterializerPluginDescriptor.MATERIALIZER); static final PluginDescriptor DESCRIPTOR = new PluginDescriptor("SDI-Default-Materializer",
static { MaterializerPluginDescriptor.MATERIALIZER);
DESCRIPTOR.setDescription("SDI Materializer. " + static {
"This plugin materialize FileSets in gCube SDI."); DESCRIPTOR.setDescription("SDI Materializer. " + "This plugin materialize FileSets in gCube SDI.");
DESCRIPTOR.setVersion(new Semver("1.0.0")); DESCRIPTOR.setVersion(new Semver("1.0.0"));
} }
/** /**
* Gets the descriptor. * Gets the descriptor.
* *
* @return the descriptor * @return the descriptor
*/ */
@Override @Override
public PluginDescriptor getDescriptor() { public PluginDescriptor getDescriptor() {
return DESCRIPTOR; return DESCRIPTOR;
} }
} }