use of org.eclipse.vorto.repository.core.Diagnostic in project vorto by eclipse.
the class ModelValidationDiagnostic method apply.
@Override
public Collection<Diagnostic> apply(Node node) {
Objects.requireNonNull(node);
Collection<Diagnostic> diagnostics = Lists.newArrayList();
String nodeId = null;
try {
nodeId = node.getIdentifier();
Node contentNode = node.getNode("jcr:content");
if (contentNode == null) {
diagnostics.add(new Diagnostic(ModelIdHelper.fromPath(node.getPath()), "Model node is empty. No <jcr:content>."));
return diagnostics;
}
Property contentProperty = contentNode.getProperty("jcr:data");
if (contentProperty == null) {
diagnostics.add(new Diagnostic(ModelIdHelper.fromPath(node.getPath()), "Model node has no file. No <jcr:data> property."));
return diagnostics;
}
try {
logger.debug("Validating \n" + IOUtils.toString(contentProperty.getBinary().getStream()));
modelParserFactory.getParser(node.getName()).enableValidation().parse(contentProperty.getBinary().getStream());
} catch (ValidationException e) {
diagnostics.add(new Diagnostic(NodeDiagnosticUtils.getModelId(node.getPath()).orElse(null), e.getMessage()));
} catch (NotAuthorizedException e) {
diagnostics.add(new Diagnostic(e.getModelId(), "Not authorized to view model '" + e.getModelId().getPrettyFormat() + "'"));
} catch (Exception e) {
logger.error("Caught error in diagnosing '" + node + "'", e);
diagnostics.add(new Diagnostic(NodeDiagnosticUtils.getModelId(node.getPath()).orElse(null), NodeDiagnosticUtils.compileErrorMessage(e)));
}
} catch (RepositoryException e) {
throw new NodeDiagnosticException("Exception while trying to validate node '" + nodeId + "'", e);
}
return diagnostics;
}
use of org.eclipse.vorto.repository.core.Diagnostic in project vorto by eclipse.
the class MetadataIntegrityDiagnostic method checkModelType.
private Optional<Diagnostic> checkModelType(final Node node) {
ModelId modelId = null;
try {
modelId = NodeDiagnosticUtils.getModelId(node.getPath()).orElseThrow(() -> new NodeDiagnosticException("Cannot get modelId of node"));
ModelType fileModelType = ModelType.create(node.getName());
ModelType nodeModelType = ModelType.valueOf(node.getProperty("vorto:type").getString());
if (!fileModelType.equals(nodeModelType)) {
String message = new StringBuilder("The model type should be '").append(fileModelType.name()).append("' but is '").append(nodeModelType.name()).append("'").toString();
return Optional.of(new Diagnostic(modelId, message));
}
return Optional.empty();
} catch (RepositoryException e) {
return Optional.of(new Diagnostic(modelId, "Got exception while checking node 'vorto:type' : " + e.getMessage()));
}
}
use of org.eclipse.vorto.repository.core.Diagnostic in project vorto by eclipse.
the class RepositoryDiagnostics method diagnose.
public Collection<Diagnostic> diagnose(final Node node) {
if (node == null) {
return Collections.emptyList();
}
Collection<Diagnostic> diagnostics = Lists.newArrayList();
try {
NodeIterator iterator = node.getNodes();
while (iterator.hasNext()) {
Node childNode = iterator.nextNode();
if (!isSystemNode(childNode)) {
if (isModelNode(childNode)) {
Collection<Diagnostic> modelDiagnostics = diagnoseNode(childNode);
for (Diagnostic diagnostic : modelDiagnostics) {
diagnostic.setNodeId(childNode.getIdentifier());
}
diagnostics.addAll(modelDiagnostics);
} else {
diagnostics.addAll(diagnose(childNode));
}
}
}
} catch (RepositoryException e) {
throw new FatalModelRepositoryException("Diagnostics threw an exception", e);
}
return diagnostics.stream().distinct().collect(Collectors.toList());
}
use of org.eclipse.vorto.repository.core.Diagnostic in project vorto by eclipse.
the class DiagnosticsController method diagnose.
@GetMapping
@PreAuthorize("hasAuthority('sysadmin')")
public Collection<Diagnostic> diagnose() {
Collection<Diagnostic> diagnostics = new ArrayList<>();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
diagnostics = namespaceRepository.findAll().stream().flatMap(namespace -> repoFactory.getDiagnosticsService(namespace.getWorkspaceId(), auth).diagnoseAllModels().stream().map(d -> d.setWorkspaceId(namespace.getWorkspaceId()))).collect(Collectors.toList());
return diagnostics;
}
use of org.eclipse.vorto.repository.core.Diagnostic in project vorto by eclipse.
the class ReferenceIntegrityDiagnostic method apply.
@Override
public Collection<Diagnostic> apply(Node node) {
Collection<Diagnostic> diagnostics = Lists.newArrayList();
try {
String nodeId = node.getIdentifier();
ModelId modelId = NodeDiagnosticUtils.getModelId(node.getPath()).orElseThrow(() -> new NodeDiagnosticException("Cannot generate modelId from supplied node with identifier '" + nodeId + "'"));
if (node.hasProperty(VORTO_REFERENCES)) {
for (Value value : node.getProperty(VORTO_REFERENCES).getValues()) {
try {
node.getSession().getNodeByIdentifier(value.getString());
} catch (ItemNotFoundException e) {
diagnostics.add(new Diagnostic(modelId, "The model has reference to node '" + value.getString() + "' but it cannot be found in the repository."));
}
}
}
PropertyIterator propIter = node.getReferences();
while (propIter.hasNext()) {
Property prop = propIter.nextProperty();
try {
Node referencedByFileNode = prop.getParent();
ModelIdHelper.fromPath(referencedByFileNode.getParent().getPath());
} catch (Exception e) {
diagnostics.add(new Diagnostic(modelId, "The model is being referenced by a stale node."));
}
}
} catch (RepositoryException e) {
throw new NodeDiagnosticException("Error in accessing some properties of node", e);
}
return diagnostics;
}
Aggregations