Search in sources :

Example 11 with NotAuthorizedException

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

the class ModelRepository method getById.

@Override
public ModelInfo getById(ModelId modelId) {
    final ModelId finalModelId = getLatestModelVersionIfLatestTagIsSet(modelId);
    return doInSession(session -> {
        try {
            ModelIdHelper modelIdHelper = new ModelIdHelper(finalModelId);
            Node folderNode = session.getNode(modelIdHelper.getFullPath());
            return getModelResource(finalModelId, folderNode);
        } catch (PathNotFoundException e) {
            return null;
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(finalModelId, e);
        }
    });
}
Also used : ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) ModelId(org.eclipse.vorto.model.ModelId)

Example 12 with NotAuthorizedException

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

the class ModelRepository method getFileContent.

@Override
public Optional<FileContent> getFileContent(ModelId modelId, Optional<String> fileName) {
    return doInSession(session -> {
        try {
            ModelId finalModelId = getLatestModelVersionIfLatestTagIsSet(modelId);
            ModelIdHelper modelIdHelper = new ModelIdHelper(finalModelId);
            Node folderNode = session.getNode(modelIdHelper.getFullPath());
            Node fileNode;
            if (fileName.isPresent()) {
                fileNode = folderNode.getNode(fileName.get());
            } else {
                if (!folderNode.getNodes(FILE_NODES).hasNext()) {
                    throw new NotAuthorizedException(finalModelId);
                }
                fileNode = (Node) folderNode.getNodes(FILE_NODES).next();
            }
            Node fileItem = (Node) fileNode.getPrimaryItem();
            InputStream is = fileItem.getProperty(JCR_DATA).getBinary().getStream();
            final String fileContent = IOUtils.toString(is);
            return Optional.of(new FileContent(fileNode.getName(), fileContent.getBytes()));
        } catch (PathNotFoundException e) {
            return Optional.empty();
        } catch (IOException e) {
            throw new FatalModelRepositoryException("Something went wrong accessing the repository", e);
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    });
}
Also used : 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) IOException(java.io.IOException) ModelId(org.eclipse.vorto.model.ModelId)

Example 13 with NotAuthorizedException

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

the class ModelRepository method getAttachmentsInElevatedSession.

@Override
public List<Attachment> getAttachmentsInElevatedSession(ModelId modelId, IUserContext context) {
    return doInElevatedSession(session -> {
        try {
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Node modelFolderNode = session.getNode(modelIdHelper.getFullPath());
            if (modelFolderNode.hasNode(ATTACHMENTS_NODE)) {
                Node attachmentFolderNode = modelFolderNode.getNode(ATTACHMENTS_NODE);
                List<Attachment> attachments = new ArrayList<>();
                NodeIterator nodeIt = attachmentFolderNode.getNodes();
                while (nodeIt.hasNext()) {
                    Node fileNode = (Node) nodeIt.next();
                    Attachment attachment = Attachment.newInstance(modelId, fileNode.getName());
                    if (fileNode.hasProperty(VORTO_TAGS)) {
                        final List<Value> tags = Arrays.asList(fileNode.getProperty(VORTO_TAGS).getValues());
                        attachment.setTags(tags.stream().map(ModelRepository::fromModeshapeValue).filter(Objects::nonNull).collect(Collectors.toList()));
                    }
                    attachments.add(attachment);
                }
                return attachments;
            }
            return Collections.emptyList();
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    }, context, privilegeService);
}
Also used : ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) Attachment(org.eclipse.vorto.repository.core.Attachment) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Example 14 with NotAuthorizedException

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

the class ModelRepository method updatePropertyInElevatedSession.

@Override
public ModelId updatePropertyInElevatedSession(ModelId modelId, Map<String, String> properties, IUserContext context) {
    return doInElevatedSession(session -> {
        try {
            Node folderNode = createNodeForModelId(session, modelId);
            Node fileNode = folderNode.getNodes(FILE_NODES).hasNext() ? folderNode.getNodes(FILE_NODES).nextNode() : null;
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                fileNode.setProperty(entry.getKey(), entry.getValue());
            }
            fileNode.addMixin(MIX_LAST_MODIFIED);
            session.save();
            eventPublisher.publishEvent(new AppEvent(this, getBasicInfoInElevatedSession(modelId, context), null, EventType.MODEL_UPDATED));
            return modelId;
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    }, context, privilegeService);
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Example 15 with NotAuthorizedException

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

the class ModelRepository method getAttachments.

@Override
public List<Attachment> getAttachments(ModelId modelId) {
    return doInSession(session -> {
        try {
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Node modelFolderNode = session.getNode(modelIdHelper.getFullPath());
            if (modelFolderNode.hasNode(ATTACHMENTS_NODE)) {
                Node attachmentFolderNode = modelFolderNode.getNode(ATTACHMENTS_NODE);
                List<Attachment> attachments = new ArrayList<>();
                NodeIterator nodeIt = attachmentFolderNode.getNodes();
                while (nodeIt.hasNext()) {
                    Node fileNode = (Node) nodeIt.next();
                    Attachment attachment = Attachment.newInstance(modelId, fileNode.getName());
                    if (fileNode.hasProperty(VORTO_TAGS)) {
                        final List<Value> tags = Arrays.asList(fileNode.getProperty(VORTO_TAGS).getValues());
                        attachment.setTags(tags.stream().map(ModelRepository::fromModeshapeValue).filter(Objects::nonNull).collect(Collectors.toList()));
                    }
                    attachments.add(attachment);
                }
                return attachments;
            }
            return Collections.emptyList();
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    });
}
Also used : ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) Attachment(org.eclipse.vorto.repository.core.Attachment) 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