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;
}
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();
}
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;
}
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());
}
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);
}
}
Aggregations