Search in sources :

Example 21 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class GenerationDelegateProxyService method generate.

@Override
public GeneratedOutput generate(ModelId modelId, String serviceKey, Map<String, String> requestParams) {
    ModelInfo modelResource = modelRepositoryService.getById(modelId);
    if (modelResource == null) {
        throw new ModelNotFoundException("Model with the given ID does not exist", null);
    }
    if (modelResource.getType() == ModelType.Datatype || modelResource.getType() == ModelType.Mapping) {
        throw new GenerationException("Provided model is neither an information model nor a function block model!");
    }
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    Generator generatorEntity = getGenerator(serviceKey);
    if (generatorEntity == null) {
        throw new GenerationException("Generator with key " + serviceKey + " is not a registered generator");
    }
    generatorEntity.increaseInvocationCount();
    this.registeredGeneratorsRepository.save(generatorEntity);
    ResponseEntity<byte[]> entity = restTemplate.getForEntity(generatorEntity.getGenerationEndpointUrl() + attachRequestParams(requestParams), byte[].class, modelId.getNamespace(), modelId.getName(), modelId.getVersion());
    return new GeneratedOutput(entity.getBody(), extractFileNameFromHeader(entity), entity.getHeaders().getContentLength());
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) GeneratedOutput(org.eclipse.vorto.repository.api.generation.GeneratedOutput) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) GenerationException(org.eclipse.vorto.repository.api.exception.GenerationException)

Example 22 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class DefaultResolver method doResolve.

