Search in sources :

Example 1 with NotAuthorizedException

use of org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException 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 NotAuthorizedException

use of org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException in project vorto by eclipse.

the class ModelRepository method removeModel.

@Override
public void removeModel(ModelId modelId) {
    doInSession(session -> {
        try {
            ModelInfo modelResource = this.getById(modelId);
            if (modelResource == null) {
                throw new ModelNotFoundException("Cannot find '" + modelId.getPrettyFormat() + "' in '" + session.getWorkspace().getName() + "'");
            }
            if (modelResource.getReferencedBy() != null && !modelResource.getReferencedBy().isEmpty()) {
                throw new ModelReferentialIntegrityException("Cannot remove model because it is referenced by other model(s)", modelResource.getReferencedBy());
            }
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Item item = session.getItem(modelIdHelper.getFullPath());
            item.remove();
            session.save();
            eventPublisher.publishEvent(new AppEvent(this, modelId, null, EventType.MODEL_DELETED));
            return null;
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    });
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Example 3 with NotAuthorizedException

use of org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException in project vorto by eclipse.

the class ModelRepository method getModelVersions.

private List<ModelInfo> getModelVersions(ModelId modelId) {
    return doInSession(session -> {
        modelId.setVersion("");
        ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
        try {
            Node folderNode = session.getNode(modelIdHelper.getFullPath());
            List<ModelInfo> models = new ArrayList<>();
            NodeIterator nodeIterator = folderNode.getNodes();
            while (nodeIterator.hasNext()) {
                Node node = nodeIterator.nextNode();
                models.add(getModelResource(modelId, node));
            }
            return models;
        } catch (PathNotFoundException e) {
            throw new ModelNotFoundException(String.format("Model [%s] does not exist", modelId.getPrettyFormat()), null);
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    });
}
Also used : ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Example 4 with NotAuthorizedException

use of org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException in project vorto by eclipse.

the class ModelRepository method getEMFResource.

@Override
public ModelResource getEMFResource(ModelId modelId) {
    return doInSession(session -> {
        try {
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Node folderNode = session.getNode(modelIdHelper.getFullPath());
            if (!folderNode.getNodes(FILE_NODES).hasNext()) {
                LOGGER.warn("Folder Node :" + folderNode + " does not have any files as children. Cannot load EMF Model.");
                return null;
            }
            Node fileNode = (Node) folderNode.getNodes(FILE_NODES).next();
            Node fileItem = (Node) fileNode.getPrimaryItem();
            InputStream is = fileItem.getProperty(JCR_DATA).getBinary().getStream();
            IModelParser parser = modelParserFactory.getParser(fileNode.getName());
            return (ModelResource) parser.parse(is);
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    });
}
Also used : IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Example 5 with NotAuthorizedException

use of org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException in project vorto by eclipse.

the class ModelRepository method getBasicInfo.

public ModelInfo getBasicInfo(ModelId modelId) {
    return doInSession(session -> {
        try {
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Node folderNode = session.getNode(modelIdHelper.getFullPath());
            Node modelFileNode = folderNode.getNodes(FILE_NODES).nextNode();
            ModelInfo modelInfo = createMinimalModelInfo(modelFileNode);
            setReferencesOnResource(folderNode, modelInfo);
            Map<String, List<ModelInfo>> referencingModels = modelRetrievalService.getModelsReferencing(modelId);
            for (Map.Entry<String, List<ModelInfo>> entry : referencingModels.entrySet()) {
                for (ModelInfo referencee : entry.getValue()) {
                    modelInfo.getReferencedBy().add(referencee.getId());
                }
            }
            return modelInfo;
        } catch (PathNotFoundException e) {
            return null;
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    });
}
Also used : ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Aggregations

NotAuthorizedException (org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)21 ModelIdHelper (org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper)13 ModelId (org.eclipse.vorto.model.ModelId)8 IOException (java.io.IOException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Attachment (org.eclipse.vorto.repository.core.Attachment)5 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)5 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)4 Lists (com.google.common.collect.Lists)3 Collectors (java.util.stream.Collectors)3 ZipEntry (java.util.zip.ZipEntry)3 Node (javax.jcr.Node)3 IOUtils (org.apache.commons.io.IOUtils)3 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)3 FileContent (org.eclipse.vorto.repository.core.FileContent)3 ModelAlreadyExistsException (org.eclipse.vorto.repository.core.ModelAlreadyExistsException)3 Permission (org.eclipse.vorto.repository.core.PolicyEntry.Permission)3 ResponseEntity (org.springframework.http.ResponseEntity)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 Authentication (org.springframework.security.core.Authentication)3