Search in sources :

Example 1 with FileContent

use of org.eclipse.vorto.repository.core.FileContent in project vorto by eclipse.

the class VortoModelImporter method convert.

@Override
protected List<ModelResource> convert(FileUpload fileUpload, Context context) {
    List<ModelResource> result = new ArrayList<>();
    if (fileUpload.getFileExtension().equalsIgnoreCase(EXTENSION_ZIP)) {
        Collection<FileContent> fileContents = getFileContents(fileUpload);
        result.addAll(fileContents.stream().map(fileContent -> (ModelResource) getModelParserFactory().getParser(fileContent.getFileName()).parse(new ByteArrayInputStream(fileContent.getContent()))).collect(Collectors.toList()));
    } else {
        final ModelResource modelInfo = (ModelResource) getModelParserFactory().getParser(fileUpload.getFileExtension()).parse(new ByteArrayInputStream(fileUpload.getContent()));
        result.add(modelInfo);
    }
    return Collections.unmodifiableList(result);
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList)

Example 2 with FileContent

use of org.eclipse.vorto.repository.core.FileContent in project vorto by eclipse.

the class ModelIdToModelContentConverter method getWorkspaceForModel.

private ModelWorkspaceReader getWorkspaceForModel(final ModelId modelId) {
    Collection<ModelInfo> allModels = getModelWithAllDependencies(modelId);
    DependencyManager dm = new DependencyManager(new HashSet<>(allModels));
    allModels = dm.getSorted();
    ModelWorkspaceReader workspaceReader = IModelWorkspace.newReader();
    for (ModelInfo model : allModels) {
        FileContent modelContent = repositoryFactory.getRepositoryByModel(model.getId()).getFileContent(model.getId(), Optional.of(model.getFileName())).get();
        workspaceReader.addFile(new ByteArrayInputStream(modelContent.getContent()), model.getType());
    }
    return workspaceReader;
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) ByteArrayInputStream(java.io.ByteArrayInputStream) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager)

Example 3 with FileContent

use of org.eclipse.vorto.repository.core.FileContent in project vorto by eclipse.

the class AbstractRepositoryController method getWorkspaceForModel.

protected IModelWorkspace getWorkspaceForModel(final ModelId modelId) {
    List<ModelInfo> allModels = getModelWithAllDependencies(modelId);
    DependencyManager dm = new DependencyManager(new HashSet<>(allModels));
    allModels = dm.getSorted();
    ModelWorkspaceReader workspaceReader = IModelWorkspace.newReader();
    for (ModelInfo model : allModels) {
        FileContent modelContent = getModelRepository(model.getId()).getFileContent(model.getId(), Optional.of(model.getFileName())).get();
        workspaceReader.addFile(new ByteArrayInputStream(modelContent.getContent()), model.getType());
    }
    return workspaceReader.read();
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) ByteArrayInputStream(java.io.ByteArrayInputStream) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager)

Example 4 with FileContent

use of org.eclipse.vorto.repository.core.FileContent in project vorto by eclipse.

the class AbstractRepositoryController method createSingleModelContent.

protected void createSingleModelContent(ModelId modelId, HttpServletResponse response) {
    Optional<FileContent> fileContent = getModelRepository(modelId).getFileContent(modelId, Optional.empty());
    final byte[] modelContent = fileContent.get().getContent();
    if (modelContent != null && modelContent.length > 0) {
        response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + fileContent.get().getFileName());
        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 : FileContent(org.eclipse.vorto.repository.core.FileContent) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Example 5 with FileContent

use of org.eclipse.vorto.repository.core.FileContent in project vorto by eclipse.

the class VortolangUpgradeTask method doUpgrade.

@Override
public void doUpgrade() throws UpgradeProblem {
    setAdminUserContext();
    List<ModelInfo> searchResult = modelSearchService.search("*");
    final String newline = System.getProperty("line.separator");
    for (ModelInfo modelInfo : searchResult) {
        ModelRepository modelRepository = (ModelRepository) repositoryFactory.getRepositoryByModel(modelInfo.getId());
        Optional<FileContent> content = modelRepository.getFileContent(modelInfo.getId(), Optional.of(modelInfo.getFileName()));
        if (content.isPresent()) {
            String currentModelContent = "";
            try {
                currentModelContent = new String(content.get().getContent(), "utf-8");
            } catch (UnsupportedEncodingException e1) {
                logger.warn("Could not set vortolang field for model " + modelInfo.getId().getPrettyFormat() + ".Skipping...", e1);
            }
            if (currentModelContent.contains("vortolang 1.0")) {
                continue;
            }
            StringBuilder contentBuilder = new StringBuilder();
            contentBuilder.append("vortolang 1.0");
            contentBuilder.append(newline);
            contentBuilder.append(newline);
            try {
                contentBuilder.append(currentModelContent);
                logger.info("Upgrading " + modelInfo.toString() + " for vortolang attribute....");
                modelRepository.save(modelInfo.getId(), contentBuilder.toString().getBytes(), modelInfo.getFileName(), UserContext.user(modelInfo.getAuthor(), modelRepository.getWorkspaceId()), false);
                logger.info("Upgrade of " + modelInfo.toString() + " successful.");
            } catch (Throwable e) {
                logger.warn("Could not set vortolang field for model " + modelInfo.getId().getPrettyFormat() + ".Skipping...", e);
            }
        }
    }
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelRepository(org.eclipse.vorto.repository.core.impl.ModelRepository) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

FileContent (org.eclipse.vorto.repository.core.FileContent)14 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)8 IOException (java.io.IOException)7 ModelId (org.eclipse.vorto.model.ModelId)4 DependencyManager (org.eclipse.vorto.repository.core.impl.utils.DependencyManager)4 ArrayList (java.util.ArrayList)3 ZipEntry (java.util.zip.ZipEntry)3 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)3 GenericApplicationException (org.eclipse.vorto.repository.web.GenericApplicationException)3 ModelWorkspaceReader (org.eclipse.vorto.utilities.reader.ModelWorkspaceReader)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 IUserContext (org.eclipse.vorto.repository.core.IUserContext)2 ModelAlreadyExistsException (org.eclipse.vorto.repository.core.ModelAlreadyExistsException)2 ModelResource (org.eclipse.vorto.repository.core.ModelResource)2 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)2 BulkUploadException (org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)2 NotAuthorizedException (org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)2 ApiOperation (io.swagger.annotations.ApiOperation)1