@Override
protected ModelId doResolve(ModelInfo mappingModelResource, ResolveQuery query) {
    IModelContent content = this.repository.getModelContent(mappingModelResource.getId(), ContentType.DSL);
    MappingModel mappingModel = (MappingModel) content.getModel();
    Optional<MappingRule> objectRule = mappingModel.getRules().stream().filter(rule -> rule.getTarget() instanceof StereoTypeTarget && ((StereoTypeTarget) rule.getTarget()).getName().equals(query.getStereoType())).findFirst();
    if (objectRule.isPresent()) {
        Optional<Attribute> objectIdAttribute = ((StereoTypeTarget) objectRule.get().getTarget()).getAttributes().stream().filter(attribute -> attribute.getName().equals(query.getAttributeId())).findFirst();
        if (objectIdAttribute.isPresent() && objectIdAttribute.get().getValue().equals(query.getAttributeValue())) {
            return ModelId.fromReference(mappingModel.getReferences().get(0).getImportedNamespace(), mappingModel.getReferences().get(0).getVersion());
        }
    }
    return null;
}
Also used : IModelContent(org.eclipse.vorto.repository.core.IModelContent) Service(org.springframework.stereotype.Service) ContentType(org.eclipse.vorto.repository.core.IModelRepository.ContentType) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Optional(java.util.Optional) MappingRule(org.eclipse.vorto.core.api.model.mapping.MappingRule) StereoTypeTarget(org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget) ResolveQuery(org.eclipse.vorto.repository.api.resolver.ResolveQuery) ModelId(org.eclipse.vorto.repository.api.ModelId) Attribute(org.eclipse.vorto.core.api.model.mapping.Attribute) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) Attribute(org.eclipse.vorto.core.api.model.mapping.Attribute) IModelContent(org.eclipse.vorto.repository.core.IModelContent) MappingRule(org.eclipse.vorto.core.api.model.mapping.MappingRule) StereoTypeTarget(org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Example 23 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class DefaultUserAccountService method makeModelsAnonymous.

private void makeModelsAnonymous(String username) {
    List<ModelInfo> userModels = this.modelRepository.search("author:" + username);
    for (ModelInfo model : userModels) {
        model.setAuthor(USER_ANONYMOUS);
        this.modelRepository.updateMeta(model);
    }
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo)

Example 24 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class ModelRepositoryController method getModelContentForTargetPlatform.

@ApiOperation(value = "Returns the model content including target platform specific attributes")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/content/{namespace}/{name}/{version:.+}/mapping/{targetplatformKey}", method = RequestMethod.GET)
public AbstractModel getModelContentForTargetPlatform(@ApiParam(value = "The namespace of vorto model, e.g. com.mycompany", required = true) @PathVariable final String namespace, @ApiParam(value = "The name of vorto model, e.g. NewInfomodel", required = true) @PathVariable final String name, @ApiParam(value = "The version of vorto model, e.g. 1.0.0", required = true) @PathVariable final String version, @ApiParam(value = "The key of the targetplatform, e.g. lwm2m", required = true) @PathVariable final String targetplatformKey) {
    List<ModelInfo> mappingResource = modelRepository.getMappingModelsForTargetPlatform(new ModelId(name, namespace, version), targetplatformKey);
    if (!mappingResource.isEmpty()) {
        byte[] mappingContentZip = createZipWithAllDependencies(mappingResource.get(0).getId(), ContentType.DSL);
        IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(mappingContentZip))).read();
        MappingModel mappingModel = (MappingModel) workspace.get().stream().filter(p -> p instanceof MappingModel).findFirst().get();
        byte[] modelContent = createZipWithAllDependencies(new ModelId(name, namespace, version), ContentType.DSL);
        workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelContent))).read();
        return ModelDtoFactory.createResource(workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get(), Optional.of(mappingModel));
    } else {
        return getModelContent(namespace, name, version);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URLDecoder(java.net.URLDecoder) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiParam(io.swagger.annotations.ApiParam) ApiResponses(io.swagger.annotations.ApiResponses) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) Value(org.springframework.beans.factory.annotation.Value) Logger(org.apache.log4j.Logger) ApiOperation(io.swagger.annotations.ApiOperation) AbstractRepositoryController(org.eclipse.vorto.repository.web.AbstractRepositoryController) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractModel(org.eclipse.vorto.repository.api.AbstractModel) Api(io.swagger.annotations.Api) ZipEntry(java.util.zip.ZipEntry) ModelId(org.eclipse.vorto.repository.api.ModelId) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) ContentType(org.eclipse.vorto.repository.core.IModelRepository.ContentType) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) ApiResponse(io.swagger.annotations.ApiResponse) Optional(java.util.Optional) MultipartFile(org.springframework.web.multipart.MultipartFile) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ModelId(org.eclipse.vorto.repository.api.ModelId) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class ModelRepositoryController method createSingleModelContent.

private void createSingleModelContent(ModelId modelId, ContentType contentType, HttpServletResponse response) {
    byte[] modelContent = modelRepository.getModelContent(modelId, contentType).getContent();
    if (modelContent != null && modelContent.length > 0) {
        final ModelInfo modelResource = modelRepository.getById(modelId);
        response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + getFileName(modelResource, contentType));
        response.setContentType(APPLICATION_OCTET_STREAM);
        try {
            IOUtils.copy(new ByteArrayInputStream(modelContent), response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            throw new RuntimeException("Error copying file.", e);
        }
    } else {
        throw new RuntimeException("File not found.");
    }
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Aggregations

ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)35 Test (org.junit.Test)12 ModelId (org.eclipse.vorto.repository.api.ModelId)11 IOException (java.io.IOException)9 ModelNotFoundException (org.eclipse.vorto.repository.api.exception.ModelNotFoundException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 AbstractIntegrationTest (org.eclipse.vorto.repository.AbstractIntegrationTest)6 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)6 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)6 ArrayList (java.util.ArrayList)5 Node (javax.jcr.Node)5 PathNotFoundException (javax.jcr.PathNotFoundException)5 RepositoryException (javax.jcr.RepositoryException)5 ApiOperation (io.swagger.annotations.ApiOperation)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ZipEntry (java.util.zip.ZipEntry)4 UploadTooLargeException (org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ApiResponses (io.swagger.annotations.ApiResponses)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3