use of org.eclipse.vorto.repository.web.GenericApplicationException in project vorto by eclipse.
the class BackupRestoreService method restoreRepository.
@Override
public Collection<Namespace> restoreRepository(byte[] backupFile, Predicate<Namespace> namespaceFilter) {
Preconditions.checkNotNull(backupFile, "Backup file must not be null");
try {
Collection<Namespace> namespacesRestored = Lists.newArrayList();
Map<String, byte[]> backups = getBackups(backupFile);
backups.forEach((namespaceName, backup) -> {
LOGGER.info(String.format("Restoring backup for [%s]", namespaceName));
Namespace namespace = namespaceRepository.findByName(namespaceName);
if (null != namespace && namespaceFilter.test(namespace)) {
try {
String workspaceId = namespace.getWorkspaceId();
IRepositoryManager repoMgr = modelRepositoryFactory.getRepositoryManager(workspaceId, authSupplier.get());
if (!repoMgr.exists(workspaceId)) {
repoMgr.createWorkspace(workspaceId);
} else {
repoMgr.removeWorkspace(workspaceId);
repoMgr.createWorkspace(workspaceId);
}
repoMgr.restore(backup);
this.modelRepositoryFactory.getPolicyManager(workspaceId, SecurityContextHolder.getContext().getAuthentication()).restorePolicyEntries();
namespacesRestored.add(namespace);
} catch (Exception e) {
LOGGER.error(String.format("Error while restoring [%s]", namespaceName), e);
}
} else {
LOGGER.info(String.format("Skipping restoration of [%s] either because the namespace could not be found, or was filtered out.", namespaceName));
}
});
if (!namespacesRestored.isEmpty()) {
indexingService.reindexAllModels();
}
return namespacesRestored;
} catch (IOException e) {
throw new GenericApplicationException("Problem while reading zip file during restore", e);
}
}
use of org.eclipse.vorto.repository.web.GenericApplicationException in project vorto by eclipse.
the class BackupRestoreService method createZippedInputStream.
private byte[] createZippedInputStream(Map<String, byte[]> backups) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
try {
for (Map.Entry<String, byte[]> entry : backups.entrySet()) {
ZipEntry zipEntry = new ZipEntry(entry.getKey() + ".xml");
zos.putNextEntry(zipEntry);
zos.write(entry.getValue());
zos.closeEntry();
}
zos.close();
baos.close();
return baos.toByteArray();
} catch (Exception ex) {
throw new GenericApplicationException("Error while generating zip file.", ex);
}
}
use of org.eclipse.vorto.repository.web.GenericApplicationException 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);
}
}
use of org.eclipse.vorto.repository.web.GenericApplicationException in project vorto by eclipse.
the class BackupController method backupRepository.
private void backupRepository(final HttpServletResponse response, Predicate<Namespace> namespaceFilter, Optional<String> ext) {
response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + String.format("backup-%s%s.zip", SIMPLEDATEFORMAT.format(new Date()), ext.orElse("")));
response.setContentType(APPLICATION_OCTET_STREAM);
try {
IOUtils.copy(new ByteArrayInputStream(backupRestoreService.createBackup(namespaceFilter)), response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
throw new GenericApplicationException("Error copying file.", e);
}
}
use of org.eclipse.vorto.repository.web.GenericApplicationException in project vorto by eclipse.
the class ModelRepositoryController method getModelImage.
@ApiOperation(value = "Returns the image of a vorto model")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@GetMapping("/{modelId:.+}/images")
public void getModelImage(@ApiParam(value = "The modelId of vorto model, e.g. com.mycompany.Car:1.0.0", required = true) @PathVariable final String modelId, @ApiParam(value = "Response", required = true) final HttpServletResponse response) {
Objects.requireNonNull(modelId, "modelId must not be null");
final ModelId modelID = ModelId.fromPrettyFormat(modelId);
IModelRepository modelRepo = getModelRepository(modelID);
// first searches by "display image" tag
List<Attachment> imageAttachments = modelRepo.getAttachmentsByTag(modelID, Attachment.TAG_DISPLAY_IMAGE);
// if none present, searches just by "image" tag (for backwards compatibility)
if (imageAttachments.isEmpty()) {
imageAttachments = modelRepo.getAttachmentsByTag(modelID, Attachment.TAG_IMAGE);
}
// still nope
if (imageAttachments.isEmpty()) {
response.setStatus(404);
return;
}
// fetches the first element: either it's the only one (if the display image tag is present)
// or arbitrarily the first image found (for backwards compatibility)
Optional<FileContent> imageContent = modelRepo.getAttachmentContent(modelID, imageAttachments.get(0).getFilename());
if (!imageContent.isPresent()) {
response.setStatus(404);
return;
}
try {
response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + modelID.getName() + ".png");
response.setContentType(APPLICATION_OCTET_STREAM);
IOUtils.copy(new ByteArrayInputStream(imageContent.get().getContent()), response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
throw new GenericApplicationException("Error copying file.", e);
}
}
Aggregations