Search in sources :

Example 6 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 7 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)

Example 8 with NotAuthorizedException

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

the class ModelRepository method doAttachFileInSession.

private boolean doAttachFileInSession(ModelId modelId, FileContent fileContent, IUserContext userContext, Session session, Tag[] tags) throws RepositoryException {
    try {
        ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
        Node modelFolderNode = session.getNode(modelIdHelper.getFullPath());
        Node attachmentFolderNode;
        if (!modelFolderNode.hasNode(ATTACHMENTS_NODE)) {
            attachmentFolderNode = modelFolderNode.addNode(ATTACHMENTS_NODE, NT_FOLDER);
        } else {
            attachmentFolderNode = modelFolderNode.getNode(ATTACHMENTS_NODE);
        }
        String[] tagIds = Arrays.stream(tags).filter(Objects::nonNull).map(Tag::getId).collect(Collectors.toList()).toArray(new String[tags.length]);
        // purposes), removes the tag from all other attachments
        if (Arrays.asList(tags).contains(TAG_DISPLAY_IMAGE)) {
            NodeIterator attachments = attachmentFolderNode.getNodes();
            while (attachments.hasNext()) {
                Node next = attachments.nextNode();
                Property attachmentTags = next.getProperty(VORTO_TAGS);
                Value[] attachmentTagsValuesFiltered = Arrays.stream(attachmentTags.getValues()).filter(v -> {
                    try {
                        return !v.getString().equals(TAG_DISPLAY_IMAGE.getId());
                    // swallowing here
                    } catch (RepositoryException re) {
                        return false;
                    }
                }).toArray(Value[]::new);
                next.setProperty(VORTO_TAGS, attachmentTagsValuesFiltered);
            }
        }
        Node contentNode;
        if (attachmentFolderNode.hasNode(fileContent.getFileName())) {
            Node attachmentNode = attachmentFolderNode.getNode(fileContent.getFileName());
            attachmentNode.addMixin(VORTO_META);
            attachmentNode.setProperty(VORTO_TAGS, tagIds, PropertyType.STRING);
            contentNode = (Node) attachmentNode.getPrimaryItem();
        } else {
            Node attachmentNode = attachmentFolderNode.addNode(fileContent.getFileName(), NT_FILE);
            attachmentNode.addMixin(VORTO_META);
            attachmentNode.setProperty(VORTO_TAGS, tagIds, PropertyType.STRING);
            contentNode = attachmentNode.addNode(JCR_CONTENT, NT_RESOURCE);
        }
        Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(fileContent.getContent()));
        contentNode.setProperty(JCR_DATA, binary);
        session.save();
        eventPublisher.publishEvent(new AppEvent(this, getById(modelId), userContext, EventType.MODEL_UPDATED));
        return true;
    } catch (AccessDeniedException e) {
        throw new NotAuthorizedException(modelId, e);
    }
}
Also used : DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager) java.util(java.util) EventType(org.eclipse.vorto.repository.core.events.EventType) javax.jcr.query(javax.jcr.query) AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) Function(java.util.function.Function) ModelLink(org.eclipse.vorto.repository.web.api.v1.dto.ModelLink) Model(org.eclipse.vorto.core.api.model.model.Model) Logger(org.apache.log4j.Logger) Lists(com.google.common.collect.Lists) ObjectMapperFactory(org.eclipse.vorto.plugin.generator.adapter.ObjectMapperFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) org.eclipse.vorto.repository.core(org.eclipse.vorto.repository.core) ModelState(org.eclipse.vorto.repository.workflow.ModelState) ModelParserFactory(org.eclipse.vorto.repository.core.impl.parser.ModelParserFactory) Attachment(org.eclipse.vorto.repository.core.Attachment) PrivilegeService(org.eclipse.vorto.repository.services.PrivilegeService) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) ModelSearchUtil(org.eclipse.vorto.repository.core.impl.utils.ModelSearchUtil) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ModelReferencesHelper(org.eclipse.vorto.repository.core.impl.utils.ModelReferencesHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ModelId(org.eclipse.vorto.model.ModelId) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) Namespace(org.eclipse.vorto.repository.domain.Namespace) Collectors(java.util.stream.Collectors) ModelType(org.eclipse.vorto.model.ModelType) NamespaceService(org.eclipse.vorto.repository.services.NamespaceService) IOUtils(org.apache.commons.io.IOUtils) ChangeSet(org.eclipse.vorto.model.refactor.ChangeSet) RefactoringTask(org.eclipse.vorto.model.refactor.RefactoringTask) AttachmentValidator(org.eclipse.vorto.repository.core.impl.validation.AttachmentValidator) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) NewNamespacesNotSupersetException(org.eclipse.vorto.repository.tenant.NewNamespacesNotSupersetException) ModelUtils(org.eclipse.vorto.repository.utils.ModelUtils) IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) javax.jcr(javax.jcr) InputStream(java.io.InputStream) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 9 with NotAuthorizedException

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

the class ModelRepository method updateProperty.

private ModelId updateProperty(ModelId modelId, NodeConsumer nodeConsumer) {
    return doInSession(session -> {
        try {
            Node folderNode = createNodeForModelId(session, modelId);
            Node fileNode = folderNode.getNodes(FILE_NODES).hasNext() ? folderNode.getNodes(FILE_NODES).nextNode() : null;
            nodeConsumer.accept(fileNode);
            fileNode.addMixin(MIX_LAST_MODIFIED);
            session.save();
            eventPublisher.publishEvent(new AppEvent(this, getBasicInfo(modelId), null, EventType.MODEL_UPDATED));
            return modelId;
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    });
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Example 10 with NotAuthorizedException

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

the class ModelRepository method getBasicInfoInElevatedSession.

public ModelInfo getBasicInfoInElevatedSession(ModelId modelId, IUserContext context) {
    return doInElevatedSession(session -> {
        try {
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Node folderNode = session.getNode(modelIdHelper.getFullPath());
            Node modelFileNode = folderNode.getNodes(FILE_NODES).nextNode();
            ModelInfo modelInfo = createMinimalModelInfo(modelFileNode, true, context);
            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);
        }
    }, context, privilegeService);
}
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