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;
}
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);
}
});
}
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);
}
});
}
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);
}
});
}
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);
}
});
}
Aggregations