Search in sources :

Example 1 with Diagnostic

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;
}
Also used : ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) Node(javax.jcr.Node) Diagnostic(org.eclipse.vorto.repository.core.Diagnostic) NodeDiagnostic(org.eclipse.vorto.repository.core.impl.RepositoryDiagnostics.NodeDiagnostic) RepositoryException(javax.jcr.RepositoryException) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) Property(javax.jcr.Property) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) RepositoryException(javax.jcr.RepositoryException)

Example 2 with Diagnostic

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()));
    }
}
Also used : ModelType(org.eclipse.vorto.core.api.model.model.ModelType) Diagnostic(org.eclipse.vorto.repository.core.Diagnostic) NodeDiagnostic(org.eclipse.vorto.repository.core.impl.RepositoryDiagnostics.NodeDiagnostic) RepositoryException(javax.jcr.RepositoryException) ModelId(org.eclipse.vorto.model.ModelId)

Example 3 with Diagnostic

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());
}
Also used : NodeIterator(javax.jcr.NodeIterator) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) Node(javax.jcr.Node) Diagnostic(org.eclipse.vorto.repository.core.Diagnostic) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Example 4 with Diagnostic

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;
}
Also used : PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Collection(java.util.Collection) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ArrayList(java.util.ArrayList) NamespaceRepository(org.eclipse.vorto.repository.repositories.NamespaceRepository) Diagnostic(org.eclipse.vorto.repository.core.Diagnostic) GetMapping(org.springframework.web.bind.annotation.GetMapping) Authentication(org.springframework.security.core.Authentication) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) IModelRepositoryFactory(org.eclipse.vorto.repository.core.IModelRepositoryFactory) Authentication(org.springframework.security.core.Authentication) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.vorto.repository.core.Diagnostic) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with Diagnostic

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;
}
Also used : Node(javax.jcr.Node) Value(javax.jcr.Value) PropertyIterator(javax.jcr.PropertyIterator) Diagnostic(org.eclipse.vorto.repository.core.Diagnostic) NodeDiagnostic(org.eclipse.vorto.repository.core.impl.RepositoryDiagnostics.NodeDiagnostic) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property) ModelId(org.eclipse.vorto.model.ModelId) ItemNotFoundException(javax.jcr.ItemNotFoundException) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Aggregations

Diagnostic (org.eclipse.vorto.repository.core.Diagnostic)5 RepositoryException (javax.jcr.RepositoryException)4 Node (javax.jcr.Node)3 NodeDiagnostic (org.eclipse.vorto.repository.core.impl.RepositoryDiagnostics.NodeDiagnostic)3 Property (javax.jcr.Property)2 ModelId (org.eclipse.vorto.model.ModelId)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collectors (java.util.stream.Collectors)1 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 NodeIterator (javax.jcr.NodeIterator)1 PropertyIterator (javax.jcr.PropertyIterator)1 Value (javax.jcr.Value)1 ModelType (org.eclipse.vorto.core.api.model.model.ModelType)1 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)1 IModelRepositoryFactory (org.eclipse.vorto.repository.core.IModelRepositoryFactory)1 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)1 NamespaceRepository (org.eclipse.vorto.repository.repositories.NamespaceRepository)1 NotAuthorizedException (org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1