Search in sources :

Example 1 with ModelNotFoundException

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);
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) ModelId(org.eclipse.vorto.model.ModelId) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with ModelNotFoundException

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;
    }
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) PathVariable(org.springframework.web.bind.annotation.PathVariable) ModelIdFactory(org.eclipse.vorto.core.api.model.model.ModelIdFactory) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager) IPayloadMappingService(org.eclipse.vorto.repository.mapping.IPayloadMappingService) MappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.MappingSpecification) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Infomodel(org.eclipse.vorto.model.Infomodel) ModelIdToModelContentConverter(org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter) ArrayList(java.util.ArrayList) ModelConversionUtils(org.eclipse.vorto.core.api.model.ModelConversionUtils) Model(org.eclipse.vorto.core.api.model.model.Model) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) HashSet(java.util.HashSet) Logger(org.apache.log4j.Logger) IWorkflowService(org.eclipse.vorto.repository.workflow.IWorkflowService) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) IMappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.IMappingSpecification) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityModel(org.eclipse.vorto.model.EntityModel) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Service(org.springframework.stereotype.Service) Map(java.util.Map) IModelRepositoryFactory(org.eclipse.vorto.repository.core.IModelRepositoryFactory) MappingSpecificationSerializer(org.eclipse.vorto.mapping.engine.serializer.MappingSpecificationSerializer) ModelProperty(org.eclipse.vorto.model.ModelProperty) IModel(org.eclipse.vorto.model.IModel) FileContent(org.eclipse.vorto.repository.core.FileContent) MappingEngine(org.eclipse.vorto.mapping.engine.MappingEngine) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ModelDtoFactory(org.eclipse.vorto.repository.web.core.ModelDtoFactory) ModelId(org.eclipse.vorto.model.ModelId) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) Stereotype(org.eclipse.vorto.model.Stereotype) List(java.util.List) ModelContent(org.eclipse.vorto.model.ModelContent) EnumModel(org.eclipse.vorto.model.EnumModel) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) Optional(java.util.Optional) ModelUtils(org.eclipse.vorto.repository.utils.ModelUtils) WorkflowException(org.eclipse.vorto.repository.workflow.WorkflowException) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Model(org.eclipse.vorto.core.api.model.model.Model) EntityModel(org.eclipse.vorto.model.EntityModel) IModel(org.eclipse.vorto.model.IModel) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) EnumModel(org.eclipse.vorto.model.EnumModel) ModelId(org.eclipse.vorto.model.ModelId) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Example 3 with ModelNotFoundException

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);
}
Also used : ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) ModelId(org.eclipse.vorto.model.ModelId)

Example 4 with ModelNotFoundException

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());
}
Also used : Arrays(java.util.Arrays) GenerationException(org.eclipse.vorto.repository.plugin.generator.GenerationException) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) IGeneratorPluginService(org.eclipse.vorto.repository.plugin.generator.IGeneratorPluginService) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ModelIdToModelContentConverter(org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter) ArrayList(java.util.ArrayList) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Service(org.springframework.stereotype.Service) Map(java.util.Map) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) IModelRepositoryFactory(org.eclipse.vorto.repository.core.IModelRepositoryFactory) RestTemplate(org.springframework.web.client.RestTemplate) OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) HttpHeaders(org.springframework.http.HttpHeaders) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ModelId(org.eclipse.vorto.model.ModelId) ModelType(org.eclipse.vorto.model.ModelType) GeneratorPluginConfiguration(org.eclipse.vorto.repository.plugin.generator.GeneratorPluginConfiguration) List(java.util.List) ModelContent(org.eclipse.vorto.model.ModelContent) HttpEntity(org.springframework.http.HttpEntity) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) Comparator(java.util.Comparator) Authentication(org.springframework.security.core.Authentication) GeneratorPluginInfo(org.eclipse.vorto.plugin.generator.GeneratorPluginInfo) HttpHeaders(org.springframework.http.HttpHeaders) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) GenerationException(org.eclipse.vorto.repository.plugin.generator.GenerationException) ModelId(org.eclipse.vorto.model.ModelId)

Example 5 with ModelNotFoundException

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;
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) Permission(org.eclipse.vorto.repository.core.PolicyEntry.Permission) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) ModelId(org.eclipse.vorto.model.ModelId)

Aggregations

ModelId (org.eclipse.vorto.model.ModelId)7 ModelNotFoundException (org.eclipse.vorto.repository.core.ModelNotFoundException)7 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)5 ModelContent (org.eclipse.vorto.model.ModelContent)3 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ModelIdToModelContentConverter (org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter)2 IModelRepositoryFactory (org.eclipse.vorto.repository.core.IModelRepositoryFactory)2 IUserContext (org.eclipse.vorto.repository.core.IUserContext)2 IModelWorkspace (org.eclipse.vorto.utilities.reader.IModelWorkspace)2 ModelWorkspaceReader (org.eclipse.vorto.utilities.reader.ModelWorkspaceReader)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Service (org.springframework.stereotype.Service)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1