Search in sources :

Example 6 with FileContent

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

the class VortoModelImporter method getFileContents.

private Collection<FileContent> getFileContents(FileUpload fileUpload) {
    Collection<FileContent> fileContents = new ArrayList<>();
    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(fileUpload.getContent()));
    ZipEntry entry = null;
    try {
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory() && ModelParserFactory.hasParserFor(entry.getName())) {
                fileContents.add(new FileContent(entry.getName(), copyStream(zis, entry)));
            }
        }
    } catch (IOException e) {
        throw new BulkUploadException("IOException while getting next entry from zip file", e);
    }
    return fileContents;
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) BulkUploadException(org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException) IOException(java.io.IOException)

Example 7 with FileContent

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

the class DefaultPayloadMappingService method getWorkspaceForModel.

private 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 = this.modelRepositoryFactory.getRepositoryByModel(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 8 with FileContent

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

the class AbstractModelImporter method sortAndSaveToRepository.

private List<ModelInfo> sortAndSaveToRepository(List<ModelResource> resources, FileUpload extractedFile, Context context) {
    final IUserContext user = context.getUser();
    List<ModelInfo> savedModels = new ArrayList<>();
    DependencyManager dm = new DependencyManager();
    for (ModelResource resource : resources) {
        dm.addResource(resource);
    }
    dm.getSorted().forEach(resource -> {
        try {
            IModelRepository modelRepository = modelRepoFactory.getRepositoryByModel(resource.getId());
            ModelInfo importedModel = modelRepository.save((ModelResource) resource, user);
            savedModels.add(importedModel);
            postProcessImportedModel(importedModel, new FileContent(extractedFile.getFileName(), extractedFile.getContent()), user);
        } catch (Exception e) {
            logger.error("Problem importing model", e);
            throw new ModelImporterException("Problem importing model", e);
        }
    });
    return savedModels;
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) FileContent(org.eclipse.vorto.repository.core.FileContent) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ArrayList(java.util.ArrayList) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager) IOException(java.io.IOException) BulkUploadException(org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)

Example 9 with FileContent

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

the class GeneratedOutputAttachmentHandler method attachGeneratedOutput.

/**
 * This method creates a new attachment on the model with the given output of a generator.
 *
 * @param userContext   - The current user context
 * @param modelId       - the ID of the model used to invoke the generator
 * @param serviceKey    - the service key of the generator
 * @param requestParams - the parameters of the request
 * @param response      - the output of the generator
 * @param plugin        - the info about the invoked generator
 * @return the generated output that has been saved as attachment. The returned value has a
 * different file name than the one in the response parameter, as attachments of generator outputs
 * follow a naming convention.
 */
GeneratedOutput attachGeneratedOutput(IUserContext userContext, ModelId modelId, String serviceKey, Map<String, String> requestParams, GeneratedOutput response, GeneratorPluginConfiguration plugin) {
    String filename = buildAttachmentFilename(modelId, serviceKey, requestParams, response, plugin);
    FileContent fc = new FileContent(filename, response.getContent());
    modelRepositoryFactory.getRepositoryByModel(modelId, userContext).attachFileInElevatedSession(modelId, fc, userContext, GeneratedOutputAttachmentHandler.tagsForRequest(plugin, requestParams));
    return new GeneratedOutput(response.getContent(), filename, response.getSize());
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput)

Example 10 with FileContent

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

the class ModelRepositoryController method sendAsZipFile.

private void sendAsZipFile(final HttpServletResponse response, final String fileName, Map<ModelInfo, FileContent> models) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    try {
        for (Map.Entry<ModelInfo, FileContent> model : models.entrySet()) {
            ModelInfo modelResource = model.getKey();
            ZipEntry zipEntry = new ZipEntry(modelResource.getId().getPrettyFormat() + modelResource.getType().getExtension());
            zos.putNextEntry(zipEntry);
            zos.write(model.getValue().getContent());
            zos.closeEntry();
        }
        zos.close();
        baos.close();
    } catch (Exception ex) {
        throw new GenericApplicationException("error in creating zip file.", ex);
    }
    response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + fileName);
    response.setContentType(APPLICATION_OCTET_STREAM);
    try {
        IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream());
        response.flushBuffer();
    } catch (IOException e) {
        throw new GenericApplicationException("Error copying file.", e);
    }
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ModelAlreadyExistsException(org.eclipse.vorto.repository.core.ModelAlreadyExistsException) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ModelNotReleasedException(org.eclipse.vorto.repository.model.ModelNotReleasedException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) WorkflowException(org.eclipse.vorto.repository.workflow.WorkflowException) ModelNamespaceNotOfficialException(org.eclipse.vorto.repository.model.ModelNamespaceNotOfficialException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException)

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