use of org.eclipse.vorto.repository.core.ModelNotFoundException in project vorto by eclipse.
the class ModelController method getModelInfo.
@PreAuthorize("isAuthenticated() or hasAuthority('model_viewer')")
@GetMapping("/{modelId:.+}")
public ModelInfo getModelInfo(@ApiParam(value = "The modelId of vorto model, e.g. com.mycompany:Car:1.0.0", required = true) @PathVariable final String modelId) {
Objects.requireNonNull(modelId, "modelId must not be null");
ModelId modelID = ModelId.fromPrettyFormat(modelId);
LOGGER.info(String.format("Generated model info: [%s]", modelID.getPrettyFormat()));
ModelInfo resource = getModelRepository(modelID).getByIdWithPlatformMappings(modelID);
if (resource == null) {
LOGGER.warn(String.format("Could not find model with ID [%s] in repository", modelID));
throw new ModelNotFoundException("Model does not exist", null);
}
return ModelDtoFactory.createDto(resource);
}
use of org.eclipse.vorto.repository.core.ModelNotFoundException in project vorto by eclipse.
the class DefaultPayloadMappingService method getModelContentByModelAndMappingId.
private IModel getModelContentByModelAndMappingId(final String _modelId, @PathVariable final String mappingId) {
final ModelId modelId = ModelId.fromPrettyFormat(_modelId);
final ModelId mappingModelId = ModelId.fromPrettyFormat(mappingId);
IModelRepository repository = this.modelRepositoryFactory.getRepositoryByModel(modelId);
ModelInfo vortoModelInfo = repository.getById(modelId);
ModelInfo mappingModelInfo = this.modelRepositoryFactory.getRepositoryByModel(mappingModelId).getById(mappingModelId);
if (vortoModelInfo == null) {
throw new ModelNotFoundException(String.format("Could not find vorto model with ID: %s", modelId));
} else if (mappingModelInfo == null) {
throw new ModelNotFoundException(String.format("Could not find mapping with ID: %s", mappingId));
}
IModelWorkspace mappingWorkspace = getWorkspaceForModel(mappingModelInfo.getId());
Optional<Model> model = mappingWorkspace.get().stream().filter(_model -> ModelUtils.fromEMFModelId(ModelIdFactory.newInstance(_model)).equals(vortoModelInfo.getId())).findFirst();
if (model.isPresent()) {
final Model flattenedModel = ModelConversionUtils.convertToFlatHierarchy(model.get());
return ModelDtoFactory.createResource(flattenedModel, Optional.of((MappingModel) mappingWorkspace.get().stream().filter(_model -> _model instanceof MappingModel && mappingMatchesModelId((MappingModel) _model, vortoModelInfo)).findFirst().get()));
} else {
return null;
}
}
use of org.eclipse.vorto.repository.core.ModelNotFoundException in project vorto by eclipse.
the class LocalModelWorkspace method loadFromRepository.
public void loadFromRepository(Collection<ModelId> modelIds) {
Collection<ModelId> allReferences = modelIds;
allReferences.removeAll(this.modelIds);
allReferences.forEach(refModelId -> {
try {
repoFactory.getRepositoryByModel(refModelId).getFileContent(refModelId, Optional.empty()).ifPresent(refFile -> {
createResource(refFile.getFileName(), refFile.getContent(), resourceSet);
});
} catch (ModelNotFoundException notFoundException) {
throw new ValidationException("Could not find reference " + refModelId.getPrettyFormat(), null);
}
});
// add references, so that they are not looked up again
this.modelIds.addAll(allReferences);
}
use of org.eclipse.vorto.repository.core.ModelNotFoundException in project vorto by eclipse.
the class DefaultGeneratorPluginService method doGenerateWithApiVersion1.
private GeneratedOutput doGenerateWithApiVersion1(ModelInfo modelInfo, String serviceKey, Map<String, String> requestParams, String baseUrl) {
if (modelInfo == null) {
throw new ModelNotFoundException("Model with the given ID does not exist", null);
}
if (modelInfo.getType() == ModelType.Datatype || modelInfo.getType() == ModelType.Mapping) {
throw new GenerationException("Provided model is neither an information model nor a function block model!");
}
HttpEntity<String> entity = getUserToken().map(token -> {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer " + token);
return new HttpEntity<>("parameters", headers);
}).orElse(null);
ModelId modelId = modelInfo.getId();
ResponseEntity<byte[]> response = restTemplate.exchange(baseUrl + "/rest/generators/{pluginkey}/generate/{namespace}/{name}/{version}" + attachRequestParams(requestParams), HttpMethod.GET, entity, byte[].class, serviceKey, modelId.getNamespace(), modelId.getName(), modelId.getVersion());
return new GeneratedOutput(response.getBody(), extractFileNameFromHeader(response), response.getHeaders().getContentLength());
}
use of org.eclipse.vorto.repository.core.ModelNotFoundException in project vorto by eclipse.
the class HasPermissionEvaluator method hasPermission.
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object targetPermission) {
final String username = authentication.getName();
if (targetDomainObject instanceof ModelId) {
if (targetPermission instanceof String) {
try {
ModelId modelId = (ModelId) targetDomainObject;
String workspaceId = namespaceService.resolveWorkspaceIdForNamespace(modelId.getNamespace()).orElseThrow(() -> new ModelNotFoundException("Model '" + modelId.getPrettyFormat() + "' can't be found in any workspace."));
String permission = (String) targetPermission;
ModelInfo modelInfo = repositoryFactory.getRepository(workspaceId, authentication).getById(modelId);
if (modelInfo != null) {
if ("model:delete".equalsIgnoreCase(permission)) {
return modelInfo.getAuthor().equalsIgnoreCase(username);
} else if ("model:get".equalsIgnoreCase(permission)) {
return modelInfo.getState().equals(SimpleWorkflowModel.STATE_RELEASED.getName()) || modelInfo.getState().equals(SimpleWorkflowModel.STATE_DEPRECATED.getName()) || modelInfo.getAuthor().equals(username);
} else if ("model:owner".equalsIgnoreCase(permission)) {
return modelInfo.getAuthor().equals(username);
}
}
} catch (NotAuthorizedException ex) {
return false;
}
} else if (targetPermission instanceof Permission) {
ModelId modelId = (ModelId) targetDomainObject;
Permission permission = (Permission) targetPermission;
String workspaceId = namespaceService.resolveWorkspaceIdForNamespace(modelId.getNamespace()).orElseThrow(() -> new ModelNotFoundException("The workspace for '" + modelId.getPrettyFormat() + "' could not be found."));
return repositoryFactory.getPolicyManager(workspaceId, authentication).hasPermission(modelId, permission);
}
} else if (targetDomainObject instanceof String) {
return username.equalsIgnoreCase((String) targetDomainObject);
}
return false;
}
Aggregations